Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
Make check for Authorization header case insensitive and also check f…
…or WSGI's HTTP_AUTHORIZATION in the headers.
  • Loading branch information
joestump committed Jul 29, 2015
commit eb5e0f751d2baeb891fab87ea3ad9e53d7a00ff2
11 changes: 8 additions & 3 deletions oauth2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,15 @@ def from_request(cls, http_method, http_url, headers=None, parameters=None,
parameters = {}

# Headers
if headers and 'Authorization' in headers:
auth_header = headers['Authorization']
if headers:
auth_header = None
for k, v in headers.items():
if k.lower() == 'authorization' or \
k.upper() == 'HTTP_AUTHORIZATION':
auth_header = v

# Check that the authorization header is OAuth.
if auth_header[:6] == 'OAuth ':
if auth_header and auth_header[:6] == 'OAuth ':
auth_header = auth_header[6:]
try:
# Get the parameters from the header.
Expand Down
57 changes: 57 additions & 0 deletions tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,63 @@ def test_sign_request(self):
req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), con, tok)
self.assertEquals(req['oauth_signature'], 'IBw5mfvoCsDjgpcsVKbyvsDqQaU=')


def test_from_request_works_with_wsgi(self):
"""Make sure WSGI header HTTP_AUTHORIZATION is detected correctly."""
url = "http://sp.example.com/"

params = {
'oauth_version': "1.0",
'oauth_nonce': "4572616e48616d6d65724c61686176",
'oauth_timestamp': "137131200",
'oauth_consumer_key': "0685bd9184jfhq22",
'oauth_signature_method': "HMAC-SHA1",
'oauth_token': "ad180jjd733klru7",
'oauth_signature': "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
}

req = oauth.Request("GET", url, params)
headers = req.to_header()

# Munge the headers
headers['HTTP_AUTHORIZATION'] = headers['Authorization']
del headers['Authorization']

# Test from the headers
req = oauth.Request.from_request("GET", url, headers)
self.assertEquals(req.method, "GET")
self.assertEquals(req.url, url)
self.assertEquals(params, req.copy())


def test_from_request_is_case_insensitive_checking_for_auth(self):
"""Checks for the Authorization header should be case insensitive."""
url = "http://sp.example.com/"

params = {
'oauth_version': "1.0",
'oauth_nonce': "4572616e48616d6d65724c61686176",
'oauth_timestamp': "137131200",
'oauth_consumer_key': "0685bd9184jfhq22",
'oauth_signature_method': "HMAC-SHA1",
'oauth_token': "ad180jjd733klru7",
'oauth_signature': "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
}

req = oauth.Request("GET", url, params)
headers = req.to_header()

# Munge the headers
headers['authorization'] = headers['Authorization']
del headers['Authorization']

# Test from the headers
req = oauth.Request.from_request("GET", url, headers)
self.assertEquals(req.method, "GET")
self.assertEquals(req.url, url)
self.assertEquals(params, req.copy())


def test_from_request(self):
url = "http://sp.example.com/"

Expand Down