Skip to content

Commit f3739e7

Browse files
committed
Release 1.4.
1 parent 248bc59 commit f3739e7

File tree

3 files changed

+88
-23
lines changed

3 files changed

+88
-23
lines changed

CHANGES

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11

2+
Changes with lua-resty-session 1.4 26 Nov 2014
3+
4+
*) Bugfix: Fixed an issue where session configurations did get cached
5+
on a module level. This issue is discussed in pull-request #4:
6+
https://github.com/bungle/lua-resty-session/pull/4
7+
8+
Thanks @kipras.
9+
10+
*) Feature: Added session.new function.
11+
12+
*) Change: session.start{ ... } (a call with config parameters) works
13+
now as expected.
14+
15+
*) Change: session.start now returns additional extra boolean parameter
16+
that can be used to check if the session is s new session (false) or
17+
s previously started one (true).
18+
19+
*) Documentation: Added documentation about Nginx configuration used
20+
as defaults (not read on every request), and documented session.new.
21+
222
Changes with lua-resty-session 1.3 14 Nov 2014
323

424
*) Feature: Added support for persistent sessions. See issue #2.

README.md

+65-20
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,54 @@ could be added to this module as well (all contributions are welcomed).
116116

117117
### Functions and Methods
118118

119-
#### table session.start(opts or nil)
120-
121-
With this function you can start a new session. It will create a new session Lua `table` on each call.
122-
Right now you should only start session once per request as calling this function repeatedly will overwrite the previously
123-
started session cookie and session data. This function will return a new session `table` as a result. If the session cookie
124-
is supplied with user's HTTP(S) client then this function validates the supplied session cookie. If validation
125-
is successful, the user supplied session data will be used (if not, a new session is generated with empty data).
126-
You may supply optional session configuration variables with `opts` argument, but be aware that many of these
127-
will only have effect if the session is a fresh session (i.e. not loaded from user supplied cookie). This function
128-
does also manage session cookie renewing configured with `$session_cookie_renew`. E.g. it will send a new cookie
129-
with a new expiration time if the following is met `session.expires - now < session.cookie.renew`.
119+
#### table session.new(opts or nil)
120+
121+
With this function you can create a new session table (i.e. the actual session instance). This allows
122+
you to generate session table first, and set invidual configuration before calling `session:start()`.
123+
You can also pass in `opts` Lua `table` with the configurations.
124+
125+
```lua
126+
local session = require("resty.session").new()
127+
-- set the configuration parameters before calling start
128+
session.cookie.domain = "mydomain.com"
129+
-- call start before setting session.data parameters
130+
session:start()
131+
session.data.uid = 1
132+
-- save session and update the cookie to be sent to the client
133+
session:save()
134+
```
135+
136+
This is equivalent to this:
137+
138+
```lua
139+
local session = require("resty.session").new{ cookie = { domain = "mydomain.com" } }
140+
session:start()
141+
session.data.uid = 1
142+
session:save()
143+
```
144+
145+
As well as with this:
146+
147+
```lua
148+
local session = require("resty.session").start{ cookie = { domain = "mydomain.com" } }
149+
session.data.uid = 1
150+
session:save()
151+
```
152+
153+
#### table, boolean session.start(opts or nil)
154+
155+
With this function you can start a new session. It will create a new session Lua `table` on each call (unless called with
156+
colon `:` as in examples above with `session.new`). Right now you should only start session once per request as calling
157+
this function repeatedly will overwrite the previously started session cookie and session data. This function will return
158+
a (new) session `table` as a result. If the session cookie is supplied with user's HTTP(S) client then this function
159+
validates the supplied session cookie. If validation is successful, the user supplied session data will be used
160+
(if not, a new session is generated with empty data). You may supply optional session configuration variables
161+
with `opts` argument, but be aware that many of these will only have effect if the session is a fresh session
162+
(i.e. not loaded from user supplied cookie). This function does also manage session cookie renewing configured
163+
with `$session_cookie_renew`. E.g. it will send a new cookie with a new expiration time if the following is
164+
met `session.expires - now < session.cookie.renew`. The second `boolean` return argument will be `true` if the user
165+
client send a valid cookie (meaning that session was already started on some earlier request), and `false` if the
166+
new session was created (either because user client didn't send a cookie or that the cookie was not a valid one).
130167

131168
```lua
132169
local session = require("resty.session").start()
@@ -136,8 +173,8 @@ local session = require("resty.session").start{ identifier = { length = 32 }}
136173

137174
#### boolean session:regenerate(flush or nil)
138175

139-
This function regenerates a session. It will generate a new session identifier and optionally flush the
140-
session data if `flush` argument evaluates `true`. It will automatically `session:save` which
176+
This function regenerates a session. It will generate a new session identifier (`session.id`) and optionally
177+
flush the session data if `flush` argument evaluates `true`. It will automatically `session:save` which
141178
means that a new expires flag is set on the cookie, and the data is encrypted with the new parameters. With
142179
client side sessions (server side sessions are not yet supported) this overwrites the current cookie with
143180
a new one (but it doesn't invalidate the old one as there is no state held on server side - invalidation
@@ -153,10 +190,11 @@ session:regenerate(true)
153190

154191
#### boolean session:save()
155192

156-
This function saves the session and sends a new cookie to client (with a new expiration time and encrypted data).
157-
You need to call this function whenever you want to save the changes made to `session.data` table. It is
158-
advised that you call this function only once per request (no need to encrypt and set cookie many times).
159-
This function returns a boolean value if everything went as planned (you may assume that it is always the case).
193+
This function saves the session and sends (not immediate though, as actual sending is handled by Nginx/OpenResty)
194+
a new cookie to client (with a new expiration time and encrypted data). You need to call this function whenever
195+
you want to save the changes made to `session.data` table. It is advised that you call this function only once
196+
per request (no need to encrypt and set cookie many times). This function returns a boolean value if everything
197+
went as planned (you may assume that it is always the case).
160198

161199
```lua
162200
local session = require("resty.session").start()
@@ -191,7 +229,7 @@ This can be configured with Nginx `set $session_identifier_length 16;`.
191229
#### string session.key
192230

193231
`session.key` holds the HMAC key. It is automatically generated. Nginx configuration like
194-
`set $session_check_ua on;`, `set $session_check_scheme on;` and `set $session_check_addr on;`
232+
`set $session_check_ssi on;`, `set $session_check_ua on;`, `set $session_check_scheme on;` and `set $session_check_addr on;`
195233
will have effect on the generated key.
196234

197235
#### table session.data
@@ -238,8 +276,8 @@ with Nginx `set $session_cookie_renew 600;` (600 seconds is the default value).
238276

239277
`session.cookie.lifetime` holds the cookie lifetime in seconds in the future. By default this is set
240278
to 3,600 seconds. This can be configured with Nginx `set $session_cookie_lifetime 3600;`. This does not
241-
set cookie's expiration time on session only (by default), but it is used if the cookies are configured
242-
persistent with `session.cookie.persistent`.
279+
set cookie's expiration time on session only (by default) cookies, but it is used if the cookies are
280+
configured persistent with `session.cookie.persistent == true`.
243281

244282
#### string session.cookie.path
245283

@@ -311,6 +349,13 @@ that was used when the cookie was originally delivered.
311349

312350
## Nginx Configuration Variables
313351

352+
You can set default configuration parameters directly from Nginx configuration. It's *IMPORTANT* to understand
353+
that these are read only once (not on every request), for performance reasons. This is especially important if
354+
you run multiple sites (with different configurations) on the same Nginx server. You can of course set the common
355+
parameters on Nginx configuration even on that case. But if you are really supporting multiple site with different
356+
configurations (e.g. different `session.secret` on each site), you should set these in code (see: `session.new`
357+
and `session.start`).
358+
314359
Here is a list of Nginx configuration variables that you can use to control `lua-resty-session`:
315360

316361
```nginx

lib/resty/session.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ local defaults = {
156156
defaults.secret = ngx_var.session_secret or random(defaults.cipher.size / 8)
157157

158158
local session = {
159-
_VERSION = "1.4-rc1"
159+
_VERSION = "1.4"
160160
}
161161
session.__index = session
162162

@@ -230,13 +230,13 @@ function session.start(opts)
230230
if self.expires - now < self.cookie.renew then
231231
self:save()
232232
end
233-
return self
233+
return self, true
234234
end
235235
end
236236
end
237237
if type(self.data) ~= "table" then self.data = {} end
238238
self:regenerate()
239-
return self
239+
return self, false
240240
end
241241

242242
function session:regenerate(flush)

0 commit comments

Comments
 (0)