77from oidcmsg .message import OPTIONAL_LIST_OF_STRINGS
88from oidcmsg .message import SINGLE_OPTIONAL_STRING
99from oidcmsg .message import SINGLE_REQUIRED_STRING
10+ from oidcmsg .message import SINGLE_OPTIONAL_JSON
1011from oidcmsg .message import msg_ser
1112from oidcmsg .oidc import AuthorizationRequest
1213
@@ -144,12 +145,16 @@ def __getitem__(self, item):
144145 if _info is None :
145146 sid = self .handler .sid (item )
146147 _info = self ._db .get (sid )
147-
148- if _info :
148+ if _info :
149+ _si = SessionInfo ().from_json (_info )
150+ if any (item == val for val in _si .values ()):
151+ _si ['sid' ] = sid
152+ return _si
153+ else :
149154 _si = SessionInfo ().from_json (_info )
155+ _si ['sid' ] = item
150156 return _si
151- else :
152- return None
157+ raise KeyError
153158
154159 def __setitem__ (self , sid , instance ):
155160 try :
@@ -268,9 +273,9 @@ def do_sub(
268273
269274 return sub
270275
271- def is_valid (self , item ):
276+ def is_valid (self , typ , item ):
272277 try :
273- return not self . handler . is_black_listed ( item )
278+ return typ in self [ item ]
274279 except KeyError :
275280 return False
276281
@@ -295,9 +300,8 @@ def replace_token(self, sid, sinfo, token_type):
295300
296301 if token_type in self .handler :
297302 refresh_token = self .handler [token_type ](sid , sinfo = sinfo )
298- # blacklist the old is there is one
299- if sinfo .get (token_type ):
300- self .handler [token_type ].black_list (sinfo [token_type ])
303+ # blacklist the old
304+ self .revoke_token (sid , token_type , sinfo )
301305
302306 sinfo [token_type ] = refresh_token
303307 return sinfo
@@ -333,25 +337,17 @@ def upgrade_to_token(
333337 :return: The session information as a SessionInfo instance
334338 """
335339 if grant :
340+ # The caller is responsible for checking if the access code exists.
336341 _tinfo = self .handler ["code" ].info (grant )
337342
338- session_info = self [_tinfo ["sid" ]]
339-
340- if self .handler ["code" ].is_black_listed (grant ):
341- # invalidate the released access token and refresh token
342- for item in ["access_token" , "refresh_token" ]:
343- try :
344- self .handler [item ].black_list (session_info [item ])
345- except KeyError :
346- pass
347- raise AccessCodeUsed (grant )
343+ key = _tinfo ["sid" ]
344+ session_info = self [key ]
348345
349346 # mint a new access token
350347 _at = self ._make_at (_tinfo ["sid" ], session_info )
351348
352349 # make sure the code can't be used again
353- self .handler ["code" ].black_list (grant )
354- key = _tinfo ["sid" ]
350+ self .revoke_token (key , "code" , session_info )
355351 else :
356352 session_info = self [key ]
357353 _at = self ._make_at (key , session_info )
@@ -386,17 +382,15 @@ def refresh_token(self, token, new_refresh=False):
386382 :raises: ExpiredToken for invalid refresh token
387383 WrongTokenType for wrong token type
388384 """
389-
390385 try :
391386 _tinfo = self .handler ["refresh_token" ].info (token )
392387 except KeyError :
393388 return False
394389
395- if is_expired (int (_tinfo ["exp" ])) or _tinfo ["black_listed" ]:
396- raise ExpiredToken ()
397-
398390 _sid = _tinfo ["sid" ]
399391 session_info = self [_sid ]
392+ if is_expired (int (_tinfo ["exp" ])):
393+ raise ExpiredToken ()
400394
401395 session_info = self .replace_token (_sid , session_info , "access_token" )
402396
@@ -420,11 +414,10 @@ def is_token_valid(self, token):
420414 except KeyError :
421415 return False
422416
423- if is_expired (int (_tinfo ["exp" ])) or _tinfo ["black_listed" ]:
424- return False
425-
426417 # Dependent on what state the session is in.
427418 session_info = self [_tinfo ["sid" ]]
419+ if is_expired (int (_tinfo ["exp" ])):
420+ return False
428421
429422 if session_info ["oauth_state" ] == "authz" :
430423 if _tinfo ["handler" ] != self .handler ["code" ]:
@@ -435,22 +428,25 @@ def is_token_valid(self, token):
435428
436429 return True
437430
438- def revoke_token (self , token , token_type = "" ):
431+ def revoke_token (self , sid , token_type , session_info = None ):
439432 """
440- Revokes access token
433+ Revokes token
441434
442- :param token: access token
435+ :param sid: session id
436+ :param token_type: token type, one of "code", "access_token" or
437+ "refresh_token"
443438 """
444- if token_type :
445- self . handler [ token_type ]. black_list ( token )
446- else :
447- self . handler . black_list ( token )
439+ if not session_info :
440+ session_info = self [ sid ]
441+ session_info . pop ( token_type , None )
442+ self [ sid ] = session_info
448443
449444 def revoke_all_tokens (self , token ):
450- _sinfo = self [token ]
451- for typ in self .handler .keys ():
452- if _sinfo .get (typ ):
453- self .revoke_token (_sinfo [typ ], typ )
445+ sid = self .handler .sid (token )
446+ _sinfo = self [sid ]
447+ for token_type in self .handler .keys ():
448+ _sinfo .pop (token_type , None )
449+ self [sid ] = _sinfo
454450
455451 def revoke_session (self , sid = "" , token = "" ):
456452 """
@@ -465,11 +461,9 @@ def revoke_session(self, sid="", token=""):
465461 else :
466462 raise ValueError ('Need one of "sid" or "token"' )
467463
468- for typ in ["access_token" , "refresh_token" , "code" ]:
469- try :
470- self .revoke_token (self [sid ][typ ], typ )
471- except KeyError : # If no such token has been issued
472- pass
464+ _sinfo = self [sid ]
465+ for token_type in self .handler .keys ():
466+ _sinfo .pop (token_type , None )
473467
474468 self .update (sid , revoked = True )
475469
0 commit comments