o
    Yh!                     @  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   T/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/auth_util.pyr      s   
 r   c                   @  sD   e Zd ZdZdddZdd	d
ZddddZdddZdddZdS )	AuthCachezBSimple cache implementation for storing info required for Authlib.returnNonec                 C  s
   i | _ d S Ncacheselfr   r   r   __init__#   s   
zAuthCache.__init__keyr   r   c                 C  s   | j |S r   )r   getr!   r#   r   r   r   r$   &   s   zAuthCache.getNvalue
expires_in
int | Nonec                 C  s   || j |< d S r   r   )r!   r#   r&   r'   r   r   r   set+   s   zAuthCache.setdict[str, Any]c                 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.delete)r   r   )r#   r   r   r   r   )r#   r   r&   r   r'   r(   r   r   )r   r*   )r#   r   r   r   )	r   r   r   __doc__r"   r$   r)   r+   r-   r   r   r   r   r       s    


r   r   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)r4   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   

rD   r   c                  C  s&   t i } 	 t rtdtd} | S )Nr   r>   )r   r   rA   r
   r$   )rC   r   r   r   get_secrets_auth_sectionN   s
   rE   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HS256r3   )minutes)r   r   zlatin-1)authlib.joserF   r9   r   r   nowr   utcr   encoderD   decode)r   rF   headerpayloadprovider_tokenr   r   r   encode_provider_tokenW   s   
rS   rR   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	JWTClaimsrF   rG   N	essentialT)r   r   )claims_optionszError decoding provider token: r   )
rK   rT   rU   rF   r9   r   rO   rD   validater
   )rR   rT   rU   rF   claim_optionsrQ   er   r   r   decode_provider_tokenj   s(   
r[   rC   r*   c                 C  sx   i }|  dr|  d|d< |  dr|  d|d< |  dr&|  d|d< |  dr:td|  dti  |d< |S )zKGenerate a default provider section for the 'auth' section of secrets.toml.	client_idclient_secretserver_metadata_urlclient_kwargsr   )r$   r
   r   to_dict)rC   default_provider_sectionr   r   r   !generate_default_provider_section   s    



rb   r   c                   s  t  stdt d}|du rtdd|vrtdd|vr%td||  d| v r6td	|  d
 du rB| dkrBt|  du rV| dkrNtdtd|  dt tsctd|  dg d} fdd|D }|r| dkr~td| dtd|  d| ddS )z[Validate 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._zAuth provider name "zI" contains an underscore. Please use a provider name without underscores.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.)r\   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   rA   r   r$   rb   
isinstancer   )r   rC   required_keysmissing_keysr   rg   r   validate_auth_credentials   sr   



rm   N)r   r/   )r   r   )r   r   )r   r   r   r   )rR   r   r   r   )rC   r   r   r*   )r   r   r   r   )
__future__r   collections.abcr   r   r   r   typingr   r   r	   r
   	streamlitr   streamlit.errorsr   streamlit.runtime.secretsr   r   r   r   r=   rD   rE   rS   r[   rb   rm   r   r   r   r   <module>   s"   




	

