Line data Source code
1 4 : (ns matchscore.domain.session.repository.redis
2 : "Redis-backed opaque-token session store for the website auth flow.
3 :
4 : Sessions are referenced from the client by an HttpOnly cookie. The cookie
5 : value is a 32-char nanoid. Stored as JSON under `session:<id>` with a TTL."
6 : (:require
7 : [cheshire.core :as json]
8 : [clojure.string]
9 : [nano-id.core :as nano-id]
10 : [taoensso.carmine :as car]))
11 :
12 :
13 8 : (def ^:private session-alphabet
14 : "URL-safe alphabet (alphanumeric)."
15 : "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
16 :
17 :
18 8 : (def ^:private session-id-length 32)
19 :
20 :
21 12 : (defn- key-for [id] (str "session:" id))
22 :
23 :
24 8 : (def ^:private promote-script
25 : "Atomically upgrades a pending session by setting :user-uuid, preserving TTL.
26 : Returns 1 on success.
27 : Returns nil if the key is missing, has expired, or already has a user-uuid
28 : (prevents rebinding live sessions).
29 : Note: Redis cjson decodes JSON null as Lua false, so both nil (absent) and
30 : false (null) are treated as 'unset'."
31 : "local raw = redis.call('GET', KEYS[1])
32 : if not raw then return nil end
33 : local pttl = redis.call('PTTL', KEYS[1])
34 : if pttl <= 0 then return nil end
35 : local data = cjson.decode(raw)
36 : local existing = data['user-uuid']
37 : if existing ~= nil and existing ~= false then return nil end
38 : data['user-uuid'] = ARGV[1]
39 : redis.call('SET', KEYS[1], cjson.encode(data), 'PX', pttl)
40 : return 1")
41 :
42 :
43 14 : (defn- generate-id [] ((nano-id/custom session-alphabet session-id-length)))
44 :
45 :
46 4 : (defprotocol
47 : SessionRepository
48 : (create!
49 : [this user-uuid telegram-id ttl-seconds]
50 : "Creates a session for the given user. Returns the session-id (string).")
51 : (find-by-id
52 : [this session-id]
53 : "Returns the stored session map (with :user-uuid, :telegram-id, :created-at) or nil.")
54 : (promote!
55 : [this session-id user-uuid]
56 : "Upgrades a pending session (nil user-uuid) by setting its user-uuid. Preserves existing TTL.")
57 : (delete!
58 : [this session-id]
59 : "Deletes the session. Returns nil."))
60 :
61 :
62 4 : (defrecord
63 : RedisSessionRepository
64 : [redis]
65 : SessionRepository
66 :
67 : (create!
68 : [_ user-uuid telegram-id ttl-seconds]
69 6 : (let [id (generate-id)
70 10 : data {:user-uuid user-uuid
71 2 : :telegram-id telegram-id
72 4 : :created-at (System/currentTimeMillis)}]
73 94 : (car/wcar redis (car/set (key-for id) (json/generate-string data) "EX" (long ttl-seconds)))
74 2 : id))
75 :
76 : (find-by-id
77 : [_ session-id]
78 22 : (when (and session-id (not (clojure.string/blank? session-id)))
79 102 : (when-let [raw (car/wcar redis (car/get (key-for session-id)))] (json/parse-string raw true))))
80 :
81 : (promote!
82 : [_ session-id user-uuid]
83 0 : (when (and session-id (not (clojure.string/blank? session-id)))
84 0 : (car/wcar redis (car/eval promote-script 1 (key-for session-id) user-uuid))))
85 :
86 1 : (delete!
87 : [_ session-id]
88 11 : (when (and session-id (not (clojure.string/blank? session-id)))
89 41 : (car/wcar redis (car/del (key-for session-id))))
90 : nil))
91 :
92 :
93 4 : (defn make-repository
94 : "Creates a RedisSessionRepository."
95 : [redis]
96 12 : (->RedisSessionRepository redis))
|