o
     i!                     @   s   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
 Zg dZedd	d
gZ	 G dd dZG dd deZG dd deZG dd deZG dd deZd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                   @   s    e Zd ZdZdd Zdd ZdS )Basez>Provides OAuth2 access tokens for accessing Firebase services.c                 C   s    |   }|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_cred r   z/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/env/lib/python3.10/site-packages/firebase_admin/credentials.pyget_access_token0   s   
zBase.get_access_tokenc                 C   s   t )z?Returns the Google credential instance used for authentication.)NotImplementedErrorr   r   r   r   r
   :   s   zBase.get_credentialN)__name__
__module____qualname____doc__r   r
   r   r   r   r   r	   -   s    
r	   c                       s.   e Zd ZdZdef fddZdd Z  ZS )_ExternalCredentialszLA wrapper for google.auth.credentials.Credentials typed credential instances
credentialc                    s   t    || _d S Nsuper__init___g_credential)r   r   	__class__r   r   r   A   s   

z_ExternalCredentials.__init__c                 C      | j S )zReturns the underlying Google Credential

        Returns:
          google.auth.credentials.Credentials: A Google Auth credential instance.r   r   r   r   r   r
   E      z#_ExternalCredentials.get_credential)r   r   r   r   GoogleAuthCredentialsr   r
   __classcell__r   r   r    r   r   >   s    r   c                       P   e Zd ZdZdZ fddZedd Zedd Zed	d
 Z	dd Z
  ZS )Certificatez9A credential initialized from a JSON certificate keyfile.r   c              
      s   t    t|r%t|dd}t|}W d   n1 sw   Y  nt|tr-|}ntd| d|	d| j
krFtd| j
 dztjj|td	| _W dS  tyg } z	td
| d|d}~w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    r   r   r   Q   s:   



zCertificate.__init__c                 C      | j jS r   )r   
project_idr   r   r   r   rA   u      zCertificate.project_idc                 C   r@   r   )r   signerr   r   r   r   rC   y   rB   zCertificate.signerc                 C   r@   r   )r   service_account_emailr   r   r   r   rD   }   rB   z!Certificate.service_account_emailc                 C   r"   zReturns the underlying Google credential.

        Returns:
          google.auth.credentials.Credentials: A Google Auth credential instance.r#   r   r   r   r   r
      r$   zCertificate.get_credential)r   r   r   r   r9   r   propertyrA   rC   rD   r
   r&   r   r   r    r   r(   L   s    $


r(   c                       s<   e Zd ZdZ fddZdd Zedd Zdd	 Z  Z	S )
ApplicationDefaultz(A Google Application Default credential.c                    s   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.
        Nr   r   r    r   r   r      s   

zApplicationDefault.__init__c                 C      |    | 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   r   r   r   r
      s   z!ApplicationDefault.get_credentialc                 C   rH   )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.)rI   _project_idr   r   r   r   rA      s   	zApplicationDefault.project_idc                 C   s$   | j stjjtd\| _ | _d S d S )Nr.   )r   googleauthdefaultr;   rJ   r   r   r   r   rI      s   z#ApplicationDefault._load_credential)
r   r   r   r   r   r
   rF   rA   rI   r&   r   r   r    r   rG      s    	
rG   c                       r'   )RefreshTokenz8A credential initialized from an existing refresh token.authorized_userc                    s   t    t|r%t|dd}t|}W d   n1 sw   Y  nt|tr-|}ntd| d|	d| j
krFtd| j
 d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.
        r)   r*   Nz!Invalid refresh token argument: "z]". Refresh token argument must be a file path, or a dict containing the parsed file contents.r,   zNInvalid refresh token configuration. JSON must contain a "type" field set to "r-   )r   r   r1   r2   r3   r4   r5   r6   r7   r8   r9   r   r   from_authorized_user_infor;   r   )r   refresh_tokenr=   r>   r    r   r   r      s$   


zRefreshToken.__init__c                 C   r@   r   )r   	client_idr   r   r   r   rR      rB   zRefreshToken.client_idc                 C   r@   r   )r   client_secretr   r   r   r   rS      rB   zRefreshToken.client_secretc                 C   r@   r   )r   rQ   r   r   r   r   rQ      rB   zRefreshToken.refresh_tokenc                 C   r"   rE   r#   r   r   r   r   r
      r$   zRefreshToken.get_credential)r   r   r   r   r9   r   rF   rR   rS   rQ   r
   r&   r   r   r    r   rN      s    !


rN   c                 C   s&   zt |  W dS  ty   Y dS w )NTF)pathlibPath	TypeError)pathr   r   r   r1      s   
r1   )r   collectionsr3   rT   google.authrK   google.auth.credentialsr   r%   google.auth.transportr   google.oauth2r   r   Requestr   r;   
namedtupler   r	   r   r(   rG   rN   r1   r   r   r   r   <module>   s&   	=':