@@ -16,12 +16,6 @@ local resty_session = require "resty.session"
1616local t = require (" api-umbrella.web-app.utils.gettext" ).gettext
1717local 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-
2519local supported_languages = table_keys (LOCALE_DATA )
2620
2721-- Custom error handler so we only show the default lapis debug details in
7872-- server-side control on expiring sessions, and it can't be spoofed even with
7973-- knowledge of the encryption secret key.
8074local 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" ,
87- 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
75+ storage = " postgres" ,
76+ postgres = {
77+ host = pg_utils .db_config .host ,
78+ port = pg_utils .db_config .port ,
79+ database = pg_utils .db_config .database ,
80+ username = pg_utils .db_config .user ,
81+ password = pg_utils .db_config .password ,
82+ ssl = pg_utils .db_config .ssl ,
83+ ssl_verify = pg_utils .db_config .ssl_verify ,
84+ ssl_required = pg_utils .db_config .ssl_required ,
85+ table = " api_umbrella.sessions" ,
86+ pool = " session_db" ,
9887 },
88+ secret = assert (config [" secret_key" ]),
89+ cookie_name = " _api_umbrella_session" ,
90+ cookie_same_site = " Lax" ,
91+ cookie_secure = true ,
92+ cookie_http_only = true ,
93+ idling_timeout = 30 * 60 , -- 30 minutes
94+ rolling_timeout = 0 , -- disabled, matches v3 renew=-1
95+ absolute_timeout = 12 * 60 * 60 , -- 12 hours
9996}
10097local function init_session_db (self )
10198 if not self .session_db then
@@ -113,22 +110,14 @@ end
113110-- session records in the database for the CSRF token).
114111local session_cookie_options = {
115112 storage = " cookie" ,
116- cipher = " api_umbrella" ,
117- hmac = " api_umbrella" ,
118- serializer = " api_umbrella" ,
119- identifier = " api_umbrella" ,
120- name = " _api_umbrella_session_client" ,
121113 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- },
114+ cookie_name = " _api_umbrella_session_client" ,
115+ cookie_same_site = " Lax" ,
116+ cookie_secure = true ,
117+ cookie_http_only = true ,
118+ idling_timeout = 0 , -- disabled for cookie-only sessions
119+ rolling_timeout = 1 * 60 * 60 , -- 1 hour
120+ absolute_timeout = 48 * 60 * 60 , -- 48 hours
132121}
133122local function init_session_cookie (self )
134123 if not self .session_cookie then
@@ -139,17 +128,19 @@ end
139128local function current_admin_from_session (self )
140129 local current_admin
141130 self :init_session_db ()
142- local _ , _ , open_err = self .session_db :start ()
143- if open_err then
144- if open_err == " session cookie idle time has passed" or open_err == " session cookie has expired" then
145- flash .session (self , " info" , t (" Your session expired. Please sign in again to continue." ))
146- else
147- ngx .log (ngx .ERR , " session open error: " , open_err )
131+ local ok , open_err = self .session_db :open ()
132+ if not ok then
133+ if open_err and open_err ~= " missing session cookie" then
134+ if open_err == " session idling timeout exceeded" or open_err == " session absolute timeout exceeded" then
135+ flash .session (self , " info" , t (" Your session expired. Please sign in again to continue." ))
136+ else
137+ ngx .log (ngx .ERR , " session open error: " , open_err )
138+ end
148139 end
149140 end
150141
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 " ]
142+ local admin_id = self .session_db : get ( " admin_id" )
143+ if admin_id then
153144 local admin = Admin :find ({ id = admin_id })
154145 if admin and not admin :is_access_locked () then
155146 current_admin = admin
0 commit comments