
    !<i                         d dl Z d dlZd dlmZ d dlmZ d dlmZ ddlmZ ddlmZ ddlm	Z	  ej
        d	          Z ej
        d	          Zd
 Zd Zd Z G d d          ZdS )    N)to_bytes)
to_unicode)urlsafe_b64encode   )InvalidGrantError)InvalidRequestError)OAuth2Requestz^[a-zA-Z0-9\-._~]{43,128}$c                     t          j        t          | d                                                    }t	          t          |                    S )z8Create S256 code_challenge with the given code_verifier.ascii)hashlibsha256r   digestr   r   )code_verifierdatas     /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/authlib/oauth2/rfc7636/challenge.pycreate_s256_code_challenger      s>    >(='::;;BBDDD'--...    c                     | |k    S N r   code_challenges     r   compare_plain_code_challenger      s     N**r   c                 (    t          |           |k    S r   )r   r   s     r   compare_s256_code_challenger      s    %m44FFr   c                   N    e Zd ZdZdZddgZeedZddZ	d Z
d Zd	 Zd
 Zd ZdS )CodeChallengea  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   Tc                     || _         d S r   )required)selfr!   s     r   __init__zCodeChallenge.__init__8   s     r   c                 r    |                     d| j                   |                     d| j                   d S )N,after_validate_authorization_request_payloadafter_validate_token_request)register_hookvalidate_code_challengevalidate_code_verifier)r"   grants     r   __call__zCodeChallenge.__call__;   sP    :(	
 	
 	
 	*'	
 	
 	
 	
 	
r   c                 D   |j         }|j        j                            d          }|j        j                            d          }|s|sd S |st	          d          t          |j        j                            dg                     dk    rt	          d          t                              |          st	          d          |r|| j	        vrt	          d          t          |j        j                            dg                     dk    rt	          d          d S )	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_urir/   	challengemethods         r   r(   z%CodeChallenge.validate_code_challengeE   s+   !&O(,,-=>>	%))*ABB 	 	F 	B%&@AAAw'++,<bAABBQFF%&MNNN%++I66 	B%&@AAA 	MfD$HHH%&KLLLw'++,CRHHIIAMM%&TUUU NMr   c                    |j         }|j                            d          }| j        r|j        dk    r|st          d          |j        }|                     |          }|s|sd S |st          d          t          	                    |          st          d          | 
                    |          }|| j        }| j                            |          }|st          d| d           |||          st          d          d S )	Nr   nonezMissing 'code_verifier'zInvalid 'code_verifier'zNo verify method for ''zCode challenge failed.)description)r/   formr1   r!   auth_methodr   authorization_code get_authorization_code_challengeCODE_VERIFIER_PATTERNr5   'get_authorization_code_challenge_methodDEFAULT_CODE_CHALLENGE_METHODCODE_CHALLENGE_METHODSRuntimeErrorr   )	r"   r*   resultr/   verifierr@   r8   r9   funcs	            r   r)   z$CodeChallenge.validate_code_verifier[   sP   !&<##O44 = 	AW0F::8:%&?@@@$799:LMM	  	 	F  	A%&?@@@$**844 	A%&?@@@ ==>PQQ>7F*..v66 	CAAAABBB tHi(( 	J#0HIIII	J 	Jr   c                     |j         S )a[  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"   r@   s     r   rA   z.CodeChallenge.get_authorization_code_challenge   s     "00r   c                     |j         S )ap  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
        )r-   rK   s     r   rC   z5CodeChallenge.get_authorization_code_challenge_method   s     "77r   N)T)__name__
__module____qualname____doc__rD   r6   r   r   rE   r#   r+   r(   r)   rA   rC   r   r   r   r   r   !   s        
 
 %,!'.&7# .+ 
! ! ! !
 
 
V V V,"J "J "JH	1 	1 	1	8 	8 	8 	8 	8r   r   )r   reauthlib.common.encodingr   r   r   rfc6749r   r   r	   compilerB   r4   r   r   r   r   r   r   r   <module>rU      s    				 , , , , , , . . . . . . 5 5 5 5 5 5 ' ' ' ' ' ' ) ) ) ) ) ) # # # # # #"
#@AA #$ABB / / /+ + +G G G
r8 r8 r8 r8 r8 r8 r8 r8 r8 r8r   