
    k7iI                         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
  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S.r " S S5      rS rg)    )generate_token)
url_decode   )
ClientAuth)	TokenAuth)OAuth2Error)!parse_authorization_code_response)parse_implicit_response)prepare_grant_uri)prepare_token_request)prepare_revoke_token_request)create_s256_code_challengezapplication/jsonz/application/x-www-form-urlencoded;charset=UTF-8)AcceptzContent-Typec                   L   \ rS rSrSr\r\r\	r
Sr/ r            SS jrS rS r\S 5       r\R$                  S	 5       rSS
 jr       SS jrSS jr SS jrSS jr     S S jr     S S jrS rS r S!S jr S"S jr     S S jrS rS r S#S jr!S r"Sr#g)$OAuth2Client   a  Construct a new OAuth 2 protocol client.

:param session: Requests session object to communicate with
                authorization server.
:param client_id: Client ID, which you get from client registration.
:param client_secret: Client Secret, which you get from registration.
:param token_endpoint_auth_method: client authentication method for
    token endpoint.
:param revocation_endpoint_auth_method: client authentication method for
    revocation endpoint.
:param scope: Scope that you needed to access user resources.
:param state: Shared secret to prevent CSRF attack.
:param redirect_uri: Redirect URI you registered as callback.
:param code_challenge_method: PKCE method name, only S256 is supported.
:param token: A dict of token attributes such as ``access_token``,
    ``token_type`` and ``expires_at``.
:param token_placement: The place to put token in HTTP request. Available
    values: "header", "body", "uri".
:param update_token: A function for you to update token. It accept a
    :class:`OAuth2Token` as parameter.
:param leeway: Time window in seconds before the actual expiration of the
    authentication token, that the token is considered expired and will
    be refreshed.
)response_modenonceprompt
login_hintNc                    Xl         X l        X0l        Xpl        Uc  U(       a  SnOSnX@l        Uc  U(       a  SnOSnXPl        X`l        Xl        Xl        U R                  XU 5      U l
        Xl        UR                  SS 5      nU(       a  [        S5      eXl        [        5       [        5       [        5       [        5       [        5       S.U l        0 U l        Xl        g )Nclient_secret_basicnonetoken_updaterz<update token has been redesigned, checkout the documentation)access_token_responserefresh_token_requestrefresh_token_responserevoke_token_requestintrospect_token_request)session	client_idclient_secretstatetoken_endpoint_auth_methodrevocation_endpoint_auth_methodscoperedirect_uricode_challenge_methodtoken_auth_class
token_authupdate_tokenpop
ValueErrormetadatasetcompliance_hook_auth_methodsleeway)selfr    r!   r"   r$   r%   r&   r#   r'   r(   tokentoken_placementr+   r2   r.   r   s                   O/home/james-whalen/.local/lib/python3.13/site-packages/authlib/oauth2/client.py__init__OAuth2Client.__init__5   s    " "*
%--B*-3**D'*22G/28//N,
(%:"//M( _d;N  ! &)U%(U&)e$'E(+ 
      c                     [        U[        5      (       a  US   U R                  US   '   gXR                  UR                  '   g)z]Extend client authenticate for token endpoint.

:param auth: an instance to sign the request
r   r   N)
isinstancetupler1   name)r3   auths     r6   register_client_auth_method(OAuth2Client.register_client_auth_methodu   s;    
 dE""*.q'DtAw',0tyy)r9   c                     [        U[        5      (       a  XR                  ;   a  U R                  U   nU R                  U R                  U R
                  US9$ )N)r!   r"   auth_method)r;   strr1   client_auth_classr!   r"   )r3   rB   s     r6   client_authOAuth2Client.client_auth   sU    k3''K;M;M,M,,[9K%%nn,,# & 
 	
r9   c                 .    U R                   R                  $ N)r*   r4   r3   s    r6   r4   OAuth2Client.token   s    $$$r9   c                 :    U R                   R                  U5        g rH   )r*   	set_token)r3   r4   s     r6   r4   rJ      s    !!%(r9   c                    Uc
  [        5       nU R                  R                  SS5      nUR                  SU5      nSU;  a  U R                  US'   SU;  a  U R
                  US'   U(       a3  US:X  a-  U R                  S:X  a  [        U5      US'   U R                  US'   U R                   H,  nXd;  d  M
  X`R                  ;   d  M  U R                  U   XF'   M.     [        U4U R                  UUS.UD6nXr4$ )	ah  Generate an authorization URL and state.

:param url: Authorization endpoint url, must be HTTPS.
:param state: An optional state string for CSRF protection. If not
              given it will be generated for you.
:param code_verifier: An optional code_verifier for code challenge.
:param kwargs: Extra parameters to include.
:return: authorization_url, state
response_typecoder'   r&   S256code_challenger(   )r!   rN   r#   )r   r.   getr,   r'   r&   r(   r   EXTRA_AUTHORIZE_PARAMSr   r!   )r3   urlr#   code_verifierkwargsrN   kuris           r6   create_authorization_url%OAuth2Client.create_authorization_url   s    ="$E))/6B

?MB'%)%6%6F>"& "jjF7O '**f4'A-'PF#$.2.H.HF*+,,A1#5 MM!,	 -  
nn'	

 
 zr9   c                 @   U=(       d    U R                   nUR                  SS5      n	U	(       a  SU	;   a  U R                  X5      $ U R                  U5      n
U	(       a  SU	;   a  Sn[	        U	US9nUS   US'   Uc  U R
                  R                  S5      nUc  [        U5      nX`R
                  S'   U R                  " X&40 UD6nUc  U R                  U R                  5      nUc  [        nUc  U R
                  R                  S	5      nU R                  " U4X%X4S
.U
D6$ )a;  Generic method for fetching an access token from the token endpoint.

:param url: Access Token endpoint URL, if not configured,
            ``authorization_response`` is used to extract token from
            its fragment (implicit way).
:param body: Optional application/x-www-form-urlencoded body to add the
             include in the token request. Prefer kwargs over body.
:param method: The HTTP method used to make the request. Defaults
               to POST, but may also be GET. Other methods should
               be added as needed.
:param headers: Dict to default request headers with.
:param auth: An auth tuple or method as accepted by requests.
:param grant_type: Use specified grant_type to fetch token.
:param state: Optional "state" value to fetch token.
:return: A :class:`OAuth2Token` object (a dict too).
authorization_responseN#zcode=authorization_code)r#   rO   
grant_typetoken_endpoint)bodyr>   methodheaders)r#   r,   token_from_fragment_extract_session_request_paramsr	   r.   rR   _guess_grant_type_prepare_token_endpoint_bodyrE   r$   DEFAULT_HEADERS_fetch_token)r3   rT   ra   rb   rc   r>   r_   r#   rV   r\   session_kwargsparamss               r6   fetch_tokenOAuth2Client.fetch_token   s2   6 #!',Dd!K!c-C&C++,BJJ==fE!g1G&G-J6&F $F^F6N**<8J*62J*4MM,'00LVL<##D$C$CDD?%G;--##$45C  
f
IW
 	
r9   c                 x    [        X5      nSU;   a"  U R                  US   UR                  S5      S9eX0l        U$ )Nerrorerror_descriptionro   description)r
   oauth_error_classrR   r4   )r3   r\   r#   r4   s       r6   rd    OAuth2Client.token_from_fragment   sL    '(>Fe((Gn%))<O2P )   
r9   c                    U R                  U5      nU=(       d    U R                  R                  S5      nSU;  a   U R                  (       a  U R                  US'   [	        SU4SU0UD6nUc  [
        R                  5       nUc  U R                  R                  S5      nU R                  S    H  nU" XU5      u  pnM     Uc  U R                  U R                  5      nU R                  " U4UUUUS.UD6$ )a  Fetch a new access token using a refresh token.

:param url: Refresh Token endpoint, must be HTTPS.
:param refresh_token: The refresh_token to use.
:param body: Optional application/x-www-form-urlencoded body to add the
             include in the token request. Prefer kwargs over body.
:param auth: An auth tuple or method as accepted by requests.
:param headers: Dict to default request headers with.
:return: A :class:`OAuth2Token` object (a dict too).
refresh_tokenr&   r`   r   )rv   ra   rc   r>   )re   r4   rR   r&   r   rh   copyr.   r0   rE   r$   _refresh_token)	r3   rT   rv   ra   r>   rc   rV   rj   hooks	            r6   rv   OAuth2Client.refresh_token  s
    ==fE%H)H& TZZ"jjF7O$T
1>
BH
 ?%**,G;--##$45C(()@AD!%cD!9C$ B <##D$C$CDD""
'
 
 	
r9   c                    Uc  U R                   nUR                  U R                  S9(       d  gUR                  S5      nU R                  R                  S5      nU(       a  U(       a  U R                  X2S9  gU R                  R                  S5      S:X  a6  US   nU R                  USS	9nU R                  (       a  U R                  XTS
9  gg )N)r2   Trv   r`   rv   r_   client_credentialsaccess_token)r_   )r~   )r4   
is_expiredr2   rR   r.   rv   rl   r+   )r3   r4   rv   rT   r~   	new_tokens         r6   ensure_active_token OAuth2Client.ensure_active_token-  s    =JJEt{{3		/2mm 01Ss@]]|,0DD 0L((9M(NI  !!)!G Er9   c           	      r    Uc  U R                  U R                  5      nU R                  " SU4UUUUUS.UD6$ )a^  Revoke token method defined via `RFC7009`_.

:param url: Revoke Token endpoint, must be HTTPS.
:param token: The token to be revoked.
:param token_type_hint: The type of the token that to be revoked.
                        It can be "access_token" or "refresh_token".
:param body: Optional application/x-www-form-urlencoded body to add the
             include in the token request. Prefer kwargs over body.
:param auth: An auth tuple or method as accepted by requests.
:param headers: Dict to default request headers with.
:return: Revocation Response

.. _`RFC7009`: https://tools.ietf.org/html/rfc7009
r   r4   token_type_hintra   r>   rc   )rE   r%   _handle_token_hintr3   rT   r4   r   ra   r>   rc   rV   s           r6   revoke_tokenOAuth2Client.revoke_token>  sX    0 <##D$H$HID&&"	
 +	
 	
 		
r9   c           	      r    Uc  U R                  U R                  5      nU R                  " SU4UUUUUS.UD6$ )a  Implementation of OAuth 2.0 Token Introspection defined via `RFC7662`_.

:param url: Introspection Endpoint, must be HTTPS.
:param token: The token to be introspected.
:param token_type_hint: The type of the token that to be revoked.
                        It can be "access_token" or "refresh_token".
:param body: Optional application/x-www-form-urlencoded body to add the
             include in the token request. Prefer kwargs over body.
:param auth: An auth tuple or method as accepted by requests.
:param headers: Dict to default request headers with.
:return: Introspection Response

.. _`RFC7662`: https://tools.ietf.org/html/rfc7662
r   r   )rE   r$   r   r   s           r6   introspect_tokenOAuth2Client.introspect_tokenc  sX    0 <##D$C$CDD&&&	
 +	
 	
 		
r9   c                     US:X  a&  U R                   R                  R                  U5        gXR                  ;  a  [	        SXR                  5      eU R                  U   R                  U5        g)a  Register a hook for request/response tweaking.

Available hooks are:

* access_token_response: invoked before token parsing.
* refresh_token_request: invoked before refreshing token.
* refresh_token_response: invoked before refresh token parsing.
* protected_request: invoked before making a request.
* revoke_token_request: invoked before revoking a token.
* introspect_token_request: invoked before introspecting a token.
protected_requestNzHook type %s is not in %s.)r*   hooksaddr0   r-   )r3   	hook_typery   s      r6   register_compliance_hook%OAuth2Client.register_compliance_hook  sg     ++OO!!%%d+000,i9M9M  	Y'++D1r9   c                     UR                   S:  a  UR                  5         UR                  5       nSU;   a"  U R                  US   UR	                  S5      S9eX l        U R
                  $ )Ni  ro   rp   rq   )status_coderaise_for_statusjsonrs   rR   r4   )r3   respr4   s      r6   parse_response_token!OAuth2Client.parse_response_token  sk    s"!!#		e((Gn%))<O2P )   
zzr9   c                    UR                  5       S:X  a3  U R                  R                  " U4[        [	        U5      5      X4S.UD6nOJSU;   a  SR                  X/5      nOSR                  X/5      nU R                  R                  " XQ4X4S.UD6nU R                  S    H  nU" U5      nM     U R                  U5      $ )NPOSTdatarc   r>   ?&)rc   r>   r   )	upperr    postdictr   joinrequestr0   r   )	r3   rT   ra   rc   r>   rb   rV   r   ry   s	            r6   ri   OAuth2Client._fetch_token  s     <<>V#<<$$z$/0'PVD czhh{+hh{+<<''%,;AD (()@AD:D B ((..r9   c                 4   U R                   " U4X5US.UD6nU R                  S    H  nU" U5      nM     U R                  U5      n	SU	;  a  X R                  S'   [	        U R
                  5      (       a  U R                  U R                  US9  U R                  $ )N)ra   r>   rc   r   rv   r|   )
_http_postr0   r   r4   callabler+   )
r3   rT   rv   ra   rc   r>   rV   r   ry   r4   s
             r6   rx   OAuth2Client._refresh_token  s     sT'TVT(()ABD:D C ))$/%'*7JJ'D%%&&djjFzzr9   c                    UcN  U R                   (       a=  U R                   R                  S5      =(       d    U R                   R                  S5      nUc  Sn[        X4XW5      u  pWU R                  U    H  n	U	" X'U5      u  p'nM     Uc  U R	                  U R
                  5      nU R                  U5      n
U R                  " X%4XgS.U
D6$ )Nrv   r~    )r>   rc   )r4   rR   r   r0   rE   r%   re   r   )r3   ry   rT   r4   r   ra   r>   rc   rV   r0   rj   s              r6   r   OAuth2Client._handle_token_hint  s     =TZZJJNN?3Utzz~~n7UE<D4D
  $33D9O!0t!DC$  : <##D$H$HID==fEsWtWWWr9   c                     US:X  a!  SU;  a  U R                   US'   [        X!40 UD6$ SU;  a   U R                  (       a  U R                  US'   [        X!40 UD6$ )Nr^   r'   r&   )r'   r   r&   )r3   ra   r_   rV   s       r6   rg   )OAuth2Client._prepare_token_endpoint_body  s^    --V+)-):):~&(DVDD& TZZ"jjF7O$Z@@@r9   c                 d    0 nU R                    H  nX1;   d  M
  UR                  U5      X#'   M     U$ )zDExtract parameters for session object from the passing ``**kwargs``.)SESSION_REQUEST_PARAMSr,   )r3   rV   rvrW   s       r6   re   ,OAuth2Client._extract_session_request_params  s2    ,,A{

1 - 	r9   c                 f    U R                   R                  " U4[        [        U5      5      XCS.UD6$ )Nr   )r    r   r   r   )r3   rT   ra   r>   rc   rV   s         r6   r   OAuth2Client._http_post   s8    ||  
:d+,g
LR
 	
r9   c                     U ? g rH   )r    rI   s    r6   __del__OAuth2Client.__del__  s    Lr9   )r1   r!   r"   r(   r0   r2   r.   r'   r%   r&   r    r#   r4   r*   r$   r+   )NNNNNNNNNheaderN<   )NN)Nr   r   NNNNrH   )NNr   NN)NNNNN)r   NNr   )Nr   NN)NNN)$__name__
__module____qualname____firstlineno____doc__r   rD   r   r)   r   rs   rS   r   r7   r?   rE   propertyr4   setterrY   rl   rd   rv   r   r   r   r   r   ri   rx   r   rg   re   r   r   __static_attributes__ r9   r6   r   r      s>   2 # #O
 #'(," >@1
 % % \\) )'V ?
B IM(
T( #
P #
J2,
 =C/, DH* X<A

r9   r   c                 >    SU ;   a  SnU$ SU ;   a
  SU ;   a  SnU$ SnU$ )NrO   r^   usernamepasswordr}   r   )rV   r_   s     r6   rf   rf   	  sB    )

 	 
v	*"6
  *
r9   N)authlib.common.securityr   authlib.common.urlsr   r>   r   r   baser   rfc6749.parametersr	   r
   r   r   rfc7009r   rfc7636r   rh   r   rf   r   r9   r6   <module>r      sG    2 *    A 7 1 5 1 / !Er rjr9   