o
                         @   s   d Z ddlZddlZdZdZdZeg dddgd	eg dddgd	i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ddZdS )zUtilities for reading OAuth 2.0 client secret files.

A client_secrets.json file contains all the information needed to interact with
an OAuth 2.0 protected service.
    Nz$jcgregorio@google.com (Joe Gregorio)web	installed)	client_idclient_secretredirect_urisauth_uri	token_urir   r   )requiredstringc                   @      e Zd ZdZdS )ErrorzBase error for this module.N__name__
__module____qualname____doc__ r   r   C/tmp/google-cloud-sdk/lib/third_party/oauth2client/clientsecrets.pyr   =       r   c                   @   r   )InvalidClientSecretsErrorz(Format of ClientSecrets file is invalid.Nr   r   r   r   r   r   A   r   r   c              	   C   s   d}| du r
t |z	|  \\}}W n ttfy"   t |d w |tvr.t d|t| d D ]}||vrBt d||q4t| d D ]}|| drYt d	|qI||fS )
zValidate parsed client secrets from a file.

    Args:
        clientsecrets_dict: dict, a dictionary holding the client secrets.

    Returns:
        tuple, a string of the client type and the information parsed
        from the file.
    ziInvalid file format. See https://developers.google.com/api-client-library/python/guide/aaa_client_secretsNzU Expected a JSON object with a single property for a "web" or "installed" applicationzUnknown client type: {0}.r	   z1Missing property "{0}" in a client type of "{1}".r
   z[[z!Property "{0}" is not configured.)r   items
ValueErrorAttributeErrorVALID_CLIENTformat
startswith)clientsecrets_dict_INVALID_FILE_FORMAT_MSGclient_typeclient_info	prop_namer   r   r   _validate_clientsecretsE   s<   r!   c                 C      t | }t|S N)jsonloadr!   )fpobjr   r   r   r%   n      
r%   c                 C   r"   r#   )r$   loadsr!   )sr'   r   r   r   r)   s   r(   r)   c              
   C   sv   z$t | d}t|}W d    n1 sw   Y  W t|S W t|S  ty: } z
td|j|j|jd }~ww )NrzError opening file)	openr$   r%   IOErrorr   filenamestrerrorerrnor!   )r.   r&   r'   excr   r   r   	_loadfilex   s   r2   c                 C   sX   d}|st | S |j| |d}|du r%t | \}}||i}|j| ||d tt|S )a  Loading of client_secrets JSON file, optionally backed by a cache.

    Typical cache storage would be App Engine memcache service,
    but you can pass in any other cache client that implements
    these methods:

    * ``get(key, namespace=ns)``
    * ``set(key, value, namespace=ns)``

    Usage::

        # without caching
        client_type, client_info = loadfile('secrets.json')
        # using App Engine memcache service
        from google.appengine.api import memcache
        client_type, client_info = loadfile('secrets.json', cache=memcache)

    Args:
        filename: string, Path to a client_secrets.json file on a filesystem.
        cache: An optional cache service client that implements get() and set()
        methods. If not specified, the file is always being loaded from
                 a filesystem.

    Raises:
        InvalidClientSecretsError: In case of a validation error or some
                                   I/O failure. Can happen only on cache miss.

    Returns:
        (client_type, client_info) tuple, as _loadfile() normally would.
        JSON contents is validated only during first load. Cache hits are not
        validated.
    zoauth2client:secrets#ns)	namespaceN)r2   getsetnextsix	iteritems)r.   cache_SECRET_NAMESPACEr'   r   r   r   r   r   loadfile   s   !r;   r#   )r   r$   r7   
__author__TYPE_WEBTYPE_INSTALLEDr   	Exceptionr   r   r!   r%   r)   r2   r;   r   r   r   r   <module>   s4   )
