U
    6d                     @   s   d 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 zddlmZ W n e	k
rd   dZY nX G dd	 d	ej
Z
d
d ZG dd dejejejZdS )a  Google App Engine standard environment support.

This module provides authentication and signing for applications running on App
Engine in the standard environment using the `App Identity API`_.


.. _App Identity API:
    https://cloud.google.com/appengine/docs/python/appidentity/
    N)_helpers)credentials)crypt)
exceptions)app_identityc                   @   s0   e Zd ZdZedd Zeej	dd Z
dS )SignerzSigns messages using the App Engine App Identity service.

    This can be used in place of :class:`google.auth.crypt.Signer` when
    running in the App Engine standard environment.
    c                 C   s   dS )zOptional[str]: The key ID used to identify this private key.

        .. warning::
           This is always ``None``. The key ID used by App Engine can not
           be reliably determined ahead of time.
        N selfr   r   :/tmp/pip-unpacked-wheel-h3lf9jv9/google/auth/app_engine.pykey_id/   s    zSigner.key_idc                 C   s   t |}t|\}}|S N)r   to_bytesr   Z	sign_blob)r
   message_	signaturer   r   r   sign9   s    
zSigner.signN)__name__
__module____qualname____doc__propertyr   r   copy_docstringr   r   r   r   r   r   r   r   (   s
   
	
r   c                   C   s   t dkrtdt  S )zGets the project ID for the current App Engine application.

    Returns:
        str: The project ID

    Raises:
        google.auth.exceptions.OSError: If the App Engine APIs are unavailable.
    N&The App Engine APIs are not available.)r   r   OSErrorZget_application_idr   r   r   r   get_project_id@   s    
r   c                       s   e Zd ZdZd fdd	Zeejdd Z	e
dd Ze
d	d
 ZeejdddZeejdd Zeejdd Ze
eejdd Ze
eejdd Z  ZS )CredentialszApp Engine standard environment credentials.

    These credentials use the App Engine App Identity API to obtain access
    tokens.
    Nc                    sD   t dkrtdtt|   || _|| _|| _t	 | _
|| _dS )a  
        Args:
            scopes (Sequence[str]): Scopes to request from the App Identity
                API.
            default_scopes (Sequence[str]): Default scopes passed by a
                Google client library. Use 'scopes' for user-defined scopes.
            service_account_id (str): The service account ID passed into
                :func:`google.appengine.api.app_identity.get_access_token`.
                If not specified, the default application service account
                ID will be used.
            quota_project_id (Optional[str]): The project ID used for quota
                and billing.

        Raises:
            google.auth.exceptions.OSError: If the App Engine APIs are unavailable.
        Nr   )r   r   r   superr   __init___scopes_default_scopes_service_account_idr   _signerZ_quota_project_id)r
   scopesdefault_scopesservice_account_idquota_project_id	__class__r   r   r   Z   s    
zCredentials.__init__c                 C   sF   | j d k	r| j n| j}t|| j\}}tj|}|| | _| _d S r   )	r   r    r   Zget_access_tokenr!   datetimeutcfromtimestamptokenexpiry)r
   requestr#   r+   Zttlr,   r   r   r   refresh~   s    zCredentials.refreshc                 C   s   | j dkrt | _ | j S )zThe service account email.N)r!   r   Zget_service_account_namer	   r   r   r   service_account_email   s    

z!Credentials.service_account_emailc                 C   s   | j  o| j S )zChecks if the credentials requires scopes.

        Returns:
            bool: True if there are no scopes set otherwise False.
        )r   r    r	   r   r   r   requires_scopes   s    zCredentials.requires_scopesc                 C   s   | j ||| j| jdS )N)r#   r$   r%   r&   )r(   r!   r&   )r
   r#   r$   r   r   r   with_scopes   s    zCredentials.with_scopesc                 C   s   | j | j| j|dS )N)r#   r%   r&   )r(   r   r!   )r
   r&   r   r   r   with_quota_project   s
    zCredentials.with_quota_projectc                 C   s   | j |S r   )r"   r   )r
   r   r   r   r   
sign_bytes   s    zCredentials.sign_bytesc                 C   s   | j S r   )r/   r	   r   r   r   signer_email   s    zCredentials.signer_emailc                 C   s   | j S r   )r"   r	   r   r   r   signer   s    zCredentials.signer)NNNN)N)r   r   r   r   r   r   r   r   r   r.   r   r/   r0   Scopedr1   CredentialsWithQuotaProjectr2   Signingr3   r4   r5   __classcell__r   r   r'   r   r   Q   s0       $










r   )r   r)   Zgoogle.authr   r   r   r   Zgoogle.appengine.apir   ImportErrorr   r   r6   r8   r7   r   r   r   r   r   <module>   s    


  