U
    
ڲg                     @   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
 e Zdddd	d
dg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dd ZdS )zFirebase credentials module.    N)requests)credentials)service_accountz.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 )zFetches a Google OAuth2 access token using this credential instance.

        Returns:
          AccessTokenInfo: An access token obtained using the credential.
        )get_credentialZrefresh_requestr   tokenr   )selfZgoogle_cred r   >/tmp/pip-unpacked-wheel-p0r7i5ii/firebase_admin/credentials.pyget_access_token/   s    
zBase.get_access_tokenc                 C   s   t dS )z?Returns the Google credential instance used for authentication.N)NotImplementedErrorr   r   r   r   r   9   s    zBase.get_credentialN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   ,   s   
r   c                       sP   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|   t|r6t|}t|}W 5 Q R X nt|trF|}nt	d
||d| jkrtt	d
| jztjj|td| _W n0 t	k
r } zt	d
|W 5 d}~X Y nX dS )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.
        z}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}".Zscopesz?Failed to initialize a certificate credential. Caused by: "{0}"N)superr   __init___is_file_pathopenjsonload
isinstancedict
ValueErrorformatget_CREDENTIAL_TYPEr   CredentialsZfrom_service_account_info_scopes_g_credential)r   cert	json_file	json_dataerror	__class__r   r   r   C   s0    

 zCertificate.__init__c                 C   s   | j jS N)r&   
project_idr   r   r   r   r.   f   s    zCertificate.project_idc                 C   s   | j jS r-   )r&   signerr   r   r   r   r/   j   s    zCertificate.signerc                 C   s   | j jS r-   )r&   service_account_emailr   r   r   r   r0   n   s    z!Certificate.service_account_emailc                 C   s   | j S zReturns the underlying Google credential.

        Returns:
          google.auth.credentials.Credentials: A Google Auth credential instance.r&   r   r   r   r   r   r   s    zCertificate.get_credential)r   r   r   r   r#   r   propertyr.   r/   r0   r   __classcell__r   r   r+   r   r   >   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 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   r5   r   r&   r   r+   r   r   r   }   s    zApplicationDefault.__init__c                 C   s   |    | 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   s   |    | 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.)r6   _project_idr   r   r   r   r.      s    	zApplicationDefault.project_idc                 C   s    | j stjjtd\| _ | _d S )Nr   )r&   googleauthdefaultr%   r7   r   r   r   r   r6      s    z#ApplicationDefault._load_credential)
r   r   r   r   r   r   r3   r.   r6   r4   r   r   r+   r   r5   z   s   	
r5   c                       sP   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 )RefreshTokenz8A credential initialized from an existing refresh token.Zauthorized_userc              	      s   t t|   t|r6t|}t|}W 5 Q R X nt|trF|}nt	d
||d| jkrtt	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.
        zInvalid refresh token argument: "{0}". Refresh token argument must be a file path, or a dict containing the parsed file contents.r   zSInvalid refresh token configuration. JSON must contain a "type" field set to "{0}".N)r   r;   r   r   r   r   r   r   r   r    r!   r"   r#   r   r$   Zfrom_authorized_user_infor%   r&   )r   refresh_tokenr(   r)   r+   r   r   r      s     

zRefreshToken.__init__c                 C   s   | j jS r-   )r&   	client_idr   r   r   r   r=      s    zRefreshToken.client_idc                 C   s   | j jS r-   )r&   client_secretr   r   r   r   r>      s    zRefreshToken.client_secretc                 C   s   | j jS r-   )r&   r<   r   r   r   r   r<      s    zRefreshToken.refresh_tokenc                 C   s   | j S r1   r2   r   r   r   r   r      s    zRefreshToken.get_credential)r   r   r   r   r#   r   r3   r=   r>   r<   r   r4   r   r   r+   r   r;      s    


r;   c                 C   s,   zt |  W dS  tk
r&   Y dS X d S )NTF)pathlibPath	TypeError)pathr   r   r   r      s
    
r   )r   collectionsr   r?   Zgoogle.authr8   Zgoogle.auth.transportr   Zgoogle.oauth2r   r   Requestr	   r%   
namedtupler   r   r   r5   r;   r   r   r   r   r   <module>   s*   	<'9