
    .
0h!                     8   d Z ddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ  e
j                    Zg dZ ej        dd	d
g          Z	  G d d          Z G d de          Z G d de          Z G d de          Z G d de          Zd ZdS )zFirebase credentials module.    N)Credentials)requests)credentials)service_account)z.https://www.googleapis.com/auth/cloud-platformz)https://www.googleapis.com/auth/datastorez5https://www.googleapis.com/auth/devstorage.read_writez(https://www.googleapis.com/auth/firebasez/https://www.googleapis.com/auth/identitytoolkitz.https://www.googleapis.com/auth/userinfo.emailAccessTokenInfoaccess_tokenexpiryc                       e Zd ZdZd Zd ZdS )Basez>Provides OAuth2 access tokens for accessing Firebase services.c                     |                                  }|                    t                     t          |j        |j                  S )zFetches a Google OAuth2 access token using this credential instance.

        Returns:
          AccessTokenInfo: An access token obtained using the credential.
        )get_credentialrefresh_requestr   tokenr	   )selfgoogle_creds     U/var/www/html/nourish/venv/lib/python3.11/site-packages/firebase_admin/credentials.pyget_access_tokenzBase.get_access_token0   s?     ))++H%%%{0+2DEEE    c                     t           )z?Returns the Google credential instance used for authentication.)NotImplementedErrorr   s    r   r   zBase.get_credential:   s    !!r   N)__name__
__module____qualname____doc__r   r    r   r   r   r   -   s;        HHF F F" " " " "r   r   c                   .     e Zd ZdZdef fdZd Z xZS )_ExternalCredentialszLA wrapper for google.auth.credentials.Credentials typed credential instances
credentialc                 d    t          t          |                                            || _        d S N)superr   __init___g_credential)r   r    	__class__s     r   r$   z_ExternalCredentials.__init__A   s.    "D))22444'r   c                     | j         S )zReturns the underlying Google Credential

        Returns:
          google.auth.credentials.Credentials: A Google Auth credential instance.r%   r   s    r   r   z#_ExternalCredentials.get_credentialE       
 !!r   )r   r   r   r   GoogleAuthCredentialsr$   r   __classcell__r&   s   @r   r   r   >   sZ        VV(#8 ( ( ( ( ( (" " " " " " "r   r   c                   n     e Zd ZdZdZ fdZed             Zed             Zed             Z	d Z
 xZS )Certificatez9A credential initialized from a JSON certificate keyfile.r   c                    t          t          |                                            t          |          r<t	          |          5 }t          j        |          }ddd           n# 1 swxY w Y   n:t          |t                    r|}n"t          d
                    |                    |                    d          | j        k    r't          d
                    | j                            	 t          j                            |t                     | _        dS # t          $ r'}t          d
                    |                    d}~ww xY w)a]  Initializes a credential from a Google service account certificate.

        Service account certificates can be downloaded as JSON files from the Firebase console.
        To instantiate a credential from a certificate file, either specify the file path or a
        dict representing the parsed contents of the file.

        Args:
          cert: Path to a certificate file or a dict representing the contents of a certificate.

        Raises:
          IOError: If the specified certificate file doesn't exist or cannot be read.
          ValueError: If the specified certificate is invalid.
        Nz}Invalid certificate argument: "{0}". Certificate argument must be a file path, or a dict containing the parsed file contents.typezZInvalid service account certificate. Certificate must contain a "type" field set to "{0}".scopesz?Failed to initialize a certificate credential. Caused by: "{0}")r#   r.   r$   _is_file_pathopenjsonload
isinstancedict
ValueErrorformatget_CREDENTIAL_TYPEr   r   from_service_account_info_scopesr%   )r   cert	json_file	json_dataerrorr&   s        r   r$   zCertificate.__init__Q   s    	k4  ))+++ 	Od 1y Ii00	1 1 1 1 1 1 1 1 1 1 1 1 1 1 1d## 	OIIAAGO O O ==  D$999 ::@&AV:W:WY Y Y	?!0!<!V!V' "W "+ "+D 	? 	? 	? 006u? ? ?	?s*   A((A,/A,4+D! !
E+"EEc                     | j         j        S r"   )r%   
project_idr   s    r   rD   zCertificate.project_idt   s    !,,r   c                     | j         j        S r"   )r%   signerr   s    r   rF   zCertificate.signerx   s    !((r   c                     | j         j        S r"   )r%   service_account_emailr   s    r   rH   z!Certificate.service_account_email|   s    !77r   c                     | j         S zReturns the underlying Google credential.

        Returns:
          google.auth.credentials.Credentials: A Google Auth credential instance.r(   r   s    r   r   zCertificate.get_credential   r)   r   )r   r   r   r   r<   r$   propertyrD   rF   rH   r   r+   r,   s   @r   r.   r.   L   s        CC(!? !? !? !? !?F - - X- ) ) X) 8 8 X8" " " " " " "r   r.   c                   D     e Zd ZdZ fdZd Zed             Zd Z xZ	S )ApplicationDefaultz(A Google Application Default credential.c                 d    t          t          |                                            d| _        dS )zCreates an instance that will use Application Default credentials.

        The credentials will be lazily initialized when get_credential() or
        project_id() is called. See those methods for possible errors raised.
        N)r#   rM   r$   r%   )r   r&   s    r   r$   zApplicationDefault.__init__   s0     	 $''00222!r   c                 8    |                                   | j        S )a:  Returns the underlying Google credential.

        Raises:
          google.auth.exceptions.DefaultCredentialsError: If Application Default
              credentials cannot be initialized in the current environment.
        Returns:
          google.auth.credentials.Credentials: A Google Auth credential instance.)_load_credentialr%   r   s    r   r   z!ApplicationDefault.get_credential   s     	!!r   c                 8    |                                   | j        S )a  Returns the project_id from the underlying Google credential.

        Raises:
          google.auth.exceptions.DefaultCredentialsError: If Application Default
              credentials cannot be initialized in the current environment.
        Returns:
          str: The project id.)rP   _project_idr   s    r   rD   zApplicationDefault.project_id   s     	r   c                 |    | j         s4t          j                            t                    \  | _         | _        d S d S )Nr1   )r%   googleauthdefaultr>   rR   r   s    r   rP   z#ApplicationDefault._load_credential   sC    ! 	W39;3F3Fg3F3V3V0D 0 0 0	W 	Wr   )
r   r   r   r   r$   r   rK   rD   rP   r+   r,   s   @r   rM   rM      s~        22" " " " "	" 	" 	" 	  	  X	 W W W W W W Wr   rM   c                   n     e Zd ZdZdZ fdZed             Zed             Zed             Z	d Z
 xZS )RefreshTokenz8A credential initialized from an existing refresh token.authorized_userc                 >   t          t          |                                            t          |          r<t	          |          5 }t          j        |          }ddd           n# 1 swxY w Y   n:t          |t                    r|}n"t          d
                    |                    |                    d          | j        k    r't          d
                    | j                            t          j                            |t                     | _        dS )a  Initializes a credential from a refresh token JSON file.

        The JSON must consist of client_id, client_secret and refresh_token fields. Refresh
        token files are typically created and managed by the gcloud SDK. To instantiate
        a credential from a refresh token file, either specify the file path or a dict
        representing the parsed contents of the file.

        Args:
          refresh_token: Path to a refresh token file or a dict representing the contents of a
              refresh token file.

        Raises:
          IOError: If the specified file doesn't exist or cannot be read.
          ValueError: If the refresh token configuration is invalid.
        NzInvalid refresh token argument: "{0}". Refresh token argument must be a file path, or a dict containing the parsed file contents.r0   zSInvalid refresh token configuration. JSON must contain a "type" field set to "{0}".)r#   rX   r$   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r   r   from_authorized_user_infor>   r%   )r   refresh_tokenr@   rA   r&   s       r   r$   zRefreshToken.__init__   sL     	lD!!**,,,'' 	^m$$ 1	 Ii00	1 1 1 1 1 1 1 1 1 1 1 1 1 1 1t,, 	^%IIGGMvmG\G\^ ^ ^ ==  D$999 ::@&AV:W:WY Y Y(4NNyZabbs   A((A,/A,c                     | j         j        S r"   )r%   	client_idr   s    r   r^   zRefreshToken.client_id   s    !++r   c                     | j         j        S r"   )r%   client_secretr   s    r   r`   zRefreshToken.client_secret       !//r   c                     | j         j        S r"   )r%   r\   r   s    r   r\   zRefreshToken.refresh_token   ra   r   c                     | j         S rJ   r(   r   s    r   r   zRefreshToken.get_credential   r)   r   )r   r   r   r   r<   r$   rK   r^   r`   r\   r   r+   r,   s   @r   rX   rX      s        BB(c c c c c@ , , X, 0 0 X0 0 0 X0" " " " " " "r   rX   c                 R    	 t          j        |            dS # t          $ r Y dS w xY w)NTF)pathlibPath	TypeError)paths    r   r3   r3      s>    Tt   uus    
&&)r   collectionsr5   re   google.authrT   google.auth.credentialsr   r*   google.auth.transportr   google.oauth2r   r   Requestr   r>   
namedtupler   r   r   r.   rM   rX   r3   r   r   r   <module>rp      s   # "           H H H H H H * * * * * * % % % % % % ) ) ) ) ) ) 8   )+():^X<VWW" " " " " " " """ " " " "4 " " "9" 9" 9" 9" 9"$ 9" 9" 9"x%W %W %W %W %W %W %W %WN6" 6" 6" 6" 6"4 6" 6" 6"r    r   