forked from bungle/lua-resty-session
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.lua
More file actions
362 lines (300 loc) · 9.82 KB
/
redis.lua
File metadata and controls
362 lines (300 loc) · 9.82 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
---
-- Redis backend for session library
--
-- @module resty.session.redis
local utils = require "resty.session.utils"
local common = require "resty.session.redis.common"
local redis = require "resty.redis"
local setmetatable = setmetatable
local error = error
local tostring = tostring
local assert = assert
local null = ngx.null
local rand_bytes = utils.rand_bytes
local DEFAULT_HOST = "127.0.0.1"
local DEFAULT_PORT = 6379
local DEFAULT_DATABASE = 0
local KEY_SIZE = 32
local DEFAULT_IKM = rand_bytes(KEY_SIZE)
local DEFAULT_NONCE = rand_bytes(KEY_SIZE)
local SET = common.SET
local GET = common.GET
local UNLINK = common.UNLINK
local READ_METADATA = common.READ_METADATA
local get_pool do
local mac_sha256 = utils.mac_sha256
local derive_hmac_sha256_key = utils.derive_hmac_sha256_key
--- get connection pool name for the current session
-- must ensure the pool name of a session is unique for
-- each combination of host, port, database and password
get_pool = function(config)
local opts = config or {}
local mac do
if opts.password then
local key, err = derive_hmac_sha256_key(opts.ikm, opts.nonce)
if not key then
return nil, err
end
mac, err = mac_sha256(key, tostring(opts.password))
if not mac then
return nil, err
end
end
end
--- examples
-- with password: foo:redis-ssl-auth:6379:0:default:de0d22d975dcc57a721171fa079aa54a788df759c4a68ae75fd5f26c5490c9ca:true:false:redis:bar
-- without password: foo:redis-ssl-auth:6379:0:default::false:false:redis:bar
return (opts.prefix or "") .. ":" ..
(opts.host or "") .. ":" ..
(tostring(opts.port) or "") .. ":" ..
(tostring(opts.database) or "") .. ":" ..
(opts.username or "default") .. ":" ..
(mac or "") .. ":" ..
(tostring(opts.ssl) or "") .. ":" ..
(tostring(opts.ssl_verify) or "") .. ":" ..
(opts.server_name or "") .. ":" ..
(opts.suffix or "")
end
end
local function exec(self, func, ...)
local red = redis:new()
local connect_timeout = self.connect_timeout
local send_timeout = self.send_timeout
local read_timeout = self.read_timeout
if connect_timeout or send_timeout or read_timeout then
red:set_timeouts(connect_timeout, send_timeout, read_timeout)
end
local ok, err do
local socket = self.socket
-- ngx.log(ngx.NOTICE, "pool_name = ", self.options.pool)
if socket then
ok, err = red:connect(socket, self.options)
else
ok, err = red:connect(self.host, self.port, self.options)
end
end
if not ok then
return nil, err
end
if red:get_reused_times() == 0 then
local password = self.password
if password then
local username = self.username
if username then
ok, err = red:auth(username, password)
else
ok, err = red:auth(password)
end
if not ok then
red:close()
return nil, err
end
end
--- select database for new connection in case
-- there is no need to select before every Redis
-- operation as the connection pool is only shared
-- clients that use the same database
ok, err = red:select(self.database)
if not ok then
return nil, err
end
end
ok, err = func(self, red, ...)
if err then
red:close()
return nil, err
end
if not red:set_keepalive(self.keepalive_timeout) then
red:close()
end
if ok == null then
ok = nil
end
return ok, err
end
---
-- Storage
-- @section instance
local metatable = {}
metatable.__index = metatable
function metatable.__newindex()
error("attempt to update a read-only table", 2)
end
---
-- Store session data.
--
-- @function instance:set
-- @tparam string name cookie name
-- @tparam string key session key
-- @tparam string value session value
-- @tparam number ttl session ttl
-- @tparam number current_time current time
-- @tparam[opt] string old_key old session id
-- @tparam string stale_ttl stale ttl
-- @tparam[opt] table metadata table of metadata
-- @tparam boolean remember whether storing persistent session or not
-- @treturn true|nil ok
-- @treturn string error message
function metatable:set(...)
return exec(self, SET, ...)
end
---
-- Retrieve session data.
--
-- @function instance:get
-- @tparam string name cookie name
-- @tparam string key session key
-- @treturn string|nil session data
-- @treturn string error message
function metatable:get(...)
return exec(self, GET, ...)
end
---
-- Delete session data.
--
-- @function instance:delete
-- @tparam string name cookie name
-- @tparam string key session key
-- @tparam[opt] table metadata session meta data
-- @treturn boolean|nil session data
-- @treturn string error message
function metatable:delete(...)
return exec(self, UNLINK, ...)
end
---
-- Read session metadata.
--
-- @function instance:read_metadata
-- @tparam string name cookie name
-- @tparam string audience session key
-- @tparam string subject session key
-- @tparam number current_time current time
-- @treturn table|nil session metadata
-- @treturn string error message
function metatable:read_metadata(...)
return exec(self, READ_METADATA, ...)
end
local storage = {}
---
-- Configuration
-- @section configuration
---
-- Redis storage backend configuration
-- @field prefix Prefix for the keys stored in Redis.
-- @field suffix Suffix for the keys stored in Redis.
-- @field host The host to connect (defaults to `"127.0.0.1"`).
-- @field port The port to connect (defaults to `6379`).
-- @field socket The socket file to connect to (defaults to `nil`).
-- @field username The database username to authenticate.
-- @field password Password for authentication.
-- @field database The database to connect.
-- @field connect_timeout Controls the default timeout value used in TCP/unix-domain socket object's `connect` method.
-- @field send_timeout Controls the default timeout value used in TCP/unix-domain socket object's `send` method.
-- @field read_timeout Controls the default timeout value used in TCP/unix-domain socket object's `receive` method.
-- @field keepalive_timeout Controls the default maximal idle time of the connections in the connection pool.
-- @field pool A custom name for the connection pool being used.
-- @field pool_size The size of the connection pool.
-- @field backlog A queue size to use when the connection pool is full (configured with @pool_size).
-- @field ssl Enable SSL (defaults to `false`).
-- @field ssl_verify Verify server certificate (defaults to `nil`).
-- @field server_name The server name for the new TLS extension Server Name Indication (SNI).
-- @table configuration
---
-- Constructors
-- @section constructors
---
-- Create a Redis storage.
--
-- This creates a new Redis storage instance.
--
-- @function module.new
-- @tparam[opt] table configuration redis storage @{configuration}
-- @treturn table redis storage instance
function storage.new(configuration)
local prefix = configuration and configuration.prefix
local suffix = configuration and configuration.suffix
local host = configuration and configuration.host or DEFAULT_HOST
local port = configuration and configuration.port or DEFAULT_PORT
local socket = configuration and configuration.socket
local username = configuration and configuration.username
local password = configuration and configuration.password
local database = configuration and configuration.database or DEFAULT_DATABASE
local connect_timeout = configuration and configuration.connect_timeout
local send_timeout = configuration and configuration.send_timeout
local read_timeout = configuration and configuration.read_timeout
local keepalive_timeout = configuration and configuration.keepalive_timeout
local pool = configuration and configuration.pool
local pool_size = configuration and configuration.pool_size
local backlog = configuration and configuration.backlog
local ssl = configuration and configuration.ssl
local ssl_verify = configuration and configuration.ssl_verify
local server_name = configuration and configuration.server_name
local ikm = configuration and configuration.ikm
if ikm then
assert(#ikm == KEY_SIZE, "ikm size must be " .. KEY_SIZE)
end
local meta = {
ikm = ikm or DEFAULT_IKM,
nonce = DEFAULT_NONCE,
}
if not pool then
pool = get_pool {
host = host,
port = port,
database = database,
username = username,
password = password,
ssl = ssl,
ssl_verify = ssl_verify,
server_name = server_name,
ikm = meta.ikm,
nonce = meta.nonce,
prefix = prefix,
suffix = suffix,
}
end
if ssl ~= nil or ssl_verify ~= nil or server_name or pool or pool_size or backlog then
return setmetatable({
prefix = prefix,
suffix = suffix,
host = host,
port = port,
socket = socket,
username = username,
password = password,
database = database,
connect_timeout = connect_timeout,
send_timeout = send_timeout,
read_timeout = read_timeout,
keepalive_timeout = keepalive_timeout,
options = {
ssl = ssl,
ssl_verify = ssl_verify,
server_name = server_name,
pool = pool,
pool_size = pool_size,
backlog = backlog,
},
meta = meta,
}, metatable)
end
return setmetatable({
prefix = prefix,
suffix = suffix,
host = host,
port = port,
socket = socket,
username = username,
password = password,
database = database,
connect_timeout = connect_timeout,
send_timeout = send_timeout,
read_timeout = read_timeout,
keepalive_timeout = keepalive_timeout,
options = {
pool = pool,
},
meta = meta,
}, metatable)
end
return storage