blob: 5bf98114b75bc8692bef3aee81cfe214d68afbee (
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
(ns cookie-login
(:use compojure
config))
(def *login-token-key* :login-token)
(def *login-token-expiry* (* 1000 60 60 24 7)) ; one week
(defn clear-login-token
"Creates an expiration cookie for a given cookie name."
[token-key]
(set-cookie token-key "dummy"
:expires "Thu, 01-Jan-1970 00:00:01 GMT"
:domain *cookie-domain*))
(defn handle-request-with-login-token
"Validates login token, handles request, and updates cookies and session
repository. If the token is invalid, the token cookie is expired."
[handler request token-maker token-reader login-token-key login-token-expiry]
(if-let [session-info (token-reader (get-in request
[:cookies login-token-key]))]
(let [response (handler (merge-with merge
request
{:session session-info}))
; Session variable priority:
; 1) variables set by handler
; 2) variables from token-reader
; 3) variables from repository
session-map (merge (request :session)
session-info
(response :session))]
(merge-with merge
response
{:session session-map}
(token-maker session-info)))
(merge (handler request)
(clear-login-token login-token-key))))
(defn with-cookie-login
"Middleware to support automatic cookie login. Must be placed after
the with-session middleware.
Must be given three arguments:
- process-login-token?
Function to apply to request map to determine whether to
process login token or not. If a false value is returned,
then the next handler is called without further processing.
- token-maker:
Function to generate new login token from session map.
- token-reader:
Function to generate session map from login token. Should return nil
if login token is invalid.
The following variables can be rebound:
- *login-token-key*
The cookie name to store the login-token under.
Defaults to 'login-token'.
- *login-token-expiry*
The number of milliseconds a login token is valid for.
Defaults to one week."
[handler process-login-token? token-maker token-reader]
(let [login-token-key *login-token-key*
login-token-expiry *login-token-expiry*]
(fn [request]
(if (and (get-in request [:cookies login-token-key])
(process-login-token? request))
(handle-request-with-login-token
handler
request
token-maker
token-reader
login-token-key
login-token-expiry)
(handler request)))))
|