U
    	ڲgh                     @   s   d Z ddlZzddlZddlZdZW n ek
r<   dZY nX zddlZW n ek
rb   dZY nX zddlZddl	ZdZ
W n ek
r   dZ
Y nX dddZdddZd	d
 Zdd Zdd Zdd Zdd Zdd ZdS )z=Helpers for authentication using oauth2client or google-auth.    NTFc                 C   s*   t rtjj| ||d\}}|S tddS )z'Returns credentials loaded from a file.scopesquota_project_idzAclient_options.credentials_file is only supported in google-auth.N)HAS_GOOGLE_AUTHgoogleauthZload_credentials_from_fileEnvironmentError)filenamer   r   credentials_ r   9/tmp/pip-unpacked-wheel-rfrvjkbl/googleapiclient/_auth.pycredentials_from_file)   s      
r   c                 C   sP   t rtjj| |d\}}|S trD| dk	s0|dk	r8tdtjj	 S tddS )z(Returns Application Default Credentials.r   Nzwclient_options.scopes and client_options.quota_project_id are not supported in oauth2client.Please install google-auth.zZNo authentication library is available. Please install either google-auth or oauth2client.)
r   r   r   defaultHAS_OAUTH2CLIENTr   oauth2clientclientZGoogleCredentialsZget_application_default)r   r   r
   r   r   r   r   default_credentials6   s     
r   c                 C   s`   t r$t| tjjjr$tjj| |S z|  r:| |W S | W S W n t	k
rZ   |  Y S X dS )a  Scopes the credentials if necessary.

    Args:
        credentials (Union[
            google.auth.credentials.Credentials,
            oauth2client.client.Credentials]): The credentials to scope.
        scopes (Sequence[str]): The list of scopes.

    Returns:
        Union[google.auth.credentials.Credentials,
            oauth2client.client.Credentials]: The scoped credentials.
    N)
r   
isinstancer   r   r
   CredentialsZwith_scopes_if_requiredZcreate_scoped_requiredZcreate_scopedAttributeError)r
   r   r   r   r   with_scopesK   s    
r   c                 C   sP   ddl m} tr@t| tjjjr@tdkr0t	dtj
| | dS | | S dS )aa  Returns an http client that is authorized with the given credentials.

    Args:
        credentials (Union[
            google.auth.credentials.Credentials,
            oauth2client.client.Credentials]): The credentials to use.

    Returns:
        Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An
            authorized http client.
    r   )
build_httpNzCredentials from google.auth specified, but google-api-python-client is unable to use these credentials unless google-auth-httplib2 is installed. Please install google-auth-httplib2.http)Zgoogleapiclient.httpr   r   r   r   r   r
   r   google_auth_httplib2
ValueErrorZAuthorizedHttpZ	authorize)r
   r   r   r   r   authorized_httpd   s    r   c                 C   s>   t  }tr0t| tjjjr0t	|}| 
|S | 
|S d S N)httplib2ZHttpr   r   r   r   r
   r   r   RequestZrefresh)r
   Zrefresh_httprequestr   r   r   refresh_credentials   s
    

r"   c                 C   s   t | st|  | |S r   )is_validr"   apply)r
   headersr   r   r   apply_credentials   s    r&   c                 C   s0   t rt| tjjjr| jS | jd k	o*| j S d S r   )	r   r   r   r   r
   r   ZvalidZaccess_tokenZaccess_token_expired)r
   r   r   r   r#      s
    
r#   c                 C   sF   | d krd S t | jdr | jjS t | dr>t| jtjs>| jS d S d S )Nr
   )hasattrr!   r
   r   r   r   r   r   r   r   get_credentials_from_http   s     r(   )NN)NN)__doc__r   Zgoogle.authr   Zgoogle.auth.credentialsr   ImportErrorr   r   Zoauth2client.clientr   r   r   r   r   r"   r&   r#   r(   r   r   r   r   <module>   s2   





