Fix oauth to respond with unhashed token on creation

environments/review-docs-renov-k6e5t2/deployments/15370
Georg Krause 2022-11-08 19:37:05 +00:00 zatwierdzone przez JuniorJPDJ
rodzic 59072e5f00
commit 515b502364
3 zmienionych plików z 19 dodań i 1 usunięć

Wyświetl plik

@ -32,7 +32,7 @@ class CreateApplicationSerializer(serializers.ModelSerializer):
"updated",
"redirect_uris",
]
read_only_fields = ["client_id", "client_secret", "created", "updated"]
read_only_fields = ["client_id", "created", "updated"]
def to_representation(self, obj):
repr = super().to_representation(obj)

Wyświetl plik

@ -1,5 +1,6 @@
import json
import urllib.parse
import secrets
from django import http
from django.utils import timezone
@ -49,6 +50,21 @@ class ApplicationViewSet(
}
}
def create(self, request, *args, **kwargs):
request_data = request.data.copy()
try:
secret = request_data["client_secret"]
except KeyError:
secret = secrets.token_hex(64)
request_data["client_secret"] = secret
serializer = self.get_serializer(data=request_data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
data = serializer.data
data["client_secret"] = secret
return response.Response(data, status=201, headers=headers)
def get_serializer_class(self):
if self.request.method.lower() == "post":
return serializers.CreateApplicationSerializer

Wyświetl plik

@ -19,6 +19,7 @@ def test_apps_post(api_client, db):
assert response.status_code == 201
app = models.Application.objects.get(name=data["name"])
setattr(app, "client_secret", response.data["client_secret"])
assert app.client_type == models.Application.CLIENT_CONFIDENTIAL
assert app.authorization_grant_type == models.Application.GRANT_AUTHORIZATION_CODE
@ -40,6 +41,7 @@ def test_apps_post_logged_in_user(logged_in_api_client, db):
assert response.status_code == 201
app = models.Application.objects.get(name=data["name"])
setattr(app, "client_secret", response.data["client_secret"])
assert app.client_type == models.Application.CLIENT_CONFIDENTIAL
assert app.authorization_grant_type == models.Application.GRANT_AUTHORIZATION_CODE