diff --git a/testproject/tests/test_add_mfa.py b/testproject/tests/test_add_mfa.py index d8ecb574..a74197b2 100644 --- a/testproject/tests/test_add_mfa.py +++ b/testproject/tests/test_add_mfa.py @@ -2,7 +2,6 @@ from django.conf import settings from django.contrib.auth import get_user_model -from django.contrib.auth.models import AbstractUser from flaky import flaky from rest_framework.status import HTTP_200_OK, HTTP_400_BAD_REQUEST @@ -12,7 +11,7 @@ from trench.command.create_secret import create_secret_command -User: AbstractUser = get_user_model() +User = get_user_model() @pytest.mark.django_db diff --git a/trench/backends/application.py b/trench/backends/application.py index 879270a3..3bca7168 100644 --- a/trench/backends/application.py +++ b/trench/backends/application.py @@ -1,5 +1,4 @@ from django.contrib.auth import get_user_model -from django.contrib.auth.models import AbstractUser import logging @@ -12,7 +11,7 @@ from trench.settings import trench_settings -User: AbstractUser = get_user_model() +User = get_user_model() class ApplicationMessageDispatcher(AbstractMessageDispatcher): diff --git a/trench/command/authenticate_second_factor.py b/trench/command/authenticate_second_factor.py index e7540a31..f497cd78 100644 --- a/trench/command/authenticate_second_factor.py +++ b/trench/command/authenticate_second_factor.py @@ -1,5 +1,4 @@ from django.contrib.auth import get_user_model -from django.contrib.auth.models import AbstractUser from typing import Type @@ -11,7 +10,7 @@ from trench.utils import get_mfa_model, user_token_generator -User: AbstractUser = get_user_model() +User = get_user_model() class AuthenticateSecondFactorCommand: diff --git a/trench/command/authenticate_user.py b/trench/command/authenticate_user.py index 37d23f7b..b22a82a3 100644 --- a/trench/command/authenticate_user.py +++ b/trench/command/authenticate_user.py @@ -6,7 +6,7 @@ from trench.exceptions import UnauthenticatedError -User: AbstractUser = get_user_model() +User = get_user_model() class AuthenticateUserCommand: diff --git a/trench/responses.py b/trench/responses.py index b7679f61..c2e4ac44 100644 --- a/trench/responses.py +++ b/trench/responses.py @@ -14,7 +14,7 @@ class DispatchResponse(Response): class SuccessfulDispatchResponse(DispatchResponse): def __init__( - self, details: str, status: str = HTTP_200_OK, *args, **kwargs + self, details: str, status: int = HTTP_200_OK, *args, **kwargs ) -> None: super().__init__( data={self._FIELD_DETAILS: details}, status=status, *args, **kwargs @@ -23,7 +23,7 @@ def __init__( class FailedDispatchResponse(DispatchResponse): def __init__( - self, details: str, status: str = HTTP_422_UNPROCESSABLE_ENTITY, *args, **kwargs + self, details: str, status: int = HTTP_422_UNPROCESSABLE_ENTITY, *args, **kwargs ) -> None: super().__init__( data={self._FIELD_DETAILS: details}, status=status, *args, **kwargs @@ -36,7 +36,7 @@ class ErrorResponse(Response): def __init__( self, error: MFAValidationError, - status: str = HTTP_400_BAD_REQUEST, + status: int = HTTP_400_BAD_REQUEST, *args, **kwargs ) -> None: diff --git a/trench/serializers.py b/trench/serializers.py index efb803a1..fa197c8a 100644 --- a/trench/serializers.py +++ b/trench/serializers.py @@ -1,5 +1,4 @@ from django.contrib.auth import get_user_model -from django.contrib.auth.models import AbstractUser from django.db.models import Model from abc import abstractmethod @@ -23,7 +22,7 @@ from trench.utils import available_method_choices, get_mfa_model -User: AbstractUser = get_user_model() +User = get_user_model() class RequestBodyValidator(Serializer): diff --git a/trench/utils.py b/trench/utils.py index fe22ea8f..bc76bd19 100644 --- a/trench/utils.py +++ b/trench/utils.py @@ -1,7 +1,6 @@ from django.apps import apps from django.conf import settings from django.contrib.auth import get_user_model -from django.contrib.auth.models import AbstractUser from django.contrib.auth.tokens import PasswordResetTokenGenerator from django.utils.crypto import constant_time_compare, salted_hmac from django.utils.http import base36_to_int, int_to_base36 @@ -14,7 +13,7 @@ from trench.settings import VERBOSE_NAME, trench_settings -User: AbstractUser = get_user_model() +User = get_user_model() class UserTokenGenerator(PasswordResetTokenGenerator): diff --git a/trench/views/base.py b/trench/views/base.py index cd896413..0849bcab 100644 --- a/trench/views/base.py +++ b/trench/views/base.py @@ -1,5 +1,4 @@ from django.contrib.auth import get_user_model -from django.contrib.auth.models import AbstractUser from django.db.models import QuerySet from django.utils.translation import gettext_lazy as _ @@ -47,7 +46,7 @@ from trench.utils import available_method_choices, get_mfa_model, user_token_generator -User: AbstractUser = get_user_model() +User = get_user_model() class MFAStepMixin(APIView, ABC): @@ -132,8 +131,7 @@ def post(request: Request, method: str) -> Response: serializer = MFAMethodActivationConfirmationValidator( mfa_method_name=method, user=request.user, data=request.data ) - if not serializer.is_valid(): - return Response(status=HTTP_400_BAD_REQUEST, data=serializer.errors) + serializer.is_valid(raise_exception=True) try: backup_codes = activate_mfa_method_command( user_id=request.user.id, @@ -153,8 +151,7 @@ def post(request: Request, method: str) -> Response: serializer = MFAMethodDeactivationValidator( mfa_method_name=method, user=request.user, data=request.data ) - if not serializer.is_valid(): - return Response(status=HTTP_400_BAD_REQUEST, data=serializer.errors) + serializer.is_valid(raise_exception=True) try: deactivate_mfa_method_command( mfa_method_name=method, user_id=request.user.id @@ -174,8 +171,7 @@ def post(request: Request, method: str) -> Response: serializer = MFAMethodBackupCodesGenerationValidator( mfa_method_name=method, user=request.user, data=request.data ) - if not serializer.is_valid(): - return Response(status=HTTP_400_BAD_REQUEST, data=serializer.errors) + serializer.is_valid(raise_exception=True) try: backup_codes = regenerate_backup_codes_for_mfa_method_command( user_id=request.user.id,