2121from oidcendpoint .util import build_endpoints
2222from oidcendpoint .util import importer
2323
24- LOGGER = logging .getLogger (__name__ )
24+ logger = logging .getLogger (__name__ )
2525
2626
2727def add_path (url , path ):
@@ -67,13 +67,36 @@ def init_service(conf, endpoint_context=None):
6767 return conf ["class" ](** kwargs )
6868
6969
70+ def get_token_handlers (conf ):
71+ th_args = conf .get ("token_handler_args" , None )
72+ if not th_args :
73+ # create 3 keys
74+ keydef = [
75+ {"type" : "oct" , "bytes" : "24" , "use" : ["enc" ], "kid" : "code" },
76+ {"type" : "oct" , "bytes" : "24" , "use" : ["enc" ], "kid" : "token" },
77+ {"type" : "oct" , "bytes" : "24" , "use" : ["enc" ], "kid" : "refresh" },
78+ ]
79+
80+ jwks_def = {
81+ "private_path" : "private/token_jwks.json" ,
82+ "key_defs" : keydef ,
83+ "read_only" : False ,
84+ }
85+ th_args = {"jwks_def" : jwks_def }
86+ for typ , tid in [("code" , 600 ), ("token" , 3600 ), ("refresh" , 86400 )]:
87+ th_args [typ ] = {"lifetime" : tid }
88+
89+ return th_args
90+
91+
7092class EndpointContext :
7193 def __init__ (
7294 self ,
7395 conf ,
7496 keyjar = None ,
7597 client_db = None ,
7698 session_db = None ,
99+ sso_db = None ,
77100 cwd = "" ,
78101 cookie_dealer = None ,
79102 httpc = None ,
@@ -112,7 +135,13 @@ def __init__(
112135 # arguments for endpoints add-ons
113136 self .args = {}
114137
138+ # session db
115139 self ._sub_func = None
140+ self .sdb = session_db
141+ if not self .sdb :
142+ self .set_session_db (conf , sso_db )
143+ #
144+
116145 self .scope2claims = SCOPE2CLAIMS
117146
118147 if cookie_name :
@@ -175,11 +204,6 @@ def __init__(
175204 if _func :
176205 _func (self .conf )
177206
178- if session_db :
179- self .sdb = session_db
180- else :
181- self .do_session_db (conf )
182-
183207 _cap = self .do_endpoints (conf )
184208
185209 for item in ["userinfo" , "login_hint_lookup" , "login_hint2acrs" ,
@@ -199,6 +223,14 @@ def __init__(
199223 # client registration access tokens
200224 self .registration_access_token = {}
201225
226+ def set_session_db (self , conf , sso_db = None ):
227+ # this populate self.sdb
228+ sso_db = sso_db if sso_db else SSODb ()
229+ self .do_session_db (conf , sso_db )
230+ # this append useinfo db to the session db
231+ self .do_userinfo (conf )
232+ logger .debug ('Session DB: {}' .format (self .sdb .__dict__ ))
233+
202234 def do_add_on (self , conf ):
203235 if 'add_on' in conf :
204236 for spec in conf ["add_on" ].values ():
@@ -233,8 +265,10 @@ def do_userinfo(self, conf):
233265 except KeyError :
234266 pass
235267 else :
236- self .userinfo = init_user_info (_conf , self .cwd )
237- self .sdb .userinfo = self .userinfo
268+ if self .sdb :
269+ self .userinfo = init_user_info (_conf , self .cwd )
270+ self .sdb .userinfo = self .userinfo
271+
238272
239273 def do_id_token (self , conf ):
240274 try :
@@ -262,44 +296,23 @@ def do_cookie_dealer(self, conf):
262296 self .cookie_dealer = init_service (_conf )
263297
264298 def do_sub_func (self , conf ):
265- try :
266- _conf = conf ["sub_func" ]
267- except KeyError :
268- self ._sub_func = None
269- else :
270- self ._sub_func = {}
271- for key , args in _conf .items ():
272- if "class" in args :
273- self ._sub_func [key ] = init_service (args )
274- elif "function" in args :
275- if isinstance (args ["function" ], str ):
276- self ._sub_func [key ] = util .importer (args ["function" ])
277- else :
278- self ._sub_func [key ] = args ["function" ]
279-
280- def do_session_db (self , conf ):
281- try :
282- _th_args = conf ["token_handler_args" ]
283- except KeyError :
284- # create 3 keys
285- keydef = [
286- {"type" : "oct" , "bytes" : "24" , "use" : ["enc" ], "kid" : "code" },
287- {"type" : "oct" , "bytes" : "24" , "use" : ["enc" ], "kid" : "token" },
288- {"type" : "oct" , "bytes" : "24" , "use" : ["enc" ], "kid" : "refresh" },
289- ]
290-
291- jwks_def = {
292- "private_path" : "private/token_jwks.json" ,
293- "key_defs" : keydef ,
294- "read_only" : False ,
295- }
296-
297- _th_args = {"jwks_def" : jwks_def }
298- for typ , tid in [("code" , 600 ), ("token" , 3600 ), ("refresh" , 86400 )]:
299- _th_args [typ ] = {"lifetime" : tid }
299+ _conf = conf .get ("sub_func" , {})
300+ self ._sub_func = {}
301+ for key , args in _conf .items ():
302+ if "class" in args :
303+ self ._sub_func [key ] = init_service (args )
304+ elif "function" in args :
305+ if isinstance (args ["function" ], str ):
306+ self ._sub_func [key ] = util .importer (args ["function" ])
307+ else :
308+ self ._sub_func [key ] = args ["function" ]
300309
310+ def do_session_db (self , conf , sso_db ):
311+ th_args = get_token_handlers (conf )
301312 self .sdb = create_session_db (
302- self , _th_args , db = None , sso_db = SSODb (), sub_func = self ._sub_func
313+ self , th_args , db = None ,
314+ sso_db = sso_db ,
315+ sub_func = self ._sub_func
303316 )
304317
305318 def do_endpoints (self , conf ):
0 commit comments