Skip to content

core: Mark impersonation reason field as required in UI and fix status codes #16065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 4 additions & 5 deletions authentik/core/api/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,7 @@ def recovery_email(self, request: Request, pk: int) -> Response:
},
),
responses={
"204": OpenApiResponse(description="Successfully started impersonation"),
"401": OpenApiResponse(description="Access denied"),
204: OpenApiResponse(description="Successfully started impersonation"),
},
)
@action(detail=True, methods=["POST"], permission_classes=[])
Expand All @@ -698,7 +697,7 @@ def impersonate(self, request: Request, pk: int) -> Response:
"User attempted to impersonate without permissions",
user=request.user,
)
return Response(status=401)
return Response(status=403)
if user_to_be.pk == self.request.user.pk:
LOGGER.debug("User attempted to impersonate themselves", user=request.user)
return Response(status=401)
Expand All @@ -707,14 +706,14 @@ def impersonate(self, request: Request, pk: int) -> Response:
"User attempted to impersonate without providing a reason",
user=request.user,
)
return Response(status=401)
raise ValidationError({"reason": _("This field is required.")})

request.session[SESSION_KEY_IMPERSONATE_ORIGINAL_USER] = request.user
request.session[SESSION_KEY_IMPERSONATE_USER] = user_to_be

Event.new(EventAction.IMPERSONATION_STARTED, reason=reason).from_http(request, user_to_be)

return Response(status=201)
return Response(status=204)

@extend_schema(
request=OpenApiTypes.NONE,
Expand Down
8 changes: 4 additions & 4 deletions authentik/core/tests/test_impersonation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_impersonate_global(self):
),
data={"reason": "some reason"},
)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.status_code, 204)

response = self.client.get(reverse("authentik_api:user-me"))
response_body = loads(response.content.decode())
Expand All @@ -80,7 +80,7 @@ def test_impersonate_scoped(self):
),
data={"reason": "some reason"},
)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.status_code, 204)

response = self.client.get(reverse("authentik_api:user-me"))
response_body = loads(response.content.decode())
Expand Down Expand Up @@ -137,10 +137,10 @@ def test_impersonate_reason_required(self):
self.client.force_login(self.user)

response = self.client.post(
reverse("authentik_api:user-impersonate", kwargs={"pk": self.user.pk}),
reverse("authentik_api:user-impersonate", kwargs={"pk": self.other_user.pk}),
data={"reason": ""},
)
self.assertEqual(response.status_code, 401)
self.assertEqual(response.status_code, 400)

response = self.client.get(reverse("authentik_api:user-me"))
response_body = loads(response.content.decode())
Expand Down
2 changes: 0 additions & 2 deletions schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6006,8 +6006,6 @@ paths:
responses:
'204':
description: Successfully started impersonation
'401':
description: Access denied
'400':
content:
application/json:
Expand Down
19 changes: 16 additions & 3 deletions web/src/admin/users/UserImpersonateForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,30 @@ import { MessageLevel } from "#common/messages";
import { Form } from "#elements/forms/Form";
import { APIMessage } from "#elements/messages/Message";

import { CoreApi, ImpersonationRequest } from "@goauthentik/api";
import { AdminApi, CoreApi, ImpersonationRequest } from "@goauthentik/api";

import { msg, str } from "@lit/localize";
import { html, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators.js";
import { customElement, property, state } from "lit/decorators.js";

@customElement("ak-user-impersonate-form")
export class UserImpersonateForm extends Form<ImpersonationRequest> {
@property({ type: Number })
public instancePk?: number;

@state()
private requireReason = false;

async firstUpdated(): Promise<void> {
try {
const settings = await new AdminApi(DEFAULT_CONFIG).adminSettingsRetrieve();
this.requireReason = settings.impersonationRequireReason ?? false;
} catch (e) {
// fallback to reason not required as the backend will still validate it
this.requireReason = false;
}
}

protected override formatAPISuccessMessage(): APIMessage | null {
return {
level: MessageLevel.success,
Expand Down Expand Up @@ -45,7 +58,7 @@ export class UserImpersonateForm extends Form<ImpersonationRequest> {
help=${msg(
"A brief explanation of why you are impersonating the user. This will be included in audit logs.",
)}
required
?required=${this.requireReason}
></ak-text-input>`;
}
}
Expand Down
Loading