diff --git a/backend/ciso_assistant/urls.py b/backend/ciso_assistant/urls.py index c9cbb2067..dd80af4b5 100644 --- a/backend/ciso_assistant/urls.py +++ b/backend/ciso_assistant/urls.py @@ -22,18 +22,18 @@ # beware of the order of url patterns, this can change de behavior in case of multiple matches and avoid giving identical paths that could cause conflicts urlpatterns = [ - path("api/", include("core.urls")), - path("serdes/", include("serdes.urls")), - path("i18n/", include("django.conf.urls.i18n")), - path("api/schema/", SpectacularAPIView.as_view(), name="schema"), + path("", SpectacularAPIView.as_view(), name="schema"), path( - "api/schema/swagger/", + "schema/swagger/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger", ), path( - "api/schema/redoc/", + "schema/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc", ), + path("api/", include("core.urls")), + path("serdes/", include("serdes.urls")), + path("i18n/", include("django.conf.urls.i18n")), ] diff --git a/backend/iam/knox/__init__.py b/backend/iam/knox/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/backend/iam/knox/views.py b/backend/iam/knox/views.py deleted file mode 100644 index 85e890bf8..000000000 --- a/backend/iam/knox/views.py +++ /dev/null @@ -1,18 +0,0 @@ -from django.contrib.auth import login - -from rest_framework import permissions -from rest_framework.authtoken.serializers import AuthTokenSerializer -from knox.views import LoginView as KnoxLoginView -from iam.serializers import LoginSerializer - - -class LoginView(KnoxLoginView): - permission_classes = (permissions.AllowAny,) - serializer_class = LoginSerializer - - def post(self, request, format=None): - serializer = AuthTokenSerializer(data=request.data) - serializer.is_valid(raise_exception=True) - user = serializer.validated_data["user"] - login(request, user) - return super(LoginView, self).post(request, format=None) diff --git a/backend/iam/urls.py b/backend/iam/urls.py index a9df0a2dd..37ac4662d 100644 --- a/backend/iam/urls.py +++ b/backend/iam/urls.py @@ -2,12 +2,18 @@ from core.views import FirstConnexionPasswordConfirmView -from .views import * -from .knox.views import LoginView as KnoxLoginView +from .views import ( + LoginView, + ChangePasswordView, + CurrentUserView, + PasswordResetView, + ResetPasswordConfirmView, + SetPasswordView, +) import knox.views as knox_views urlpatterns = [ - path(r"login/", KnoxLoginView.as_view(), name="knox_login"), + path(r"login/", LoginView.as_view(), name="knox_login"), path(r"logout/", knox_views.LogoutView.as_view(), name="knox_logout"), path(r"logoutall/", knox_views.LogoutAllView.as_view(), name="knox_logoutall"), path("current-user/", CurrentUserView.as_view(), name="current-user"), diff --git a/backend/iam/views.py b/backend/iam/views.py index a3e91e4fc..970d31af4 100644 --- a/backend/iam/views.py +++ b/backend/iam/views.py @@ -14,6 +14,8 @@ ) from rest_framework.settings import api_settings from ciso_assistant.settings import EMAIL_HOST, EMAIL_HOST_RESCUE +from rest_framework.authtoken.serializers import AuthTokenSerializer +from knox.views import LoginView as KnoxLoginView from .serializers import ( ChangePasswordSerializer, @@ -31,37 +33,16 @@ User = get_user_model() -class LoginView(views.APIView): - permission_classes = [permissions.AllowAny] +class LoginView(KnoxLoginView): + permission_classes = (permissions.AllowAny,) + serializer_class = LoginSerializer - @method_decorator(ensure_csrf_cookie) - def post(self, request) -> Response: - serializer = LoginSerializer( - data=self.request.data, - context={"request": self.request}, - ) - try: - serializer.is_valid(raise_exception=True) - user = serializer.validated_data["user"] - login(request, user) - logger.info("login succesful", user=user) - except serializers.ValidationError as e: - logger.warning( - "login attempt failed", - error=e, - username=request.data.get("username"), - ) - if isinstance(e.detail, dict): - return Response(data={**e.detail}, status=HTTP_401_UNAUTHORIZED) - else: - return Response( - data={api_settings.NON_FIELD_ERRORS_KEY: [e.detail]}, - status=HTTP_401_UNAUTHORIZED, - ) - - user.first_login = False - user.save() - return Response(None, status=HTTP_202_ACCEPTED) + def post(self, request, format=None): + serializer = AuthTokenSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + user = serializer.validated_data["user"] + login(request, user) + return super(LoginView, self).post(request, format=None) class LogoutView(views.APIView):