o
    沪g                     @  s   d dl mZ d dlmZ d dlmZmZ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 er@G dd	 d	e
ZG d
d dZd#ddZd$ddZd%ddZd&ddZd'ddZd(ddZd)d d!Zd"S )*    )annotations)Mapping)datetime	timedeltatimezone)TYPE_CHECKINGAny	TypedDictcast)config)StreamlitAuthError)AttrDictsecrets_singletonc                   @  s   e Zd ZU ded< ded< dS )ProviderTokenPayloadstrproviderintexpN)__name__
__module____qualname____annotations__ r   r   O/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/streamlit/auth_util.pyr      s   
 r   c                   @  s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )	AuthCachezBSimple cache implementation for storing info required for Authlib.c                 C  s
   i | _ d S Ncacheselfr   r   r   __init__#   s   
zAuthCache.__init__c                 C  s   | j |S r   )r   getr   keyr   r   r   r!   &   s   zAuthCache.getc                 C  s   || j |< d S r   r   )r   r#   value
expires_inr   r   r   set+   s   zAuthCache.setc                 C  s   | j S r   r   r   r   r   r   get_dict.   s   zAuthCache.get_dictc                 C  s   | j |d  d S r   )r   popr"   r   r   r   delete1   s   zAuthCache.deleteN)	r   r   r   __doc__r    r!   r&   r'   r)   r   r   r   r   r       s    r   returnboolc               	   C  sP   zddl } | j}ttt|d}|dk rW dS W dS  ttfy'   Y dS w )zCheck if Authlib is installed.r   N.)         FT)authlib__version__tuplemapr   splitImportErrorModuleNotFoundError)r1   authlib_versionauthlib_version_tupler   r   r   is_authlib_installed5   s   r:   r   c                  C  s0   t d} t rtd}|r|d| } | S )zEGet the cookie signing secret from the configuration or secrets.toml.zserver.cookieSecretauthcookie_secret)r   
get_optionr   load_if_toml_existsr!   )signing_secretauth_sectionr   r   r   get_signing_secretD   s   

rA   r   c                  C  s&   t i } 	 t rtt td} | S )Nr;   )r   r   r>   r
   r!   )r@   r   r   r   get_secrets_auth_sectionN   s
   rB   r   c                 C  sh   zddl m} W n ty   tddw ddi}| ttjtdd d	}|	||t
 }|d
S )zAReturns a signed JWT token with the provider and expiration time.r   )jwtcTo use authentication features, you need to install Authlib>=1.3.2, e.g. via `pip install Authlib`.NalgHS256r0   )minutes)r   r   zlatin-1)authlib.joserC   r6   r   r   nowr   utcr   encoderA   decode)r   rC   headerpayloadprovider_tokenr   r   r   encode_provider_tokenW   s   
rP   rO   c              
   C  s   zddl m}m}m} W n ty   tddw ddiddid}z|j| t |d}|  W n |yE } ztd	| dd}~ww t	d
|S )z-Decode the JWT token and validate the claims.r   )	JoseError	JWTClaimsrC   rD   N	essentialT)r   r   )claims_optionszError decoding provider token: r   )
rH   rQ   rR   rC   r6   r   rL   rA   validater
   )rO   rQ   rR   rC   claim_optionsrN   er   r   r   decode_provider_tokenj   s(   
rX   dict[str, Any]c                 C  sl   i }|  dr|  d|d< |  dr|  d|d< |  dr&|  d|d< |  dr4|  d |d< |S )zKGenerate a default provider section for the 'auth' section of secrets.toml.	client_idclient_secretserver_metadata_urlclient_kwargs)r!   to_dict)r@   default_provider_sectionr   r   r   !generate_default_provider_section   s    



r`   Nonec                   s   t  stdt d}|du rtdd|vrtdd|vr%td||   du r6| dkr6t|  du rJ| dkrBtd	td
|  dt tsWtd|  dg d} fdd|D }|r}| dkrrtd| dtd|  d| ddS )zVValidate the general auth credentials and auth credentials for the given
    provider.zTo use authentication features you need to configure credentials for at
            least one authentication provider in `.streamlit/secrets.toml`.r;   Nredirect_urizAuthentication credentials in `.streamlit/secrets.toml` are missing the
            "redirect_uri" key. Please check your configuration.r<   zAuthentication credentials in `.streamlit/secrets.toml` are missing the
            "cookie_secret" key. Please check your configuration.defaultzAuthentication credentials in `.streamlit/secrets.toml` are missing for
                the default authentication provider. Please check your configuration.zeAuthentication credentials in `.streamlit/secrets.toml` are missing for the authentication provider "z#". Please check your configuration.zYAuthentication credentials in `.streamlit/secrets.toml` for the authentication provider "z6" must be valid TOML. Please check your configuration.)rZ   r[   r\   c                   s   g | ]}| vr|qS r   r   ).0r#   provider_sectionr   r   
<listcomp>   s    z-validate_auth_credentials.<locals>.<listcomp>zAuthentication credentials in `.streamlit/secrets.toml` for the default authentication provider are missing the following keys: z". Please check your configuration.z"" are missing the following keys: )r   r>   r   r!   r`   
isinstancer   )r   r@   required_keysmissing_keysr   re   r   validate_auth_credentials   sj   


rk   N)r+   r,   )r+   r   )r+   r   )r   r   r+   r   )rO   r   r+   r   )r+   rY   )r   r   r+   ra   )
__future__r   collections.abcr   r   r   r   typingr   r   r	   r
   	streamlitr   streamlit.errorsr   streamlit.runtime.secretsr   r   r   r   r:   rA   rB   rP   rX   r`   rk   r   r   r   r   <module>   s"   




	

