blob: 417470bfb340a8e776311d848d4a61939b19bcd4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
(ns user
(:use compojure
utils))
(defstruct user-struct :nick :user_id :avatar :last-seen)
(defn user-struct-from-session [session]
(struct user-struct (session :nick) (session :user_id) (session :avatar)
(System/currentTimeMillis)))
(def *nick-regex* #"^[A-Za-z0-9\-_âˆb˚†]*$")
(defn is-invalid-nick? [n]
(cond
(< (count n) 3) "NICK_TOO_SHORT"
(> (count n) 16) "NICK_TOO_LONG"
(not (re-matches *nick-regex* n)) "NICK_INVALID_CHARS"))
(defn fetch-nick [nick]
(let [q1 "SELECT * FROM users WHERE nick = ? LIMIT 1"
; ORDER BY ensures consistent retrieval of ambiguious names
q2 "SELECT * FROM users WHERE lower(nick) = ? ORDER BY nick LIMIT 1"]
(or (first-or-nil (do-select [q1 nick]))
(first-or-nil (do-select [q2 (lower-case nick)])))))
(defn authorize-nick-hash [nick hash]
(let [db-user (fetch-nick nick)]
(and db-user (= (db-user :hash) hash) db-user)))
(defn update-nick-hash [nick hash]
(if (not (assert-update
(do-update :users ["nick=?" nick]
{:hash hash})))
; TODO: logging
(println (format "Error updating hash for %s" nick))))
(defn reset-token [nick hash ts]
(sha1-hash nick hash ts))
(defn reset-link [nick token ts]
(url-params "http://dump.fm/reset" {"nick" nick
"ts" ts
"token" token}))
(defn valid-reset-link? [nick token ts]
(if-let [info (and nick (fetch-nick nick))]
(and (= token (reset-token (info :nick) (info :hash) ts))
(>= ts (ms-ago (days 2))))))
|