diff --git a/src/paperless/serialisers.py b/src/paperless/serialisers.py index 64abd1c18..8a9dd9bb5 100644 --- a/src/paperless/serialisers.py +++ b/src/paperless/serialisers.py @@ -365,3 +365,8 @@ class ApplicationConfigurationSerializer( class Meta: model = ApplicationConfiguration fields = "__all__" + + +class TOTPActivationSerializer(serializers.Serializer[dict[str, str]]): + secret = serializers.CharField() + code = serializers.CharField() diff --git a/src/paperless/tests/test_api_totp_activation.py b/src/paperless/tests/test_api_totp_activation.py new file mode 100644 index 000000000..f213ea21e --- /dev/null +++ b/src/paperless/tests/test_api_totp_activation.py @@ -0,0 +1,39 @@ +import pytest +from rest_framework import status +from rest_framework.test import APIClient + + +class TestTotpView: + def test_options_does_not_fail(self, admin_client: APIClient) -> None: + """ + GIVEN: + - An authenticated client + WHEN: + - OPTIONS is sent to the TOTP endpoint + THEN: + - The request succeeds, DRF's metadata needs a serializer on the view + """ + response = admin_client.options("/api/profile/totp/") + + assert response.status_code == status.HTTP_200_OK + + @pytest.mark.parametrize( + "body", + [{}, {"secret": "abc"}, {"code": "123456"}, {"secret": None, "code": None}], + ) + def test_activation_with_missing_fields_is_a_400( + self, + admin_client: APIClient, + body: dict[str, str | None], + ) -> None: + """ + GIVEN: + - An authenticated client + WHEN: + - TOTP is activated without both the secret and the code + THEN: + - The response is a 400, not a server error + """ + response = admin_client.post("/api/profile/totp/", body, format="json") + + assert response.status_code == status.HTTP_400_BAD_REQUEST diff --git a/src/paperless/views.py b/src/paperless/views.py index 327e27b25..ee2c82123 100644 --- a/src/paperless/views.py +++ b/src/paperless/views.py @@ -51,6 +51,7 @@ from paperless.serialisers import ApplicationConfigurationSerializer from paperless.serialisers import GroupSerializer from paperless.serialisers import PaperlessAuthTokenSerializer from paperless.serialisers import ProfileSerializer +from paperless.serialisers import TOTPActivationSerializer from paperless.serialisers import UserSerializer @@ -326,6 +327,7 @@ class TOTPView(GenericAPIView[Any]): """ permission_classes = [IsAuthenticated] + serializer_class = TOTPActivationSerializer def get(self, request, *args, **kwargs): """ @@ -348,15 +350,18 @@ class TOTPView(GenericAPIView[Any]): """ Validates a TOTP code and activates the TOTP authenticator """ + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + secret = serializer.validated_data["secret"] valid = totp_auth.validate_totp_code( - request.data["secret"], - request.data["code"], + secret, + serializer.validated_data["code"], ) recovery_codes = None if valid: auth = totp_auth.TOTP.activate( request.user, - request.data["secret"], + secret, ).instance signals.authenticator_added.send( sender=Authenticator,