Permalink
Browse files

normalizing params_and_data var, setting oauth_params as property

  • Loading branch information...
maxcountryman committed Mar 22, 2012
1 parent 08e8926 commit 3c427366b5466c56fb4c72fd0167e4e16fc89485
Showing with 24 additions and 22 deletions.
  1. +1 −1 tests/base.py
  2. +7 −6 tests/test_oauth.py
  3. +1 −1 webauth/__init__.py
  4. +9 −8 webauth/hook.py
  5. +6 −6 webauth/oauth.py
View
@@ -20,7 +20,7 @@ def setUp(self):
request.headers = {}
request.params = {}
request.data = {}
- request.data_and_params = {}
+ request.params_and_data = {}
self.request = request
# mock consumer object
View
@@ -16,8 +16,9 @@ class OAuthTestHmacSha1Case(WebauthTestCase):
def test_hamcsha1_signature(self):
self.request.params = {'foo': 'bar'}
HmacSha1Signature().sign(self.request, self.consumer, self.token)
- oauth_signature = self.request.data_and_params['oauth_signature']
+ oauth_signature = self.request.params_and_data['oauth_signature']
self.assertTrue(oauth_signature is not None)
+ self.assertTrue(isinstance(oauth_signature, str))
def test_normalize_request_parameters_params(self):
# params as a dict
@@ -27,30 +28,30 @@ def test_normalize_request_parameters_params(self):
self.assertEqual('foo=bar', normalized)
# params as a dict with URL encodable chars
- self.request.data_and_params = {}
+ self.request.params_and_data = {}
self.request.params = {'foo+bar': 'baz'}
normalized = \
HmacSha1Signature()._normalize_request_parameters(self.request)
self.assertEqual('foo%2Bbar=baz', normalized)
self.assertTrue('+' not in normalized)
# params as a string
- self.request.data_and_params = {}
+ self.request.params_and_data = {}
self.request.params = urlencode({'foo': 'bar'})
normalized = \
HmacSha1Signature()._normalize_request_parameters(self.request)
self.assertEqual('foo=bar', normalized)
# params as a string with URL encodable chars
- self.request.data_and_params = {}
+ self.request.params_and_data = {}
self.request.params = urlencode({'foo+bar': 'baz'})
normalized = \
HmacSha1Signature()._normalize_request_parameters(self.request)
self.assertEqual('foo%2Bbar=baz', normalized)
self.assertTrue('+' not in normalized)
# params and dict as dicts
- self.request.data_and_params = {}
+ self.request.params_and_data = {}
self.request.params = {'a': 'b'}
self.request.data = {'foo': 'bar'}
normalized = \
@@ -65,7 +66,7 @@ def test_normalize_request_parameters_data(self):
self.assertEqual('foo=bar', normalized)
# data as a dict with URL encodable chars
- self.request.data_and_params = {}
+ self.request.params_and_data = {}
self.request.data = {'foo+bar': 'baz'}
normalized = \
HmacSha1Signature()._normalize_request_parameters(self.request)
View
@@ -6,4 +6,4 @@
'''
-__version__ = '0.2.1'
+__version__ = '0.2.2'
View
@@ -88,7 +88,7 @@ def __call__(self, request):
request.data = dict(request.data)
# generate the necessary request params
- request.oauth_params = self.generate_oauth_params()
+ request.oauth_params = self.oauth_params
# here we append an oauth_callback parameter if any
if 'oauth_callback' in request.data:
@@ -99,7 +99,7 @@ def __call__(self, request):
request.params.pop('oauth_callback')
# this is used in the Normalize Request Parameters step
- request.data_and_params = request.oauth_params.copy()
+ request.params_and_data = request.oauth_params.copy()
# sign and add the signature to the request params
self.signature.sign(request, self.consumer, self.token)
@@ -109,26 +109,27 @@ def __call__(self, request):
#
# TODO: implement the realm parameter
request.headers['Authorization'] = \
- self.generate_authorization_header(request.data_and_params)
+ self.auth_header(request.params_and_data)
elif request.method == 'POST':
# HACK: override the param encoding process
#
# BUG: body can't be recalculated in a pre-request hook; this is a
# known issue: https://github.com/kennethreitz/requests/issues/445
request.data, request._enc_data = \
- request._encode_params(request.data_and_params)
+ request._encode_params(request.params_and_data)
request.body = request._enc_data
request.headers['Content-Type'] = \
'application/x-www-form-urlencoded'
else:
# HACK: override the param encoding process
request.params, request._enc_params = \
- request._encode_params(request.data_and_params)
+ request._encode_params(request.params_and_data)
# we're done with these now
- del request.data_and_params
+ del request.params_and_data
- def generate_oauth_params(self):
+ @property
+ def oauth_params(self):
'''This method handles generating the necessary URL parameters the
OAuth provider will expect.'''
oauth_params = {}
@@ -146,7 +147,7 @@ def generate_oauth_params(self):
oauth_params['oauth_signature_method'] = self.signature.NAME
return oauth_params
- def generate_authorization_header(self, oauth_params, realm=None):
+ def auth_header(self, oauth_params, realm=None):
'''This method constructs an authorization header.
:param oauth_params: The OAuth parameters to be added to the header.
View
@@ -82,11 +82,11 @@ def _normalize_request_parameters(self, request):
# if neither params nor data are a string, i.e. both are dicts
# we concatenate the respective dicts
- data_and_params = \
- dict(request.data.items() + request.params.items())
+ params_and_data = \
+ dict(request.params.items() + request.data.items())
normalized = []
- for k, v in data_and_params.items():
+ for k, v in params_and_data.items():
normalized += [(k, v)]
elif type(request.params) == str and type(request.data) == str:
# if both params and data are strings
@@ -114,11 +114,11 @@ def _normalize_request_parameters(self, request):
k, v = t
# save key/value pairs to the request and our list
- request.data_and_params[k] = v
+ request.params_and_data[k] = v
all_normalized += [(k, v)]
# add in the params from data_and_params for signing
- for k, v in request.data_and_params.items():
+ for k, v in request.params_and_data.items():
if (k, v) in all_normalized:
continue
all_normalized += [(k, v)]
@@ -165,7 +165,7 @@ def sign(self, request, consumer, token=None):
hashed = hmac.new(key, signature_base_string, sha1)
# add the signature to the request
- request.data_and_params['oauth_signature'] = \
+ request.params_and_data['oauth_signature'] = \
base64.b64encode(hashed.digest())

0 comments on commit 3c42736

Please sign in to comment.