Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/launchpadlib/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyc


mOc@sdZeZdddddddgZdd	lZdd
lmZdd	lZdd	lZddl	m	Z	dd	l
Z
ddlmZdd	l
Z
dd
lmZddlmZdd	lZddlmZmZdd	lZddlmZddlmZmZmZmZddlm Z dZ!dZ"dZ#dZ$e%e&e'fZ(defdYZ)defdYZdefdYZ*de+fdYZ,de,fdYZ-de,fdYZ.de+fdYZ/de/fd YZ0d!e1fd"YZ2d#e2fd$YZ3d%e2fd&YZ4d'e2fd(YZ5d)e2fd*YZ6d+e2fd,YZ7d-e2fd.YZ8d	S(/s4launchpadlib credentials and authentication support.tAccessTokentAnonymousAccessTokent AuthorizeRequestTokenWithBrowsertCredentialStoretRequestTokenAuthorizationEnginetConsumertCredentialsiN(tStringIO(tselect(tstdin(t	urlencode(turljoin(t	b64decodet	b64encode(t	HTTPError(RRtOAuthAuthorizertSystemWideConsumer(turiss+request-tokens
+access-tokens+authorize-tokenicBsheZdZd	ZdZdZdZdZdZ	e
dZd	ej
edZej
dZRS(
sStandard credentials storage and usage class.

    :ivar consumer: The consumer (application)
    :type consumer: `Consumer`
    :ivar access_token: Access information on behalf of the user
    :type access_token: `AccessToken`
    turitdicts<BR>s
cCs&t}|j||j}|S(seTurn this object into a string.

        This should probably be moved into OAuthAuthorizer.
        (Rtsavetgetvalue(tselftsiot
serialized((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyt	serializeQs	
cCs |}|jt||S(s}Create a `Credentials` object from a serialized string.

        This should probably be moved into OAuthAuthorizer.
        (tloadR(tclstvaluetcredentials((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pytfrom_string[s	c		Cs|jdk	std|jdks6tdtj|}td|jjdddd}|t}i|d6}||j	krd	|d
<nt
jj|ddd
|dt
|\}}|jdkrt||n||j	kr:tj|}|dk	r$||d<ntj||_|Stj||_d|t|jjf}|dk	r||j_|d|7}n|SdS(sRequest an OAuth token to Launchpad.

        Also store the token in self._request_token.

        This method must not be called on an object with no consumer
        specified or if an access token has already been obtained.

        :param context: The context of this token, that is, its scope of
            validity within Launchpad.
        :param web_root: The URL of the website on which the token
            should be requested.
        :token_format: How the token should be
            presented. URI_TOKEN_FORMAT means just return the URL to
            the page that authorizes the token.  DICT_TOKEN_FORMAT
            means return a dictionary describing the token
            and the site's authentication policy.

        :return: If token_format is URI_TOKEN_FORMAT, the URL for the
            user to authorize the `AccessToken` provided by
            Launchpad. If token_format is DICT_TOKEN_FORMAT, a dict of
            information about the new access token.
        sConsumer not specified.sAccess token already obtained.toauth_consumer_keytoauth_signature_methodt	PLAINTEXTtoauth_signaturet&tReferersapplication/jsontAccepttmethodtPOSTtheaderstbodyis
lp.contexts%s%s?oauth_token=%ss&lp.context=%sN(tconsumertNonetAssertionErrortaccess_tokenRtlookup_web_rootRtkeytrequest_token_pagetDICT_TOKEN_FORMATthttplib2tHttptrequestR
tstatusRt
simplejsontloadsRtfrom_paramst_request_tokenRtauthorize_token_pagetcontext(	RR;tweb_rootttoken_formattparamsturlR(tresponsetcontent((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pytget_request_tokenes8	


'
	c
Cs|jdk	stdtj|}td|jjddd|jjdd|jj}|t	}i|d6}t
jj|d	d
d|dt
|\}}|jd
krt||ntj||_dS(sdExchange the previously obtained request token for an access token.

        This method must not be called unless get_request_token() has been
        called and completed successfully.

        The access token will be stored as self.access_token.

        :param web_root: The base URL of the website that granted the
            request token.
        s5get_request_token() doesn't seem to have been called.RR R!toauth_tokenR"s&%sR$R&R'R(R)iN(R9R+R,RR.RR*R/tsecrettaccess_token_pageR2R3R4R
R5RRRR-(RR<R>R?R(R@RA((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyt'exchange_request_token_for_access_tokens	

'N(t__name__t
__module__t__doc__R+R9tURI_TOKEN_FORMATR1tITEM_SEPARATORtNEWLINERtclassmethodRRtSTAGING_WEB_ROOTRBRF(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRBs	

	6cBs,eZdZedZedZRS(sAn OAuth access token.cCs3|d}|d}|jd}||||S(s:Create and return a new `AccessToken` from the given dict.RCtoauth_token_secrets
lp.context(tget(RR>R/RDR;((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR8s

cCstj|dt}|d}t|dks=td|d}|d}t|dksotd|d}|jd}|d
k	rt|dkstd	|d}n||||S(s<Create and return a new `AccessToken` from the given string.tkeep_blank_valuesRCis/Query string must have exactly one oauth_token.iROs*Query string must have exactly one secret.s
lp.contexts*Query string must have exactly one contextN(tcgitparse_qstFalsetlenR,RPR+(Rtquery_stringR>R/RDR;((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRs
	


	
(RGRHRIRMR8R(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRscBseZdZdZRS(soAn OAuth access token that doesn't authenticate anybody.

    This token can be used for anonymous access.
    cCstt|jdddS(Nt(tsuperRt__init__(R((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRYs(RGRHRIRY(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRscBs>eZdZddZdZdZdZdZRS(sStore OAuth credentials locally.

    This is a generic superclass. To implement a specific way of
    storing credentials locally you'll need to subclass this class,
    and implement `do_save` and `do_load`.
    cCs
||_dS(sConstructor.

        :param credential_save_failed: A callback to be invoked if the
            save to local storage fails. You should never invoke this
            callback yourself! Instead, you should raise an exception
            from do_save().
        N(tcredential_save_failed(RRZ((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRYscCscy|j||WnHtk
r*n5tk
r^}|jdkrQ|n|jnX|S(sSave the credentials and invoke the callback on failure.

        Do not override this method when subclassing. Override
        do_save() instead.
        N(tdo_savetEXPLOSIVE_ERRORSt	ExceptionRZR+(RRtunique_consumer_idte((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRs
	cCs
tdS(sStore newly-authorized credentials locally for later use.

        :param credentials: A Credentials object to save.
        :param unique_consumer_id: A string uniquely identifying an
            OAuth consumer on a Launchpad instance.
        N(tNotImplementedError(RRR^((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR[scCs
|j|S(s0Retrieve credentials from a local store.

        This method is the inverse of `save`.

        There's no special behavior in this method--it just calls
        `do_load`. There _is_ special behavior in `save`, and this
        way, developers can remember to implement `do_save` and
        `do_load`, not `do_save` and `load`.

        :param unique_key: A string uniquely identifying an OAuth consumer
            on a Launchpad instance.

        :return: A `Credentials` object if one is found in the local
            store, and None otherise.
        (tdo_load(Rt
unique_key((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR
scCs
tdS(s@Retrieve credentials from a local store.

        This method is the inverse of `do_save`.

        :param unique_key: A string uniquely identifying an OAuth consumer
            on a Launchpad instance.

        :return: A `Credentials` object if one is found in the local
            store, and None otherise.
        N(R`(RRb((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRasN(	RGRHRIR+RYRR[RRa(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRs
				tKeyringCredentialStorecBs5eZdZdZedZdZdZRS(sStore credentials in the GNOME keyring or KDE wallet.

    This is a good solution for desktop applications and interactive
    scripts. It doesn't work for non-interactive scripts, or for
    integrating third-party websites into Launchpad.
    s<B64>cCs"dtkrddlandS(sGEnsure the keyring module is imported (postponing side effects).

        The keyring module initializes the environment-dependent backend at
        import time (nasty).  We want to avoid that initialization because it
        may do things like prompt the user to unlock their password store
        (e.g., KWallet).
        tkeyringiN(tglobalsRd(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyt_ensure_keyring_imported4s	cCs@|j|j}|jt|}tjd||dS(s2Store newly-authorized credentials in the keyring.tlaunchpadlibN(RfRt	B64MARKERR
Rdtset_password(RRRbR((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR[As

cCs|jtjd|}|dk	r|jd}|j|jr~yt|t|j}Wq~t	k
rzdSXnyt
j|}|SWqdSXndS(s&Retrieve credentials from the keyring.Rgtutf8N(RfRdtget_passwordR+tencodet
startswithRhRRUt	TypeErrorRR(RRbtcredential_stringR((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRaLs"

(RGRHRIRhtstaticmethodRfR[Ra(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRc*s

	tUnencryptedFileCredentialStorecBs,eZdZddZdZdZRS(sStore credentials unencrypted in a file on disk.

    This is a good solution for scripts that need to run without any
    user interaction.
    cCs#tt|j|||_dS(N(RXRqRYtfilename(RRrRZ((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRYmscCs|j|jdS(sSave the credentials to disk.N(tsave_to_pathRr(RRRb((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR[rscCsItjj|jrEtj|jtjdkrEtj|jSdS(sLoad the credentials from disk.iN(	tostpathtexistsRrtstattST_SIZERtload_from_pathR+(RRb((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRavs N(RGRHRIR+RYR[Ra(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRqfs	cBsYeZdZdZddddZedZdZdZ	dZ
dZRS(	s/The superclass of all request token authorizers.

    This base class does not implement request token authorization,
    since that varies depending on how you want the end-user to
    authorize a request token. You'll need to subclass this class and
    implement `make_end_user_authorize_token`.
    tUNAUTHORIZEDcCstj||_tj||_|dkrK|dkrKtdn|dk	r||dk	r|td||fn|dkrdg}t|}nt|}|}||_	||_
|pg|_dS(sDBase class initialization.

        :param service_root: The root of the Launchpad instance being
            used.

        :param application_name: The name of the application that
            wants to use launchpadlib. This is used in conjunction
            with a desktop-wide integration.

            If you specify this argument, your values for
            consumer_name and allow_access_levels are ignored.

        :param consumer_name: The OAuth consumer name, for an
            application that wants its own point of integration into
            Launchpad. In almost all cases, you want to specify
            application_name instead and do a desktop-wide
            integration. The exception is when you're integrating a
            third-party website into Launchpad.

        :param allow_access_levels: A list of the Launchpad access
            levels to present to the user. ('READ_PUBLIC' and so on.)
            Your value for this argument will be ignored during a
            desktop-wide integration.
        :type allow_access_levels: A list of strings.
        s:You must provide either application_name or consumer_name.sZYou must provide only one of application_name and consumer_name. (You provided %r and %r.)tDESKTOP_INTEGRATIONN(Rtlookup_service_roottservice_roottweb_root_for_service_rootR<R+t
ValueErrorRRR*tapplication_nametallow_access_levels(RR}Rt
consumer_nameRR*((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRYs"			cCs|jjd|jS(s7Return a string identifying this consumer on this host.t@(R*R/R}(R((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR^scCsXdt|f}d}t|jdkrH|||j|j7}nt|j|S(sReturn the authorization URL for a request token.

        This is the URL the end-user must visit to authorize the
        token. How exactly does this happen? That depends on the
        subclass implementation.
        s%s?oauth_token=%ss&allow_permission=i(R:RURtjoinRR<(Rt
request_tokentpagetallow_permission((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pytauthorization_urlscCsI|j|}|j|||jdkr2dS|j||j|S(sdAuthorize a token and associate it with the given credentials.

        If the credential store runs into a problem storing the
        credential locally, the `credential_save_failed` callback will
        be invoked. The callback will not be invoked if there's a
        problem authorizing the credentials.

        :param credentials: A `Credentials` object. If the end-user
            authorizes these credentials, this object will have its
            .access_token property set.

        :param credential_store: A `CredentialStore` object. If the
            end-user authorizes the credentials, they will be
            persisted locally using this object.

        :return: If the credentials are successfully authorized, the
            return value is the `Credentials` object originally passed
            in. Otherwise the return value is None.
        N(RBtmake_end_user_authorize_tokenR-R+RR^(RRtcredential_storetrequest_token_string((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyt__call__scCs&|jd|jdtj}|dS(s\Get a new request token from the server.

        :param return: The request token.
        R<R=RC(RBR<RR1(RRtauthorization_json((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRBs		cCs
tdS(s5Authorize the given request token using the given credentials.

        Your subclass must implement this method: it has no default
        implementation.

        Because an access token may expire or be revoked in the middle
        of a session, this method may be called at arbitrary points in
        a launchpadlib session, or even multiple times during a single
        session (with a different request token each time).

        In most cases, however, this method will be called at the
        beginning of a launchpadlib session, or not at all.
        N(R`(RRR((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRsN(RGRHRItUNAUTHORIZED_ACCESS_LEVELR+RYtpropertyR^RRRBR(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR~s8			
cBsJeZdZdZdZdZdZddddZdZ	dZ
RS(	sThe simplest (and, right now, the only) request token authorizer.

    This authorizer simply opens up the end-user's web browser to a
    Launchpad URL and lets the end-user authorize the request token
    themselves.
    sThe authorization page:
 (%s)
should be opening in your browser. Use your browser to authorize
this program to access Launchpad on your behalf.s1Press any key to continue or wait (%d) seconds...is5Waiting to hear from Launchpad about your decision...cCs#tt|j||d|dS(soConstructor.

        :param service_root: See `RequestTokenAuthorizationEngine`.
        :param application_name: See `RequestTokenAuthorizationEngine`.
        :param consumer_name: The value of this argument is
            ignored. If we have the capability to open the end-user's
            web browser, we must be running on the end-user's computer,
            so we should do a full desktop integration.
        :param credential_save_failed: See `RequestTokenAuthorizationEngine`.
        :param allow_access_levels: The value of this argument is
            ignored, for the same reason as consumer_name.
        N(RXRRYR+(RR}RRRZR((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRYs	cCs	|GHdS(sDisplay a message.

        By default, prints the message to standard output. The message
        does not require any user interaction--it's solely
        informative.
        N((Rtmessage((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pytoutput1scCs#|j|}|j|j||j|j|jttggg|j\}}}|rqtjn|j|jt	j
|x|jdkrt
jty|j|jPWqtk
r}|jjdkrt|jq|jjdkr
qdGH|GHqXqWdS(s7Have the end-user authorize the token in their browser.iis#Unexpected response from Launchpad:N(RRtWAITING_FOR_USERtTIMEOUT_MESSAGEtTIMEOUTRR	treadlinetWAITING_FOR_LAUNCHPADt
webbrowsertopenR-R+ttimetsleeptaccess_token_poll_timeRFR<RR@R5tEndUserDeclinedAuthorizationRA(RRRRtrlistt_R_((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR:s*$



N(RGRHRIRRRRR+RYRR(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRs		tTokenAuthorizationExceptioncBseZRS((RGRH(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR[stRequestTokenAlreadyAuthorizedcBseZRS((RGRH(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyR_sRcBseZRS((RGRH(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRcstClientErrorcBseZRS((RGRH(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRgstServerErrorcBseZRS((RGRH(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRkstNoLaunchpadAccountcBseZRS((RGRH(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRostTooManyAuthenticationFailurescBseZRS((RGRH(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyRss(9RIttypet
__metaclass__t__all__RRt	cStringIORR2RtRRwtsysR	RturllibR
turlparseRRtbase64RR
R6tlazr.restfulclient.errorsRt"lazr.restfulclient.authorize.oauthRt_AccessTokenRRRRgRR0RER:RtMemoryErrortKeyboardInterruptt
SystemExitR\RRtobjectRRcRqRRR]RRRRRRR(((s</usr/lib/python2.7/dist-packages/launchpadlib/credentials.pyt<module>sX	"v	K<L