Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
core: allow creating tokens for other user without superuser
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
  • Loading branch information
BeryJu committed Aug 18, 2025
commit 1b99ec8ac50d8ca7c91583b2419289e1b72f7225
7 changes: 6 additions & 1 deletion authentik/core/api/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import Any

from django.http import HttpRequest
from django.utils.timezone import now
from drf_spectacular.utils import OpenApiResponse, extend_schema, inline_serializer
from guardian.shortcuts import assign_perm, get_anonymous_user
Expand Down Expand Up @@ -46,6 +47,11 @@ def validate_user(self, user: User):
if self.instance and self.instance.user_id:
if user.pk != self.instance.user_id:
raise ValidationError("User cannot be changed")
request: HttpRequest = self.context.get("request")
if not request.user.has_perm("change_user", user) and not request.user.has_perm(
"authentik_core.change_user"
):
Comment on lines +51 to +53

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check separately, has_perm checks both.

Suggested change
if not request.user.has_perm("change_user", user) and not request.user.has_perm(
"authentik_core.change_user"
):
if not request.user.has_perm("authentik_core.change_user", user):

raise ValidationError("Cannot create token for this user")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors like this should not be raised in validate_* functions, but earlier in the process.

return user

def validate(self, attrs: dict[Any, str]) -> dict[Any, str]:
Expand Down Expand Up @@ -147,7 +153,6 @@ def get_queryset(self):
def perform_create(self, serializer: TokenSerializer):
if not self.request.user.is_superuser:
instance = serializer.save(
user=self.request.user,
expiring=self.request.user.attributes.get(USER_ATTRIBUTE_TOKEN_EXPIRING, True),
)
assign_perm("authentik_core.view_token_key", self.request.user, instance)
Expand Down
Loading