Skip to content

Commit 70916a6

Browse files
committed
WIP: Upgrading to lua-resty-session v4.
1 parent d9064a9 commit 70916a6

File tree

14 files changed

+126
-245
lines changed

14 files changed

+126
-245
lines changed

src/api-umbrella/web-app/actions/admin/sessions.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,8 @@ end
139139

140140
function _M.destroy(self)
141141
self:init_session_db()
142-
local _, _, open_err = self.session_db:start()
143-
if open_err then
144-
ngx.log(ngx.ERR, "session open error: ", open_err)
145-
end
146-
147-
local sign_in_provider = self.session_db.data["sign_in_provider"]
142+
self.session_db:open()
143+
local sign_in_provider = self.session_db:get("sign_in_provider")
148144
self.session_db:destroy()
149145

150146
flash.session(self, "info", t("Signed out successfully."))
@@ -173,8 +169,8 @@ function _M.logout_callback(self)
173169
local state = ngx.var.arg_state
174170
if state then
175171
self:init_session_cookie()
176-
self.session_cookie:start()
177-
local session_state = self.session_cookie.data["openid_connect_state"]
172+
self.session_cookie:open()
173+
local session_state = self.session_cookie:get("openid_connect_state")
178174
if state ~= session_state then
179175
ngx.log(ngx.WARN, "state from argument: " .. (state or "nil") .. " does not match state restored from session: " .. (session_state or "nil"))
180176

src/api-umbrella/web-app/app.lua

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ local resty_session = require "resty.session"
1616
local t = require("api-umbrella.web-app.utils.gettext").gettext
1717
local table_keys = require("pl.tablex").keys
1818

19-
require "resty.session.ciphers.api_umbrella"
20-
require "resty.session.hmac.api_umbrella"
21-
require "resty.session.identifiers.api_umbrella"
22-
require "resty.session.storage.api_umbrella_db"
23-
require "resty.session.serializers.api_umbrella"
24-
2519
local supported_languages = table_keys(LOCALE_DATA)
2620

2721
-- Custom error handler so we only show the default lapis debug details in
@@ -78,24 +72,15 @@ end
7872
-- server-side control on expiring sessions, and it can't be spoofed even with
7973
-- knowledge of the encryption secret key.
8074
local session_db_options = {
81-
storage = "api_umbrella_db",
82-
cipher = "api_umbrella",
83-
hmac = "api_umbrella",
84-
serializer = "api_umbrella",
85-
identifier = "api_umbrella",
86-
name = "_api_umbrella_session",
75+
storage = "postgres",
76+
postgres = pg_utils.db_config,
8777
secret = assert(config["secret_key"]),
88-
random = {
89-
length = 40,
90-
},
91-
cookie = {
92-
samesite = "Lax",
93-
secure = true,
94-
httponly = true,
95-
idletime = 30 * 60, -- 30 minutes
96-
lifetime = 12 * 60 * 60, -- 12 hours
97-
renew = -1, -- Disable renew
98-
},
78+
cookie_name = "_api_umbrella_session",
79+
cookie_same_site = "Lax",
80+
cookie_secure = true,
81+
cookie_http_only = true,
82+
idling_timeout = 30 * 60, -- 30 minutes
83+
absolute_timeout = 12 * 60 * 60, -- 12 hours
9984
}
10085
local function init_session_db(self)
10186
if not self.session_db then
@@ -113,22 +98,13 @@ end
11398
-- session records in the database for the CSRF token).
11499
local session_cookie_options = {
115100
storage = "cookie",
116-
cipher = "api_umbrella",
117-
hmac = "api_umbrella",
118-
serializer = "api_umbrella",
119-
identifier = "api_umbrella",
120-
name = "_api_umbrella_session_client",
121101
secret = assert(config["secret_key"]),
122-
random = {
123-
length = 40,
124-
},
125-
cookie = {
126-
samesite = "Lax",
127-
secure = true,
128-
httponly = true,
129-
lifetime = 48 * 60 * 60, -- 48 hours
130-
renew = 1 * 60 * 60, -- 1 hour
131-
},
102+
cookie_name = "_api_umbrella_session_client",
103+
cookie_same_site = "Lax",
104+
cookie_secure = true,
105+
cookie_http_only = true,
106+
rolling_timeout = 1 * 60 * 60, -- 1 hour
107+
absolute_timeout = 48 * 60 * 60, -- 48 hours
132108
}
133109
local function init_session_cookie(self)
134110
if not self.session_cookie then
@@ -139,17 +115,19 @@ end
139115
local function current_admin_from_session(self)
140116
local current_admin
141117
self:init_session_db()
142-
local _, _, open_err = self.session_db:start()
143-
if open_err then
118+
local _, open_err = self.session_db:open()
119+
if open_err and open_err ~= "missing session cookie" then
144120
if open_err == "session cookie idle time has passed" or open_err == "session cookie has expired" then
145121
flash.session(self, "info", t("Your session expired. Please sign in again to continue."))
146122
else
147123
ngx.log(ngx.ERR, "session open error: ", open_err)
148124
end
125+
126+
return nil
149127
end
150128

151-
if self.session_db and self.session_db.data and self.session_db.data["admin_id"] then
152-
local admin_id = self.session_db.data["admin_id"]
129+
local admin_id = self.session_db:get("admin_id")
130+
if admin_id then
153131
local admin = Admin:find({ id = admin_id })
154132
if admin and not admin:is_access_locked() then
155133
current_admin = admin

src/api-umbrella/web-app/hooks/init_preload_modules.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ require "resty.http"
178178
require "resty.mlcache"
179179
require "resty.openidc"
180180
require "resty.session"
181-
require "resty.session.ciphers.api_umbrella"
182-
require "resty.session.hmac.api_umbrella"
183-
require "resty.session.identifiers.api_umbrella"
184-
require "resty.session.serializers.api_umbrella"
185-
require "resty.session.storage.api_umbrella_db"
186181
require "resty.uuid"
187182
require "resty.validation"
188183
require "resty.validation.ngx"

src/api-umbrella/web-app/utils/auth_external_oauth2.lua

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ end
8989
function _M.authorize(self, strategy_name, url, params)
9090
local state = random_token(64)
9191
self:init_session_cookie()
92-
self.session_cookie:start()
93-
self.session_cookie.data["oauth2_state"] = state
92+
self.session_cookie:open()
93+
self.session_cookie:set("oauth2_state", state)
9494
self.session_cookie:save()
9595

9696
local callback_url = build_url(auth_external_path(strategy_name, "/callback"))
@@ -119,17 +119,14 @@ function _M.userinfo(self, strategy_name, options)
119119
end
120120

121121
self:init_session_cookie()
122-
local _, _, open_err = self.session_cookie:start()
123-
if open_err then
124-
ngx.log(ngx.ERR, "session open error: ", open_err)
125-
end
122+
self.session_cookie:open()
123+
local stored_state = self.session_cookie:get("oauth2_state")
126124

127-
if not self.session_cookie or not self.session_cookie.data or not self.session_cookie.data["oauth2_state"] then
125+
if not stored_state then
128126
ngx.log(ngx.ERR, "oauth2 state not available")
129127
return nil, t("Cross-site request forgery detected")
130128
end
131129

132-
local stored_state = self.session_cookie.data["oauth2_state"]
133130
local state = self.params["state"]
134131
if state ~= stored_state then
135132
ngx.log(ngx.ERR, "oauth2 state does not match")

src/api-umbrella/web-app/utils/auth_external_openid_connect.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ function _M.authenticate(self, strategy_name, callback)
4343
-- Call the provider-specific callback logic, which should handle
4444
-- authorizing the API Umbrella session and redirecting as appropriate.
4545
callback({
46-
id_token = session["data"]["id_token"],
47-
user = session["data"]["user"],
46+
id_token = session:get("id_token"),
47+
user = session:get("user"),
4848
})
4949

5050
-- This shouldn't get hit, since callback should perform it's own
@@ -82,14 +82,15 @@ function _M.authenticate(self, strategy_name, callback)
8282
end
8383
if discovery and discovery["end_session_endpoint"] then
8484
-- Generate the state parameter to send.
85+
local openid_connect_state = random_token(64)
8586
self:init_session_cookie()
86-
self.session_cookie:start()
87-
self.session_cookie.data["openid_connect_state"] = random_token(64)
87+
self.session_cookie:open()
88+
self.session_cookie:set("openid_connect_state", openid_connect_state)
8889
self.session_cookie:save()
8990

9091
-- Add the "state" param to the logout URL.
9192
local extra_logout_args = {
92-
state = self.session_cookie.data["openid_connect_state"]
93+
state = openid_connect_state,
9394
}
9495

9596
-- Add the "client_id" param to the logout URL if id_token_hint won't be

src/api-umbrella/web-app/utils/csrf.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ local _M = {}
1717

1818
function _M.generate_token(self)
1919
self:init_session_cookie()
20-
self.session_cookie:start()
21-
local csrf_token_key = self.session_cookie.data["csrf_token_key"]
22-
local csrf_token_iv = self.session_cookie.data["csrf_token_iv"]
20+
self.session_cookie:open()
21+
local csrf_token_key = self.session_cookie:get("csrf_token_key")
22+
ngx.log(ngx.ERR, "-DEBUG- GET generate csrf_token_key: ", csrf_token_key)
23+
local csrf_token_iv = self.session_cookie:get("csrf_token_iv")
2324
if not csrf_token_key or not csrf_token_iv then
2425
if not csrf_token_key then
2526
csrf_token_key = random_token(40)
26-
self.session_cookie.data["csrf_token_key"] = csrf_token_key
27+
ngx.log(ngx.ERR, "-DEBUG- SET generate csrf_token_key: ", csrf_token_key)
28+
self.session_cookie:set("csrf_token_key", csrf_token_key)
2729
end
2830

2931
if not csrf_token_iv then
3032
csrf_token_iv = random_token(12)
31-
self.session_cookie.data["csrf_token_iv"] = csrf_token_iv
33+
self.session_cookie:set("csrf_token_iv", csrf_token_iv)
3234
end
3335

3436
self.session_cookie:save()
@@ -41,12 +43,11 @@ end
4143

4244
local function validate_token(self)
4345
self:init_session_cookie()
44-
local _, _, open_err = self.session_cookie:start()
45-
if open_err then
46-
ngx.log(ngx.ERR, "session open error: ", open_err)
47-
end
48-
49-
local key = self.session_cookie.data["csrf_token_key"]
46+
self.session_cookie:open()
47+
local key = self.session_cookie:get("csrf_token_key")
48+
ngx.log(ngx.ERR, "-DEBUG- GET csrf_token_key: ", key)
49+
ngx.log(ngx.ERR, "-DEBUG- ngx.var.cookie__api_umbrella_session_client", ngx.var.cookie__api_umbrella_session_client)
50+
ngx.log(ngx.ERR, "-DEBUG- ngx.var.cookie__api_umbrella_session", ngx.var.cookie__api_umbrella_session)
5051
if not key then
5152
return false, "Missing CSRF token key"
5253
end

src/api-umbrella/web-app/utils/flash.lua

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ function _M.session(self, flash_type, message, options)
1414
data["message"] = message
1515

1616
self:init_session_cookie()
17-
self.session_cookie:start()
18-
if not self.session_cookie.data["flash"] then
19-
self.session_cookie.data["flash"] = {}
17+
self.session_cookie:open()
18+
local flash = self.session_cookie:get("flash")
19+
if not flash then
20+
flash = {}
2021
end
21-
self.session_cookie.data["flash"][flash_type] = data
22+
flash[flash_type] = data
23+
self.session_cookie:set("flash", flash)
2224
self.session_cookie:save()
2325
end
2426

@@ -27,17 +29,14 @@ function _M.setup(self)
2729

2830
self.restore_flashes = function()
2931
self:init_session_cookie()
30-
local _, _, open_err = self.session_cookie:start()
31-
if open_err then
32-
ngx.log(ngx.ERR, "session open error: ", open_err)
33-
end
34-
35-
if self.session_cookie.data and not is_empty(self.session_cookie.data["flash"]) then
36-
for flash_type, data in pairs(self.session_cookie.data["flash"]) do
32+
self.session_cookie:open()
33+
local flash_value = self.session_cookie:get("flash")
34+
if not is_empty(flash_value) then
35+
for flash_type, data in pairs(flash_value) do
3736
_M.now(self, flash_type, data["message"], data)
3837
end
3938

40-
self.session_cookie.data["flash"] = nil
39+
self.session_cookie:set("flash", nil)
4140
self.session_cookie:save()
4241
end
4342

src/api-umbrella/web-app/utils/login_admin.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ return function(self, admin, provider)
1717
db.query("COMMIT")
1818

1919
self:init_session_db()
20-
self.session_db:start()
21-
self.session_db.data["admin_id"] = admin_id
22-
self.session_db.data["sign_in_provider"] = provider
20+
self.session_db:open()
21+
self.session_db:set("admin_id", admin_id)
22+
self.session_db:set("sign_in_provider", provider)
2323
self.session_db:save()
2424

2525
return build_url("/admin/#/login")

src/resty/session/ciphers/api_umbrella.lua

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/resty/session/hmac/api_umbrella.lua

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)