Esempio n. 1
0
    def __init__(
        self,
        oauth_consumer_key=None,
        oauth_access_key=None,
        handle_errors=True,
        domain="api.launchpad.dev",
        protocol="http",
    ):
        """Create a LaunchpadWebServiceCaller.
        :param oauth_consumer_key: The OAuth consumer key to use.
        :param oauth_access_key: The OAuth access key to use for the request.
        :param handle_errors: Should errors raise exception or be handled by
            the publisher. Default is to let the publisher handle them.

        Other parameters are passed to the HTTPCaller used to make the calls.
        """
        if oauth_consumer_key is not None and oauth_access_key is not None:
            login(ANONYMOUS)
            consumers = getUtility(IOAuthConsumerSet)
            self.consumer = consumers.getByKey(oauth_consumer_key)
            if oauth_access_key == "":
                # The client wants to make an anonymous request.
                self.access_token = OAuthToken(oauth_access_key, "")
                if self.consumer is None:
                    # The client is trying to make an anonymous
                    # request with a previously unknown consumer. This
                    # is fine: we manually create a "fake"
                    # OAuthConsumer (it's "fake" because it's not
                    # really an IOAuthConsumer as returned by
                    # IOAuthConsumerSet.getByKey) to be used in the
                    # requests we make.
                    self.consumer = OAuthConsumer(oauth_consumer_key, "")
            else:
                if self.consumer is None:
                    # Requests using this caller will be rejected by
                    # the server, but we have a test that verifies
                    # such requests _are_ rejected, so we'll create a
                    # fake OAuthConsumer object.
                    self.consumer = OAuthConsumer(oauth_consumer_key, "")
                    self.access_token = OAuthToken(oauth_access_key, "")
                else:
                    # The client wants to make an authorized request
                    # using a recognized consumer key.
                    self.access_token = self.consumer.getAccessToken(oauth_access_key)
            logout()
        else:
            self.consumer = None
            self.access_token = None

        self.handle_errors = handle_errors
        WebServiceCaller.__init__(self, handle_errors, domain, protocol)
Esempio n. 2
0
    def __init__(self,
                 oauth_consumer_key=None,
                 oauth_access_key=None,
                 handle_errors=True,
                 domain='api.launchpad.dev',
                 protocol='http'):
        """Create a LaunchpadWebServiceCaller.
        :param oauth_consumer_key: The OAuth consumer key to use.
        :param oauth_access_key: The OAuth access key to use for the request.
        :param handle_errors: Should errors raise exception or be handled by
            the publisher. Default is to let the publisher handle them.

        Other parameters are passed to the HTTPCaller used to make the calls.
        """
        if oauth_consumer_key is not None and oauth_access_key is not None:
            login(ANONYMOUS)
            consumers = getUtility(IOAuthConsumerSet)
            self.consumer = consumers.getByKey(oauth_consumer_key)
            if oauth_access_key == '':
                # The client wants to make an anonymous request.
                self.access_token = OAuthToken(oauth_access_key, '')
                if self.consumer is None:
                    # The client is trying to make an anonymous
                    # request with a previously unknown consumer. This
                    # is fine: we manually create a "fake"
                    # OAuthConsumer (it's "fake" because it's not
                    # really an IOAuthConsumer as returned by
                    # IOAuthConsumerSet.getByKey) to be used in the
                    # requests we make.
                    self.consumer = OAuthConsumer(oauth_consumer_key, '')
            else:
                if self.consumer is None:
                    # Requests using this caller will be rejected by
                    # the server, but we have a test that verifies
                    # such requests _are_ rejected, so we'll create a
                    # fake OAuthConsumer object.
                    self.consumer = OAuthConsumer(oauth_consumer_key, '')
                    self.access_token = OAuthToken(oauth_access_key, '')
                else:
                    # The client wants to make an authorized request
                    # using a recognized consumer key.
                    self.access_token = self.consumer.getAccessToken(
                        oauth_access_key)
            logout()
        else:
            self.consumer = None
            self.access_token = None

        self.handle_errors = handle_errors
        WebServiceCaller.__init__(self, handle_errors, domain, protocol)
Esempio n. 3
0
    def _getRequestForPersonAndAccountWithDifferentIDs(self):
        """Return a LaunchpadTestRequest with the correct OAuth parameters in
        its form.
        """
        # Create a lone account followed by an account-with-person just to
        # make sure in the second one the ID of the account and the person are
        # different.
        self.factory.makeAccount('Personless account')
        person = self.factory.makePerson()
        self.assertNotEqual(person.id, person.account.id)

        # Create an access token for our new person.
        consumer = getUtility(IOAuthConsumerSet).new(u'test-consumer')
        request_token, _ = consumer.newRequestToken()
        request_token.review(person,
                             permission=OAuthPermission.READ_PUBLIC,
                             context=None)
        access_token, access_secret = request_token.createAccessToken()

        # Use oauth.OAuthRequest just to generate a dictionary containing all
        # the parameters we need to use in a valid OAuth request, using the
        # access token we just created for our new person.
        oauth_consumer = OAuthConsumer(consumer.key, u'')
        oauth_token = OAuthToken(access_token.key, access_secret)
        oauth_request = OAuthRequest.from_consumer_and_token(
            oauth_consumer, oauth_token)
        oauth_request.sign_request(OAuthSignatureMethod_PLAINTEXT(),
                                   oauth_consumer, oauth_token)
        return LaunchpadTestRequest(form=oauth_request.parameters)
Esempio n. 4
0
    def __init__(self,
                 oauth_consumer_key=None,
                 oauth_access_key=None,
                 oauth_access_secret=None,
                 handle_errors=True,
                 domain='api.launchpad.dev',
                 protocol='http'):
        """Create a LaunchpadWebServiceCaller.
        :param oauth_consumer_key: The OAuth consumer key to use.
        :param oauth_access_key: The OAuth access key to use for the request.
        :param handle_errors: Should errors raise exception or be handled by
            the publisher. Default is to let the publisher handle them.

        Other parameters are passed to the HTTPCaller used to make the calls.
        """
        if oauth_consumer_key is not None and oauth_access_key is not None:
            # XXX cjwatson 2016-01-25: Callers should be updated to pass
            # Unicode directly, but that's a big change.
            oauth_consumer_key = six.ensure_text(oauth_consumer_key)
            self.consumer = OAuthConsumer(oauth_consumer_key, u'')
            if oauth_access_secret is None:
                oauth_access_secret = SAMPLEDATA_ACCESS_SECRETS.get(
                    oauth_access_key, u'')
            self.access_token = OAuthToken(oauth_access_key,
                                           oauth_access_secret)
            # This shouldn't be here, but many old tests expect it.
            logout()
        else:
            self.consumer = None
            self.access_token = None
        self.handle_errors = handle_errors
        WebServiceCaller.__init__(self, handle_errors, domain, protocol)
Esempio n. 5
0
class LaunchpadWebServiceCaller(WebServiceCaller):
    """A class for making calls to Launchpad web services."""

    def __init__(
        self,
        oauth_consumer_key=None,
        oauth_access_key=None,
        handle_errors=True,
        domain="api.launchpad.dev",
        protocol="http",
    ):
        """Create a LaunchpadWebServiceCaller.
        :param oauth_consumer_key: The OAuth consumer key to use.
        :param oauth_access_key: The OAuth access key to use for the request.
        :param handle_errors: Should errors raise exception or be handled by
            the publisher. Default is to let the publisher handle them.

        Other parameters are passed to the HTTPCaller used to make the calls.
        """
        if oauth_consumer_key is not None and oauth_access_key is not None:
            login(ANONYMOUS)
            consumers = getUtility(IOAuthConsumerSet)
            self.consumer = consumers.getByKey(oauth_consumer_key)
            if oauth_access_key == "":
                # The client wants to make an anonymous request.
                self.access_token = OAuthToken(oauth_access_key, "")
                if self.consumer is None:
                    # The client is trying to make an anonymous
                    # request with a previously unknown consumer. This
                    # is fine: we manually create a "fake"
                    # OAuthConsumer (it's "fake" because it's not
                    # really an IOAuthConsumer as returned by
                    # IOAuthConsumerSet.getByKey) to be used in the
                    # requests we make.
                    self.consumer = OAuthConsumer(oauth_consumer_key, "")
            else:
                if self.consumer is None:
                    # Requests using this caller will be rejected by
                    # the server, but we have a test that verifies
                    # such requests _are_ rejected, so we'll create a
                    # fake OAuthConsumer object.
                    self.consumer = OAuthConsumer(oauth_consumer_key, "")
                    self.access_token = OAuthToken(oauth_access_key, "")
                else:
                    # The client wants to make an authorized request
                    # using a recognized consumer key.
                    self.access_token = self.consumer.getAccessToken(oauth_access_key)
            logout()
        else:
            self.consumer = None
            self.access_token = None

        self.handle_errors = handle_errors
        WebServiceCaller.__init__(self, handle_errors, domain, protocol)

    default_api_version = "beta"

    def addHeadersTo(self, full_url, full_headers):
        if self.consumer is not None and self.access_token is not None:
            request = OAuthRequest.from_consumer_and_token(self.consumer, self.access_token, http_url=full_url)
            request.sign_request(OAuthSignatureMethod_PLAINTEXT(), self.consumer, self.access_token)
            full_headers.update(request.to_header(OAUTH_REALM))
        if not self.handle_errors:
            full_headers["X_Zope_handle_errors"] = "False"
Esempio n. 6
0
class LaunchpadWebServiceCaller(WebServiceCaller):
    """A class for making calls to Launchpad web services."""
    def __init__(self,
                 oauth_consumer_key=None,
                 oauth_access_key=None,
                 handle_errors=True,
                 domain='api.launchpad.dev',
                 protocol='http'):
        """Create a LaunchpadWebServiceCaller.
        :param oauth_consumer_key: The OAuth consumer key to use.
        :param oauth_access_key: The OAuth access key to use for the request.
        :param handle_errors: Should errors raise exception or be handled by
            the publisher. Default is to let the publisher handle them.

        Other parameters are passed to the HTTPCaller used to make the calls.
        """
        if oauth_consumer_key is not None and oauth_access_key is not None:
            login(ANONYMOUS)
            consumers = getUtility(IOAuthConsumerSet)
            self.consumer = consumers.getByKey(oauth_consumer_key)
            if oauth_access_key == '':
                # The client wants to make an anonymous request.
                self.access_token = OAuthToken(oauth_access_key, '')
                if self.consumer is None:
                    # The client is trying to make an anonymous
                    # request with a previously unknown consumer. This
                    # is fine: we manually create a "fake"
                    # OAuthConsumer (it's "fake" because it's not
                    # really an IOAuthConsumer as returned by
                    # IOAuthConsumerSet.getByKey) to be used in the
                    # requests we make.
                    self.consumer = OAuthConsumer(oauth_consumer_key, '')
            else:
                if self.consumer is None:
                    # Requests using this caller will be rejected by
                    # the server, but we have a test that verifies
                    # such requests _are_ rejected, so we'll create a
                    # fake OAuthConsumer object.
                    self.consumer = OAuthConsumer(oauth_consumer_key, '')
                    self.access_token = OAuthToken(oauth_access_key, '')
                else:
                    # The client wants to make an authorized request
                    # using a recognized consumer key.
                    self.access_token = self.consumer.getAccessToken(
                        oauth_access_key)
            logout()
        else:
            self.consumer = None
            self.access_token = None

        self.handle_errors = handle_errors
        WebServiceCaller.__init__(self, handle_errors, domain, protocol)

    default_api_version = "beta"

    def addHeadersTo(self, full_url, full_headers):
        if self.consumer is not None and self.access_token is not None:
            request = OAuthRequest.from_consumer_and_token(self.consumer,
                                                           self.access_token,
                                                           http_url=full_url)
            request.sign_request(OAuthSignatureMethod_PLAINTEXT(),
                                 self.consumer, self.access_token)
            full_headers.update(request.to_header(OAUTH_REALM))
        if not self.handle_errors:
            full_headers['X_Zope_handle_errors'] = 'False'