
    k7i                         S SK r S SKrS SKJr  S SKJr  S SKJr  SSKJr  SSKJr  SSKJ	r	  \R                  " S	5      r\R                  " S	5      rS
 rS rS r " S S5      rg)    N)to_bytes)
to_unicode)urlsafe_b64encode   )InvalidGrantError)InvalidRequestError)OAuth2Requestz^[a-zA-Z0-9\-._~]{43,128}$c                     [         R                  " [        U S5      5      R                  5       n[	        [        U5      5      $ )z8Create S256 code_challenge with the given code_verifier.ascii)hashlibsha256r   digestr   r   )code_verifierdatas     Z/home/james-whalen/.local/lib/python3.13/site-packages/authlib/oauth2/rfc7636/challenge.pycreate_s256_code_challenger      s1    >>(=':;BBDD'-..    c                 
    X:H  $ N r   code_challenges     r   compare_plain_code_challenger      s     **r   c                     [        U 5      U:H  $ r   )r   r   s     r   compare_s256_code_challenger      s    %m4FFr   c                   V    \ rS rSrSrSrSS/r\\S.r	SS jr
S rS rS	 rS
 rS rSrg)CodeChallenge!   a  CodeChallenge extension to Authorization Code Grant. It is used to
improve the security of Authorization Code flow for public clients by
sending extra "code_challenge" and "code_verifier" to the authorization
server.

The AuthorizationCodeGrant SHOULD save the ``code_challenge`` and
``code_challenge_method`` into database when ``save_authorization_code``.
Then register this extension via::

    server.register_grant(AuthorizationCodeGrant, [CodeChallenge(required=True)])
plainS256)r   r    c                     Xl         g r   required)selfr#   s     r   __init__CodeChallenge.__init__8   s     r   c                 t    UR                  SU R                  5        UR                  SU R                  5        g )N,after_validate_authorization_request_payloadafter_validate_token_request)register_hookvalidate_code_challengevalidate_code_verifier)r$   grants     r   __call__CodeChallenge.__call__;   s8    :((	
 	*''	
r   c                 v   UR                   nUR                  R                  R                  S5      nUR                  R                  R                  S5      nU(       d  U(       d  g U(       d  [	        S5      e[        UR                  R                  R                  S/ 5      5      S:  a  [	        S5      e[        R                  U5      (       d  [	        S5      eU(       a  XPR                  ;  a  [	        S5      e[        UR                  R                  R                  S/ 5      5      S:  a  [	        S5      eg )	Nr   code_challenge_methodzMissing 'code_challenge'   z%Multiple 'code_challenge' in request.zInvalid 'code_challenge'z#Unsupported 'code_challenge_method'z,Multiple 'code_challenge_method' in request.)
requestpayloadr   getr   lendatalistCODE_CHALLENGE_PATTERNmatchSUPPORTED_CODE_CHALLENGE_METHOD)r$   r-   redirect_urir3   	challengemethods         r   r+   %CodeChallenge.validate_code_challengeE   s    !&OO((,,-=>	%%))*AB%&@AAw''++,<bABQF%&MNN%++I66%&@AAf$H$HH%&KLLw''++,CRHIAM%&TUU Nr   c                 L   UR                   nUR                  R                  S5      nU R                  (       a"  UR                  S:X  a  U(       d  [        S5      eUR                  nU R                  U5      nU(       d  U(       d  g U(       d  [        S5      e[        R                  U5      (       d  [        S5      eU R                  U5      nUc  U R                  nU R                  R                  U5      nU(       d  [        SU S35      eU" XF5      (       d	  [        SS9eg )	Nr   nonezMissing 'code_verifier'zInvalid 'code_verifier'zNo verify method for ''zCode challenge failed.)description)r3   formr5   r#   auth_methodr   authorization_code get_authorization_code_challengeCODE_VERIFIER_PATTERNr9   'get_authorization_code_challenge_methodDEFAULT_CODE_CHALLENGE_METHODCODE_CHALLENGE_METHODSRuntimeErrorr   )	r$   r-   resultr3   verifierrE   r<   r=   funcs	            r   r,   $CodeChallenge.validate_code_verifier[   s   !&<<##O4 ==W00F:8%&?@@$7799:LM	  %&?@@$**844%&?@@ ==>PQ>77F**..v6!7xqABB H((#0HII )r   c                     UR                   $ )a3  Get "code_challenge" associated with this authorization code.
Developers MAY re-implement it in subclass, the default logic::

    def get_authorization_code_challenge(self, authorization_code):
        return authorization_code.code_challenge

:param authorization_code: the instance of authorization_code
)r   r$   rE   s     r   rF   .CodeChallenge.get_authorization_code_challenge   s     "000r   c                     UR                   $ )aH  Get "code_challenge_method" associated with this authorization code.
Developers MAY re-implement it in subclass, the default logic::

    def get_authorization_code_challenge_method(self, authorization_code):
        return authorization_code.code_challenge_method

:param authorization_code: the instance of authorization_code
)r1   rQ   s     r   rH   5CodeChallenge.get_authorization_code_challenge_method   s     "777r   r"   N)T)__name__
__module____qualname____firstlineno____doc__rI   r:   r   r   rJ   r%   r.   r+   r,   rF   rH   __static_attributes__r   r   r   r   r   !   sK    
 %,!'.&7# .+
!
V,"JH	1	8r   r   )r   reauthlib.common.encodingr   r   r   rfc6749r   r   r	   compilerG   r8   r   r   r   r   r   r   r   <module>r_      sY     	 , . 5 ' ) #

#@A $AB /+G
r8 r8r   