authentik.enterprise.stages.password.api

Enterprise password lockout API extensions.

 1"""Enterprise password lockout API extensions."""
 2
 3from django.db import transaction
 4from django.utils.timezone import now
 5from django.utils.translation import gettext as _
 6from drf_spectacular.utils import OpenApiResponse, extend_schema
 7from rest_framework.decorators import action
 8from rest_framework.exceptions import ValidationError
 9from rest_framework.request import Request
10from rest_framework.response import Response
11
12from authentik.core.models import SERVICE_ACCOUNT_TYPES
13from authentik.enterprise.api import enterprise_action
14from authentik.events.models import Event, EventAction
15from authentik.stages.password.models import PasswordDevice
16
17
18class PasswordDeviceLockoutMixin:
19    """Enterprise lock action for password authenticators."""
20
21    @extend_schema(
22        request=None,
23        responses={204: OpenApiResponse(description="Successfully locked authenticator")},
24    )
25    @action(detail=True, methods=["POST"])
26    @enterprise_action
27    @transaction.atomic
28    def lock(self, request: Request, pk: int) -> Response:
29        """Prevent a password authenticator from authenticating."""
30        device = self.get_object()
31        if device.user.type in SERVICE_ACCOUNT_TYPES:
32            raise ValidationError(
33                {"non_field_errors": _("A service account's password cannot be locked.")}
34            )
35        if PasswordDevice.objects.filter(pk=device.pk, locked_at__isnull=True).update(
36            failed_attempts=0, locked_at=now()
37        ):
38            Event.new(
39                EventAction.AUTHENTICATOR_LOCKED,
40                affected_user=device.user,
41                authenticator=device,
42                reason="administrator",
43            ).from_http(request)
44        return Response(status=204)
class PasswordDeviceLockoutMixin:
19class PasswordDeviceLockoutMixin:
20    """Enterprise lock action for password authenticators."""
21
22    @extend_schema(
23        request=None,
24        responses={204: OpenApiResponse(description="Successfully locked authenticator")},
25    )
26    @action(detail=True, methods=["POST"])
27    @enterprise_action
28    @transaction.atomic
29    def lock(self, request: Request, pk: int) -> Response:
30        """Prevent a password authenticator from authenticating."""
31        device = self.get_object()
32        if device.user.type in SERVICE_ACCOUNT_TYPES:
33            raise ValidationError(
34                {"non_field_errors": _("A service account's password cannot be locked.")}
35            )
36        if PasswordDevice.objects.filter(pk=device.pk, locked_at__isnull=True).update(
37            failed_attempts=0, locked_at=now()
38        ):
39            Event.new(
40                EventAction.AUTHENTICATOR_LOCKED,
41                affected_user=device.user,
42                authenticator=device,
43                reason="administrator",
44            ).from_http(request)
45        return Response(status=204)

Enterprise lock action for password authenticators.

@extend_schema(request=None, responses={204: OpenApiResponse(description='Successfully locked authenticator')})
@action(detail=True, methods=['POST'])
@enterprise_action
@transaction.atomic
def lock( self, request: rest_framework.request.Request, pk: int) -> rest_framework.response.Response:
22    @extend_schema(
23        request=None,
24        responses={204: OpenApiResponse(description="Successfully locked authenticator")},
25    )
26    @action(detail=True, methods=["POST"])
27    @enterprise_action
28    @transaction.atomic
29    def lock(self, request: Request, pk: int) -> Response:
30        """Prevent a password authenticator from authenticating."""
31        device = self.get_object()
32        if device.user.type in SERVICE_ACCOUNT_TYPES:
33            raise ValidationError(
34                {"non_field_errors": _("A service account's password cannot be locked.")}
35            )
36        if PasswordDevice.objects.filter(pk=device.pk, locked_at__isnull=True).update(
37            failed_attempts=0, locked_at=now()
38        ):
39            Event.new(
40                EventAction.AUTHENTICATOR_LOCKED,
41                affected_user=device.user,
42                authenticator=device,
43                reason="administrator",
44            ).from_http(request)
45        return Response(status=204)

Prevent a password authenticator from authenticating.