aboutsummaryrefslogtreecommitdiffstats
path: root/server/resty/session/strategies/default.lua
blob: a43ef5a676320060920da2bf99b1ac63c149f25f (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
local type   = type
local concat = table.concat

local strategy = {}

function strategy.load(session, cookie, key, keep_lock)
  local storage    = session.storage
  local id         = cookie.id
  local id_encoded = session.encoder.encode(id)

  local data, err, tag
  if storage.open then
    data, err = storage:open(id_encoded, keep_lock)
    if not data then
      return nil, err or "cookie data was not found"
    end

  else
    data = cookie.data
  end

  local expires   = cookie.expires
  local usebefore = cookie.usebefore
  local hash      = cookie.hash

  if not key then
    key = concat{ id, expires, usebefore }
  end

  local hkey = session.hmac(session.secret, key)

  data, err, tag = session.cipher:decrypt(data, hkey, id, session.key, hash)
  if not data then
    if storage.close then
      storage:close(id_encoded)
    end

    return nil, err or "unable to decrypt data"
  end

  if tag then
    if tag ~= hash then
      if storage.close then
        storage:close(id_encoded)
      end

      return nil, "cookie has invalid tag"
    end

  else
    local input = concat{ key, data, session.key }
    if session.hmac(hkey, input) ~= hash then
      if storage.close then
        storage:close(id_encoded)
      end

      return nil, "cookie has invalid signature"
    end
  end

  data, err = session.compressor:decompress(data)
  if not data then
    if storage.close then
      storage:close(id_encoded)
    end

    return nil, err or "unable to decompress data"
  end

  data, err = session.serializer.deserialize(data)
  if not data then
    if storage.close then
      storage:close(id_encoded)
    end

    return nil, err or "unable to deserialize data"
  end

  session.id        = id
  session.expires   = expires
  session.usebefore = usebefore
  session.data      = type(data) == "table" and data or {}
  session.present   = true

  return true
end

function strategy.open(session, cookie, keep_lock)
  return strategy.load(session, cookie, nil, keep_lock)
end

function strategy.start(session)
  local storage = session.storage
  if not storage.start then
    return true
  end

  local id_encoded = session.encoder.encode(session.id)

  local ok, err = storage:start(id_encoded)
  if not ok then
    return nil, err or "unable to start session"
  end

  return true
end

function strategy.modify(session, action, close, key)
  local id         = session.id
  local id_encoded = session.encoder.encode(id)
  local storage    = session.storage
  local expires    = session.expires
  local usebefore  = session.usebefore
  local ttl        = expires - session.now

  if ttl <= 0 then
    if storage.close then
      storage:close(id_encoded)
    end

    return nil, "session is already expired"
  end

  if not key then
    key = concat{ id, expires, usebefore }
  end

  local data, err = session.serializer.serialize(session.data)
  if not data then
    if close and storage.close then
      storage:close(id_encoded)
    end

    return nil, err or "unable to serialize data"
  end

  data, err = session.compressor:compress(data)
  if not data then
    if close and storage.close then
      storage:close(id_encoded)
    end

    return nil, err or "unable to compress data"
  end

  local hkey = session.hmac(session.secret, key)

  local encrypted_data, tag
  encrypted_data, err, tag = session.cipher:encrypt(data, hkey, id, session.key)
  if not encrypted_data then
    if close and storage.close then
      storage:close(id_encoded)
    end

    return nil, err
  end

  local hash
  if tag then
    hash = tag
  else
    -- it would be better to calculate signature from encrypted_data,
    -- but this is kept for backward compatibility
    hash = session.hmac(hkey, concat{ key, data, session.key })
  end

  if action == "save" and storage.save then
    local ok
    ok, err = storage:save(id_encoded, ttl, encrypted_data, close)
    if not ok then
      return nil, err
    end
  elseif close and storage.close then
    local ok
    ok, err = storage:close(id_encoded)
    if not ok then
      return nil, err
    end
  end

  if usebefore then
    expires = expires .. ":" .. usebefore
  end

  hash = session.encoder.encode(hash)

  local cookie
  if storage.save then
    cookie = concat({ id_encoded, expires, hash }, "|")
  else
    local encoded_data = session.encoder.encode(encrypted_data)
    cookie = concat({ id_encoded, expires, encoded_data, hash }, "|")
  end

  return cookie
end

function strategy.touch(session, close)
  return strategy.modify(session, "touch", close)
end

function strategy.save(session, close)
  return strategy.modify(session, "save", close)
end

function strategy.destroy(session)
  local id = session.id
  if id then
    local storage = session.storage
    if storage.destroy then
      return storage:destroy(session.encoder.encode(id))
    elseif storage.close then
      return storage:close(session.encoder.encode(id))
    end
  end

  return true
end

function strategy.close(session)
  local id = session.id
  if id then
    local storage = session.storage
    if storage.close then
      return storage:close(session.encoder.encode(id))
    end
  end

  return true
end

return strategy