�
u��ec@s�ddlZyddlZeZWnek
r;eZnXddlZddlmZmZdZ dZ
ejd�Zddd��YZ
dS( i����N(tdatetimet timedeltas sos-toolss,urn:ietf:params:oauth:grant-type:device_codetsostDeviceAuthorizationClasscBsbeZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z
d
d �ZRS(s$
Device Authorization Class
cCs;d|_d|_d|_||_||_|j�dS(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)|j�dj|j�GH|j�dS(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
cCs�djt�}idd6}ts1td��ny�tj|jd|d|�}|j�|j�}|j d�|_
|j d�|_|j d �|_|j d
�|_
|j d�|_Wn1tjk
r�}tjdj|j���nXd
S(sl
Initialize new Device Authorization Grant attempt by
requesting a new device code.
sclient_id={}s!application/x-www-form-urlencodedscontent-typesRpython3-requests is not installed and is required for obtaining device auth token.tdatatheaderst user_codetverification_uritintervaltdevice_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 HTTPErrortstatus_code(RRRtrestresponsete((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR
6s*
cCs;itd6td6|jd6}ts3td��nx|jd
kr6tj|j �y�t
j|jd|�}|j
}|dkr�tjd�|j|j��n|dkr�t||j��n|dkr|j�d dkrt||j��nWq6t
jjk
r2}tjdj|��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.Ri�s$The SSO authentication is successfuli�terrortauthorization_pendingt slow_downs+Error was found while posting a request: {}N(i�i�(R+R,(tGRANT_TYPE_DEVICE_CODERRRRRRttimetsleepR"RRR R$tloggertinfot_set_token_dataRttextt
exceptionstRequestExceptionR*R(Rt
token_datatcheck_auth_completionR$R'((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRSs.
cCs�|jd�|_tj�td|jd��|_|jd�|_|jd�|_|jdkrytj|_ ntj�td|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.
taccess_tokentsecondst
expires_int
refresh_tokentrefresh_expires_iniN(
RRRtutcnowRRt_refresh_tokent_refresh_expires_intmaxt_refresh_expires_at(RR6((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyR2ts cCsE|j�r|jS|j�r0|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_token�s
cCs2|jo1|jo1|jtdd�tj�kS(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__.pyRB�scCs2|jo1|jo1|jtdd�tj�kS(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__.pyRC�scCs�tstd��nitd6dd6|s5|jn|d6}tj|jd|�}|jdkr||j|j ��n}|jdkr�d|j �d kr�t
jd
j|j|j �d ��|j
�n%tdj|j|j �d ���dS(
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(Ri�i�tinvalidR*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
(RR;trefresh_token_datatrefresh_token_res((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRD�s&
N(
t__name__t
__module__t__doc__RR
R
RR2RERBRCRRD(((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyRs
!
((tloggingRtTrueRtImportErrortFalseR.RRRR-t getLoggerR0R(((s>/usr/lib/python2.7/site-packages/sos/policies/auth/__init__.pyt<module>s
|