diff options
author | Ib Lundgren <ib.lundgren@gmail.com> | 2013-02-12 00:26:05 +0100 |
---|---|---|
committer | Ib Lundgren <ib.lundgren@gmail.com> | 2013-02-12 00:26:05 +0100 |
commit | 88e8b1ae28cbaeb69bc76830a2f2ba3c9562c5c5 (patch) | |
tree | f088d7debd44ebe6355ebf4f42f6c83346e28868 | |
parent | 857ad9c5ed778e00d42a5333863c10a6209bdd43 (diff) | |
download | oauthlib-88e8b1ae28cbaeb69bc76830a2f2ba3c9562c5c5.tar.gz |
Client.sign return value encoding.
-rw-r--r-- | oauthlib/oauth1/rfc5849/__init__.py | 41 | ||||
-rw-r--r-- | tests/oauth1/rfc5849/test_client.py | 10 |
2 files changed, 47 insertions, 4 deletions
diff --git a/oauthlib/oauth1/rfc5849/__init__.py b/oauthlib/oauth1/rfc5849/__init__.py index 657adf5..f5eca0a 100644 --- a/oauthlib/oauth1/rfc5849/__init__.py +++ b/oauthlib/oauth1/rfc5849/__init__.py @@ -48,8 +48,30 @@ class Client(object): signature_method=SIGNATURE_HMAC, signature_type=SIGNATURE_TYPE_AUTH_HEADER, rsa_key=None, verifier=None, realm=None, - encoding='utf-8', nonce=None, timestamp=None): - + encoding='utf-8', decoding=None, + nonce=None, timestamp=None): + """Create an OAuth 1 client. + + :param client_key: Client key (consumer key), mandatory. + :param resource_owner_key: Resource owner key (oauth token). + :param resource_owner_secret: Resource owner secret (oauth token secret). + :param callback_uri: Callback used when obtaining request token. + :param signature_method: SIGNATURE_HMAC, SIGNATURE_RSA or SIGNATURE_PLAINTEXT. + :param signature_type: SIGNATURE_TYPE_AUTH_HEADER (default), + SIGNATURE_TYPE_QUERY or SIGNATURE_TYPE_BODY + depending on where you want to embed the oauth + credentials. + :param rsa_key: RSA key used with SIGNATURE_RSA. + :param verifier: Verifier used when obtaining an access token. + :param realm: Realm (scope) to which access is being requested. + :param encoding: If you provide non-unicode input you may use this + to have oauthlib automatically convert. + :param decoding: If you wish that the returned uri, headers and body + from sign be encoded back from unicode, then set + decoding to your preferred encoding, i.e. utf-8. + :param nonce: Use this nonce instead of generating one. (Mainly for testing) + :param timestamp: Use this timestamp instead of using current. (Mainly for testing) + """ # Convert to unicode using encoding if given, else assume unicode encode = lambda x: to_unicode(x, encoding) if encoding else x @@ -64,6 +86,7 @@ class Client(object): self.verifier = encode(verifier) self.realm = encode(realm) self.encoding = encode(encoding) + self.decoding = encode(decoding) self.nonce = encode(nonce) self.timestamp = encode(timestamp) @@ -234,8 +257,18 @@ class Client(object): request.oauth_params.append(('oauth_signature', self.get_oauth_signature(request))) # render the signed request and return it - return self._render(request, formencode=True, - realm=(realm or self.realm)) + uri, headers, body = self._render(request, formencode=True, + realm=(realm or self.realm)) + + if self.decoding: + logging.debug('Encoding URI, headers and body to %s.', self.decoding) + uri = uri.encode(self.decoding) + body = body.encode(self.decoding) if body else body + new_headers = {} + for k, v in headers.items(): + new_headers[k.encode(self.decoding)] = v.encode(self.decoding) + headers = new_headers + return uri, headers, body class Server(object): diff --git a/tests/oauth1/rfc5849/test_client.py b/tests/oauth1/rfc5849/test_client.py index ff94f10..ce6518b 100644 --- a/tests/oauth1/rfc5849/test_client.py +++ b/tests/oauth1/rfc5849/test_client.py @@ -46,3 +46,13 @@ class ClientConstructorTests(TestCase): client = Client('client-key', nonce='1') params = dict(client.get_oauth_params()) self.assertEqual(params['oauth_nonce'], '1') + + def test_decoding(self): + client = Client('client_key', decoding='utf-8') + uri, headers, body = client.sign('http://a.b/path?query', body='a=b', + headers={'Content-Type': 'application/x-www-form-urlencoded'}) + self.assertIsInstance(uri, bytes_type) + self.assertIsInstance(body, bytes_type) + for k, v in headers.items(): + self.assertIsInstance(k, bytes_type) + self.assertIsInstance(v, bytes_type) |