diff options
author | Michael Terry <michael.terry@canonical.com> | 2012-08-31 17:52:09 -0400 |
---|---|---|
committer | Michael Terry <michael.terry@canonical.com> | 2012-08-31 17:52:09 -0400 |
commit | 95c24638a73e895fb42449b876266b8029b752bb (patch) | |
tree | 5c3d3caefe5aa037dc1670f5f7116407b8b371e3 /oauthlib/oauth1/rfc5849/signature.py | |
parent | dcbc028f2af02cb1a9c54faedf0defca9841c048 (diff) | |
download | oauthlib-95c24638a73e895fb42449b876266b8029b752bb.tar.gz |
Support Python 3
These are mostly unicode string related changes and a few syntax ones.
https://github.com/idan/oauthlib/issues/55
Diffstat (limited to 'oauthlib/oauth1/rfc5849/signature.py')
-rw-r--r-- | oauthlib/oauth1/rfc5849/signature.py | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/oauthlib/oauth1/rfc5849/signature.py b/oauthlib/oauth1/rfc5849/signature.py index dbd43aa..860e61d 100644 --- a/oauthlib/oauth1/rfc5849/signature.py +++ b/oauthlib/oauth1/rfc5849/signature.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import absolute_import +from __future__ import absolute_import, unicode_literals """ oauthlib.oauth1.rfc5849.signature ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -25,9 +25,12 @@ Steps for signing a request: import binascii import hashlib import hmac -import urlparse +try: + import urlparse +except ImportError: + import urllib.parse as urlparse from . import utils -from oauthlib.common import extract_params, safe_string_equals +from oauthlib.common import bytes_type, extract_params, safe_string_equals, unicode_type def construct_base_string(http_method, base_string_uri, @@ -73,7 +76,7 @@ def construct_base_string(http_method, base_string_uri, base_string = utils.escape(http_method.upper()) # 2. An "&" character (ASCII code 38). - base_string += u'&' + base_string += '&' # 3. The base string URI from `Section 3.4.1.2`_, after being encoded # (`Section 3.6`_). @@ -83,7 +86,7 @@ def construct_base_string(http_method, base_string_uri, base_string += utils.escape(base_string_uri) # 4. An "&" character (ASCII code 38). - base_string += u'&' + base_string += '&' # 5. The request parameters as normalized in `Section 3.4.1.3.2`_, after # being encoded (`Section 3.6`). @@ -115,7 +118,7 @@ def normalize_base_string_uri(uri): .. _`section 3.4.1.2`: http://tools.ietf.org/html/rfc5849#section-3.4.1.2 """ - if not isinstance(uri, unicode): + if not isinstance(uri, unicode_type): raise ValueError('uri must be a unicode object.') # FIXME: urlparse does not support unicode @@ -144,15 +147,15 @@ def normalize_base_string_uri(uri): # .. _`RFC2616`: http://tools.ietf.org/html/rfc2616 # .. _`RFC2818`: http://tools.ietf.org/html/rfc2818 default_ports = ( - (u'http', u'80'), - (u'https', u'443'), + ('http', '80'), + ('https', '443'), ) - if u':' in netloc: - host, port = netloc.split(u':', 1) + if ':' in netloc: + host, port = netloc.split(':', 1) if (scheme, port) in default_ports: netloc = host - return urlparse.urlunparse((scheme, netloc, path, u'', u'', u'')) + return urlparse.urlunparse((scheme, netloc, path, '', '', '')) # ** Request Parameters ** @@ -167,7 +170,7 @@ def normalize_base_string_uri(uri): # # .. _`section 3.4.1.3`: http://tools.ietf.org/html/rfc5849#section-3.4.1.3 -def collect_parameters(uri_query='', body=[], headers=None, +def collect_parameters(uri_query=b'', body=[], headers=None, exclude_oauth_signature=True): """**Parameter Sources** @@ -252,10 +255,10 @@ def collect_parameters(uri_query='', body=[], headers=None, # .. _`Section 3.5.1`: http://tools.ietf.org/html/rfc5849#section-3.5.1 if headers: headers_lower = dict((k.lower(), v) for k, v in headers.items()) - authorization_header = headers_lower.get(u'authorization') + authorization_header = headers_lower.get('authorization') if authorization_header is not None: params.extend([i for i in utils.parse_authorization_header( - authorization_header) if i[0] != u'realm']) + authorization_header) if i[0] != 'realm']) # * The HTTP request entity-body, but only if all of the following # conditions are met: @@ -277,15 +280,15 @@ def collect_parameters(uri_query='', body=[], headers=None, # ensure all oauth params are unescaped unescaped_params = [] for k, v in params: - if k.startswith(u'oauth_'): + if k.startswith('oauth_'): v = utils.unescape(v) unescaped_params.append((k, v)) # The "oauth_signature" parameter MUST be excluded from the signature # base string if present. if exclude_oauth_signature: - unescaped_params = filter(lambda i: i[0] != u'oauth_signature', - unescaped_params) + unescaped_params = list(filter(lambda i: i[0] != 'oauth_signature', + unescaped_params)) return unescaped_params @@ -380,12 +383,12 @@ def normalize_parameters(params): # 3. The name of each parameter is concatenated to its corresponding # value using an "=" character (ASCII code 61) as a separator, even # if the value is empty. - parameter_parts = [u'{0}={1}'.format(k, v) for k, v in key_values] + parameter_parts = ['{0}={1}'.format(k, v) for k, v in key_values] # 4. The sorted name/value pairs are concatenated together into a # single string by using an "&" character (ASCII code 38) as # separator. - return u'&'.join(parameter_parts) + return '&'.join(parameter_parts) def sign_hmac_sha1(base_string, client_secret, resource_owner_secret): @@ -414,16 +417,16 @@ def sign_hmac_sha1(base_string, client_secret, resource_owner_secret): # 1. The client shared-secret, after being encoded (`Section 3.6`_). # # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6 - key = utils.escape(client_secret or u'') + key = utils.escape(client_secret or '') # 2. An "&" character (ASCII code 38), which MUST be included # even when either secret is empty. - key += u'&' + key += '&' # 3. The token shared-secret, after being encoded (`Section 3.6`_). # # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6 - key += utils.escape(resource_owner_secret or u'') + key += utils.escape(resource_owner_secret or '') # FIXME: HMAC does not support unicode! key_utf8 = key.encode('utf-8') @@ -461,6 +464,8 @@ def sign_rsa_sha1(base_string, rsa_private_key): from Crypto.Signature import PKCS1_v1_5 from Crypto.Hash import SHA key = RSA.importKey(rsa_private_key) + if isinstance(base_string, unicode_type): + base_string = base_string.encode('utf-8') h = SHA.new(base_string) p = PKCS1_v1_5.new(key) return binascii.b2a_base64(p.sign(h))[:-1].decode('utf-8') @@ -487,16 +492,16 @@ def sign_plaintext(client_secret, resource_owner_secret): # 1. The client shared-secret, after being encoded (`Section 3.6`_). # # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6 - signature = utils.escape(client_secret or u'') + signature = utils.escape(client_secret or '') # 2. An "&" character (ASCII code 38), which MUST be included even # when either secret is empty. - signature += u'&' + signature += '&' # 3. The token shared-secret, after being encoded (`Section 3.6`_). # # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6 - signature += utils.escape(resource_owner_secret or u'') + signature += utils.escape(resource_owner_secret or '') return signature @@ -534,9 +539,9 @@ def verify_rsa_sha1(request, rsa_public_key): norm_params = normalize_parameters(request.params) uri = normalize_base_string_uri(request.uri) message = construct_base_string(request.http_method, uri, norm_params) - h = SHA.new(message) + h = SHA.new(message.encode('utf-8')) p = PKCS1_v1_5.new(key) - sig = binascii.a2b_base64(request.signature) + sig = binascii.a2b_base64(request.signature.encode('utf-8')) return p.verify(h, sig) |