Skip to content

Commit

Permalink
Merge pull request #49 from akatsoulas/auth-headers-userinfo
Browse files Browse the repository at this point in the history
Pass token in Authorization header.
  • Loading branch information
akatsoulas committed Nov 15, 2016
2 parents 3de64a9 + 825e7bd commit f7be3e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
17 changes: 6 additions & 11 deletions mozilla_django_oidc/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import logging
import requests

try:
from urllib import urlencode
except ImportError:
from urllib.parse import urlencode

try:
from django.utils.encoding import smart_bytes
except ImportError:
Expand Down Expand Up @@ -98,7 +93,7 @@ def authenticate(self, code=None, state=None):

# Get the token
response = requests.post(self.OIDC_OP_TOKEN_ENDPOINT,
json=token_payload,
data=token_payload,
verify=import_from_settings('OIDC_VERIFY_SSL', True))
response.raise_for_status()

Expand All @@ -107,11 +102,11 @@ def authenticate(self, code=None, state=None):
payload = self.verify_token(token_response.get('id_token'))

if payload:
query = urlencode({
'access_token': token_response.get('access_token')
})
user_response = requests.get('{url}?{query}'.format(url=self.OIDC_OP_USER_ENDPOINT,
query=query))
access_token = token_response.get('access_token')
user_response = requests.get(self.OIDC_OP_USER_ENDPOINT,
headers={
'Authorization': 'Bearer {0}'.format(access_token)
})
user_response.raise_for_status()
user_info = user_response.json()
email = user_info.get('email')
Expand Down
10 changes: 6 additions & 4 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ def test_successful_authentication_existing_user(self, token_mock, request_mock)
self.assertEqual(self.backend.authenticate(code='foo', state='bar'), user)
token_mock.assert_called_once_with('id_token')
request_mock.post.assert_called_once_with('https://server.example.com/token',
json=post_data,
data=post_data,
verify=True)
request_mock.get.assert_called_once_with(
'https://server.example.com/user?access_token=access_granted'
'https://server.example.com/user',
headers={'Authorization': 'Bearer access_granted'}
)

@patch.object(settings, 'OIDC_USERNAME_ALGO')
Expand Down Expand Up @@ -126,10 +127,11 @@ def test_successful_authentication_new_user(self, token_mock, request_mock, algo

token_mock.assert_called_once_with('id_token')
request_mock.post.assert_called_once_with('https://server.example.com/token',
json=post_data,
data=post_data,
verify=True)
request_mock.get.assert_called_once_with(
'https://server.example.com/user?access_token=access_granted'
'https://server.example.com/user',
headers={'Authorization': 'Bearer access_granted'}
)

def test_authenticate_no_code_no_state(self):
Expand Down

0 comments on commit f7be3e8

Please sign in to comment.