@@ -90,27 +90,8 @@ class SessionInfo(Message):
9090 "client_id" : SINGLE_REQUIRED_STRING ,
9191 "authn_event" : SINGLE_REQUIRED_AUTHN_EVENT ,
9292 "si_redirects" : OPTIONAL_LIST_OF_STRINGS ,
93- "black_list" : SINGLE_OPTIONAL_JSON ,
9493 }
9594
96- def __init__ (self , * args , ** kwargs ):
97- super (SessionInfo , self ).__init__ (* args , ** kwargs )
98- self ["black_list" ] = {}
99-
100- def is_black_listed (self , typ , token ):
101- # If session is revoked
102- if "revoked" in self :
103- return True
104-
105- return typ in self ["black_list" ] and token in self ["black_list" ][typ ]
106-
107- def black_list (self , typ ):
108- if typ in self :
109- if typ in self ["black_list" ]:
110- self ["black_list" ][typ ].append (self [typ ])
111- else :
112- self ["black_list" ][typ ] = [self [typ ]]
113-
11495
11596def pairwise_id (uid , sector_identifier , salt , ** kwargs ):
11697 return hashlib .sha256 (
@@ -164,12 +145,16 @@ def __getitem__(self, item):
164145 if _info is None :
165146 sid = self .handler .sid (item )
166147 _info = self ._db .get (sid )
167-
168- 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 :
169154 _si = SessionInfo ().from_json (_info )
155+ _si ['sid' ] = item
170156 return _si
171- else :
172- return None
157+ raise KeyError
173158
174159 def __setitem__ (self , sid , instance ):
175160 try :
@@ -290,7 +275,7 @@ def do_sub(
290275
291276 def is_valid (self , typ , item ):
292277 try :
293- return not self [item ]. is_black_listed ( typ , item )
278+ return typ in self [item ]
294279 except KeyError :
295280 return False
296281
@@ -316,7 +301,7 @@ def replace_token(self, sid, sinfo, token_type):
316301 if token_type in self .handler :
317302 refresh_token = self .handler [token_type ](sid , sinfo = sinfo )
318303 # blacklist the old
319- sinfo . black_list ( token_type )
304+ self . revoke_token ( sid , token_type , sinfo )
320305
321306 sinfo [token_type ] = refresh_token
322307 return sinfo
@@ -352,23 +337,17 @@ def upgrade_to_token(
352337 :return: The session information as a SessionInfo instance
353338 """
354339 if grant :
340+ # The caller is responsible for checking if the access code exists.
355341 _tinfo = self .handler ["code" ].info (grant )
356342
357- session_info = self [_tinfo ["sid" ]]
358343 key = _tinfo ["sid" ]
359-
360- if session_info .is_black_listed ("code" , grant ):
361- # invalidate the released access token and refresh token
362- for item in ["access_token" , "refresh_token" ]:
363- session_info .black_list (item )
364- self [key ] = session_info
365- raise AccessCodeUsed (grant )
344+ session_info = self [key ]
366345
367346 # mint a new access token
368347 _at = self ._make_at (_tinfo ["sid" ], session_info )
369348
370349 # make sure the code can't be used again
371- session_info . black_list ( "code" )
350+ self . revoke_token ( key , "code" , session_info )
372351 else :
373352 session_info = self [key ]
374353 _at = self ._make_at (key , session_info )
@@ -403,16 +382,14 @@ def refresh_token(self, token, new_refresh=False):
403382 :raises: ExpiredToken for invalid refresh token
404383 WrongTokenType for wrong token type
405384 """
406-
407385 try :
408386 _tinfo = self .handler ["refresh_token" ].info (token )
409387 except KeyError :
410388 return False
411389
412390 _sid = _tinfo ["sid" ]
413391 session_info = self [_sid ]
414- if is_expired (int (_tinfo ["exp" ])) or \
415- session_info .is_black_listed ("refresh_token" , token ):
392+ if is_expired (int (_tinfo ["exp" ])):
416393 raise ExpiredToken ()
417394
418395 session_info = self .replace_token (_sid , session_info , "access_token" )
@@ -439,8 +416,7 @@ def is_token_valid(self, token):
439416
440417 # Dependent on what state the session is in.
441418 session_info = self [_tinfo ["sid" ]]
442- if is_expired (int (_tinfo ["exp" ])) or \
443- session_info .is_black_listed ("access_token" , token ):
419+ if is_expired (int (_tinfo ["exp" ])):
444420 return False
445421
446422 if session_info ["oauth_state" ] == "authz" :
@@ -452,23 +428,24 @@ def is_token_valid(self, token):
452428
453429 return True
454430
455- def revoke_token (self , sid , token_type ):
431+ def revoke_token (self , sid , token_type , session_info = None ):
456432 """
457433 Revokes token
458434
459435 :param sid: session id
460436 :param token_type: token type, one of "code", "access_token" or
461437 "refresh_token"
462438 """
463- _sinfo = self [sid ]
464- _sinfo .black_list (token_type )
465- self [sid ] = _sinfo
439+ if not session_info :
440+ session_info = self [sid ]
441+ session_info .pop (token_type , None )
442+ self [sid ] = session_info
466443
467444 def revoke_all_tokens (self , token ):
468445 sid = self .handler .sid (token )
469446 _sinfo = self [sid ]
470- for typ in self .handler .keys ():
471- _sinfo .black_list ( typ )
447+ for token_type in self .handler .keys ():
448+ _sinfo .pop ( token_type , None )
472449 self [sid ] = _sinfo
473450
474451 def revoke_session (self , sid = "" , token = "" ):
@@ -485,8 +462,8 @@ def revoke_session(self, sid="", token=""):
485462 raise ValueError ('Need one of "sid" or "token"' )
486463
487464 _sinfo = self [sid ]
488- for typ in self .handler .keys ():
489- _sinfo .black_list ( typ )
465+ for token_type in self .handler .keys ():
466+ _sinfo .pop ( token_type , None )
490467
491468 self .update (sid , revoked = True )
492469
0 commit comments