Skip to content

Commit b909b48

Browse files
committed
Merge pull request #174 from joestump/fix-authorization-detection
Make check for Authorization header more robust.
2 parents aaa4a5d + eb5e0f7 commit b909b48

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

oauth2/__init__.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,15 @@ def from_request(cls, http_method, http_url, headers=None, parameters=None,
519519
parameters = {}
520520

521521
# Headers
522-
if headers and 'Authorization' in headers:
523-
auth_header = headers['Authorization']
522+
if headers:
523+
auth_header = None
524+
for k, v in headers.items():
525+
if k.lower() == 'authorization' or \
526+
k.upper() == 'HTTP_AUTHORIZATION':
527+
auth_header = v
528+
524529
# Check that the authorization header is OAuth.
525-
if auth_header[:6] == 'OAuth ':
530+
if auth_header and auth_header[:6] == 'OAuth ':
526531
auth_header = auth_header[6:]
527532
try:
528533
# Get the parameters from the header.

tests/test_oauth.py

+57
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,63 @@ def test_sign_request(self):
914914
req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), con, tok)
915915
self.assertEquals(req['oauth_signature'], 'IBw5mfvoCsDjgpcsVKbyvsDqQaU=')
916916

917+
918+
def test_from_request_works_with_wsgi(self):
919+
"""Make sure WSGI header HTTP_AUTHORIZATION is detected correctly."""
920+
url = "http://sp.example.com/"
921+
922+
params = {
923+
'oauth_version': "1.0",
924+
'oauth_nonce': "4572616e48616d6d65724c61686176",
925+
'oauth_timestamp': "137131200",
926+
'oauth_consumer_key': "0685bd9184jfhq22",
927+
'oauth_signature_method': "HMAC-SHA1",
928+
'oauth_token': "ad180jjd733klru7",
929+
'oauth_signature': "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
930+
}
931+
932+
req = oauth.Request("GET", url, params)
933+
headers = req.to_header()
934+
935+
# Munge the headers
936+
headers['HTTP_AUTHORIZATION'] = headers['Authorization']
937+
del headers['Authorization']
938+
939+
# Test from the headers
940+
req = oauth.Request.from_request("GET", url, headers)
941+
self.assertEquals(req.method, "GET")
942+
self.assertEquals(req.url, url)
943+
self.assertEquals(params, req.copy())
944+
945+
946+
def test_from_request_is_case_insensitive_checking_for_auth(self):
947+
"""Checks for the Authorization header should be case insensitive."""
948+
url = "http://sp.example.com/"
949+
950+
params = {
951+
'oauth_version': "1.0",
952+
'oauth_nonce': "4572616e48616d6d65724c61686176",
953+
'oauth_timestamp': "137131200",
954+
'oauth_consumer_key': "0685bd9184jfhq22",
955+
'oauth_signature_method': "HMAC-SHA1",
956+
'oauth_token': "ad180jjd733klru7",
957+
'oauth_signature': "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
958+
}
959+
960+
req = oauth.Request("GET", url, params)
961+
headers = req.to_header()
962+
963+
# Munge the headers
964+
headers['authorization'] = headers['Authorization']
965+
del headers['Authorization']
966+
967+
# Test from the headers
968+
req = oauth.Request.from_request("GET", url, headers)
969+
self.assertEquals(req.method, "GET")
970+
self.assertEquals(req.url, url)
971+
self.assertEquals(params, req.copy())
972+
973+
917974
def test_from_request(self):
918975
url = "http://sp.example.com/"
919976

0 commit comments

Comments
 (0)