Skip to content

Commit 8f7a9bd

Browse files
authored
Merge branch 'master' into BUGFIX/models-pk-instead-of-models-id
2 parents 16cc0d8 + 7e13413 commit 8f7a9bd

24 files changed

+318
-85
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,19 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
python-version:
13-
- '3.8'
14-
- '3.9'
1513
- '3.10'
1614
- '3.11'
1715
- '3.12'
1816
django-version:
19-
- '3.2'
20-
- '4.0'
21-
- '4.1'
2217
- '4.2'
2318
- '5.0'
19+
- '5.1'
2420
- 'main'
25-
exclude:
21+
include:
2622
# https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django
27-
28-
# < Python 3.10 is not supported by Django 5.0+
29-
- python-version: '3.8'
30-
django-version: '5.0'
31-
- python-version: '3.9'
32-
django-version: '5.0'
3323
- python-version: '3.8'
34-
django-version: 'main'
24+
django-version: '4.2'
3525
- python-version: '3.9'
36-
django-version: 'main'
37-
38-
# Python 3.12 is not supported by Django < 5.0
39-
- python-version: '3.12'
40-
django-version: '3.2'
41-
- python-version: '3.12'
42-
django-version: '4.0'
43-
- python-version: '3.12'
44-
django-version: '4.1'
45-
- python-version: '3.12'
4626
django-version: '4.2'
4727

4828
steps:

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/psf/black
3-
rev: 24.4.2
3+
rev: 24.8.0
44
hooks:
55
- id: black
66
exclude: ^(oauth2_provider/migrations/|tests/migrations/)
@@ -21,7 +21,7 @@ repos:
2121
- id: isort
2222
exclude: ^(oauth2_provider/migrations/|tests/migrations/)
2323
- repo: https://github.com/PyCQA/flake8
24-
rev: 7.1.0
24+
rev: 7.1.1
2525
hooks:
2626
- id: flake8
2727
exclude: ^(oauth2_provider/migrations/|tests/migrations/)

AUTHORS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Dylan Tack
5151
Eduardo Oliveira
5252
Egor Poderiagin
5353
Emanuele Palazzetti
54+
Fazeel Ghafoor
5455
Federico Dolce
5556
Florian Demmer
5657
Frederico Vieira
@@ -83,6 +84,7 @@ Kristian Rune Larsen
8384
Lazaros Toumanidis
8485
Ludwig Hähne
8586
Łukasz Skarżyński
87+
Madison Swain-Bowden
8688
Marcus Sonestedt
8789
Matias Seniquiel
8890
Michael Howitz
@@ -105,6 +107,7 @@ Shaun Stanworth
105107
Sayyid Hamid Mahdavi
106108
Silvano Cerza
107109
Sora Yanai
110+
Sören Wegener
108111
Spencer Carroll
109112
Stéphane Raimbault
110113
Tom Evans

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
## [unreleased]
1818
### Added
19+
* Add migration to include `token_checksum` field in AbstractAccessToken model.
20+
* #1404 Add a new setting `REFRESH_TOKEN_REUSE_PROTECTION`
1921
### Changed
22+
* Update token to TextField from CharField with 255 character limit and SHA-256 checksum in AbstractAccessToken model. Removing the 255 character limit enables supporting JWT tokens with additional claims
23+
* Update middleware, validators, and views to use token checksums instead of token for token retrieval and validation.
24+
* #1446 use generic models pk instead of id.
25+
2026
### Deprecated
2127
### Removed
2228
* #1425 Remove deprecated `RedirectURIValidator`, `WildcardSet` per #1345; `validate_logout_request` per #1274
29+
* Remove support for Django versions below 4.2
2330

2431
### Fixed
25-
* now all part of code use pk instead of id for models.
32+
* #1443 Query strings with invalid hex values now raise a SuspiciousOperation exception (in DRF extension) instead of raising a 500 ValueError: Invalid hex encoding in query string.
2633
### Security
2734

2835
## [2.4.0] - 2024-05-13

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Requirements
4444
------------
4545

4646
* Python 3.8+
47-
* Django 3.2, 4.0 (4.0.1+ due to a regression), 4.1, 4.2, or 5.0
47+
* Django 4.2, 5.0 or 5.1
4848
* oauthlib 3.1+
4949

5050
Installation

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Requirements
2222
------------
2323

2424
* Python 3.8+
25-
* Django 3.2, 4.0 (4.0.1+ due to a regression), 4.1, 4.2, or 5.0
25+
* Django 4.2, 5.0 or 5.1
2626
* oauthlib 3.1+
2727

2828
Index

docs/settings.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ The import string of the class (model) representing your refresh tokens. Overwri
185185
this value if you wrote your own implementation (subclass of
186186
``oauth2_provider.models.RefreshToken``).
187187

188+
REFRESH_TOKEN_REUSE_PROTECTION
189+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190+
When this is set to ``True`` (default ``False``), and ``ROTATE_REFRESH_TOKEN`` is used, the server will check
191+
if a previously, already revoked refresh token is used a second time. If it detects a reuse, it will automatically
192+
revoke all related refresh tokens.
193+
A reused refresh token indicates a breach. Since the server can't determine which request came from the legitimate
194+
user and which from an attacker, it will end the session for both. The user is required to perform a new login.
195+
196+
Can be used in combination with ``REFRESH_TOKEN_GRACE_PERIOD_SECONDS``
197+
198+
More details at https://datatracker.ietf.org/doc/html/draft-ietf-oauth-security-topics-29#name-recommendations
199+
188200
ROTATE_REFRESH_TOKEN
189201
~~~~~~~~~~~~~~~~~~~~
190202
When is set to ``True`` (default) a new refresh token is issued to the client when the client refreshes an access token.

oauth2_provider/contrib/rest_framework/authentication.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import OrderedDict
22

3+
from django.core.exceptions import SuspiciousOperation
34
from rest_framework.authentication import BaseAuthentication
45

56
from ...oauth2_backends import get_oauthlib_core
@@ -23,10 +24,18 @@ def authenticate(self, request):
2324
Returns two-tuple of (user, token) if authentication succeeds,
2425
or None otherwise.
2526
"""
27+
if request is None:
28+
return None
2629
oauthlib_core = get_oauthlib_core()
27-
valid, r = oauthlib_core.verify_request(request, scopes=[])
28-
if valid:
29-
return r.user, r.access_token
30+
try:
31+
valid, r = oauthlib_core.verify_request(request, scopes=[])
32+
except ValueError as error:
33+
if str(error) == "Invalid hex encoding in query string.":
34+
raise SuspiciousOperation(error)
35+
raise
36+
else:
37+
if valid:
38+
return r.user, r.access_token
3039
request.oauth2_error = getattr(r, "oauth2_error", {})
3140
return None
3241

oauth2_provider/middleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hashlib
12
import logging
23

34
from django.contrib.auth import authenticate
@@ -55,7 +56,8 @@ def __call__(self, request):
5556
tokenstring = authheader.split()[1]
5657
AccessToken = get_access_token_model()
5758
try:
58-
token = AccessToken.objects.get(token=tokenstring)
59+
token_checksum = hashlib.sha256(tokenstring.encode("utf-8")).hexdigest()
60+
token = AccessToken.objects.get(token_checksum=token_checksum)
5961
request.access_token = token
6062
except AccessToken.DoesNotExist as e:
6163
log.exception(e)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 5.2 on 2024-08-09 16:40
2+
3+
from django.db import migrations, models
4+
from oauth2_provider.settings import oauth2_settings
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('oauth2_provider', '0010_application_allowed_origins'),
10+
migrations.swappable_dependency(oauth2_settings.REFRESH_TOKEN_MODEL)
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name='refreshtoken',
16+
name='token_family',
17+
field=models.UUIDField(blank=True, editable=False, null=True),
18+
),
19+
]

0 commit comments

Comments
 (0)