Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oauth_body_hash #110

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions oauth2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,13 @@ def sign_request(self, signature_method, consumer, token):

self['oauth_signature_method'] = signature_method.name
self['oauth_signature'] = signature_method.sign(self, consumer, token)

def verify_body(self):
"""Compare the body hash to the one from actual body."""
try:
return self['oauth_body_hash'] == base64.b64encode(sha(self.body).digest())
except KeyError:
raise Error('oauth_body_hash expected, none found in request')

@classmethod
def make_timestamp(cls):
Expand All @@ -513,7 +520,7 @@ def make_nonce(cls):

@classmethod
def from_request(cls, http_method, http_url, headers=None, parameters=None,
query_string=None):
query_string=None, body=''):
"""Combines multiple parameter sources."""
if parameters is None:
parameters = {}
Expand All @@ -531,6 +538,10 @@ def from_request(cls, http_method, http_url, headers=None, parameters=None,
except:
raise Error('Unable to parse OAuth parameters from '
'Authorization header.')
is_form_encoded = False
if headers and 'Content-Type' in headers:
is_form_encoded = \
headers.get('Content-Type') == 'application/x-www-form-urlencoded'

# GET or POST query string.
if query_string:
Expand All @@ -543,7 +554,8 @@ def from_request(cls, http_method, http_url, headers=None, parameters=None,
parameters.update(url_params)

if parameters:
return cls(http_method, http_url, parameters)
return cls(http_method, http_url, parameters, body=body,
is_form_encoded=is_form_encoded)

return None

Expand Down Expand Up @@ -695,8 +707,9 @@ class Server(object):
version = OAUTH_VERSION
signature_methods = None

def __init__(self, signature_methods=None):
def __init__(self, signature_methods=None, body_hashing=False):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this check_body_hashing? Or verify_body_hash?

self.signature_methods = signature_methods or {}
self.body_hashing = body_hashing

def add_signature_method(self, signature_method):
self.signature_methods[signature_method.name] = signature_method
Expand Down Expand Up @@ -751,6 +764,11 @@ def _get_verifier(self, request):
def _check_signature(self, request, consumer, token):
timestamp, nonce = request._get_timestamp_nonce()
self._check_timestamp(timestamp)

if self.body_hashing and not request.is_form_encoded and \
not request.verify_body():
raise Error('Invalid oauth_body_hash.')

signature_method = self._get_signature_method(request)

try:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,20 @@ def test_verify_request(self):
self.assertEquals(parameters['foo'], 59)
self.assertEquals(parameters['multi'], ['FOO','BAR'])

def test_verify_body_hashed_request(self):
server = oauth.Server(body_hashing=True)
server.add_signature_method(oauth.SignatureMethod_HMAC_SHA1())

parameters = server.verify_request(self.request, self.consumer,
self.token)

self.assertTrue('bar' in parameters)
self.assertTrue('foo' in parameters)
self.assertTrue('multi' in parameters)
self.assertEquals(parameters['bar'], 'blerg')
self.assertEquals(parameters['foo'], 59)
self.assertEquals(parameters['multi'], ['FOO','BAR'])

def test_build_authenticate_header(self):
server = oauth.Server()
headers = server.build_authenticate_header('example.com')
Expand Down