ó
‚¾^Yc           @   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 d e	 f d „  ƒ  YZ
 d e
 f d	 „  ƒ  YZ d
 e
 f d „  ƒ  YZ d Z d e f d „  ƒ  YZ d „  Z d e j f d „  ƒ  YZ d S(   s;   OAuth 2.0 utitilies for Google Developer Shell environment.iÿÿÿÿN(   t   _helpers(   t   clientt   DEVSHELL_CLIENT_PORTt   Errorc           B   s   e  Z d  Z RS(   s   Errors for this module.(   t   __name__t
   __module__t   __doc__(    (    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyR      s   t   CommunicationErrorc           B   s   e  Z d  Z RS(   s9   Errors for communication with the Developer Shell server.(   R   R   R   (    (    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyR   !   s   t   NoDevshellServerc           B   s   e  Z d  Z RS(   s6   Error when no Developer Shell server can be contacted.(   R   R   R   (    (    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyR   %   s   s   []t   CredentialInfoResponsec           B   s   e  Z d  Z d „  Z RS(   sä  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  j | ƒ } t | t ƒ s7 t d t | ƒ ƒ ‚ n  t | ƒ } | d k rY | d n d |  _ | d k rx | d n d |  _	 | d k r— | d n d |  _
 | d k r¶ | d n d |  _ d S(   s4   Initialize the response data from JSON PBLite array.s   Not a list: i    i   i   i   N(   t   jsont   loadst
   isinstancet   listt
   ValueErrort   strt   lent   Nonet
   user_emailt
   project_idt   access_tokent
   expires_in(   t   selft   json_stringt   pblt   pbl_len(    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyt   __init__<   s    (   R   R   R   R   (    (    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyR	   /   s   c          C   s  t  t j t d ƒ ƒ }  |  d k r0 t ƒ  ‚ n  t j ƒ  } | j d |  f ƒ t } d j t	 | ƒ | ƒ } | j
 t j | d d ƒƒ | j d ƒ j ƒ  } d | k r¹ t d ƒ ‚ n  | j d d	 ƒ \ } } t  | ƒ t	 | ƒ } | d k r| | j | t j ƒ j ƒ  7} n  t | ƒ S(
   s3   Communicate with the Developer Shell server socket.i    t	   localhosts   {0}
{1}t   encodings   utf-8i   s   
s#   saw no newline in the first 6 bytesi   (   t   intt   ost   getenvt   DEVSHELL_ENVR   t   sockett   connectt   CREDENTIAL_INFO_REQUEST_JSONt   formatR   t   sendallR    t	   _to_bytest   recvt   decodeR   t   splitt   MSG_WAITALLR	   (   t   portt   sockt   datat   msgt   headert   len_strt   json_strt   to_read(    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyt	   _SendRecvH   s     "t   DevshellCredentialsc           B   s_   e  Z d  Z d d „ Z d „  Z e d „  ƒ Z e d „  ƒ Z e	 d „  ƒ Z
 e d „  ƒ Z RS(   sý  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.
    c         C   s9   t  t |  ƒ j d  d  d  d  d  d  | ƒ |  j d  ƒ d  S(   N(   t   superR4   R   R   t   _refresh(   R   t
   user_agent(    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyR   n   s    c         C   sh   t  ƒ  |  _ |  j j |  _ |  j j } | d k	 r[ t j d | ƒ } t j ƒ  | |  _	 n	 d |  _	 d S(   sX   Refreshes the access token.

        Args:
            http: unused HTTP object
        t   secondsN(
   R3   t   devshell_responseR   R   R   t   datetimet	   timedeltaR   t   _UTCNOWt   token_expiry(   R   t   httpR   t   delta(    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyR6   y   s    c         C   s
   |  j  j S(   N(   R9   R   (   R   (    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyR   ˆ   s    c         C   s
   |  j  j S(   N(   R9   R   (   R   (    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyR   Œ   s    c         C   s   t  d ƒ ‚ d  S(   Ns2   Cannot load Developer Shell credentials from JSON.(   t   NotImplementedError(   t   clst	   json_data(    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyt	   from_json   s    c         C   s   t  d ƒ ‚ d  S(   Ns-   Cannot serialize Developer Shell credentials.(   R@   (   R   (    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyt   serialization_data•   s    N(   R   R   R   R   R   R6   t   propertyR   R   t   classmethodRC   RD   (    (    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyR4   a   s   	(   R   R:   R
   R   R!   t   oauth2clientR    R   R    t	   ExceptionR   R   R   R#   t   objectR	   R3   t   GoogleCredentialsR4   (    (    (    sC   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/devshell.pyt   <module>   s   	