Skip to content

Commit 727ace3

Browse files
committed
Multiple fixes
* Absolutify redirect_uri * Fix typo in token endpoint POST request. * Fix typo in token_payload `grant_type`. * Fix `default_val` check in `import_from_settings`. * Use correct claim names on user get/create
1 parent 2c3253f commit 727ace3

File tree

5 files changed

+44
-23
lines changed

5 files changed

+44
-23
lines changed

mozilla_django_oidc/auth.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from django.contrib.auth import get_user_model
1111
from django.core.urlresolvers import reverse
1212

13-
from mozilla_django_oidc.utils import import_from_settings
13+
from mozilla_django_oidc.utils import absolutify, import_from_settings
1414

1515

1616
class OIDCAuthenticationBackend(object):
@@ -48,9 +48,9 @@ def authenticate(self, code=None, state=None):
4848
token_payload = {
4949
'client_id': self.OIDC_OP_CLIENT_ID,
5050
'client_secret': self.OIDC_OP_CLIENT_SECRET,
51-
'grand_type': 'authorization_code',
51+
'grant_type': 'authorization_code',
5252
'code': code,
53-
'redirect_url': reverse('oidc_authentication_callback')
53+
'redirect_uri': absolutify(reverse('oidc_authentication_callback'))
5454
}
5555

5656
# Get the token
@@ -70,10 +70,10 @@ def authenticate(self, code=None, state=None):
7070
user_info = user_response.json()
7171

7272
try:
73-
return self.UserModel.objects.get(email=user_info['verified_email'])
73+
return self.UserModel.objects.get(email=user_info['email'])
7474
except self.UserModel.DoesNotExist:
75-
return self.UserModel.objects.create_user(username=user_info['username'],
76-
email=user_info['verified_email'])
75+
return self.UserModel.objects.create_user(username=user_info['nickname'],
76+
email=user_info['email'])
7777
return None
7878

7979
def get_user(self, user_id):

mozilla_django_oidc/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
try:
2+
from urlparse import urljoin
3+
except ImportError:
4+
from urllib.parse import urljoin
5+
16
from django.conf import settings
27
from django.core.exceptions import ImproperlyConfigured
38

@@ -10,8 +15,15 @@ def import_from_settings(attr, default_val=None):
1015
ImproperlyConfigured
1116
"""
1217
try:
13-
if default_val:
18+
if default_val is not None:
1419
return getattr(settings, attr, default_val)
1520
return getattr(settings, attr)
1621
except AttributeError:
1722
raise ImproperlyConfigured('Setting {0} not found'.format(attr))
23+
24+
25+
def absolutify(path):
26+
"""Return the absolute URL of url_path."""
27+
28+
site_url = import_from_settings('SITE_URL')
29+
return urljoin(site_url, path)

runtests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'mozilla_django_oidc',
2121
],
2222
SITE_ID=1,
23+
SITE_URL='http://example.com',
2324
MIDDLEWARE_CLASSES=(),
2425
)
2526

tests/test_auth.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from mock import Mock, call, patch
22

33
from django.contrib.auth import get_user_model
4-
from django.core.urlresolvers import reverse
54
from django.test import TestCase, override_settings
65

76
from mozilla_django_oidc.auth import OIDCAuthenticationBackend
@@ -28,8 +27,8 @@ def test_invalid_token(self, request_mock, token_mock):
2827
token_mock.return_value = None
2928
get_json_mock = Mock()
3029
get_json_mock.json.return_value = {
31-
'username': 'username',
32-
'verified_email': '[email protected]'
30+
'nickname': 'username',
31+
'email': '[email protected]'
3332
}
3433
request_mock.get.return_value = get_json_mock
3534
post_json_mock = Mock()
@@ -53,6 +52,7 @@ def test_get_invalid_user(self):
5352

5453
@patch('mozilla_django_oidc.auth.requests')
5554
@patch('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token')
55+
@override_settings(SITE_URL='http://site-url.com')
5656
def test_successful_authentication_existing_user(self, token_mock, request_mock):
5757
"""Test successful authentication for existing user."""
5858

@@ -61,8 +61,8 @@ def test_successful_authentication_existing_user(self, token_mock, request_mock)
6161
token_mock.return_value = True
6262
get_json_mock = Mock()
6363
get_json_mock.json.return_value = {
64-
'username': 'a_username',
65-
'verified_email': '[email protected]'
64+
'nickname': 'a_username',
65+
'email': '[email protected]'
6666
}
6767
request_mock.get.return_value = get_json_mock
6868
post_json_mock = Mock()
@@ -75,9 +75,9 @@ def test_successful_authentication_existing_user(self, token_mock, request_mock)
7575
post_data = {
7676
'client_id': 'example_id',
7777
'client_secret': 'example_secret',
78-
'grand_type': 'authorization_code',
78+
'grant_type': 'authorization_code',
7979
'code': 'foo',
80-
'redirect_url': reverse('oidc_authentication_callback')
80+
'redirect_uri': 'http://site-url.com/oidc/authentication_callback/'
8181
}
8282
self.assertEqual(self.backend.authenticate(code='foo', state='bar'), user)
8383
token_mock.assert_called_once_with('id_token')
@@ -90,14 +90,15 @@ def test_successful_authentication_existing_user(self, token_mock, request_mock)
9090

9191
@patch('mozilla_django_oidc.auth.requests')
9292
@patch('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token')
93+
@override_settings(SITE_URL='http://site-url.com')
9394
def test_successful_authentication_new_user(self, token_mock, request_mock):
9495
"""Test successful authentication and user creation."""
9596

9697
token_mock.return_value = True
9798
get_json_mock = Mock()
9899
get_json_mock.json.return_value = {
99-
'username': 'a_username',
100-
'verified_email': '[email protected]'
100+
'nickname': 'a_username',
101+
'email': '[email protected]'
101102
}
102103
request_mock.get.return_value = get_json_mock
103104
post_json_mock = Mock()
@@ -109,9 +110,9 @@ def test_successful_authentication_new_user(self, token_mock, request_mock):
109110
post_data = {
110111
'client_id': 'example_id',
111112
'client_secret': 'example_secret',
112-
'grand_type': 'authorization_code',
113+
'grant_type': 'authorization_code',
113114
'code': 'foo',
114-
'redirect_url': reverse('oidc_authentication_callback')
115+
'redirect_uri': 'http://site-url.com/oidc/authentication_callback/',
115116
}
116117
self.assertEqual(User.objects.all().count(), 0)
117118
self.backend.authenticate(code='foo', state='bar')
@@ -143,8 +144,8 @@ def test_jwt_decode_params(self, request_mock, jwt_mock):
143144
}
144145
get_json_mock = Mock()
145146
get_json_mock.json.return_value = {
146-
'username': 'username',
147-
'verified_email': '[email protected]'
147+
'nickname': 'username',
148+
'email': '[email protected]'
148149
}
149150
request_mock.get.return_value = get_json_mock
150151
post_json_mock = Mock()
@@ -171,8 +172,8 @@ def test_jwt_decode_params_verify_false(self, request_mock, jwt_mock):
171172
}
172173
get_json_mock = Mock()
173174
get_json_mock.json.return_value = {
174-
'username': 'username',
175-
'verified_email': '[email protected]'
175+
'nickname': 'username',
176+
'email': '[email protected]'
176177
}
177178
request_mock.get.return_value = get_json_mock
178179
post_json_mock = Mock()

tests/test_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.core.exceptions import ImproperlyConfigured
22
from django.test import TestCase, override_settings
33

4-
from mozilla_django_oidc.utils import import_from_settings
4+
from mozilla_django_oidc.utils import absolutify, import_from_settings
55

66

77
class SettingImportTestCase(TestCase):
@@ -18,3 +18,10 @@ def test_attr_nonexisting_no_default_value(self):
1818
def test_attr_nonexisting_default_value(self):
1919
s = import_from_settings('EXAMPLE_VARIABLE', 'example_default')
2020
self.assertEqual(s, 'example_default')
21+
22+
23+
class AbsolutifyTestCase(TestCase):
24+
@override_settings(SITE_URL='http://site-url.com')
25+
def test_absolutify(self):
26+
url = absolutify('/foo/bar')
27+
self.assertEqual(url, 'http://site-url.com/foo/bar')

0 commit comments

Comments
 (0)