U
    6d                  
   @   s0  d Z ddlmZ ddlZddlmZ ddlZddlZddl	Zddl
ZzddlZddlZddlZW n2 ek
r Z zeede W 5 dZ[X Y nX zddlZdZW n ek
r   dZY nX dZd	d
 Zdd Zdd Zdd Zejjeejjeejjeejjeejj j!eiZ"er$ee"ejj#j!< dd Z$dS )zHelpers for transitioning from oauth2client to google-auth.

.. warning::
    This module is private as it is intended to assist first-party downstream
    clients with the transition from oauth2client to google-auth.
    )absolute_importN)_helperszoauth2client is not installed.TFz8Unable to convert {} to a google-auth credentials class.c                 C   s2   t jjj| j| j| j| j| j| j	d}| j
|_|S )aB  Converts to :class:`google.oauth2.credentials.Credentials`.

    Args:
        credentials (Union[oauth2client.client.OAuth2Credentials,
            oauth2client.client.GoogleCredentials]): The credentials to
            convert.

    Returns:
        google.oauth2.credentials.Credentials: The converted credentials.
    )tokenrefresh_token	token_uri	client_idclient_secretscopes)googleoauth2credentialsCredentialsZaccess_tokenr   r   r   r   r	   Ztoken_expiryZ_expires)r   Znew_credentials r   =/tmp/pip-unpacked-wheel-h3lf9jv9/google/auth/_oauth2client.py_convert_oauth2_credentials2   s    	r   c                 C   s$   | j  }| j|d< tjjj|S )au  Converts to :class:`google.oauth2.service_account.Credentials`.

    Args:
        credentials (Union[
            oauth2client.service_account.ServiceAccountCredentials,
            oauth2client.service_account._JWTAccessCredentials]): The
            credentials to convert.

    Returns:
        google.oauth2.service_account.Credentials: The converted credentials.
    r   )Zserialization_datacopyr   r
   r   service_accountr   Zfrom_service_account_info)r   infor   r   r   $_convert_service_account_credentialsK   s    

r   c                 C   s   t jjj| jdS )a  Converts to :class:`google.auth.compute_engine.Credentials`.

    Args:
        credentials (oauth2client.contrib.gce.AppAssertionCredentials): The
            credentials to convert.

    Returns:
        google.oauth2.service_account.Credentials: The converted credentials.
    )service_account_email)r
   authZcompute_enginer   r   r   r   r   r   &_convert_gce_app_assertion_credentials\   s    
r   c                 C   s   t jjjt| j| jdS )a  Converts to :class:`google.auth.app_engine.Credentials`.

    Args:
        credentials (oauth2client.contrib.app_engine.AppAssertionCredentials):
            The credentials to convert.

    Returns:
        google.oauth2.service_account.Credentials: The converted credentials.
    )r	   service_account_id)r
   r   Z
app_enginer   r   Zstring_to_scopesZscoper   r   r   r   r   ,_convert_appengine_app_assertion_credentialsk   s    
r   c              
   C   sX   t | }zt| | W S  tk
rR } ztt|}t|| W 5 d}~X Y nX dS )a  Convert oauth2client credentials to google-auth credentials.

    This class converts:

    - :class:`oauth2client.client.OAuth2Credentials` to
      :class:`google.oauth2.credentials.Credentials`.
    - :class:`oauth2client.client.GoogleCredentials` to
      :class:`google.oauth2.credentials.Credentials`.
    - :class:`oauth2client.service_account.ServiceAccountCredentials` to
      :class:`google.oauth2.service_account.Credentials`.
    - :class:`oauth2client.service_account._JWTAccessCredentials` to
      :class:`google.oauth2.service_account.Credentials`.
    - :class:`oauth2client.contrib.gce.AppAssertionCredentials` to
      :class:`google.auth.compute_engine.Credentials`.
    - :class:`oauth2client.contrib.appengine.AppAssertionCredentials` to
      :class:`google.auth.app_engine.Credentials`.

    Returns:
        google.auth.credentials.Credentials: The converted credentials.

    Raises:
        ValueError: If the credentials could not be converted.
    N)type_CLASS_CONVERSION_MAPKeyError
ValueError_CONVERT_ERROR_TMPLformatsix
raise_from)r   Zcredentials_class
caught_excnew_excr   r   r   convert   s    r%   )%__doc__
__future__r   r!   Zgoogle.authr   Zgoogle.auth.app_enginer
   Zgoogle.auth.compute_engineZgoogle.oauth2.credentialsZgoogle.oauth2.service_accountZoauth2client.clientZoauth2clientZoauth2client.contrib.gceZoauth2client.service_accountImportErrorr#   r"   Zoauth2client.contrib.appengineZ_HAS_APPENGINEr   r   r   r   r   clientZOAuth2CredentialsZGoogleCredentialsr   ZServiceAccountCredentialsZ_JWTAccessCredentialscontribZgceZAppAssertionCredentialsr   Z	appenginer%   r   r   r   r   <module>   sP   "
     