o ¢kaì'ã @súddlmZddlmZe ¡ddlmZddlZddlZddl Z ddl Z ddl Z ddl mZmZddlZzddlZddlmZmZddlmZWney^Zzedeeƒƒ‚dZ[wwdZd Zej d ¡ZGd d „d ej j!ƒZ"Gd d„deƒZ#dS)é)Úprint_function)Ústandard_library)ÚstrN)ÚquoteÚ quote_plus)Ú BaseIdentityÚServicezPHubic backend requires the pyrax library available from Rackspace. Exception: %szhttps://api.hubic.com/oauth/zhttps://api.hubic.com/1.0/z~/.hubic_tokensc@seZdZdd„Zdd„ZdS)ÚBearerTokenAuthcCs ||_dS©N)Ú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 csXeZdZ‡fdd„Zdd„Z ddd„Zdd „Zd d „Zd d „Zdd„Z dd„Z ‡Z S)Ú HubicIdentitycstt|ƒ |¡dSr )Úsuperrr©r ©Ú __class__r rr,szHubicIdentity.__init__cCsdS)NÚr 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 ÚemailÚpasswordÚ client_idÚ client_secretÚ redirect_urir$r r rÚset_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 |jd¡j}ttj |¡ƒ}|d|ddœS)NÚlocationÚerrorÚerror_description)r0r1)rÚurllibÚparseÚurlsplitÚqueryÚdictÚ parse_qsl)r Úrespr5Úqsr r rÚ _parse_errorJs zHubicIdentity._parse_errorcCstjtd||jddœ|j|jfd}|jdkr3z | ¡}|j|d<Wni}Yt  dt |ƒ¡‚| ¡}t   ¡}|  t¡| d¡sJ| d¡|d durt| dd |d ¡ttd ƒ }| |¡Wdƒn1snwYn t  dt |ƒ¡‚|d dur§| dd |d ¡ttd ƒ }| |¡Wdƒn1s¡wYnt  d ¡‚| dd ¡rÖ| dd ¡ttd ƒ }| |¡Wdƒn1sÍwYtdƒ| dd¡r| dd¡ttd ƒ }| |¡Wdƒn1søwYtdƒ|S)Nútoken/Úauthorization_code)Úcoder)Ú grant_type©ÚdataÚauthéÈr=úIUnable to get oauth access token, wrong client_id or client_secret ? (%s)r+Ú access_tokenÚwbÚ 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.)ÚrequestsÚpostÚOAUTH_ENDPOINTr#r r"Ú status_codeÚjsonÚexcÚAuthenticationFailedrÚ configparserÚ ConfigParserÚreadÚ TOKENS_FILEÚ has_sectionÚ add_sectionÚsetÚopenÚwriteÚ has_optionÚ remove_optionÚprint)r r=ÚrÚerrÚ oauth_tokenÚconfigÚ configfiler r rÚ_get_access_tokenQsjý ù þ      ÿ€ÿÿ   ÿ€     ÿ   ÿzHubicIdentity._get_access_tokenc Cs0t ¡}| t¡| dd¡}|durt d¡‚d}d}d}d}d}||kr€|s€tjt d |dd œ|j |j fd }|j d krx|j d kr]t d|dƒ|d7}t |¡|d}||kr\|}nz | ¡} |j | d<Wni} Yt dt| ƒ¡‚d}||kr€|r(|s‡t d¡‚| ¡} | ddur“| St d¡‚)Nr+rFz-refresh_token is null. Not acquiered before ?Féréir;)rFr>r?rBiýzstatus_code 509: attempt #z failedéér=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"rJrYÚtimeÚsleeprKr) r r]rFÚsuccessÚ max_retriesÚretriesÚ sleep_timeÚmax_sleep_timerZr[r\r r rÚ_refresh_access_tokenŒs^    þ ú    €ÿÿ äÿ  z#HubicIdentity._refresh_access_tokenc Cs t ¡}| t¡| dd¡r| ¡}n®tjtd  t |j ƒt |j ƒtj ¡¡dd}|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)ÚactionÚoauthÚloginÚuser_pwdÚaccountÚ credentials)r@rlr/r=z7Unable to authorize client_id, invalid login/password ?Ú token_typeÚbearerzUnsupported access token typezaccount/credentialsrD)rATr ÚexpiresÚHubiCÚ cloudfilesÚ public_urlÚendpoint)ÚnameÚtypeÚ endpointsÚ object_store)6rNrOrPrQrWrkrGr,rIÚformatrr rr#ÚpyraxÚutilsÚ random_asciirJrLrMrr:ÚlxmlrmÚ ImportErrorÚdocument_fromstringÚcontentÚxpathÚvalueÚreÚsearchÚgrouprrrHr2r3r4rr5r6r7r_ÚlowerÚ API_ENDPOINTr rKÚ authenticatedr rwrÚservicesÚusernamer&) 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__rÚfuturerÚinstall_aliasesÚbuiltinsrrNÚosrŠrdÚ urllib.parser2Úrequests.compatrrrGrÚpyrax.base_identityrrÚpyrax.exceptionsÚ exceptionsrLr…ÚeÚBackendExceptionrIrŽÚpathÚ expanduserrQrAÚAuthBaser rr r r rÚs4   þ€ÿ