o
    €Ï·  ã                   @   sº   d Z ddlmZ ddlmZ ddlm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 d
ZG dd„ dejƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )z$Manages logic for external accounts.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Ú
exceptions©Úcreds)Ú
introspect)ÚfilesÚexternal_accountc                   @   ó   e Zd ZdZdS )ÚErrorzErrors raised by this module.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úI/tmp/google-cloud-sdk/lib/googlecloudsdk/api_lib/auth/external_account.pyr      ó    r   c                   @   r   )ÚBadCredentialFileExceptionz Raised when file cannot be read.Nr   r   r   r   r   r   #   r   r   c                   @   r   )ÚBadCredentialJsonFileExceptionz2Raised when the JSON file is in an invalid format.Nr   r   r   r   r   r   '   r   r   c              
   C   sR   t  | ¡}zt |¡}W n ty  } ztd | |¡ƒ‚d}~ww t|ƒr'|S dS )a  Returns the JSON content if the file corresponds to an external account.

  This function is useful when the content of a file need to be inspected first
  before determining how to handle it. More specifically, it would check a
  config file contains an external account cred and return its content which can
  then be used with CredentialsFromAdcDictGoogleAuth (if the contents
  correspond to an external account cred) to avoid having to open the file
  twice.

  Args:
    filename (str): The filepath to the ADC file representing an external
      account credentials.

  Returns:
    Optional(Mapping): The JSON content if the configuration represents an
      external account. Otherwise None is returned.

  Raises:
    BadCredentialFileException: If JSON parsing of the file fails.
  z!Could not read json file {0}: {1}N)r	   ÚReadFileContentsÚjsonÚloadsÚ
ValueErrorr   ÚformatÚIsExternalAccountConfig)ÚfilenameÚcontentÚcontent_jsonÚer   r   r   Ú#GetExternalAccountCredentialsConfig+   s   
ÿ€þr!   c                 C   s   | pi   d¡tkS )zGReturns whether a JSON content corresponds to an external account cred.Útype)ÚgetÚ_EXTERNAL_ACCOUNT_TYPE)r   r   r   r   r   N   s   r   c                 C   s,   d| vs
| d t krtdƒ‚t t | ¡¡S )a   Creates external account creds from a dict of application default creds.

  Args:
    external_config (Mapping): The configuration dictionary representing the
      credentials. This is loaded from the ADC file typically.

  Returns:
    google.auth.external_account.Credentials: The initialized external account
      credentials.

  Raises:
    BadCredentialJsonFileException: If the config format is invalid.
    googlecloudsdk.core.credentials.creds.InvalidCredentialsError: If the
      provided configuration is invalid or unsupported.
  r"   z@The provided credentials configuration is not in a valid format.)r$   r   Úc_credsÚFromJsonGoogleAuthr   Údumps)Úexternal_configr   r   r   Ú CredentialsFromAdcDictGoogleAuthS   s   ÿr)   c                 C   s8   t  | ¡st  | ¡st  | ¡rt| ddƒpt | ¡S dS )aF  Returns the account identifier corresponding to the external account creds.

  Args:
    creds (google.auth.credentials.Credentials): The credentials whose account
      ID is to be returned.

  Returns:
    Optional(str): The corresponding account ID, or None if the credentials are
      not external_account credentials.
  Úservice_account_emailN)r%   ÚIsExternalAccountCredentialsÚ IsExternalAccountUserCredentialsÚ*IsExternalAccountAuthorizedUserCredentialsÚgetattrÚc_introspectÚGetExternalAccountIdr   r   r   r   r0   k   s   
ÿþÿr0   )r   Ú
__future__r   r   r   r   Úgooglecloudsdk.corer   Úgooglecloudsdk.core.credentialsr   r%   r   r/   Úgooglecloudsdk.core.utilr	   r$   r   r   r   r!   r   r)   r0   r   r   r   r   Ú<module>   s"   #