o .^V(@sdZzddlmZWneyddlmZYnwddlZddlZddlZddlZddl m Z ddl Z ddl m Z mZddlmZddlmZeZgd Zd ZGd d d ZGd ddZGddde jZGdddeZGdddeZdS)z.OAuth classes for use with lazr.restfulclient.) ConfigParser)SafeConfigParserN)oauth1)parse_qs urlencode)HttpAuthorizer)CredentialsFileError) AccessTokenConsumerOAuthAuthorizerSystemWideConsumer1c@seZdZdZdddZdS)r z An OAuth consumer (application).NcCs||_||_||_dS)anInitialize :param key: The OAuth consumer key :param secret: The OAuth consumer secret. Don't use this. It's a misfeature, and lazr.restful doesn't expect it. :param application_name: An application name, if different from the consumer key. If present, this will be used in the User-Agent header. N)keysecretapplication_name)selfrrrrD/usr/lib/python3/dist-packages/lazr/restfulclient/authorize/oauth.py__init__;s  zConsumer.__init__rN)__name__ __module__ __qualname____doc__rrrrrr 8sr c@s2eZdZdZd ddZddZeZedd ZdS) r zAn OAuth access token.rNcCs||_||_||_dSN)rrcontext)rrrrrrrrMs zAccessToken.__init__cCstd|jfd|jfgS)Noauth_token_secret oauth_token)rrrrrrr to_stringRszAccessToken.to_stringcCs.t|dd}|dd}|dd}|||S)NF)keep_blank_valuesrrr)r)clssparamsrrrrr from_stringZs    zAccessToken.from_stringr) rrrrrr __str__ classmethodr%rrrrr Js r c@seZdZdZddZeZdS) TruthyStringz&A Unicode string which is always true.cCsdS)NTrrrrr__bool__eszTruthyString.__bool__N)rrrrr) __nonzero__rrrrr(bsr(cs2eZdZdZdZdfdd ZeddZZS) r zA consumer associated with the logged-in user rather than an app. This can be used to share a single OAuth token among multiple desktop applications. The OAuth consumer key will be derived from system information (platform and hostname). zSystem-wide: %s (%s)rcstt||j||dS)a Constructor. :param application_name: An application name. This will be used in the User-Agent header. :param secret: The OAuth consumer secret. Don't use this. It's a misfeature, and lazr.restful doesn't expect it. N)superr r consumer_key)rrr __class__rrrts zSystemWideConsumer.__init__cCsNz ddl}|}Wn tyd}Ynw|dkrt}|j|tfS)zThe system-wide OAuth consumer key for this computer. This key identifies the platform and the computer's hostname. It does not identify the active user. rNr)distroname Exceptionplatformsystem KEY_FORMATsocket gethostname)rr/distnamerrrr,s  zSystemWideConsumer.consumer_key)r) rrrrr4rpropertyr, __classcell__rrr-rr ks  r c@sXeZdZdZ   dddZeddZd d Zed d Z d dZ ddZ ddZ dS)r zBA client that signs every outgoing request with OAuth credentials.NrOAuthcCs,d|_|durt||||_||_||_dSr)consumerr access_token oauth_realm)r consumer_nameconsumer_secretr<r=rrrrrs zOAuthAuthorizer.__init__cCs:i}|jdur |S|jj|d<|jjdur|jj|d<|S)zoAny information necessary to identify this user agent. In this case, the OAuth consumer name. Noauth_consumer application)r;rr)rr$rrruser_agent_paramss    z!OAuthAuthorizer.user_agent_paramscCs~t}t|d|j}|||tstdt|td}|td}t|||_|td}|td}t |||_ dS)akLoad credentials from a file-like object. This overrides the consumer and access token given in the constructor and replaces them with the values read from the file. :param readable_file: A file-like object to read the credentials from :type readable_file: Any object supporting the file-like `read()` method read_filezNo configuration for version %sr,r?r< access_secretN) rgetattrreadfp has_sectionCREDENTIALS_FILE_VERSIONrgetr r;r r<)r readable_fileparserreaderr,r?r<rDrrrloads*   zOAuthAuthorizer.loadcCs&|}t|d}||||S)aConvenience method for loading credentials from a file. Open the file, create the Credentials and load from the file, and finally close the file and return the newly created Credentials instance. :param path: In which file the credential file should be saved. :type path: string :return: The loaded Credentials instance. :rtype: `Credentials` r)openrMclose)r"path credentialscredentials_filerrrload_from_paths   zOAuthAuthorizer.load_from_pathcCs|jdur td|jdurtdt}|t|td|jj|td|jj|td|jj|td|jj| |dS)aHWrite the credentials to the file-like object. :param writable_file: A file-like object to write the credentials to :type writable_file: Any object supporting the file-like `write()` method :raise CredentialsFileError: when there is either no consumer or no access token Nz No consumerzNo access tokenr,r?r<rD) r;rr<r add_sectionrHsetrrwrite)r writable_filerKrrrsaves&   zOAuthAuthorizer.savecCsBtt|tjtjBtjBtjtjBd}| || dS)aTConvenience method for saving credentials to a file. Create the file, call self.save(), and close the file. Existing files are overwritten. The resulting file will be readable and writable only by the user. :param path: In which file the credential file should be saved. :type path: string wN) osfdopenrOO_CREATO_TRUNCO_WRONLYstatS_IREADS_IWRITErYrP)rrQrSrrr save_to_paths    zOAuthAuthorizer.save_to_pathc Cstj|jj|jjt|jjpd|jjtj|jd}t|j |_ | |\}}}| D]\}} t j r<|d}| d} | ||<q+dS)z&Sign a request with OAuth credentials.r) client_secretresource_owner_keyresource_owner_secretsignature_methodrealmzUTF-8N)rClientr;rrr(r<SIGNATURE_PLAINTEXTr=resignitemssixPY2encode) r absolute_urimethodbodyheadersclient_signed_headersrvaluerrrauthorizeRequests     z OAuthAuthorizer.authorizeRequest)NrNr:N) rrrrrr8rBrMr'rTrYrcrxrrrrr s     r )r configparserrr ImportErrorr[r2r`r5oauthlibrrmsix.moves.urllib.parserrlazr.restfulclient.authorizerlazr.restfulclient.errorsrtype __metaclass____all__rHr r text_typer(r r rrrrs.     '