o
    0=j—!  ã                   @   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ÚAccessTokenInfoZ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 )z©Fetches a Google OAuth2 access token using this credential instance.

        Returns:
          AccessTokenInfo: An access token obtained using the credential.
        N)Úget_credentialZrefreshÚ_requestr   Útokenr   )ÚselfZgoogle_cred© r   údC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\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.N)Ú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 )zŒReturns the underlying Google Credential

        Returns:
          google.auth.credentials.Credentials: A Google Auth credential instance.N©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 "ú".©Zscopesz;Failed to initialize a certificate credential. Caused by: "ú")r   r   Ú_is_file_pathÚopenÚjsonÚloadÚ
isinstanceÚdictÚ
ValueErrorÚgetÚ_CREDENTIAL_TYPEr   r   Zfrom_service_account_infoÚ_scopesr   )r   ÚcertÚ	json_fileÚ	json_dataÚerrorr   r   r   r   Q   s8   
ÿ

ÿÿÿÿ
ÿÿ€ÿzCertificate.__init__c                 C   ó   | j jS r   )r   Ú
project_idr   r   r   r   r<   u   ó   zCertificate.project_idc                 C   r;   r   )r   Úsignerr   r   r   r   r>   y   r=   zCertificate.signerc                 C   r;   r   )r   Úservice_account_emailr   r   r   r   r?   }   r=   z!Certificate.service_account_emailc                 C   r   ©z�Returns the underlying Google credential.

        Returns:
          google.auth.credentials.Credentials: A Google Auth credential instance.Nr    r   r   r   r   r	   �   r!   zCertificate.get_credential)r   r   r   r   r5   r   Úpropertyr<   r>   r?   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 )zæCreates 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.N)Ú_load_credentialr   r   r   r   r   r	   •   s   z!ApplicationDefault.get_credentialc                 C   rC   )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.N)rD   Ú_project_idr   r   r   r   r<       s   	zApplicationDefault.project_idc                 C   s$   | j stjjtd�\| _ | _d S d S )Nr+   )r   ÚgoogleÚauthÚdefaultr6   rE   r   r   r   r   rD   ¬   s   ÿz#ApplicationDefault._load_credential)
r   r   r   r   r   r	   rA   r<   rD   r#   r   r   r   r   rB   ‰   s    	
rB   c                       r$   )ÚRefreshTokenz8A credential initialized from an existing refresh token.Z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   r-   r.   r/   r0   r1   r2   r3   r4   r5   r   r   Zfrom_authorized_user_infor6   r   )r   Úrefresh_tokenr8   r9   r   r   r   r   µ   s"   
ÿ

ÿÿÿzRefreshToken.__init__c                 C   r;   r   )r   Ú	client_idr   r   r   r   rK   Ö   r=   zRefreshToken.client_idc                 C   r;   r   )r   Úclient_secretr   r   r   r   rL   Ú   r=   zRefreshToken.client_secretc                 C   r;   r   )r   rJ   r   r   r   r   rJ   Þ   r=   zRefreshToken.refresh_tokenc                 C   r   r@   r    r   r   r   r   r	   â   r!   zRefreshToken.get_credential)r   r   r   r   r5   r   rA   rK   rL   rJ   r	   r#   r   r   r   r   rI   °   s    !


rI   c                 C   s&   zt  | ¡ W dS  ty   Y dS w )NTF)ÚpathlibÚPathÚ	TypeError)Úpathr   r   r   r-   ê   s   
ÿr-   )r   Úcollectionsr/   rM   Zgoogle.authrF   Zgoogle.auth.credentialsr   r"   Zgoogle.auth.transportr   Zgoogle.oauth2r   r   ÚRequestr
   r6   Ú
namedtupler   r   r   r%   rB   rI   r-   r   r   r   r   Ú<module>   s&   	=':