@@ -330,7 +330,10 @@ def handle_token_request(self, request_body, # type: str
330330 raise InvalidTokenRequest ('grant_type \' {}\' unknown' .format (token_request ['grant_type' ]), token_request ,
331331 oauth_error = 'unsupported_grant_type' )
332332
333- def _compute_code_challenge (self , code_verifier ):
333+ def _compute_code_challenge (self ,
334+ code_verifier # type: str
335+ ):
336+ # type: (...) -> str
334337 """
335338 Given a code verifier compute the code_challenge. This code_challenge is computed as defined (https://datatracker.ietf.org/doc/html/rfc7636#section-4.2):
336339
@@ -344,14 +347,18 @@ def _compute_code_challenge(self, code_verifier):
344347 verifier_hash = nacl .hash .sha256 (code_verifier .encode ('ISO_8859_1' ), encoder = URLSafeBase64Encoder )
345348 return verifier_hash .decode ().replace ('=' , '' )
346349
347- def _PKCE_verify (self , token_request , authentication_request ):
350+ def _PKCE_verify (self ,
351+ token_request , # type: AccessTokenRequest
352+ authentication_request # type: AuthorizationRequest
353+ ):
354+ # type: (...) -> bool
348355 """
349356 Verify that the given code_verifier complies with the initially supplied code_challenge.
350357
351358 Only supports the SHA256 code challenge method, plaintext is regarded as unsafe.
352359
353- :param cc_cm : the initially supplied Code Challenge Code challenge Method dictionary
354- :param code_verifier : the code_verfier to check against the code challenge.
360+ :param token_request : the token request containing the initially supplied code challenge and code_challenge method.
361+ :param authentication_request : the code_verfier to check against the code challenge.
355362 :returns: whether the code_verifier is what was expected given the cc_cm
356363 """
357364 code_challenge_method = authentication_request ['code_challenge_method' ]
@@ -361,7 +368,20 @@ def _PKCE_verify(self, token_request, authentication_request):
361368 code_challenge = self ._compute_code_challenge (token_request ['code_verifier' ])
362369 return code_challenge == authentication_request ['code_challenge' ]
363370
364- def _verify_code_exchange_req (self , token_request , authentication_request ):
371+ def _verify_code_exchange_req (self ,
372+ token_request , # type: AccessTokenRequest
373+ authentication_request # type: AuthorizationRequest
374+ ):
375+ # type: (...) -> None
376+ """
377+ Verify that the code exchange request is valid. In order to be valid we validate
378+ the expected client and redirect_uri. Finally, if requested by the client, perform a
379+ PKCE check.
380+
381+ :param token_request: The request asking for a token given a code, and optionally a code_verifier
382+ :param authentication_request: The authentication request belonging to the provided code.
383+ :raises InvalidTokenRequest, InvalidAuthorizationCode: If request is invalid, throw a representing exception.
384+ """
365385 if token_request ['client_id' ] != authentication_request ['client_id' ]:
366386 logger .info ('Authorization code \' %s\' belonging to \' %s\' was used by \' %s\' ' ,
367387 token_request ['code' ], authentication_request ['client_id' ], token_request ['client_id' ])
0 commit comments