a
    !f                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZ dZG dd de	Z
G dd	 d	e
ZG d
d de
ZdZG dd deZdd ZG dd dejZdS )z;OAuth 2.0 utitilies for Google Developer Shell environment.    N)_helpers)clientZDEVSHELL_CLIENT_PORTc                   @   s   e Zd ZdZdS )ErrorzErrors for this module.N__name__
__module____qualname____doc__ r
   r
   ^/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/contrib/devshell.pyr      s   r   c                   @   s   e Zd ZdZdS )CommunicationErrorz9Errors for communication with the Developer Shell server.Nr   r
   r
   r
   r   r   !   s   r   c                   @   s   e Zd ZdZdS )NoDevshellServerz6Error when no Developer Shell server can be contacted.Nr   r
   r
   r
   r   r   %   s   r   z[]c                   @   s   e Zd ZdZdd ZdS )CredentialInfoResponsea  Credential information response from Developer Shell server.

    The credential information response from Developer Shell socket is a
    PBLite-formatted JSON array with fields encoded by their index in the
    array:

    * Index 0 - user email
    * Index 1 - default project ID. None if the project context is not known.
    * Index 2 - OAuth2 access token. None if there is no valid auth context.
    * Index 3 - Seconds until the access token expires. None if not present.
    c                 C   s   t |}t|ts$tdt| t|}|dkr<|d nd| _|dkrR|d nd| _|dkrh|d nd| _	|dkr~|d nd| _
dS )z4Initialize the response data from JSON PBLite array.zNot a list: r   N         )jsonloads
isinstancelist
ValueErrorstrlen
user_email
project_idaccess_token
expires_in)selfZjson_stringZpblZpbl_lenr
   r
   r   __init__<   s    

zCredentialInfoResponse.__init__N)r   r   r   r	   r   r
   r
   r
   r   r   /   s   r   c                  C   s   t ttd} | dkrt t }|d| f t}dt	||}|
tj|dd |d }d|vrztd|dd	\}}t |t	| }|dkr|||tj 7 }t|S )
z3Communicate with the Developer Shell server socket.r   	localhostz{0}
{1}zutf-8)encoding   
z#saw no newline in the first 6 bytesr   )intosgetenvDEVSHELL_ENVr   socketconnectCREDENTIAL_INFO_REQUEST_JSONformatr   sendallr   	_to_bytesrecvdecoder   splitMSG_WAITALLr   )portsockdatamsgheaderlen_strZjson_strZto_readr
   r
   r   	_SendRecvH   s     r7   c                       sZ   e Zd ZdZd fdd	Zdd Zedd Zed	d
 Ze	dd Z
edd Z  ZS )DevshellCredentialsa  Credentials object for Google Developer Shell environment.

    This object will allow a Google Developer Shell session to identify its
    user to Google and other OAuth 2.0 servers that can verify assertions. It
    can be used for the purpose of accessing data stored under the user
    account.

    This credential does not require a flow to instantiate because it
    represents a two legged flow, and therefore has all of the required
    information to generate and refresh its own access tokens.
    Nc              	      s*   t t| d d d d d d | | d  d S N)superr8   r   _refresh)r   
user_agent	__class__r
   r   r   n   s    
zDevshellCredentials.__init__c                 C   sH   t  | _| jj| _| jj}|dur>tj|d}t | | _nd| _dS )zXRefreshes the access token.

        Args:
            http: unused HTTP object
        N)seconds)	r7   devshell_responser   r   datetime	timedeltar   Z_UTCNOWZtoken_expiry)r   httpr   deltar
   r
   r   r;   y   s    
zDevshellCredentials._refreshc                 C   s   | j jS r9   )r@   r   r   r
   r
   r   r      s    zDevshellCredentials.user_emailc                 C   s   | j jS r9   )r@   r   rE   r
   r
   r   r      s    zDevshellCredentials.project_idc                 C   s   t dd S )Nz2Cannot load Developer Shell credentials from JSON.NotImplementedError)clsZ	json_datar
   r
   r   	from_json   s    zDevshellCredentials.from_jsonc                 C   s   t dd S )Nz-Cannot serialize Developer Shell credentials.rF   rE   r
   r
   r   serialization_data   s    z&DevshellCredentials.serialization_data)N)r   r   r   r	   r   r;   propertyr   r   classmethodrI   rJ   __classcell__r
   r
   r=   r   r8   a   s   


r8   )r	   rA   r   r$   r'   Zoauth2clientr   r   r&   	Exceptionr   r   r   r)   objectr   r7   ZGoogleCredentialsr8   r
   r
   r
   r   <module>   s   