
    Ѯh!                         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y)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y)Basez>Provides OAuth2 access tokens for accessing Firebase services.c                     | j                         }|j                  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     l/home/www/academy-backend.kofcorporation.com/venv/lib/python3.12/site-packages/firebase_admin/credentials.pyget_access_tokenzBase.get_access_token0   s9     ))+H%{00+2D2D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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                 0    t         |           || _        y Nsuper__init___g_credential)r   r    	__class__s     r   r%   z_ExternalCredentials.__init__A   s    '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   >   s    V(#8 ("r   r   c                   \     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        |      r,t        |d      5 }t	        j
                  |      }ddd       n"t        |t              r|}nt        d| d      j                  d      | j                  k7  rt        d| j                   d      	 t        j                  j                  |t        	      | _        y# 1 sw Y   lxY w# t        $ r}t        d
| 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.
        utf-8encodingNzInvalid certificate argument: "z[". Certificate argument must be a file path, or a dict containing the parsed file contents.typezUInvalid service account certificate. Certificate must contain a "type" field set to "".scopesz;Failed to initialize a certificate credential. Caused by: "")r$   r%   _is_file_pathopenjsonload
isinstancedict
ValueErrorget_CREDENTIAL_TYPEr   r   from_service_account_info_scopesr&   )r   cert	json_file	json_dataerrorr'   s        r   r%   zCertificate.__init__Q   s    	dW- 1 IIi0	1 1d#I1$ 8G GH H == D$9$99((,(=(='>bBC C	c!0!<!<!V!V' "W "+D1 1   	cMeWTUVX]bc	cs#   C!*C C	C6!C11C6c                 .    | j                   j                  S r"   )r&   
project_idr   s    r   rI   zCertificate.project_idu   s    !!,,,r   c                 .    | j                   j                  S r"   )r&   signerr   s    r   rK   zCertificate.signery   s    !!(((r   c                 .    | j                   j                  S r"   )r&   service_account_emailr   s    r   rM   z!Certificate.service_account_email}   s    !!7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   rA   r%   propertyrI   rK   rM   r   r,   r-   s   @r   r/   r/   L   sT    C("cH - - ) ) 8 8"r   r/   c                   >     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                 0    t         |           d| _        y)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.
        Nr#   )r   r'   s    r   r%   zApplicationDefault.__init__   s     	!r   c                 :    | j                          | 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                 :    | j                          | 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.)rU   _project_idr   s    r   rI   zApplicationDefault.project_id   s     	r   c                     | j                   s2t        j                  j                  t              \  | _         | _        y y )Nr6   )r&   googleauthdefaultrC   rW   r   s    r   rU   z#ApplicationDefault._load_credential   s2    !!39;;3F3Fg3F3V0D 0 "r   )
r   r   r   r   r%   r   rP   rI   rU   r,   r-   s   @r   rR   rR      s*    2"	" 	  	 Wr   rR   c                   \     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        |      r,t        |d      5 }t	        j
                  |      }ddd       n"t        |t              r|}nt        d| d      j                  d      | j                  k7  rt        d| j                   d      t        j                  j                  |t              | _        y# 1 sw Y   jxY w)	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.
        r1   r2   Nz!Invalid refresh token argument: "z]". Refresh token argument must be a file path, or a dict containing the parsed file contents.r4   zNInvalid refresh token configuration. JSON must contain a "type" field set to "r5   )r$   r%   r9   r:   r;   r<   r=   r>   r?   r@   rA   r   r   from_authorized_user_inforC   r&   )r   refresh_tokenrE   rF   r'   s       r   r%   zRefreshToken.__init__   s      	'mg6 1) IIi0	1 1t,%I3M? CQ QR R == D$9$99((,(=(='>bBC C )44NNyZab1 1s   C

Cc                 .    | j                   j                  S r"   )r&   	client_idr   s    r   rc   zRefreshToken.client_id   s    !!+++r   c                 .    | j                   j                  S r"   )r&   client_secretr   s    r   re   zRefreshToken.client_secret       !!///r   c                 .    | j                   j                  S r"   )r&   ra   r   s    r   ra   zRefreshToken.refresh_token   rf   r   c                     | j                   S rO   r)   r   s    r   r   zRefreshToken.get_credential   r*   r   )r   r   r   r   rA   r%   rP   rc   re   ra   r   r,   r-   s   @r   r]   r]      sT    B(cB , , 0 0 0 0"r   r]   c                 N    	 t        j                  |        y# t        $ r Y yw xY w)NTF)pathlibPath	TypeError)paths    r   r9   r9      s(    T s    	$$)r   collectionsr;   rj   google.authrY   google.auth.credentialsr   r+   google.auth.transportr   google.oauth2r   r   Requestr   rC   
namedtupler   r   r   r/   rR   r]   r9   r   r   r   <module>ru      s    #     H * % ) 8 )+(():^X<VW" """4 ":"$ :"z%W %WN7"4 7"tr   