o ka' @sddlmZddlmZeddlmZddlZddlZddl Z ddl Z ddl Z ddl mZmZddlZzddlZddlmZmZddlmZWney^ZzedeedZ[wwdZd Zejd ZGd d d ej j!Z"Gd ddeZ#dS))print_function)standard_library)strN)quote quote_plus) BaseIdentityServicezPHubic backend requires the pyrax library available from Rackspace. Exception: %szhttps://api.hubic.com/oauth/zhttps://api.hubic.com/1.0/z~/.hubic_tokensc@seZdZddZddZdS)BearerTokenAuthcCs ||_dSN)token)selfr r I/usr/lib/python3/dist-packages/duplicity/backends/pyrax_identity/hubic.py__init__#s zBearerTokenAuth.__init__cCsd|j|jd<|S)NzBearer Authorization)r headers)r reqr r r__call__&szBearerTokenAuth.__call__N)__name__ __module__ __qualname__rrr r r rr "s r csXeZdZfddZddZ dddZdd Zd d Zd d ZddZ ddZ Z S) HubicIdentitycstt||dSr )superrrr  __class__r rr,szHubicIdentity.__init__cCsdS)Nr rr r r_get_auth_endpoint/sz HubicIdentity._get_auth_endpointFcCs8||_||_||_||_||_||_|r|dSdS)z(Sets the username and password directly.N)_email _password _client_id tenant_id_client_secret _redirect_uri authenticate)r emailpassword client_id client_secret redirect_urir$r r rset_credentials2s zHubicIdentity.set_credentialscCsR|dd|_|dd|_|dd|_|j|_|dd|_|dd|_dS)zL Parses the credential file with Rackspace-specific labels. hubicr%r&r'r(r)N)getrrr r!r"r#)r cfgr r r_read_credential_file?s z#HubicIdentity._read_credential_filecCsDd|jvrdStj|jdj}ttj|}|d|ddS)Nlocationerrorerror_description)r0r1)rurllibparseurlsplitquerydict parse_qsl)r respr5qsr r r _parse_errorJs zHubicIdentity._parse_errorcCstjtd||jdd|j|jfd}|jdkr3z |}|j|d<Wni}Yt dt ||}t }| t|dsJ|d|d durt|dd |d ttd  }||Wdn1snwYn t dt ||d dur|dd |d ttd  }||Wdn1swYnt d |dd r|dd ttd  }||Wdn1swYtd|ddr|ddttd  }||Wdn1swYtd|S)Ntoken/authorization_code)coder) grant_typedataauthr=IUnable to get oauth access token, wrong client_id or client_secret ? (%s)r+ access_tokenwb refresh_tokenz Unable to get the refresh token.r%zEusername has been removed from the .hubic_tokens file sent to the CE.r&zEpassword has been removed from the .hubic_tokens file sent to the CE.)requestspostOAUTH_ENDPOINTr#r r" status_codejsonexcAuthenticationFailedr configparser ConfigParserread TOKENS_FILE has_section add_sectionsetopenwrite has_option remove_optionprint)r r=rerr oauth_tokenconfig configfiler r r_get_access_tokenQsj                   zHubicIdentity._get_access_tokenc Cs0t}|t|dd}|durtdd}d}d}d}d}||kr|stjt d |dd |j |j fd }|j d krx|j d kr]t d|d|d7}t||d}||kr\|}nz |} |j | d<Wni} Ytdt| d}||kr|r(|std|} | ddur| Std)Nr+rFz-refresh_token is null. Not acquiered before ?Frir;)rFr>r?rBizstatus_code 509: attempt #z failedr=rCTz]All the attempts failed to get the refresh token: status_code = 509: Bandwidth Limit ExceededrDz*Unable to get oauth access token from json)rNrOrPrQr,rLrMrGrHrIr r"rJrYtimesleeprKr) r r]rFsuccess max_retriesretries sleep_timemax_sleep_timerZr[r\r r r_refresh_access_tokens^            z#HubicIdentity._refresh_access_tokenc Cs t}|t|ddr|}ntjtd t |j t |j tjdd}|jdkr]*name=[\'"]?oauth[\'"]?\s+[^>]*value=[\'"]?(\d+)[\'"]?>rbz.Unable to get oauth_id from authorization pagezLCannot retrieve email and/or password. Please run expresslane-hubic-setup.shzauth/acceptedrZ)actionoauthloginuser_pwdaccount credentials)r@rlr/r=z7Unable to authorize client_id, invalid login/password ? token_typebearerzUnsupported access token typezaccount/credentialsrD)rATr expiresHubiC cloudfiles public_urlendpoint)nametype endpoints object_store)6rNrOrPrQrWrkrGr,rIformatrr rr#pyraxutils random_asciirJrLrMrr:lxmlrm ImportErrordocument_fromstringcontentxpathvalueresearchgrouprrrHr2r3r4rr5r6r7r_lower API_ENDPOINTr rK authenticatedr rwrservicesusernamer&) r r]r\rZ lxml_htmlrpr5r= swift_tokenr r rr$s                zHubicIdentity.authenticate)F) rrrrrr*r.r:r_rkr$ __classcell__r r rrr+s    ;9r)$ __future__rfuturerinstall_aliasesbuiltinsrrNosrrd urllib.parser2requests.compatrrrGrpyrax.base_identityrrpyrax.exceptions exceptionsrLreBackendExceptionrIrpath expanduserrQrAAuthBaser rr r r rs4