Skip to content

Commit 4de971a

Browse files
authored
Merge branch 'master' into honor-database-assigment-from-router
2 parents f78ba9b + 62508b4 commit 4de971a

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.6.2
3+
rev: v0.6.3
44
hooks:
55
- id: ruff
66
args: [ --fix ]

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,4 @@ pySilver
120120
Wouter Klein Heerenbrink
121121
Yaroslav Halchenko
122122
Yuri Savin
123+
Miriam Forner

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626
* Transactions wrapping writes of the Tokens now rely on Django's database routers to determine the correct
2727
database to use instead of assuming that 'default' is the correct one.
2828
* Bump oauthlib version to 3.2.0 and above
29+
* Update the OAuth2Validator's invalidate_authorization_code method to return an InvalidGrantError if the associated grant does not exist.
2930

3031
### Deprecated
3132
### Removed

oauth2_provider/oauth2_validators.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from jwcrypto import jws, jwt
2525
from jwcrypto.common import JWException
2626
from jwcrypto.jwt import JWTExpired
27-
from oauthlib.oauth2.rfc6749 import utils
27+
from oauthlib.oauth2.rfc6749 import errors, utils
2828
from oauthlib.openid import RequestValidator
2929

3030
from .exceptions import FatalClientError
@@ -318,10 +318,15 @@ def confirm_redirect_uri(self, client_id, code, redirect_uri, client, *args, **k
318318

319319
def invalidate_authorization_code(self, client_id, code, request, *args, **kwargs):
320320
"""
321-
Remove the temporary grant used to swap the authorization token
321+
Remove the temporary grant used to swap the authorization token.
322+
323+
:raises: InvalidGrantError if the grant does not exist.
322324
"""
323-
grant = Grant.objects.get(code=code, application=request.client)
324-
grant.delete()
325+
try:
326+
grant = Grant.objects.get(code=code, application=request.client)
327+
grant.delete()
328+
except Grant.DoesNotExist:
329+
raise errors.InvalidGrantError(request=request)
325330

326331
def validate_client_id(self, client_id, request, *args, **kwargs):
327332
"""

tests/test_oauth2_validators.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
from django.utils import timezone
99
from jwcrypto import jwt
1010
from oauthlib.common import Request
11+
from oauthlib.oauth2.rfc6749 import errors as rfc6749_errors
1112

1213
from oauth2_provider.exceptions import FatalClientError
13-
from oauth2_provider.models import get_access_token_model, get_application_model, get_refresh_token_model
14+
from oauth2_provider.models import (
15+
get_access_token_model,
16+
get_application_model,
17+
get_grant_model,
18+
get_refresh_token_model,
19+
)
1420
from oauth2_provider.oauth2_backends import get_oauthlib_core
1521
from oauth2_provider.oauth2_validators import OAuth2Validator
1622

@@ -30,6 +36,7 @@
3036
UserModel = get_user_model()
3137
Application = get_application_model()
3238
AccessToken = get_access_token_model()
39+
Grant = get_grant_model()
3340
RefreshToken = get_refresh_token_model()
3441

3542
CLEARTEXT_SECRET = "1234567890abcdefghijklmnopqrstuvwxyz"
@@ -580,3 +587,14 @@ def test_validate_id_token_bad_token_no_aud(oauth2_settings, mocker, oidc_key):
580587
validator = OAuth2Validator()
581588
status = validator.validate_id_token(token.serialize(), ["openid"], mocker.sentinel.request)
582589
assert status is False
590+
591+
592+
@pytest.mark.django_db
593+
def test_invalidate_authorization_token_returns_invalid_grant_error_when_grant_does_not_exist():
594+
client_id = "123"
595+
code = "12345"
596+
request = Request("/")
597+
assert Grant.objects.all().count() == 0
598+
with pytest.raises(rfc6749_errors.InvalidGrantError):
599+
validator = OAuth2Validator()
600+
validator.invalidate_authorization_code(client_id=client_id, code=code, request=request)

0 commit comments

Comments
 (0)