
    ([fD                         d dl mZ d dlmZ ddlmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ d	d
dZ G d d          Zd ZdS )    )generate_token)
url_decode   )prepare_grant_uriprepare_token_request!parse_authorization_code_responseparse_implicit_response)prepare_revoke_token_request)create_s256_code_challenge)	TokenAuth
ClientAuth)OAuth2Errorzapplication/jsonz/application/x-www-form-urlencoded;charset=UTF-8)AcceptzContent-Typec                      e Zd ZdZeZeZeZ	dZ
g Z	 	 	 	 	 ddZd Zd Zed             Zej        d	             Zdd
Z	 	 ddZddZ	 	 ddZd Z	 	 d dZ	 	 d dZd Zd Z	 	 d!dZ	 	 ddZ	 	 d dZd Zd Zd"dZ dS )#OAuth2ClientaZ  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.
    )response_modenonceprompt
login_hintNheaderc                    || _         || _        || _        || _        ||rd}nd}|| _        ||rd}nd}|| _        || _        || _        |	| _        | 	                    |
||           | _
        || _        |                    dd           }|rt          d          || _        t                      t                      t                      t                      t                      d| _        i | _        d S )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)selfr    r!   r"   r$   r%   r&   r#   r'   r(   tokentoken_placementr+   r.   r   s                  I/var/www/piapp/venv/lib/python3.11/site-packages/authlib/oauth2/client.py__init__zOAuth2Client.__init__3   s    "*
%- 4-B**-3**D'*2 92G//28//N,
(%:"//MM( _d;; 	][\\\  &)UU%(UU&)ee$'EE(+ 
  
      c                 ~    t          |t                    r|d         | j        |d         <   dS || j        |j        <   dS )zmExtend client authenticate for token endpoint.

        :param auth: an instance to sign the request
        r   r   N)
isinstancetupler1   name)r2   auths     r5   register_client_auth_methodz(OAuth2Client.register_client_auth_methodd   sF    
 dE"" 	1*.q'DtAw''',0Dty)))r7   c                     t          |t                    r|| j        v r| j        |         }|                     | j        | j        |          S )N)r!   r"   auth_method)r9   strr1   client_auth_classr!   r"   )r2   r?   s     r5   client_authzOAuth2Client.client_authn   sY    k3'' 	:K4;M,M,M,[9K%%n,# & 
 
 	
r7   c                     | j         j        S N)r*   r3   )r2   s    r5   r3   zOAuth2Client.tokenw   s    $$r7   c                 :    | j                             |           d S rD   )r*   	set_token)r2   r3   s     r5   r3   zOAuth2Client.token{   s    !!%(((((r7   c                    |t                      }| j                            dd          }|                    d|          }d|vr
| j        |d<   d|vr
| j        |d<   |r-|dk    r'| j        dk    rt          |          |d<   | j        |d<   | j        D ]}||vr|| j        v r| j        |         ||<    t          |f| j
        ||d	|}||fS )
a  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
        Nresponse_typecoder'   r&   S256code_challenger(   )r!   rH   r#   )r   r.   getr,   r'   r&   r(   r   EXTRA_AUTHORIZE_PARAMSr   r!   )r2   urlr#   code_verifierkwargsrH   kuris           r5   create_authorization_urlz%OAuth2Client.create_authorization_url   s     ="$$E))/6BB

?MBB''%)%6F>"&  "jF7O 	I]f449SW]9]9]'A-'P'PF#$.2.HF*+, 	- 	-A1#5#5 M!,q	#># #!# # Ezr7    POSTc                    |p| j         }|                    dd          }	|	rd|	v r|                     |	|          S |                     |          }
|	r"d|	v rd}t	          |	|          }|d         |d<   || j                            d          }|t          |          }|| j        d<    | j        ||fi |}|| 	                    | j
                  }|t          }|| j                            d	          } | j        |f||||d
|
S )am  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
        :return: A :class:`OAuth2Token` object (a dict too).
        authorization_responseN#zcode=authorization_code)r#   rI   
grant_typetoken_endpoint)bodyr<   methodheaders)r#   r,   token_from_fragment_extract_session_request_paramsr   r.   rL   _guess_grant_type_prepare_token_endpoint_bodyrB   r$   DEFAULT_HEADERS_fetch_token)r2   rN   r\   r]   r^   r<   rZ   r#   rP   rW   session_kwargsparamss               r5   fetch_tokenzOAuth2Client.fetch_token   sp   " #!',Dd!K!K! 	Kc-C&C&C++,BEJJJ==fEE! 	,g1G&G&G-J6&  F $F^F6N**<88J*622J*4DM,'0t0zLLVLL<##D$CDDD?%G;-##$455C t 
f
 
-
 
 	
r7   c                     t          ||          }d|v r0|                     |d         |                    d                    || _        |S )Nerrorerror_descriptionri   description)r	   oauth_error_classrL   r3   )r2   rW   r#   r3   s       r5   r_   z OAuth2Client.token_from_fragment   s_    '(>FFe((Gn!II&9:: )    
r7   c                    |                      |          }|p| j                            d          }d|vr| j        r
| j        |d<   t	          d|fd|i|}|t
                                          }| j        d         D ]} ||||          \  }}}||                     | j	                  } | j
        |f||||d|S )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&   Nr   )ro   r\   r^   r<   )r`   r3   rL   r&   r   rc   copyr0   rB   r$   _refresh_token)	r2   rN   ro   r\   r<   r^   rP   re   hooks	            r5   ro   zOAuth2Client.refresh_token   s    ==fEE%H)H)H&  TZ "jF7O$T
 
'
+1
 

 ?%**,,G()@A 	: 	:D!%c7D!9!9C$$<##D$CDDD"t"),4) )') ) 	)r7   c                    |                                 sdS |                    d          }| j                            d          }|r|r|                     ||           dS | j                            d          dk    r?|d         }|                     |d          }| j        r|                     ||	           dS d S )
NTro   r[   ro   rZ   client_credentialsaccess_token)rZ   )rv   )
is_expiredrL   r.   ro   rg   r+   )r2   r3   ro   rN   rv   	new_tokens         r5   ensure_active_tokenz OAuth2Client.ensure_active_token  s    !! 	4		/22m 011 	S 	s-@@@4]|,,0DDD 0L((9M(NNI  H!!),!GGG4 EDr7   c           	      ,     | j         d|f|||||d|S )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   r3   token_type_hintr\   r<   r^   _handle_token_hintr2   rN   r3   r|   r\   r<   r^   rP   s           r5   revoke_tokenzOAuth2Client.revoke_token  s@      't&"C=D'= = 6<= = 	=r7   c           	      ,     | j         d|f|||||d|S )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{   r}   r   s           r5   introspect_tokenzOAuth2Client.introspect_token(  s@      't&&=D'= = 6<= = 	=r7   c                     |dk    r!| j         j                            |           dS || j        vrt	          d|| j                  | j        |                             |           dS )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-   )r2   	hook_typerr   s      r5   register_compliance_hookz%OAuth2Client.register_compliance_hook=  s{     +++O!%%d+++FD0009&(<> > >Y'++D11111r7   c                     |j         dk    r|                                 |                                }d|v r0|                     |d         |                    d                    || _        | j        S )Ni  ri   rj   rk   )status_coderaise_for_statusjsonrm   rL   r3   )r2   respr3   s      r5   parse_response_tokenz!OAuth2Client.parse_response_tokenR  s    s""!!###		e((Gn!II&9:: )    
zr7   c                    |                                 dk    r2 | j        j        |ft          t	          |                    ||d|}nJd|v rd                    ||g          }nd                    ||g          } | j        j        ||f||d|}| j        d         D ]} ||          }|                     |          S )NrU   datar^   r<   ?&)r^   r<   r   )	upperr    postdictr   joinrequestr0   r   )	r2   rN   r\   r^   r<   r]   rP   r   rr   s	            r5   rd   zOAuth2Client._fetch_token_  s     <<>>V##$4<$6z$//00d6 6.46 6DD czzhhT{++hhT{++'4<'ZW4ZZSYZZD()@A 	 	D4::DD((...r7   c                     | j         |f|||d|}| j        d         D ]} ||          }|                     |          }	d|	vr
|| j        d<   t	          | j                  r|                     | j        |           | j        S )N)r\   r<   r^   r   ro   rt   )
_http_postr0   r   r3   callabler+   )
r2   rN   ro   r\   r^   r<   rP   r   rr   r3   s
             r5   rq   zOAuth2Client._refresh_tokenr  s    tsTD'TTVTT()AB 	 	D4::DD))$//%''*7DJ'D%&& 	GdjFFFzr7   c                 v   |;| j         r4| j                             d          p| j                             d          }|d}t          ||||          \  }}| j        |         D ]} ||||          \  }}}||                     | j                  }|                     |          }	 | j        ||f||d|	S )Nro   rv   rT   )r<   r^   )r3   rL   r
   r0   rB   r%   r`   r   )
r2   rr   rN   r3   r|   r\   r<   r^   rP   re   s
             r5   r~   zOAuth2Client._handle_token_hint  s    =TZ=JNN?33Utz~~n7U7UE<D4?D'3 3g (. 	: 	:D!%c7D!9!9C$$<##D$HIID==fEEtE 'E E5CE E 	Er7   c                     |dk    rd|vr
| j         |d<   t          ||fi |S d|vr| j        r
| j        |d<   t          ||fi |S )NrY   r'   r&   )r'   r   r&   )r2   r\   rZ   rP   s       r5   rb   z)OAuth2Client._prepare_token_endpoint_body  ss    ---V++)-):~&(TDDVDDD&  TZ "jF7O$Z@@@@@r7   c                 V    i }| j         D ]}||v r|                    |          ||<   |S )zDExtract parameters for session object from the passing ``**kwargs``.)SESSION_REQUEST_PARAMSr,   )r2   rP   rvrQ   s       r5   r`   z,OAuth2Client._extract_session_request_params  s;    , 	& 	&AF{{

11	r7   c                 d     | j         j        |ft          t          |                    ||d|S )Nr   )r    r   r   r   )r2   rN   r\   r<   r^   rP   s         r5   r   zOAuth2Client._http_post  sH     t| 2:d++,,$2 2*02 2 	2r7   )NNNNNNNNNr   N)NN)NrT   rU   NNNNrD   )NrT   NN)NNNNN)rT   NNrU   )NNN)!__name__
__module____qualname____doc__r   rA   r   r)   r   rm   rM   r   r6   r=   rB   propertyr3   setterrS   rg   r_   ro   ry   r   r   r   r   rd   rq   r~   rb   r`   r    r7   r5   r   r      s        * # #  >B,015RVDH	/  /  /  / b1 1 1
 
 
 % % X% \) ) \)   B EI6:6
 6
 6
 6
p    ;=)- )  )  )  )D   =A37= = = =* AE7;= = = =*2 2 2*   =A"/ / / /& HL      IM9=E E E E*A A A  2 2 2 2 2 2r7   r   c                 .    d| v rd}nd| v rd| v rd}nd}|S )NrI   rY   usernamepasswordru   r   )rP   rZ   s     r5   ra   ra     s;    )

	v		*"6"6

)
r7   N)authlib.common.securityr   authlib.common.urlsr   rfc6749.parametersr   r   r   r	   rfc7009r
   rfc7636r   r<   r   r   baser   rc   r   ra   r   r7   r5   <module>r      s   2 2 2 2 2 2 * * * * * *            2 1 1 1 1 1 / / / / / / ' ' ' ' ' ' ' '       !E X2 X2 X2 X2 X2 X2 X2 X2v    r7   