??????????????????????
???  ?????????????????
 JFIF      ?? C      


!"$"$?? C    
?? p 
" ??     
         ??             ?   
   ????

(%	aA*?XYD?(J??E  RE,P XYae?)(E  2 B  R  	BQ    X?)X     ?  @  

adadasdasdasasdasdas


.....................................................................................................................................??????????????????????
???  
 JFIF      ?? C      


!"$"$?? C    
?? p 
" ??     
         ??             ?   
   ????

(%	aA*?XYD?(J??E  RE,P XYae?)(E  2 B  R  	BQ    X?)X     ?  @  

adadasdasdasasdasdas


.....................................................................................................................................ó
trjc           @   s   d  d l  Z  y d  d l Z e Z Wn e k
 r; e Z n Xd  d l Z d  d l m Z m Z d Z	 d Z
 e  j d ƒ Z d d d „  ƒ  YZ d S(	   iÿÿÿÿN(   t   datetimet	   timedeltas	   sos-toolss,   urn:ietf:params:oauth:grant-type:device_codet   sost   DeviceAuthorizationClassc           B   sb   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d
 d	 „ Z RS(   s$   
    Device Authorization Class
    c         C   s;   d  |  _ d  |  _ d  |  _ | |  _ | |  _ |  j ƒ  d  S(   N(   t   Nonet   _access_tokent   _access_expires_att&   _DeviceAuthorizationClass__device_codet   client_identifier_urlt   token_endpointt   _use_device_code_grant(   t   selfR   R	   (    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyt   __init__   s    					c         C   s)   |  j  ƒ  d j |  j ƒ GH|  j ƒ  d S(   su   
        Start the device auth flow. In the future we will
        store the tokens in an in-memory keyring.
        s>   Please visit the following URL to authenticate this device: {}N(   t   _request_device_codet   formatt   _verification_uri_completet   poll_for_auth_completion(   R   (    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR
   )   s    
c         C   sû   d j  t ƒ } i d d 6} t s1 t d ƒ ‚ n  y’ t j |  j d | d | ƒ} | j ƒ  | j ƒ  } | j	 d ƒ |  _
 | j	 d ƒ |  _ | j	 d	 ƒ |  _ | j	 d
 ƒ |  _ | j	 d ƒ |  _ Wn1 t j k
 rö } t j d j  | j ƒ ƒ ‚ n Xd S(   sl   
        Initialize new Device Authorization Grant attempt by
        requesting a new device code.
        s   client_id={}s!   application/x-www-form-urlencodeds   content-typesR   python3-requests is not installed and is required for obtaining device auth token.t   datat   headerst	   user_codet   verification_urit   intervalt   device_codet   verification_uri_completesQ   HTTP request failed while attempting to acquire the tokens. Error returned was {}N(   R   t   DEVICE_AUTH_CLIENT_IDt   REQUESTS_LOADEDt	   Exceptiont   requestst   postR   t   raise_for_statust   jsont   gett
   _user_codet   _verification_urit	   _intervalR   R   t	   HTTPErrort   status_code(   R   R   R   t   rest   responset   e(    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR   6   s*    		
c         C   s;  i t  d 6t d 6|  j d 6} t s3 t d ƒ ‚ n  x|  j d k r6t j |  j	 ƒ y¬ t
 j |  j d | ƒ} | j } | d k r¨ t j d ƒ |  j | j ƒ  ƒ n  | d k rÉ t | | j ƒ ‚ n  | d k r | j ƒ  d	 d k r t | | j ƒ ‚ n  Wq6 t
 j j k
 r2} t j d j | ƒ ƒ q6 Xq6 Wd S(   s€   
        Continuously poll OIDC token endpoint until the user is successfully
        authenticated or an error occurs.
        t
   grant_typet	   client_idR   sR   python3-requests is not installed and is required for obtaining device auth token.R   iÈ   s$   The SSO authentication is successfuli  t   errort   authorization_pendingt	   slow_downs+   Error was found while posting a request: {}N(   iÈ   i  (   R+   R,   (   t   GRANT_TYPE_DEVICE_CODER   R   R   R   R   R   t   timet   sleepR"   R   R   R	   R$   t   loggert   infot   _set_token_dataR   t   textt
   exceptionst   RequestExceptionR*   R   (   R   t
   token_datat   check_auth_completionR$   R'   (    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR   S   s.    
			c         C   sœ   | j  d ƒ |  _ t j ƒ  t d | j  d ƒ ƒ |  _ | j  d ƒ |  _ | j  d ƒ |  _ |  j d k ry t j |  _	 n t j ƒ  t d |  j ƒ |  _	 d S(   s@  
        Set the class attributes as per the input token_data received.
        In the future we will persist the token data in a local,
        in-memory keyring, to avoid visting the browser frequently.
        :param token_data: Token data containing access_token, refresh_token
        and their expiry etc.
        t   access_tokent   secondst
   expires_int   refresh_tokent   refresh_expires_ini    N(
   R   R   R    t   utcnowR   R   t   _refresh_tokent   _refresh_expires_int   maxt   _refresh_expires_at(   R   R6   (    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR2   t   s    		c         C   sE   |  j  ƒ  r |  j S|  j ƒ  r0 |  j ƒ  |  j S|  j ƒ  |  j Sd S(   st   
        Get the valid access_token at any given time.
        :return: Access_token
        :rtype: string
        N(   t   is_access_token_validR   t   is_refresh_token_validt   _use_refresh_token_grantR
   (   R   (    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyt   get_access_token‡   s    

c         C   s2   |  j  o1 |  j o1 |  j t d d ƒ t j ƒ  k S(   s¡   
        Check the validity of access_token. We are considering it invalid 180
        sec. prior to it's exact expiry time.
        :return: True/False
        R9   i´   (   R   R   R   R    R=   (   R   (    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRB   —   s    c         C   s2   |  j  o1 |  j o1 |  j t d d ƒ t j ƒ  k S(   s¢   
        Check the validity of refresh_token. We are considering it invalid
        180 sec. prior to it's exact expiry time.
        :return: True/False
        R9   i´   (   R>   RA   R   R    R=   (   R   (    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRC   ¡   s    c         C   sý   t  s t d ƒ ‚ n  i t d 6d d 6| s5 |  j n | d 6} t j |  j d | ƒ} | j d k r| |  j | j	 ƒ  ƒ n} | j d k rÔ d | j	 ƒ  d	 k rÔ t
 j d
 j | j | j	 ƒ  d	 ƒ ƒ |  j ƒ  n% t d j | j | j	 ƒ  d	 ƒ ƒ ‚ d S(   s¶   
        Fetch the new access_token and refresh_token using the existing
        refresh_token and persist it.
        :param refresh_token: optional param for refresh_token
        sR   python3-requests is not installed and is required for obtaining device auth token.R)   R;   R(   R   iÈ   i  t   invalidR*   sk   Problem while fetching the new tokens from refresh token grant - {} {}. New Device code will be requested !ss   Something went wrong while using the Refresh token grant for fetching tokens:Returned status code {0} and error {1}N(   R   R   R   R>   R   R   R	   R$   R2   R   R0   t   warningR   R
   (   R   R;   t   refresh_token_datat   refresh_token_res(    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRD   «   s&    
	N(   t   __name__t
   __module__t   __doc__R   R
   R   R   R2   RE   RB   RC   R   RD   (    (    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR      s   	
			!			
	
(    (   t   loggingR   t   TrueR   t   ImportErrort   FalseR.   R    R   R   R-   t	   getLoggerR0   R   (    (    (    s>   /usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyt   <module>   s   

