Skip to content

Commit 2e013c3

Browse files
committed
Rebase fixing conflicts and tests
1 parent eaee164 commit 2e013c3

File tree

3 files changed

+68
-99
lines changed

3 files changed

+68
-99
lines changed

oauth2_provider/oauth2_validators.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,24 @@ def validate_user_match(self, id_token_hint, scopes, claims, request):
856856
# https://github.com/idan/oauthlib/blob/master/oauthlib/oauth2/rfc6749/request_validator.py#L556
857857
# http://openid.net/specs/openid-connect-core-1_0.html#AuthRequest id_token_hint section
858858
return True
859+
860+
def get_authorization_code_nonce(self, client_id, code, redirect_uri, request):
861+
""" Extracts nonce from saved authorization code.
862+
If present in the Authentication Request, Authorization
863+
Servers MUST include a nonce Claim in the ID Token with the
864+
Claim Value being the nonce value sent in the Authentication
865+
Request. Authorization Servers SHOULD perform no other
866+
processing on nonce values used. The nonce value is a
867+
case-sensitive string.
868+
Only code param should be sufficient to retrieve grant code from
869+
any storage you are using. However, `client_id` and `redirect_uri`
870+
have been validated and can be used also.
871+
:param client_id: Unicode client identifier
872+
:param code: Unicode authorization code grant
873+
:param redirect_uri: Unicode absolute URI
874+
:return: Unicode nonce
875+
Method is used by:
876+
- Authorization Token Grant Dispatcher
877+
"""
878+
# TODO: Fix this ;)
879+
return ""

oauth2_provider/views/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def form_valid(self, form):
144144
except OAuthToolkitError as error:
145145
return self.error_response(error, application)
146146

147-
self.success_url = redirect_uri
147+
self.success_url = uri
148148
log.debug("Success url for the request: {0}".format(self.success_url))
149149
return self.redirect(self.success_url, application)
150150

@@ -173,9 +173,9 @@ def get(self, request, *args, **kwargs):
173173
client_id=credentials["client_id"]
174174
)
175175

176-
uri_query = parse.urlparse(self.request.get_raw_uri()).query
176+
uri_query = urllib.parse.urlparse(self.request.get_raw_uri()).query
177177
uri_query_params = dict(
178-
parse.parse_qsl(uri_query, keep_blank_values=True, strict_parsing=True)
178+
urllib.parse.parse_qsl(uri_query, keep_blank_values=True, strict_parsing=True)
179179
)
180180

181181
kwargs["application"] = application
@@ -211,7 +211,7 @@ def get(self, request, *args, **kwargs):
211211
credentials=credentials,
212212
allow=True,
213213
)
214-
return self.redirect(redirect_uri, application)
214+
return self.redirect(uri, application)
215215

216216
elif require_approval == "auto":
217217
tokens = (
@@ -234,7 +234,7 @@ def get(self, request, *args, **kwargs):
234234
credentials=credentials,
235235
allow=True,
236236
)
237-
return self.redirect(redirect_uri, application)
237+
return self.redirect(uri, application)
238238

239239
except OAuthToolkitError as error:
240240
return self.error_response(error, application)

tests/test_models.py

Lines changed: 42 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from datetime import datetime as dt
2-
31
import pytest
42
from django.contrib.auth import get_user_model
53
from django.core.exceptions import ImproperlyConfigured, ValidationError
@@ -8,30 +6,23 @@
86
from django.utils import timezone
97

108
from oauth2_provider.models import (
11-
clear_expired,
12-
get_access_token_model,
13-
get_application_model,
14-
get_grant_model,
15-
get_refresh_token_model,
16-
get_id_token_model,
9+
clear_expired, get_access_token_model, get_application_model,
10+
get_grant_model, get_refresh_token_model
1711
)
1812
from oauth2_provider.settings import oauth2_settings
1913

20-
from .models import SampleRefreshToken
2114

2215
Application = get_application_model()
2316
Grant = get_grant_model()
2417
AccessToken = get_access_token_model()
2518
RefreshToken = get_refresh_token_model()
2619
UserModel = get_user_model()
27-
IDToken = get_id_token_model()
2820

2921

3022
class TestModels(TestCase):
23+
3124
def setUp(self):
32-
self.user = UserModel.objects.create_user(
33-
"test_user", "[email protected]", "123456"
34-
)
25+
self.user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
3526

3627
def test_allow_scopes(self):
3728
self.client.login(username="test_user", password="123456")
@@ -44,7 +35,11 @@ def test_allow_scopes(self):
4435
)
4536

4637
access_token = AccessToken(
47-
user=self.user, scope="read write", expires=0, token="", application=app
38+
user=self.user,
39+
scope="read write",
40+
expires=0,
41+
token="",
42+
application=app
4843
)
4944

5045
self.assertTrue(access_token.allow_scopes(["read", "write"]))
@@ -99,30 +94,35 @@ def test_scopes_property(self):
9994
)
10095

10196
access_token = AccessToken(
102-
user=self.user, scope="read write", expires=0, token="", application=app
97+
user=self.user,
98+
scope="read write",
99+
expires=0,
100+
token="",
101+
application=app
103102
)
104103

105104
access_token2 = AccessToken(
106-
user=self.user, scope="write", expires=0, token="", application=app
105+
user=self.user,
106+
scope="write",
107+
expires=0,
108+
token="",
109+
application=app
107110
)
108111

109-
self.assertEqual(
110-
access_token.scopes, {"read": "Reading scope", "write": "Writing scope"}
111-
)
112+
self.assertEqual(access_token.scopes, {"read": "Reading scope", "write": "Writing scope"})
112113
self.assertEqual(access_token2.scopes, {"write": "Writing scope"})
113114

114115

115116
@override_settings(
116117
OAUTH2_PROVIDER_APPLICATION_MODEL="tests.SampleApplication",
117118
OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL="tests.SampleAccessToken",
118119
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL="tests.SampleRefreshToken",
119-
OAUTH2_PROVIDER_GRANT_MODEL="tests.SampleGrant",
120+
OAUTH2_PROVIDER_GRANT_MODEL="tests.SampleGrant"
120121
)
121122
class TestCustomModels(TestCase):
123+
122124
def setUp(self):
123-
self.user = UserModel.objects.create_user(
124-
"test_user", "[email protected]", "123456"
125-
)
125+
self.user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
126126

127127
def test_custom_application_model(self):
128128
"""
@@ -132,8 +132,7 @@ def test_custom_application_model(self):
132132
See issue #90 (https://github.com/jazzband/django-oauth-toolkit/issues/90)
133133
"""
134134
related_object_names = [
135-
f.name
136-
for f in UserModel._meta.get_fields()
135+
f.name for f in UserModel._meta.get_fields()
137136
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
138137
]
139138
self.assertNotIn("oauth2_provider:application", related_object_names)
@@ -164,8 +163,7 @@ def test_custom_access_token_model(self):
164163
"""
165164
# Django internals caches the related objects.
166165
related_object_names = [
167-
f.name
168-
for f in UserModel._meta.get_fields()
166+
f.name for f in UserModel._meta.get_fields()
169167
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
170168
]
171169
self.assertNotIn("oauth2_provider:access_token", related_object_names)
@@ -196,8 +194,7 @@ def test_custom_refresh_token_model(self):
196194
"""
197195
# Django internals caches the related objects.
198196
related_object_names = [
199-
f.name
200-
for f in UserModel._meta.get_fields()
197+
f.name for f in UserModel._meta.get_fields()
201198
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
202199
]
203200
self.assertNotIn("oauth2_provider:refresh_token", related_object_names)
@@ -228,8 +225,7 @@ def test_custom_grant_model(self):
228225
"""
229226
# Django internals caches the related objects.
230227
related_object_names = [
231-
f.name
232-
for f in UserModel._meta.get_fields()
228+
f.name for f in UserModel._meta.get_fields()
233229
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
234230
]
235231
self.assertNotIn("oauth2_provider:grant", related_object_names)
@@ -255,6 +251,7 @@ def test_custom_grant_model_not_installed(self):
255251

256252

257253
class TestGrantModel(TestCase):
254+
258255
def test_str(self):
259256
grant = Grant(code="test_code")
260257
self.assertEqual("%s" % grant, grant.code)
@@ -266,10 +263,9 @@ def test_expires_can_be_none(self):
266263

267264

268265
class TestAccessTokenModel(TestCase):
266+
269267
def setUp(self):
270-
self.user = UserModel.objects.create_user(
271-
"test_user", "[email protected]", "123456"
272-
)
268+
self.user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
273269

274270
def test_str(self):
275271
access_token = AccessToken(token="test_token")
@@ -283,9 +279,7 @@ def test_user_can_be_none(self):
283279
client_type=Application.CLIENT_CONFIDENTIAL,
284280
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
285281
)
286-
access_token = AccessToken.objects.create(
287-
token="test_token", application=app, expires=timezone.now()
288-
)
282+
access_token = AccessToken.objects.create(token="test_token", application=app, expires=timezone.now())
289283
self.assertIsNone(access_token.user)
290284

291285
def test_expires_can_be_none(self):
@@ -295,58 +289,16 @@ def test_expires_can_be_none(self):
295289

296290

297291
class TestRefreshTokenModel(TestCase):
292+
298293
def test_str(self):
299294
refresh_token = RefreshToken(token="test_token")
300295
self.assertEqual("%s" % refresh_token, refresh_token.token)
301296

302297

303298
class TestClearExpired(TestCase):
299+
304300
def setUp(self):
305-
self.user = UserModel.objects.create_user(
306-
"test_user", "[email protected]", "123456"
307-
)
308-
app1 = Application.objects.create(
309-
name="Test Application",
310-
redirect_uris=(
311-
"http://localhost http://example.com http://example.org custom-scheme://example.com"
312-
),
313-
user=self.user,
314-
client_type=Application.CLIENT_CONFIDENTIAL,
315-
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
316-
)
317-
app2 = Application.objects.create(
318-
name="Test Application",
319-
redirect_uris=(
320-
"http://localhost http://example.com http://example.org custom-scheme://example.com"
321-
),
322-
user=self.user,
323-
client_type=Application.CLIENT_CONFIDENTIAL,
324-
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
325-
)
326-
id1 = IDToken.objects.create(
327-
token="666",
328-
expires=dt.now(),
329-
scope=2,
330-
application=app1,
331-
user=self.user,
332-
created=dt.now(),
333-
updated=dt.now(),
334-
)
335-
id2 = IDToken.objects.create(
336-
token="999",
337-
expires=dt.now(),
338-
scope=2,
339-
application=app2,
340-
user=self.user,
341-
created=dt.now(),
342-
updated=dt.now(),
343-
)
344-
refresh_token1 = SampleRefreshToken.objects.create(
345-
token="test_token", application=app1, user=self.user,
346-
)
347-
refresh_token2 = SampleRefreshToken.objects.create(
348-
token="test_token2", application=app2, user=self.user,
349-
)
301+
self.user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
350302
# Insert two tokens on database.
351303
app = Application.objects.create(
352304
name="test_app",
@@ -359,24 +311,20 @@ def setUp(self):
359311
token="555",
360312
expires=timezone.now(),
361313
scope=2,
362-
application=app1,
363-
id_token=id1,
314+
application=app,
364315
user=self.user,
365-
created=dt.now(),
366-
updated=dt.now(),
367-
refresh_token=refresh_token1,
368-
)
316+
created=timezone.now(),
317+
updated=timezone.now(),
318+
)
369319
AccessToken.objects.create(
370320
token="666",
371321
expires=timezone.now(),
372322
scope=2,
373-
application=app2,
323+
application=app,
374324
user=self.user,
375-
id_token=id2,
376-
created=dt.now(),
377-
updated=dt.now(),
378-
refresh_token=refresh_token2,
379-
)
325+
created=timezone.now(),
326+
updated=timezone.now(),
327+
)
380328

381329
def test_clear_expired_tokens(self):
382330
oauth2_settings.REFRESH_TOKEN_EXPIRE_SECONDS = 60

0 commit comments

Comments
 (0)