From a35d8b1f2e2c338aa1797a977381fa4edfeb72f9 Mon Sep 17 00:00:00 2001 From: Maria Khrustaleva Date: Wed, 20 Mar 2024 23:43:56 +0100 Subject: [PATCH] Reimplement RegisterView::perform_create method to fix missing user auth token creation --- cvat/apps/iam/views.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/cvat/apps/iam/views.py b/cvat/apps/iam/views.py index 39ce9dfac784..701519d00c43 100644 --- a/cvat/apps/iam/views.py +++ b/cvat/apps/iam/views.py @@ -14,11 +14,13 @@ from django.http import HttpResponse from django.views.decorators.http import etag as django_etag from rest_framework.response import Response +from dj_rest_auth.app_settings import api_settings as dj_rest_auth_settings from dj_rest_auth.registration.views import RegisterView +from dj_rest_auth.utils import jwt_encode from dj_rest_auth.views import LoginView from allauth.account import app_settings as allauth_settings from allauth.account.views import ConfirmEmailView -from allauth.account.utils import has_verified_email, send_email_confirmation +from allauth.account.utils import complete_signup, has_verified_email, send_email_confirmation from furl import furl @@ -104,6 +106,34 @@ def get_response_data(self, user): data['key'] = user.auth_token.key return data + # NOTE: we should reimplement this method to fix the following issue: + # In the previous used version of dj-rest-auth 2.2.7, if the REST_SESSION_LOGIN setting was not defined in the settings file, + # the default value specified in the documentation (https://dj-rest-auth.readthedocs.io/en/2.2.7/configuration.html) + # was not applied for some unknown reason, and an authentication token was added to a user. + # With the dj-rest-auth version 5.0.2, there have been changes to how settings are handled, + # and now the default value is properly taken into account. + # However, even with the updated code, it still does not handle the scenario + # of handling two authentication flows simultaneously during registration process. + # Since there is no mention in the dj-rest-auth documentation that session authentication + # cannot be used alongside token authentication (https://dj-rest-auth.readthedocs.io/en/latest/configuration.html), + # and given the login implementation (https://github.com/iMerica/dj-rest-auth/blob/c6b6530eb0bfa5b10fd7b9e955a39301156e49d2/dj_rest_auth/views.py#L69-L75), + # this situation appears to be a bug. + def perform_create(self, serializer): + user = serializer.save(self.request) + if allauth_settings.EMAIL_VERIFICATION != \ + allauth_settings.EmailVerificationMethod.MANDATORY: + if dj_rest_auth_settings.USE_JWT: + self.access_token, self.refresh_token = jwt_encode(user) + elif self.token_model: + dj_rest_auth_settings.TOKEN_CREATOR(self.token_model, user, serializer) + + complete_signup( + self.request._request, user, + allauth_settings.EMAIL_VERIFICATION, + None, + ) + return user + def _etag(etag_func): """ Decorator to support conditional retrieval (or change)