core: fix token intent not defaulting correctly

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-08-23 17:33:35 +02:00
parent 09e3d616e9
commit 033c9a3bd3
1 changed files with 6 additions and 4 deletions

View File

@ -1,4 +1,5 @@
"""Tokens API Viewset"""
from typing import Any
from django.http.response import Http404
from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework.decorators import action
@ -23,11 +24,12 @@ class TokenSerializer(ManagedSerializer, ModelSerializer):
user = UserSerializer(required=False)
def validate_intent(self, value: str) -> str:
def validate(self, data: dict[Any, str]) -> dict[Any, str]:
"""Ensure only API or App password tokens are created."""
if value not in [TokenIntents.INTENT_API, TokenIntents.INTENT_APP_PASSWORD]:
raise ValidationError(f"Invalid intent {value}")
return value
data.setdefault("intent", TokenIntents.INTENT_API)
if data.get("intent") not in [TokenIntents.INTENT_API, TokenIntents.INTENT_APP_PASSWORD]:
raise ValidationError(f"Invalid intent {data.get('intent')}")
return data
class Meta: