o
    x                     @   sR   d Z ddlZddlZddlZdd Zdd Zddd	Zd
d Zdd Zdd Z	dS )z-Helper functions for commonly used utilities.    Nc                 C   s"   |  d}|dkr| |d S dS )az  Identify and extract PEM keys.

    Determines whether the given key is in the format of PEM key, and extracts
    the relevant part of the key if it is.

    Args:
        raw_key_input: The contents of a private key file (either PEM or
                       PKCS12).

    Returns:
        string, The actual key if the contents are from a PEM file, or
        else None.
    s   -----BEGIN N)find)raw_key_inputoffset r   >/tmp/google-cloud-sdk/lib/third_party/oauth2client/_helpers.py_parse_pem_key   s   
r   c                 C   s   t j| ddS )N),:)
separators)jsondumps)datar   r   r   _json_encode)   s   r   asciic                 C   s8   t | tjr| |n| }t |tjr|S td| )a4  Converts a string value to bytes, if necessary.

    Unfortunately, ``six.b`` is insufficient for this task since in
    Python2 it does not modify ``unicode`` objects.

    Args:
        value: The string/bytes value to be converted.
        encoding: The encoding to use to convert unicode to bytes. Defaults
                  to "ascii", which will not allow any characters from ordinals
                  larger than 127. Other useful values are "latin-1", which
                  which will only allows byte ordinals (up to 255) and "utf-8",
                  which will encode any unicode that needs to be.

    Returns:
        The original value converted to bytes (if unicode) or as passed in
        if it started out as bytes.

    Raises:
        ValueError if the value could not be converted to bytes.
    z%{0!r} could not be converted to bytes)
isinstancesix	text_typeencodebinary_type
ValueErrorformat)valueencodingresultr   r   r   	_to_bytes-   s   
r   c                 C   s8   t | tjr| dn| }t |tjr|S td| )aE  Converts bytes to a string value, if necessary.

    Args:
        value: The string/bytes value to be converted.

    Returns:
        The original value converted to unicode (if bytes) or as passed in
        if it started out as unicode.

    Raises:
        ValueError if the value could not be converted to unicode.
    utf-8z'{0!r} could not be converted to unicode)r   r   r   decoder   r   r   )r   r   r   r   r   _from_bytesJ   s   
r   c                 C   s   t | dd} t| dS )Nr   )r      =)r   base64urlsafe_b64encoderstrip)	raw_bytesr   r   r   _urlsafe_b64encode`   s   r$   c                 C   s*   t | } | ddt| d    }t|S )Nr      )r   lenr    urlsafe_b64decode)	b64stringpaddedr   r   r   _urlsafe_b64decodee   s   
r*   )r   )
__doc__r    r   r   r   r   r   r   r$   r*   r   r   r   r   <module>   s   
