@@ -682,13 +682,14 @@ def finalize_auth(self, client, issuer: str, response: dict,
682682 authorization_response ['state' ])
683683 return authorization_response
684684
685- def get_access_and_id_token (self , authorization_response = None , state = '' ,
686- client = None ):
685+ def get_access_and_id_token (self , authorization_response = None , state = '' , client = None ,
686+ behaviour_args : Optionl [ dict ] = None ):
687687 """
688688 There are a number of services where access tokens and ID tokens can
689689 occur in the response. This method goes through the possible places
690690 based on the response_type the client uses.
691691
692+ :param behaviour_args: For fine tuning behaviour
692693 :param authorization_response: The Authorization response
693694 :param state: The state key (the state parameter in the
694695 authorization request)
@@ -726,19 +727,26 @@ def get_access_and_id_token(self, authorization_response=None, state='',
726727 if _resp_type in [{'token' }, {'id_token' , 'token' }, {'code' , 'token' },
727728 {'code' , 'id_token' , 'token' }]:
728729 access_token = authorization_response ["access_token" ]
729- elif _resp_type in [{'code' }, {'code' , 'id_token' }]:
730+ if behaviour_args and behaviour_args .get ("collect_id_token" , False ):
731+ if "id_token" not in _resp_type :
732+ # get the access token
733+ token_resp = self .get_access_token (state , client = client )
734+ if is_error_message (token_resp ):
735+ return False , "Invalid response %s." % token_resp ["error" ]
736+ # Now which access_token should I use
737+ access_token = token_resp ["access_token" ]
738+ # May or may not get an ID Token
739+ id_token = token_resp .get ('__verified_id_token' )
730740
741+ elif _resp_type in [{'code' }, {'code' , 'id_token' }]:
731742 # get the access token
732743 token_resp = self .get_access_token (state , client = client )
733744 if is_error_message (token_resp ):
734745 return False , "Invalid response %s." % token_resp ["error" ]
735746
736747 access_token = token_resp ["access_token" ]
737-
738- try :
739- id_token = token_resp ['__verified_id_token' ]
740- except KeyError :
741- pass
748+ # May or may not get an ID Token
749+ id_token = token_resp .get ('__verified_id_token' )
742750
743751 return {'access_token' : access_token , 'id_token' : id_token }
744752
@@ -771,8 +779,8 @@ def finalize(self, issuer, response, behaviour_args: Optional[dict] = None):
771779 }
772780
773781 _state = authorization_response ['state' ]
774- token = self .get_access_and_id_token (authorization_response ,
775- state = _state , client = client )
782+ token = self .get_access_and_id_token (authorization_response , state = _state , client = client ,
783+ behaviour_args = behaviour_args )
776784 _id_token = token .get ("id_token" )
777785 logger .debug (f"ID Token: { _id_token } " )
778786
0 commit comments