Skip to content

Commit 44f50fa

Browse files
committed
Implement plulggable hmacs
1 parent 5eb399f commit 44f50fa

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ Strategy can be selected with configuration (if no configuration is present, the
191191
set $session_strategy regenerate;
192192
```
193193

194+
To implement a custom strategy, please checkout the existing ones.
195+
196+
## Pluggable HMAC Algorithms
197+
198+
If your strategy happens to be using `HMAC`, like the `default` and `regenerate` ones do,
199+
you can tell them what `HMAC` algorithm to use. At the moment only `HMAC SHA1` is available
200+
as that comes with OpenResty and works without additional dependencies. You may implement
201+
your own custom HMAC algorithms (preferrably binding to some existing crypto library,
202+
such as OpenSSL), and the strategies will pick up from there.
203+
204+
HMAC can be selected with configuration (if no configuration is present, the `sha1` strategy is picked up):
205+
206+
```nginx
207+
set $session_hmac sha1;
208+
```
194209

195210
## Pluggable Storage Adapters
196211

@@ -953,6 +968,7 @@ set $session_name session;
953968
set $session_secret 623q4hR325t36VsCD3g567922IC0073T;
954969
set $session_strategy default;
955970
set $session_storage cookie;
971+
set $session_hmac sha1;
956972
set $session_cipher aes;
957973
set $session_encoder base64;
958974
set $session_serializer json;

lib/resty/session.lua

+4
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ local function init()
178178
serializer = var.session_serializer or "json",
179179
encoder = var.session_encoder or "base64",
180180
cipher = var.session_cipher or "aes",
181+
hmac = var.session_hmac or "sha1",
181182
cookie = {
182183
persistent = enabled(var.session_cookie_persistent or false),
183184
discard = tonumber(var.session_cookie_discard) or 10,
@@ -222,12 +223,14 @@ function session.new(opts)
222223
local k, l = prequire("resty.session.ciphers.", y.cipher or z.cipher, "aes")
223224
local m, n = prequire("resty.session.storage.", y.storage or z.storage, "cookie")
224225
local o, p = prequire("resty.session.strategies.", y.strategy or z.strategy, "default")
226+
local q, r = prequire("resty.session.hmac.", y.hmac or z.hmac, "sha1")
225227
local self = {
226228
name = y.name or z.name,
227229
identifier = e,
228230
serializer = g,
229231
strategy = o,
230232
encoder = i,
233+
hmac = q,
231234
data = y.data or {},
232235
secret = y.secret or z.secret,
233236
cookie = {
@@ -254,6 +257,7 @@ function session.new(opts)
254257
if y[l] and not self[l] then self[l] = y[l] end
255258
if y[n] and not self[n] then self[n] = y[n] end
256259
if y[p] and not self[p] then self[p] = y[p] end
260+
if y[r] and not self[r] then self[r] = y[r] end
257261
self.cipher = k.new(self)
258262
self.storage = m.new(self)
259263
return setmetatable(self, session)

lib/resty/session/hmac/sha1.lua

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return ngx.hmac_sha1

lib/resty/session/strategies/default.lua

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local hmac = ngx.hmac_sha1
21
local type = type
32
local time = ngx.time
43
local concat = table.concat
@@ -7,18 +6,18 @@ local default = {}
76

87
function default:save(close)
98
local i, e, s = self.id, self.expires, self.storage
10-
local k = hmac(self.secret, i .. e)
9+
local k = self.hmac(self.secret, i .. e)
1110
local d = self.serializer.serialize(self.data)
12-
local h = hmac(k, concat{ i, e, d, self.key })
11+
local h = self.hmac(k, concat{ i, e, d, self.key })
1312
return s:save(i, e, self.cipher:encrypt(d, k, i, self.key), h, close)
1413
end
1514

1615
function default:open(cookie)
1716
local i, e, d, h = self.storage:open(cookie, self.cookie.lifetime)
1817
if i and e and e > time() and d and h then
19-
local k = hmac(self.secret, i .. e)
18+
local k = self.hmac(self.secret, i .. e)
2019
d = self.cipher:decrypt(d, k, i, self.key)
21-
if d and hmac(k, concat{ i, e, d, self.key }) == h then
20+
if d and self.hmac(k, concat{ i, e, d, self.key }) == h then
2221
d = self.serializer.deserialize(d)
2322
self.id = i
2423
self.expires = e

lib/resty/session/strategies/regenerate.lua

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local hmac = ngx.hmac_sha1
21
local type = type
32
local time = ngx.time
43
local concat = table.concat
@@ -14,18 +13,18 @@ function regenerate:save(close)
1413
i = self:identifier()
1514
self.id = i
1615

17-
local k = hmac(self.secret, i)
16+
local k = self.hmac(self.secret, i)
1817
local d = self.serializer.serialize(self.data)
19-
local h = hmac(k, concat{ i, d, self.key })
18+
local h = self.hmac(k, concat{ i, d, self.key })
2019
return s:save(i, e, self.cipher:encrypt(d, k, i, self.key), h, close)
2120
end
2221

2322
function regenerate:open(cookie)
2423
local i, e, d, h = self.storage:open(cookie, self.cookie.lifetime)
2524
if i and e and e > time() and d and h then
26-
local k = hmac(self.secret, i)
25+
local k = self.hmac(self.secret, i)
2726
d = self.cipher:decrypt(d, k, i, self.key)
28-
if d and hmac(k, concat{ i, d, self.key }) == h then
27+
if d and self.hmac(k, concat{ i, d, self.key }) == h then
2928
d = self.serializer.deserialize(d)
3029
self.id = i
3130
self.expires = e

lua-resty-session-dev-1.rockspec

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ build = {
2525
["resty.session.storage.memcached"] = "lib/resty/session/storage/memcached.lua",
2626
["resty.session.strategies.default"] = "lib/resty/session/strategies/default.lua",
2727
["resty.session.strategies.regenerate"] = "lib/resty/session/strategies/regenerate.lua",
28+
["resty.session.hmac.sha1"] = "lib/resty/session/hmac/sha1.lua",
2829
["resty.session.ciphers.aes"] = "lib/resty/session/ciphers/aes.lua",
2930
["resty.session.ciphers.none"] = "lib/resty/session/ciphers/none.lua",
3031
["resty.session.encoders.hex"] = "lib/resty/session/encoders/hex.lua",

0 commit comments

Comments
 (0)