Skip to content

Commit 3370690

Browse files
authored
Merge pull request #137 from peterbe/use-request-instead-of-site_url-fixes-136
use request instead of SITE_URL, fixes #136
2 parents 64a53df + 4d8744a commit 3370690

File tree

11 files changed

+39
-51
lines changed

11 files changed

+39
-51
lines changed

docs/installation.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ documentation for the appropriate values.
9696
OIDC_OP_USER_ENDPOINT = "<URL of the OIDC OP userinfo endpoint>"
9797
9898
99-
This value depends on your site.
100-
101-
.. code-block:: python
102-
103-
SITE_URL = "<FQDN that users access the site from eg. http://127.0.0.1:8000/ >"
104-
105-
10699
.. warning::
107100
Don't use Django's cookie-based sessions because they might open you up to
108101
replay attacks.

docs/settings.rst

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@ Settings
55
This document describes the Django settings that can be used to customize the configuration
66
of ``mozilla-django-oidc``.
77

8-
.. py:attribute:: SITE_URL
9-
10-
:default: No default
11-
12-
URL that users access your site from. Make sure that you provide the protocol, domain,
13-
path and port if needed (e.g. ``<protocol>://<domain>:<port>/<path>``)
14-
15-
.. note::
16-
This does not have to be a publicly accessible URL, so local URLs
17-
like ``http://localhost:8000`` or ``http://127.0.0.1`` are acceptable as
18-
long as they match what you are using to access your site.
19-
20-
218
.. py:attribute:: OIDC_OP_AUTHORIZATION_ENDPOINT
229
2310
:default: No default

mozilla_django_oidc/auth.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ def authenticate(self, **kwargs):
120120
'client_secret': self.OIDC_RP_CLIENT_SECRET,
121121
'grant_type': 'authorization_code',
122122
'code': code,
123-
'redirect_uri': absolutify(reverse('oidc_authentication_callback'))
123+
'redirect_uri': absolutify(
124+
self.request,
125+
reverse('oidc_authentication_callback')
126+
),
124127
}
125128

126129
# Get the token

mozilla_django_oidc/middleware.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ def process_request(self, request):
9595
params = {
9696
'response_type': 'code',
9797
'client_id': client_id,
98-
'redirect_uri': absolutify(reverse('oidc_authentication_callback')),
98+
'redirect_uri': absolutify(
99+
request,
100+
reverse('oidc_authentication_callback')
101+
),
99102
'state': state,
100103
'scope': 'openid',
101104
'prompt': 'none',

mozilla_django_oidc/utils.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
try:
2-
from urlparse import urljoin
3-
except ImportError:
4-
from urllib.parse import urljoin
5-
61
from django import VERSION
72
from django.conf import settings
83
from django.core.exceptions import ImproperlyConfigured
@@ -23,11 +18,9 @@ def import_from_settings(attr, *args):
2318
raise ImproperlyConfigured('Setting {0} not found'.format(attr))
2419

2520

26-
def absolutify(path):
27-
"""Return the absolute URL of url_path."""
28-
29-
site_url = import_from_settings('SITE_URL')
30-
return urljoin(site_url, path)
21+
def absolutify(request, path):
22+
"""Return the absolute URL of a path."""
23+
return request.build_absolute_uri(path)
3124

3225

3326
# Computed once, reused in every request

mozilla_django_oidc/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ def get(self, request):
9494
'response_type': 'code',
9595
'scope': 'openid',
9696
'client_id': self.OIDC_RP_CLIENT_ID,
97-
'redirect_uri': absolutify(reverse('oidc_authentication_callback')),
97+
'redirect_uri': absolutify(
98+
request,
99+
reverse('oidc_authentication_callback')
100+
),
98101
'state': state,
99102
}
100103

tests/settings.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232

3333
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
3434

35-
SITE_ID = 1
36-
37-
SITE_URL = 'http://example.com'
38-
3935
if tuple(django.VERSION[0:2]) >= (1, 10):
4036
MIDDLEWARE = []
4137
else:

tests/test_auth.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ def test_get_invalid_user(self):
8585

8686
self.assertEqual(self.backend.get_user(user_id=1), None)
8787

88-
@override_settings(SITE_URL='http://site-url.com')
8988
@patch('mozilla_django_oidc.auth.requests')
9089
@patch('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token')
9190
def test_successful_authentication_existing_user(self, token_mock, request_mock):
@@ -115,7 +114,7 @@ def test_successful_authentication_existing_user(self, token_mock, request_mock)
115114
'client_secret': 'client_secret',
116115
'grant_type': 'authorization_code',
117116
'code': 'foo',
118-
'redirect_uri': 'http://site-url.com/callback/'
117+
'redirect_uri': 'http://testserver/callback/'
119118
}
120119
self.assertEqual(self.backend.authenticate(request=auth_request), user)
121120
token_mock.assert_called_once_with('id_token', nonce=None)
@@ -127,7 +126,6 @@ def test_successful_authentication_existing_user(self, token_mock, request_mock)
127126
headers={'Authorization': 'Bearer access_granted'}
128127
)
129128

130-
@override_settings(SITE_URL='http://site-url.com')
131129
@patch('mozilla_django_oidc.auth.requests')
132130
@patch('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token')
133131
def test_successful_authentication_existing_user_upper_case(self, token_mock, request_mock):
@@ -157,7 +155,7 @@ def test_successful_authentication_existing_user_upper_case(self, token_mock, re
157155
'client_secret': 'client_secret',
158156
'grant_type': 'authorization_code',
159157
'code': 'foo',
160-
'redirect_uri': 'http://site-url.com/callback/'
158+
'redirect_uri': 'http://testserver/callback/'
161159
}
162160
self.assertEqual(self.backend.authenticate(request=auth_request), user)
163161
token_mock.assert_called_once_with('id_token', nonce=None)
@@ -172,7 +170,6 @@ def test_successful_authentication_existing_user_upper_case(self, token_mock, re
172170
@patch.object(settings, 'OIDC_USERNAME_ALGO')
173171
@patch('mozilla_django_oidc.auth.requests')
174172
@patch('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token')
175-
@override_settings(SITE_URL='http://site-url.com')
176173
def test_successful_authentication_new_user(self, token_mock, request_mock, algo_mock):
177174
"""Test successful authentication and user creation."""
178175
auth_request = RequestFactory().get('/foo', {'code': 'foo',
@@ -198,7 +195,7 @@ def test_successful_authentication_new_user(self, token_mock, request_mock, algo
198195
'client_secret': 'client_secret',
199196
'grant_type': 'authorization_code',
200197
'code': 'foo',
201-
'redirect_uri': 'http://site-url.com/callback/',
198+
'redirect_uri': 'http://testserver/callback/',
202199
}
203200
self.assertEqual(User.objects.all().count(), 0)
204201
self.backend.authenticate(request=auth_request)

tests/test_middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_no_oidc_token_expiration_forces_renewal(self, mock_random_string):
7878
self.assertEquals(url, 'http://example.com/authorize')
7979
expected_query = {
8080
'response_type': ['code'],
81-
'redirect_uri': ['http://example.com/callback/'],
81+
'redirect_uri': ['http://testserver/callback/'],
8282
'client_id': ['foo'],
8383
'nonce': ['examplestring'],
8484
'prompt': ['none'],
@@ -107,7 +107,7 @@ def test_expired_token_forces_renewal(self, mock_random_string):
107107
self.assertEquals(url, 'http://example.com/authorize')
108108
expected_query = {
109109
'response_type': ['code'],
110-
'redirect_uri': ['http://example.com/callback/'],
110+
'redirect_uri': ['http://testserver/callback/'],
111111
'client_id': ['foo'],
112112
'nonce': ['examplestring'],
113113
'prompt': ['none'],
@@ -249,7 +249,7 @@ def test_expired_token_redirects_to_sso(self, mock_random_string):
249249
self.assertEquals(url, 'http://example.com/authorize')
250250
expected_query = {
251251
'response_type': ['code'],
252-
'redirect_uri': ['http://example.com/callback/'],
252+
'redirect_uri': ['http://testserver/callback/'],
253253
'client_id': ['foo'],
254254
'nonce': ['examplestring'],
255255
'prompt': ['none'],

tests/test_utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.core.exceptions import ImproperlyConfigured
22
from django.test import TestCase, override_settings
3+
from django.test.client import RequestFactory
34

45
from mozilla_django_oidc.utils import absolutify, import_from_settings
56

@@ -21,7 +22,20 @@ def test_attr_nonexisting_default_value(self):
2122

2223

2324
class AbsolutifyTestCase(TestCase):
24-
@override_settings(SITE_URL='http://site-url.com')
25+
2526
def test_absolutify(self):
26-
url = absolutify('/foo/bar')
27-
self.assertEqual(url, 'http://site-url.com/foo/bar')
27+
req = RequestFactory().get('/something/else')
28+
url = absolutify(req, '/foo/bar')
29+
self.assertEqual(url, 'http://testserver/foo/bar')
30+
31+
req = RequestFactory().get('/something/else', SERVER_PORT=8888)
32+
url = absolutify(req, '/foo/bar')
33+
self.assertEqual(url, 'http://testserver:8888/foo/bar')
34+
35+
@override_settings(SECURE_PROXY_SSL_HEADER=('HTTP_X_FORWARDED_PROTO', 'https'))
36+
def test_absolutify_https(self):
37+
req = RequestFactory(
38+
HTTP_X_FORWARDED_PROTO='https'
39+
).get('/', SERVER_PORT=443)
40+
url = absolutify(req, '/foo/bar')
41+
self.assertEqual(url, 'https://testserver/foo/bar')

0 commit comments

Comments
 (0)