Skip to content

Commit 146e8bf

Browse files
sahaman2ygk
andauthored
models pk instead of models id (#1446)
* use user.pk instead of user.id which allows for a custom model to have a different PK. --------- Co-authored-by: Alan Crosswell <[email protected]>
1 parent 7e13413 commit 146e8bf

File tree

9 files changed

+23
-21
lines changed

9 files changed

+23
-21
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Rustem Saiargaliev
104104
Sandro Rodrigues
105105
Shaheed Haque
106106
Shaun Stanworth
107+
Sayyid Hamid Mahdavi
107108
Silvano Cerza
108109
Sora Yanai
109110
Sören Wegener

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
* #1404 Add a new setting `REFRESH_TOKEN_REUSE_PROTECTION`
2121
### Changed
2222
* 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-
2423
* 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+
2526
### Deprecated
2627
### Removed
2728
* #1425 Remove deprecated `RedirectURIValidator`, `WildcardSet` per #1345; `validate_logout_request` per #1274

oauth2_provider/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
class ApplicationAdmin(admin.ModelAdmin):
22-
list_display = ("id", "name", "user", "client_type", "authorization_grant_type")
22+
list_display = ("pk", "name", "user", "client_type", "authorization_grant_type")
2323
list_filter = ("client_type", "authorization_grant_type", "skip_authorization")
2424
radio_fields = {
2525
"client_type": admin.HORIZONTAL,

oauth2_provider/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def clean(self):
244244
raise ValidationError(_("You cannot use HS256 with public grants or clients"))
245245

246246
def get_absolute_url(self):
247-
return reverse("oauth2_provider:detail", args=[str(self.id)])
247+
return reverse("oauth2_provider:detail", args=[str(self.pk)])
248248

249249
def get_allowed_schemes(self):
250250
"""
@@ -520,7 +520,7 @@ def revoke(self):
520520
self = list(token)[0]
521521

522522
try:
523-
access_token_model.objects.get(id=self.access_token_id).revoke()
523+
access_token_model.objects.get(pk=self.access_token_id).revoke()
524524
except access_token_model.DoesNotExist:
525525
pass
526526
self.access_token = None

oauth2_provider/oauth2_validators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def save_bearer_token(self, token, request, *args, **kwargs):
622622
# from the db while acquiring a lock on it
623623
# We also put it in the "request cache"
624624
refresh_token_instance = RefreshToken.objects.select_for_update().get(
625-
id=refresh_token_instance.id
625+
pk=refresh_token_instance.pk
626626
)
627627
request.refresh_token_instance = refresh_token_instance
628628

@@ -756,7 +756,7 @@ def get_original_scopes(self, refresh_token, request, *args, **kwargs):
756756
rt = request.refresh_token_instance
757757
if not rt.access_token_id:
758758
try:
759-
return AccessToken.objects.get(source_refresh_token_id=rt.id).scope
759+
return AccessToken.objects.get(source_refresh_token_id=rt.pk).scope
760760
except AccessToken.DoesNotExist:
761761
return []
762762
return rt.access_token.scope
@@ -810,9 +810,9 @@ def get_jwt_bearer_token(self, token, token_handler, request):
810810

811811
def get_claim_dict(self, request):
812812
if self._get_additional_claims_is_request_agnostic():
813-
claims = {"sub": lambda r: str(r.user.id)}
813+
claims = {"sub": lambda r: str(r.user.pk)}
814814
else:
815-
claims = {"sub": str(request.user.id)}
815+
claims = {"sub": str(request.user.pk)}
816816

817817
# https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
818818
if self._get_additional_claims_is_request_agnostic():

oauth2_provider/templates/oauth2_provider/application_detail.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ <h3 class="block-center-heading">{{ application.name }}</h3>
4949

5050
<div class="btn-toolbar">
5151
<a class="btn" href="{% url "oauth2_provider:list" %}">{% trans "Go Back" %}</a>
52-
<a class="btn btn-primary" href="{% url "oauth2_provider:update" application.id %}">{% trans "Edit" %}</a>
53-
<a class="btn btn-danger" href="{% url "oauth2_provider:delete" application.id %}">{% trans "Delete" %}</a>
52+
<a class="btn btn-primary" href="{% url "oauth2_provider:update" application.pk %}">{% trans "Edit" %}</a>
53+
<a class="btn btn-danger" href="{% url "oauth2_provider:delete" application.pk %}">{% trans "Delete" %}</a>
5454
</div>
5555
</div>
5656
{% endblock content %}

oauth2_provider/templates/oauth2_provider/application_form.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% load i18n %}
44
{% block content %}
55
<div class="block-center">
6-
<form class="form-horizontal" method="post" action="{% block app-form-action-url %}{% url 'oauth2_provider:update' application.id %}{% endblock app-form-action-url %}">
6+
<form class="form-horizontal" method="post" action="{% block app-form-action-url %}{% url 'oauth2_provider:update' application.pk %}{% endblock app-form-action-url %}">
77
<h3 class="block-center-heading">
88
{% block app-form-title %}
99
{% trans "Edit application" %} {{ application.name }}
@@ -31,7 +31,7 @@ <h3 class="block-center-heading">
3131

3232
<div class="control-group">
3333
<div class="controls">
34-
<a class="btn" href="{% block app-form-back-url %}{% url "oauth2_provider:detail" application.id %}{% endblock app-form-back-url %}">
34+
<a class="btn" href="{% block app-form-back-url %}{% url "oauth2_provider:detail" application.pk %}{% endblock app-form-back-url %}">
3535
{% trans "Go Back" %}
3636
</a>
3737
<button type="submit" class="btn btn-primary">{% trans "Save" %}</button>

oauth2_provider/templates/oauth2_provider/application_list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h3 class="block-center-heading">{% trans "Your applications" %}</h3>
77
{% if applications %}
88
<ul>
99
{% for application in applications %}
10-
<li><a href="{{ application.get_absolute_url }}">{{ application.name }}</a></li>
10+
<li><a href="{% url "oauth2_provider:detail" application.pk %}">{{ application.name }}</a></li>
1111
{% endfor %}
1212
</ul>
1313

tests/test_token_revocation.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_revoke_access_token(self):
5353
response = self.client.post(url, data=data)
5454
self.assertEqual(response.status_code, 200)
5555
self.assertEqual(response.content, b"")
56-
self.assertFalse(AccessToken.objects.filter(id=tok.id).exists())
56+
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())
5757

5858
def test_revoke_access_token_public(self):
5959
public_app = Application(
@@ -101,7 +101,7 @@ def test_revoke_access_token_with_hint(self):
101101
url = reverse("oauth2_provider:revoke-token")
102102
response = self.client.post(url, data=data)
103103
self.assertEqual(response.status_code, 200)
104-
self.assertFalse(AccessToken.objects.filter(id=tok.id).exists())
104+
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())
105105

106106
def test_revoke_access_token_with_invalid_hint(self):
107107
tok = AccessToken.objects.create(
@@ -123,7 +123,7 @@ def test_revoke_access_token_with_invalid_hint(self):
123123
url = reverse("oauth2_provider:revoke-token")
124124
response = self.client.post(url, data=data)
125125
self.assertEqual(response.status_code, 200)
126-
self.assertFalse(AccessToken.objects.filter(id=tok.id).exists())
126+
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())
127127

128128
def test_revoke_refresh_token(self):
129129
tok = AccessToken.objects.create(
@@ -146,9 +146,9 @@ def test_revoke_refresh_token(self):
146146
url = reverse("oauth2_provider:revoke-token")
147147
response = self.client.post(url, data=data)
148148
self.assertEqual(response.status_code, 200)
149-
refresh_token = RefreshToken.objects.filter(id=rtok.id).first()
149+
refresh_token = RefreshToken.objects.filter(pk=rtok.pk).first()
150150
self.assertIsNotNone(refresh_token.revoked)
151-
self.assertFalse(AccessToken.objects.filter(id=rtok.access_token.id).exists())
151+
self.assertFalse(AccessToken.objects.filter(pk=rtok.access_token.pk).exists())
152152

153153
def test_revoke_refresh_token_with_revoked_access_token(self):
154154
tok = AccessToken.objects.create(
@@ -172,8 +172,8 @@ def test_revoke_refresh_token_with_revoked_access_token(self):
172172
response = self.client.post(url, data=data)
173173
self.assertEqual(response.status_code, 200)
174174

175-
self.assertFalse(AccessToken.objects.filter(id=tok.id).exists())
176-
refresh_token = RefreshToken.objects.filter(id=rtok.id).first()
175+
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())
176+
refresh_token = RefreshToken.objects.filter(pk=rtok.pk).first()
177177
self.assertIsNotNone(refresh_token.revoked)
178178

179179
def test_revoke_token_with_wrong_hint(self):
@@ -202,4 +202,4 @@ def test_revoke_token_with_wrong_hint(self):
202202
url = reverse("oauth2_provider:revoke-token")
203203
response = self.client.post(url, data=data)
204204
self.assertEqual(response.status_code, 200)
205-
self.assertFalse(AccessToken.objects.filter(id=tok.id).exists())
205+
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())

0 commit comments

Comments
 (0)