Skip to content

Commit 39a4577

Browse files
JadielTeofilon2ygk
andauthored
Fix #524 - Restrict usage of timezone aware expire dates to Django projects with USE_TZ set to True (#948)
* Add breaking tests * Add fix for breaking tests Restrict usage of timezone aware expire dates to Django projects with USE_TZ set to True. * Update authors file * Update changelog file * Update the docs * Fix broken tests (missing import) Co-authored-by: Alan Crosswell <[email protected]>
1 parent 5d53d24 commit 39a4577

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,7 @@ Spencer Carroll
4646
Stéphane Raimbault
4747
Tom Evans
4848
Will Beaufoy
49+
Rustem Saiargaliev
50+
Jadiel Teófilo
4951
pySilver
5052
Łukasz Skarżyński

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
### Added
2020
* #915 Add optional OpenID Connect support.
21+
### Fixed
22+
* #524 Restrict usage of timezone aware expire dates to Django projects with USE_TZ set to True.
2123

2224
### Changed
2325
* #942 Help via defunct Google group replaced with using GitHub issues

docs/settings.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,12 @@ OIDC_TOKEN_ENDPOINT_AUTH_METHODS_SUPPORTED
310310
Default: ``["client_secret_post", "client_secret_basic"]``
311311

312312
The authentication methods that are advertised to be supported by this server.
313+
314+
315+
Settings imported from Django project
316+
--------------------------
317+
318+
USE_TZ
319+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
320+
321+
Used to determine whether or not to make token expire dates timezone aware.

oauth2_provider/oauth2_validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def _get_token_from_authentication_server(
357357
expires = max_caching_time
358358

359359
scope = content.get("scope", "")
360-
expires = make_aware(expires)
360+
expires = make_aware(expires) if settings.USE_TZ else expires
361361

362362
access_token, _created = AccessToken.objects.update_or_create(
363363
token=token,

tests/test_introspection_auth.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33

44
import pytest
5+
from django.conf import settings
56
from django.conf.urls import include
67
from django.contrib.auth import get_user_model
78
from django.http import HttpResponse
@@ -12,6 +13,7 @@
1213

1314
from oauth2_provider.models import get_access_token_model, get_application_model
1415
from oauth2_provider.oauth2_validators import OAuth2Validator
16+
from oauth2_provider.settings import oauth2_settings
1517
from oauth2_provider.views import ScopedProtectedResourceView
1618

1719
from . import presets
@@ -154,6 +156,25 @@ def test_get_token_from_authentication_server_existing_token(self, mock_get):
154156
self.assertEqual(token.user.username, "foo_user")
155157
self.assertEqual(token.scope, "read write dolphin")
156158

159+
@mock.patch("requests.post", side_effect=mocked_requests_post)
160+
def test_get_token_from_authentication_server_expires_timezone(self, mock_get):
161+
"""
162+
Test method _get_token_from_authentication_server for projects with USE_TZ False
163+
"""
164+
settings_use_tz_backup = settings.USE_TZ
165+
settings.USE_TZ = False
166+
try:
167+
self.validator._get_token_from_authentication_server(
168+
"foo",
169+
oauth2_settings.RESOURCE_SERVER_INTROSPECTION_URL,
170+
oauth2_settings.RESOURCE_SERVER_AUTH_TOKEN,
171+
oauth2_settings.RESOURCE_SERVER_INTROSPECTION_CREDENTIALS,
172+
)
173+
except ValueError as exception:
174+
self.fail(str(exception))
175+
finally:
176+
settings.USE_TZ = settings_use_tz_backup
177+
157178
@mock.patch("requests.post", side_effect=mocked_requests_post)
158179
def test_validate_bearer_token(self, mock_get):
159180
"""

0 commit comments

Comments
 (0)