Exemplo n.º 1
0
 def test_non_string_is_invalid(self):
     self.assertFalse(is_valid_callback_url(5))
     self.assertFalse(is_valid_callback_url(None))
     self.assertFalse(is_valid_callback_url(False))
     self.assertFalse(is_valid_callback_url({}))
     self.assertFalse(is_valid_callback_url([]))
     self.assertFalse(is_valid_callback_url(()))
Exemplo n.º 2
0
    def build_temporary_credentials_request(self,
                                            method="POST",
                                            payload_params=None,
                                            headers=None,
                                            realm=None,
                                            oauth_signature_method=SIGNATURE_METHOD_HMAC_SHA1,
                                            oauth_callback="oob",
                                            **extra_oauth_params):
        """
        Builds an OAuth request for temporary credentials.

        :param method:
            HTTP request method. Default POST.
        :param payload_params:
            A dictionary of payload parameters. These will be serialized
            into the URL or the entity-body depending on the HTTP request method.
            These must not include any parameters starting with ``oauth_``. Any
            of these parameters with names starting with the ``oauth_`` prefix
            will be ignored.
        :param headers:
            A dictionary of headers that will be passed along with the request.
            Must not include the "Authorization" header.
        :param realm:
            The value to use for the realm parameter in the Authorization HTTP
            header. It will be excluded from the base string, however.
        :param oauth_signature_method:
            One of:
            1. :attr:`pyoauth.oauth1.SIGNATURE_METHOD_HMAC_SHA1`
            2. :attr:`pyoauth.oauth1.SIGNATURE_METHOD_RSA_SHA1`
            3. :attr:`pyoauth.oauth1.SIGNATURE_METHOD_PLAINTEXT`
        :param oauth_callback:
            A callback URL that you want the server to call when done
            with your requests.
        :param extra_oauth_params:
            Any additional oauth parameters you would like to include.
            The parameter names must begin with ``oauth_``. Any other parameters
            with names that do not begin with this prefix will be ignored.
        :returns:
            An instance of :class:`pyoauth.http.RequestProxy`.
        """
        if not is_valid_callback_url(oauth_callback):
            raise ValueError("`oauth_callback` parameter value is invalid: `%r`" % (oauth_callback, ))

        return self._build_request(method=method,
                                   url=self._temporary_credentials_request_uri,
                                   payload_params=payload_params,
                                   headers=headers,
                                   realm=realm,
                                   oauth_signature_method=oauth_signature_method,
                                   oauth_callback=oauth_callback,
                                   **extra_oauth_params)
Exemplo n.º 3
0
 def test_url_must_be_absolute(self):
     self.assertTrue(is_valid_callback_url(b("http://example.com/")))
     self.assertFalse(is_valid_callback_url(b("mailto:[email protected]")))
     self.assertFalse(is_valid_callback_url(b("hxp://example.com/")))
     self.assertFalse(is_valid_callback_url(b("http://")))
Exemplo n.º 4
0
 def test_oob_case_sensitive_is_valid(self):
     self.assertTrue(is_valid_callback_url(b("oob")))
     self.assertFalse(is_valid_callback_url(b("OOb")))
Exemplo n.º 5
0
    def fetch_temporary_credentials(self,
                                    method=HTTP_POST, params=None,
                                    body=None, headers=None,
                                    realm=None,
                                    async_callback=None,
                                    oauth_signature_method=\
                                        SIGNATURE_METHOD_HMAC_SHA1,
                                    oauth_callback=OAUTH_VALUE_CALLBACK_OOB,
                                    **kwargs):
        """
        Fetches temporary credentials.

        :param method:
            HTTP method.
        :param params:
            Additional query/payload parameters.
            If a `body` argument to this function is specified,
            the parameters are appended to the URL query string.
            If a `body` is not specified and a method other than GET is used
            the parameters will be added to the entity body.
        :param body:
            Entity body string.
        :param headers:
            Request headers dictionary.
        :param async_callback:
            If the HTTP client used is asynchronous, then this parameter
            will be used as a callback function with the response as its
            argument.
        :param realm:
            Authorization realm.
        :param oauth_signature_method:
            Signature method.
        :param oauth_callback:
            OAuth callback URL; default case-sensitive "oob" (out-of-band).
        :param kwargs:
            Additional parameters including those that may begin with
            ``oauth_``.
        :returns:
            HTTP response (:class:`pyoauth.http.ResponseAdapter`) if
            ``async_callback`` is not specified;
            otherwise, ``async_callback`` is called with the response as its
            argument.
        """
        if not is_valid_callback_url(oauth_callback):
            raise ValueError(
                "`%r` parameter value is invalid URL: %r" % \
                (OAUTH_PARAM_CALLBACK, oauth_callback)
            )

        if async_callback:
            def _async_callback(response):
                return async_callback(
                    *self.parse_temporary_credentials_response(response,
                                                               self._strict)
                )
        else:
            _async_callback = async_callback

        resp = self._fetch(method, self._temporary_credentials_uri, params,
                           body, headers,
                           async_callback=_async_callback,
                           realm=realm,
                           oauth_signature_method=oauth_signature_method,
                           oauth_callback=oauth_callback,
                           **kwargs)
        return self.parse_temporary_credentials_response(resp, self._strict)
Exemplo n.º 6
0
 def test_url_must_be_absolute(self):
     assert_true(is_valid_callback_url("http://example.com/"))
     assert_false(is_valid_callback_url("mailto:[email protected]"))
     assert_false(is_valid_callback_url("hxp://example.com/"))
     assert_false(is_valid_callback_url("http://"))
Exemplo n.º 7
0
 def test_oob_case_sensitive_is_valid(self):
     assert_true(is_valid_callback_url("oob"))
     assert_false(is_valid_callback_url("OOb"))