From 033c9a3bd3ef1ee08633d98d74dfe7404deab740 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 23 Aug 2021 17:33:35 +0200 Subject: [PATCH] core: fix token intent not defaulting correctly Signed-off-by: Jens Langhammer --- authentik/core/api/tokens.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/authentik/core/api/tokens.py b/authentik/core/api/tokens.py index f8e14a2e8..517f230d2 100644 --- a/authentik/core/api/tokens.py +++ b/authentik/core/api/tokens.py @@ -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: