Skip to content

Commit

Permalink
Merge pull request #341 from kdazzle/request-getattr
Browse files Browse the repository at this point in the history
#340 - calling getattr on Request should raise an AttributeError if that attribute doesn't exist
  • Loading branch information
thedrow committed Jul 2, 2015
2 parents 03ea1a1 + fb8dcb4 commit 6745435
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
22 changes: 20 additions & 2 deletions oauthlib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,31 @@ def __init__(self, uri, http_method='GET', body=None, headers=None,
self.decoded_body = extract_params(encode(body))
self.oauth_params = []

self._params = {}
self._params = {
"access_token": None,
"client": None,
"client_id": None,
"code": None,
"extra_credentials": None,
"grant_type": None,
"redirect_uri": None,
"refresh_token": None,
"response_type": None,
"scope": None,
"scopes": None,
"state": None,
"token": None,
"user": None,
}
self._params.update(dict(urldecode(self.uri_query)))
self._params.update(dict(self.decoded_body or []))
self._params.update(self.headers)

def __getattr__(self, name):
return self._params.get(name, None)
if name in self._params:
return self._params[name]
else:
raise AttributeError(name)

def __repr__(self):
return '<oauthlib.Request url="%s", http_method="%s", headers="%s", body="%s">' % (
Expand Down
2 changes: 1 addition & 1 deletion oauthlib/oauth2/rfc6749/grant_types/authorization_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def validate_token_request(self, request):
raise errors.InvalidGrantError(request=request)

for attr in ('user', 'state', 'scopes'):
if getattr(request, attr) is None:
if getattr(request, attr, None) is None:
log.debug('request.%s was not set on code validation.', attr)

# REQUIRED, if the "redirect_uri" parameter was included in the
Expand Down
3 changes: 2 additions & 1 deletion oauthlib/oauth2/rfc6749/grant_types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def create_token_response(self, request, token_handler):
raise NotImplementedError('Subclasses must implement this method.')

def validate_grant_type(self, request):
if not self.request_validator.validate_grant_type(request.client_id,
client_id = getattr(request, 'client_id', None)
if not self.request_validator.validate_grant_type(client_id,
request.grant_type, request.client, request):
log.debug('Unauthorized from %r (%r) access to grant type %s.',
request.client_id, request.client, request.grant_type)
Expand Down
2 changes: 1 addition & 1 deletion oauthlib/oauth2/rfc6749/grant_types/client_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def create_token_response(self, request, token_handler):
return headers, json.dumps(token), 200

def validate_token_request(self, request):
if not getattr(request, 'grant_type'):
if not getattr(request, 'grant_type', None):
raise errors.InvalidRequestError('Request is missing grant type.',
request=request)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def validate_token_request(self, request):
.. _`Section 3.2.1`: http://tools.ietf.org/html/rfc6749#section-3.2.1
"""
for param in ('grant_type', 'username', 'password'):
if not getattr(request, param):
if not getattr(request, param, None):
raise errors.InvalidRequestError(
'Request is missing %s parameter.' % param, request=request)

Expand Down
14 changes: 14 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ def test_dict_body(self):
r = Request(URI, body=PARAMS_DICT)
self.assertItemsEqual(r.decoded_body, PARAMS_TWOTUPLE)

def test_getattr_existing_attribute(self):
r = Request(URI, body='foo bar')
self.assertEqual('foo bar', getattr(r, 'body'))

def test_getattr_return_default(self):
r = Request(URI, body='')
actual_value = getattr(r, 'does_not_exist', 'foo bar')
self.assertEqual('foo bar', actual_value)

def test_getattr_raise_attribute_error(self):
r = Request(URI, body='foo bar')
with self.assertRaises(AttributeError):
getattr(r, 'does_not_exist')


class CaseInsensitiveDictTest(TestCase):

Expand Down

0 comments on commit 6745435

Please sign in to comment.