o
    b                     @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	m
Z
 ddlmZ ddlmZmZ dgZeeZG d	d deZdS )
    N)urlparse)JsonWebToken
JsonWebKey)UserInfoCodeIDTokenImplicitIDToken   )BaseApp)MissingRequestTokenErrorMissingTokenErrorAsyncRemoteAppc                   @   sh   e Zd Zdd ZdddZdd Zddd	Zdd
dZdddZdd Z	dddZ
dddZdd ZdS )r   c                    sB   | j rd| jvr| | j I d H }t |d< | j| | jS )NZ
_loaded_at)Z_server_metadata_urlserver_metadata_fetch_server_metadatatimeupdate)selfmetadata r   i/var/www/secure340b-portal/env/lib/python3.10/site-packages/authlib/integrations/base_client/async_app.pyload_server_metadata   s   z#AsyncRemoteApp.load_server_metadataNc                    s&   | j r| j |||dI d H  d S d S )N)refresh_tokenaccess_token)Z_update_token)r   tokenr   r   r   r   r   _on_update_token   s   zAsyncRemoteApp._on_update_tokenc                    s^   i }| j r|| j  |j| jfi |I d H }td| |j|fi |}||dS )NzFetch request token: {!r})urlrequest_token)Zrequest_token_paramsr   Zfetch_request_tokenrequest_token_urllogdebugformatcreate_authorization_url)r   clientauthorization_endpointkwargsparamsr   r   r   r   r    _create_oauth1_authorization_url!   s   
z/AsyncRemoteApp._create_oauth1_authorization_urlc              	      s   |   I dH }| j}|s| js|d}|std| jr$|| j | jdi |4 I dH 2}||_| jrN| j	||fi |I dH W  d  I dH  S | j
||fi |W  d  I dH  S 1 I dH shw   Y  dS )zGenerate the authorization url and state for HTTP redirect.

        :param redirect_uri: Callback or redirect URI for authorization.
        :param kwargs: Extra parameters to include.
        :return: dict
        Nr"   zMissing "authorize_url" valuer   )r   Zauthorize_urlr   getRuntimeErrorZauthorize_paramsr   _get_oauth_clientredirect_urir%   Z _create_oauth2_authorization_url)r   r)   r#   r   r"   r!   r   r   r   r    ,   s0   


0z'AsyncRemoteApp.create_authorization_urlc           	   	      s&  |   I dH }| j}|s| js|d}| jdi |4 I dH e}| jrW|dur,||_|du r3t i }|| || ||_| j	pFi }|j
|fi |I dH }d|_n#|dur^||_i }| j	ri|| j	 || |j|fi |I dH }|W  d  I dH  S 1 I dH sw   Y  dS )aS  Fetch access token in one step.

        :param redirect_uri: Callback or Redirect URI that is used in
                             previous :meth:`authorize_redirect`.
        :param request_token: A previous request token for OAuth 1.
        :param params: Extra parameters to fetch access token.
        :return: A token dict.
        Ntoken_endpointr   )r   Zaccess_token_urlr   r&   r(   r)   r
   r   r   Zaccess_token_paramsfetch_access_tokenZfetch_token)	r   r)   r   r$   r   r*   r!   r   r#   r   r   r   r+   H   s6   	





0z!AsyncRemoteApp.fetch_access_tokenc           	   	      s  | j r|dst| j |}|d}|s|  I d H }ni }| jdi |4 I d H O}|dd }|rM|j||fi |I d H W  d   I d H  S |d u r[|r[| 	|I d H }|d u rbt
 ||_|j||fi |I d H W  d   I d H  S 1 I d H sw   Y  d S )N)zhttps://zhttp://withhold_tokenrequestr   )Zapi_base_url
startswithr   urljoinr&   r   r(   popr-   Z_fetch_tokenr   r   )	r   methodr   r   r#   r,   r   r!   r-   r   r   r   r-   n   s&   
0zAsyncRemoteApp.requestc                    sZ   |   I dH }| j|d fi |I dH }| }|d}|r)|| |I dH }t|S )z+Fetch user info from ``userinfo_endpoint``.NZuserinfo_endpointZuserinfo_compliance_fix)r   r&   jsonr   )r   r#   r   respdataZcompliance_fixr   r   r   userinfo   s   
zAsyncRemoteApp.userinfoc              
      s  t || jd}d|v r|d |d< t}nt}|  I dH }|du r/d|v r/dd|d gii}|d}|s9dg}t|}|  I dH }	z|j|d	 t	
|	|||d
}
W n! tyv   | jddI dH }	|j|d	 t	
|	|||d
}
Y nw |
jdd t|
S )z9Return an instance of UserInfo from token's ``id_token``.)nonce	client_idr   NZissuerZissvaluesZ%id_token_signing_alg_values_supportedZRS256Zid_token)key
claims_clsclaims_optionsclaims_paramsT)forcex   )Zleeway)dictr7   r   r   r   r&   r   _fetch_jwk_setdecoder   Zimport_key_set
ValueErrorvalidater   )r   r   r6   r;   r<   r:   r   Z
alg_valuesZjwtjwk_setZclaimsr   r   r   _parse_id_token   sJ   



zAsyncRemoteApp._parse_id_tokenFc                    sZ   |   I d H }|d}|r|s|S |d}|std| |I d H }|| jd< |S )NZjwksZjwks_urizMissing "jwks_uri" in metadata)r   r&   r'   r   r   )r   r=   r   rD   urir   r   r   r@      s   


zAsyncRemoteApp._fetch_jwk_setc              	      s`   |   4 I d H }|jd|ddI d H }| W  d   I d H  S 1 I d H s)w   Y  d S )NGETT)r,   )r(   r-   r2   )r   r   r!   r3   r   r   r   r      s
   0z%AsyncRemoteApp._fetch_server_metadata)NN)N)F)__name__
__module____qualname__r   r   r%   r    r+   r-   r5   rE   r@   r   r   r   r   r   r      s    



&

,)r   loggingZauthlib.common.urlsr   Zauthlib.joser   r   Zauthlib.oidc.corer   r   r   Zbase_appr	   errorsr
   r   __all__	getLoggerrH   r   r   r   r   r   r   <module>   s    
