
    ~ g,!                         S SK r  S SKrSrS SKrS SKJrJrJr  S SKJ	r	  Sr
Sr\ R                  " S5      r " S	 S
5      rg! \ a    Sr N?f = f)    NTF)datetime	timedeltatimezone)TIMEOUT_DEFAULTz	sos-toolsz,urn:ietf:params:oauth:grant-type:device_codesosc                   R    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSS jrSrg)DeviceAuthorizationClass   z
Device Authorization Class
c                 f    S U l         S U l        S U l        Xl        X l        U R                  5         g N)_access_token_access_expires_at&_DeviceAuthorizationClass__device_codeclient_identifier_urltoken_endpoint_use_device_code_grant)selfr   r   s      </usr/lib/python3/dist-packages/sos/policies/auth/__init__.py__init__!DeviceAuthorizationClass.__init__!   s2    !"&!%:",##%    c                 t    U R                  5         [        SU R                   35        U R                  5         g)z^
Start the device auth flow. In the future we will
store the tokens in an in-memory keyring.

z<Please visit the following URL to authenticate this device: N)_request_device_codeprint_verification_uri_completepoll_for_auth_completionr   s    r   r   /DeviceAuthorizationClass._use_device_code_grant+   s<     	!!#778:	
 	%%'r   c                 6   S[          3nSS0n[        (       d  [        S5      e [        R                  " U R
                  UU[        S9nUR                  5         UR                  5       nUR                  S5      U l
        UR                  S5      U l        UR                  S5      U l        UR                  S	5      U l        UR                  S
5      U l        g! [        R                   a+  n[        R                  " SWR                    SU 35      eSnAff = f)zU
Initialize new Device Authorization Grant attempt by
requesting a new device code.

z
client_id=zcontent-typez!application/x-www-form-urlencodedRpython3-requests is not installed and is required for obtaining device auth token.)dataheaderstimeout	user_codeverification_uriintervaldevice_codeverification_uri_completezNHTTP request failed while attempting to acquire the tokens.Error returned was  N)DEVICE_AUTH_CLIENT_IDREQUESTS_LOADED	Exceptionrequestspostr   r   raise_for_statusjsonget
_user_code_verification_uri	_intervalr   r   	HTTPErrorstatus_code)r   r!   r"   resresponsees         r   r   -DeviceAuthorizationClass._request_device_code9   s    123!#FG @ A A	---**'	)C
   "xxzH&ll;7DO%-\\2D%ED"%\\*5DN!)m!<D.6ll+/-D+!! 	-$$ &;;>??:K1()s&, - -	-s   B3C D-&DDc                    [         [        U R                  S.n[        (       d  [	        S5      eU R
                  c  [        R                  " U R                  5         [        R                  " U R                  U[        S9nUR                  nUS:X  a4  [        R                  S5        U R!                  UR#                  5       5        US;  a  [	        X2R$                  5      eUS:X  a,  UR#                  5       S	   S
;  a  [	        X2R$                  5      eU R
                  c  M  gg! [        R&                  R(                   a"  n[        R+                  SU 35         SnANLSnAff = f)zi
Continuously poll OIDC token endpoint until the user is successfully
authenticated or an error occurs.

)
grant_type	client_idr'   r    Nr!   r#      z$The SSO authentication is successful)r?     r@   error)authorization_pending	slow_downz)Error was found while posting a request: )GRANT_TYPE_DEVICE_CODEr*   r   r+   r,   r   timesleepr4   r-   r.   r   r   r6   loggerinfo_set_token_datar0   text
exceptionsRequestExceptionrA   )r   
token_datacheck_auth_completionr6   r9   s        r   r   1DeviceAuthorizationClass.poll_for_auth_completionX   s@    %;#8%)%7%79
  @ A A  (JJt~~&N(0d6I6I;E>M)O% 4??#%KK FG(()>)C)C)EFj0#K1K1KLL#%)..09>?#K1K1KLL#   ($ &&77 NHLMMNs   B7D% %E%E  E%c                    UR                  S5      U l        [        R                  " [        R
                  5      [        UR                  S5      S9-   U l        UR                  S5      U l        UR                  S5      U l	        U R                  S:X  a  [        R                  U l        g[        R                  " [        R
                  5      [        U R                  S9-   U l        g)a  
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.
access_token
expires_insecondsrefresh_tokenrefresh_expires_inr   N)r1   r   r   nowr   utcr   r   _refresh_token_refresh_expires_inmax_refresh_expires_at)r   rM   s     r   rI   (DeviceAuthorizationClass._set_token_dataz   s     (^^N;"*,,x||"<jnn\:;#<(nn_=#->>2F#G ##q('/||D$'/||HLL'A$":":;(<D$r   c                     U R                  5       (       a  U R                  $ U R                  5       (       a  U R                  5         U R                  $ U R	                  5         U R                  $ )zT
Get the valid access_token at any given time.
:return: Access_token
:rtype: string
)is_access_token_validr   is_refresh_token_valid_use_refresh_token_grantr   r   s    r   get_access_token)DeviceAuthorizationClass.get_access_token   s^     %%''%%%&&(())+%%%##%!!!r   c                     U R                   =(       aN    U R                  =(       a;    U R                  [        SS9-
  [        R                  " [
        R                  5      :  $ )z
Check the validity of access_token. We are considering it invalid 180
sec. prior to it's exact expiry time.
:return: True/False

   rS   )r   r   r   r   rW   r   rX   r   s    r   r_   .DeviceAuthorizationClass.is_access_token_valid   sL     !! 'd&=&= '##i&<<LL&'	'r   c                     U R                   =(       aN    U R                  =(       a;    U R                  [        SS9-
  [        R                  " [
        R                  5      :  $ )z
Check the validity of refresh_token. We are considering it invalid
180 sec. prior to it's exact expiry time.

:return: True/False

re   rS   )rY   r\   r   r   rW   r   rX   r   s    r   r`   /DeviceAuthorizationClass.is_refresh_token_valid   sL     "" 't'?'? '$$y'==LL&'	'r   Nc                 F   [         (       d  [        S5      e[        SU(       d  U R                  OUS.n[        R
                  " U R                  U[        S9nUR                  S:X  a   U R                  UR                  5       5        gUR                  S:X  a_  SUR                  5       S   ;   aH  [        R                  S	UR                   S
UR                  5       S    S35        U R                  5         g[        SUR                   SUR                  5       S    35      e)z
Fetch the new access_token and refresh_token using the existing
refresh_token and persist it.
:param refresh_token: optional param for refresh_token

r    rU   )r=   r<   rU   r>   r?   r@   invalidrA   zAProblem while fetching the new tokens from refresh token grant - r)   z%. New Device code will be requested !zcSomething went wrong while using the Refresh token grant for fetching tokens: Returned status code z and error N)r+   r,   r*   rY   r-   r.   r   r   r6   rI   r0   rG   warningr   )r   rU   refresh_token_datarefresh_token_ress       r   ra   1DeviceAuthorizationClass._use_refresh_token_grant   s8     @ A A+@,;+ 04/B/B1>@
 %MM$*=*=/A2AC ((C/  !2!7!7!9:**c1i!&&(172NN -->-J-J,K1/446w?@ ABB C ''))):)F)F(G/446w?@BC Cr   )__device_coder   r   r4   r\   rZ   rY   r2   r3   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r   r   rI   rb   r_   r`   ra   __static_attributes__ r   r   r	   r	      s7    &(-> ND<&"	'
'"Cr   r	   )loggingr-   r+   ImportErrorrE   r   r   r   sos.utilitiesr   r*   rD   	getLoggerrG   r	   rv   r   r   <module>r{      se    O  2 2 )# G 			5	!xC xC  Os   A AA