
    g                        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	e
      Z G d
 d      ZddZddZddZddZddZddZddZy)    )annotations)Mapping)datetime	timedeltatimezone)TYPE_CHECKINGAny	TypedDictcast)config)StreamlitAuthError)AttrDictsecrets_singletonc                  "    e Zd ZU ded<   ded<   y)ProviderTokenPayloadstrproviderintexpN)__name__
__module____qualname____annotations__     H/var/www/openai/venv/lib/python3.12/site-packages/streamlit/auth_util.pyr   r      s    r   r   c                  .    e Zd ZdZd Zd Zd Zd Zd Zy)	AuthCachezBSimple cache implementation for storing info required for Authlib.c                    i | _         y Ncacheselfs    r   __init__zAuthCache.__init__#   s	    
r   c                8    | j                   j                  |      S r    )r"   getr$   keys     r   r'   zAuthCache.get&   s    zz~~c""r   c                "    || j                   |<   y r    r!   )r$   r)   value
expires_ins       r   setzAuthCache.set+   s    

3r   c                    | j                   S r    r!   r#   s    r   get_dictzAuthCache.get_dict.   s    zzr   c                <    | j                   j                  |d        y r    )r"   popr(   s     r   deletezAuthCache.delete1   s    

sD!r   N)	r   r   r   __doc__r%   r'   r-   r/   r2   r   r   r   r   r       s    L#
 "r   r   c                     	 ddl } | j                  }t        t        t        |j                  d                  }|dk  ry	 y# t        t        f$ r Y yw xY w)zCheck if Authlib is installed.r   N.)         FT)authlib__version__tuplemapr   splitImportErrorModuleNotFoundError)r9   authlib_versionauthlib_version_tuples      r   is_authlib_installedrB   5   sa    	!-- %c#/D/DS/I&J K 9, -  ,- s   =A AAc                     t        j                  d      } t        j                         r)t        j                  d      }|r|j	                  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_sections     r   get_signing_secretrJ   D   sJ     ++,ABN,,.(,,V4)--o~NNr   c                     t        i       } 	 t        j                         r#t        t         t        j                  d            } | S )NrD   )r   r   rG   r   r'   )rI   s    r   get_secrets_auth_sectionrL   N   s8    B<L5,,.H&7&;&;F&CDr   c                   	 ddl m} ddi}| t	        j
                  t        j                        t        d      z   d	}|j                  ||t                     }|j                  d
      S # t        $ r t        d      dw xY w)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HS256r8   )minutes)r   r   zlatin-1)authlib.joserN   r>   r   r   nowr   utcr   encoderJ   decode)r   rN   headerpayloadprovider_tokens        r   encode_provider_tokenr[   W   s    $ WF||HLL)Ia,@@G  JJvw8J8LMN  ++   u
	s   A+ +Bc                   	 ddl m}m}m} ddiddid}	 |j                  | t               |      }|j                          t        d
|      S # t        $ r t        d      dw xY w# |$ r}t        d	|       dd}~ww xY w)z-Decode the JWT token and validate the claims.r   )	JoseError	JWTClaimsrN   rO   N	essentialT)r   r   )claims_optionszError decoding provider token: r   )
rS   r]   r^   rN   r>   r   rW   rJ   validater   )rZ   r]   r^   rN   claim_optionsrY   es          r   decode_provider_tokenrd   j   s    :: )$/k4=PQMR ZZ.0 ( 
 	 &00!   u
	  R #B1#!FGTQRs"   
A ,A' A$'B ,A;;B c                N   i }| j                  d      r| j                  d      |d<   | j                  d      r| j                  d      |d<   | j                  d      r| j                  d      |d<   | j                  d      r"| j                  d      j                         |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)rI   default_provider_sections     r   !generate_default_provider_sectionrl      s    !$0<0@0@0M -(4@4D4D_4U 1-.:F:J:J!;
 !67 (4@4D4D5

') 	!1 $#r   c                   t        j                         st        d      t        j                  d      }|t        d      d|vrt        d      d|vrt        d      |j                  |       }|| dk(  rt	        |      }|| dk(  rt        d	      t        d
|  d      t        |t              st        d|  d      g d}|D cg c]	  }||vs| }}|r&| dk(  rt        d| d      t        d|  d| d      yc c}w )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`.rD   Nredirect_urizAuthentication credentials in `.streamlit/secrets.toml` are missing the
            "redirect_uri" key. Please check your configuration.rE   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.)rf   rg   rh   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   rG   r   r'   rl   
isinstancer   )r   rI   provider_sectionrequired_keysr)   missing_keyss         r   validate_auth_credentialsrt      s    002 O
 	

 %((0L O
 	
 \) D
 	
 l* E
 	

 $''1H	$9<\Jy $Y  !,,4: 6
 	
 &0 ((0z 2"#
 	
 JM#0P=CC?O4OC=LPy $S. BD 
 !((0z1Sn>@
 	
  Qs   	DDN)returnbool)ru   r   )ru   r   )r   r   ru   r   )rZ   r   ru   r   )ru   zdict[str, Any])r   r   ru   None)
__future__r   collections.abcr   r   r   r   typingr   r	   r
   r   	streamlitr   streamlit.errorsr   streamlit.runtime.secretsr   r   r   r   rB   rJ   rL   r[   rd   rl   rt   r   r   r   <module>r~      s^    # # 2 2 6 6  / Ay 
" "*,&1.$$?
r   