uec@sddlZyddlZeZWnek r;eZnXddlZddlmZmZdZ dZ ej dZ dddYZ dS( iN(tdatetimet timedeltas sos-toolss,urn:ietf:params:oauth:grant-type:device_codetsostDeviceAuthorizationClasscBsbeZdZdZdZdZdZdZdZdZ dZ d d Z RS( s$ Device Authorization Class cCs;d|_d|_d|_||_||_|jdS(N(tNonet _access_tokent_access_expires_att&_DeviceAuthorizationClass__device_codetclient_identifier_urlttoken_endpointt_use_device_code_grant(tselfRR ((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyt__init__s      cCs)|jdj|jGH|jdS(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_codetformatt_verification_uri_completetpoll_for_auth_completion(R ((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR )s  cCsdjt}idd6}ts1tdnytj|jd|d|}|j|j}|j d|_ |j d|_ |j d |_ |j d |_ |j d |_Wn1tjk r}tjd j|jnXd S(sl Initialize new Device Authorization Grant attempt by requesting a new device code. s client_id={}s!application/x-www-form-urlencodeds content-typesRpython3-requests is not installed and is required for obtaining device auth token.tdatatheaderst user_codetverification_uritintervalt device_codetverification_uri_completesQHTTP request failed while attempting to acquire the tokens. Error returned was {}N(RtDEVICE_AUTH_CLIENT_IDtREQUESTS_LOADEDt ExceptiontrequeststpostRtraise_for_statustjsontgett _user_codet_verification_urit _intervalRRt HTTPErrort status_code(R RRtrestresponsete((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR 6s*     cCs;itd6td6|jd6}ts3tdnx|jd kr6tj|j yt j |j d|}|j }|dkrtjd|j|jn|dkrt||jn|dkr|jd dkrt||jnWq6t jjk r2}tjd j|q6Xq6Wd S(s Continuously poll OIDC token endpoint until the user is successfully authenticated or an error occurs. t grant_typet client_idRsRpython3-requests is not installed and is required for obtaining device auth token.Ris$The SSO authentication is successfuliterrortauthorization_pendingt slow_downs+Error was found while posting a request: {}N(ii(R+R,(tGRANT_TYPE_DEVICE_CODERRRRRRttimetsleepR"RRR R$tloggertinfot_set_token_dataRttextt exceptionstRequestExceptionR*R(R t token_datatcheck_auth_completionR$R'((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRSs.           cCs|jd|_tjtd|jd|_|jd|_|jd|_|jdkrytj|_ ntjtd|j|_ dS(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_tokentsecondst expires_int refresh_tokentrefresh_expires_iniN( RRRtutcnowRRt_refresh_tokent_refresh_expires_intmaxt_refresh_expires_at(R R6((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR2ts  cCsE|jr|jS|jr0|j|jS|j|jSdS(st Get the valid access_token at any given time. :return: Access_token :rtype: string N(tis_access_token_validRtis_refresh_token_validt_use_refresh_token_grantR (R ((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pytget_access_tokens    cCs2|jo1|jo1|jtddtjkS(s Check the validity of access_token. We are considering it invalid 180 sec. prior to it's exact expiry time. :return: True/False R9i(RRRRR=(R ((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRBscCs2|jo1|jo1|jtddtjkS(s Check the validity of refresh_token. We are considering it invalid 180 sec. prior to it's exact expiry time. :return: True/False R9i(R>RARRR=(R ((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRCscCststdnitd6dd6|s5|jn|d6}tj|jd|}|jdkr||j|j n}|jdkrd|j d krt j d j |j|j d |j n%td 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 sRpython3-requests is not installed and is required for obtaining device auth token.R)R;R(RiitinvalidR*skProblem while fetching the new tokens from refresh token grant - {} {}. New Device code will be requested !ssSomething went wrong while using the Refresh token grant for fetching tokens:Returned status code {0} and error {1}N(RRRR>RRR R$R2RR0twarningRR (R R;trefresh_token_datatrefresh_token_res((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRDs&    N( t__name__t __module__t__doc__R R R RR2RERBRCRRD(((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRs  !   ((tloggingRtTrueRt ImportErrortFalseR.RRRR-t getLoggerR0R(((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyt s