diff --git a/backoffice/.envs/local/.django b/backoffice/.envs/local/.django index a0fd784e..eb740fb1 100644 --- a/backoffice/.envs/local/.django +++ b/backoffice/.envs/local/.django @@ -3,6 +3,8 @@ USE_DOCKER=yes IPYTHONDIR=/app/.ipython + + # Redis # ------------------------------------------------------------------------------ REDIS_URL=redis://redis:6379/0 diff --git a/backoffice/backoffice/users/adapters.py b/backoffice/backoffice/users/adapters.py index 6a8b8c3a..cdd83f6d 100644 --- a/backoffice/backoffice/users/adapters.py +++ b/backoffice/backoffice/users/adapters.py @@ -23,6 +23,21 @@ def is_open_for_signup( self, request: HttpRequest, sociallogin: SocialLogin ) -> bool: return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True) + + from django.http import HttpResponseRedirect + def pre_social_login(self, request, sociallogin): + print("PRE SOCIAL SIGNUP") + print(str(request.user)) + print(str(sociallogin.user)) + + + # Extract email from the social login data + email = sociallogin.account.extra_data.get('email') + + # If email is missing, redirect to the 'fill-email' page + if not email: + request.session['sociallogin'] = sociallogin.serialize() # Store social login data in session + return HttpResponseRedirect(reverse('fill_email')) def populate_user( self, @@ -35,6 +50,7 @@ def populate_user( See: https://django-allauth.readthedocs.io/en/latest/advanced.html?#creating-and-populating-user-instances """ + print("adapter is adapting") user = sociallogin.user if name := data.get("name"): user.name = name diff --git a/backoffice/backoffice/users/api/views.py b/backoffice/backoffice/users/api/views.py index 18a351ff..2b7eb20b 100644 --- a/backoffice/backoffice/users/api/views.py +++ b/backoffice/backoffice/users/api/views.py @@ -36,14 +36,20 @@ def me(self, request): class OrcidLogin(SocialLoginView): adapter_class = OrcidOAuth2Adapter client_class = OAuth2Client - callback_url = "http://localhost:8000/api/v1/auth/google/callback/" + callback_url = "http://localhost:8000/api/oauth/authorized/orcid/" class OrcidConnect(SocialConnectView): adapter_class = OrcidOAuth2Adapter +from django.views.decorators.csrf import csrf_exempt +from rest_framework.permissions import AllowAny +from django.shortcuts import redirect class OrcidLoginCallback(APIView): + permission_classes = [AllowAny] # Allow access to everyone + + @csrf_exempt # Disable CSRF token check for testing purposes (optional) def get(self, request, *args, **kwargs): """ If you are building a fullstack application (eq. with React app next to Django) @@ -51,13 +57,23 @@ def get(self, request, *args, **kwargs): the JWT tokens there - and store them in the state """ + params = request.GET.urlencode() + + # Redirect to the target view with all parameters + return redirect(f"http://localhost:5000/callback?{params}") + code = request.GET.get("code") if code is None: return Response(status=status.HTTP_400_BAD_REQUEST) # Remember to replace the localhost:8000 with the actual domain name before deployment - token_endpoint_url = urljoin("http://localhost:8000", reverse("orcid_login")) + token_endpoint_url = urljoin("http://localhost:8000", reverse("orcid_login2")) response = requests.post(url=token_endpoint_url, data={"code": code}) + data = response.json() + if data["user"]["email"]=="": + + redirect + return Response(response.json(), status=status.HTTP_200_OK) diff --git a/backoffice/backoffice/users/views.py b/backoffice/backoffice/users/views.py index c64683e0..bfb73b2c 100644 --- a/backoffice/backoffice/users/views.py +++ b/backoffice/backoffice/users/views.py @@ -42,4 +42,22 @@ def get_redirect_url(self): return reverse("users:detail", kwargs={"pk": self.request.user.pk}) +import requests + +from django.http import HttpResponse, JsonResponse +from django.shortcuts import redirect +def orcid_callback(request): + + #url = request.build_absolute_uri('/accounts/orcid/login/callback/')) + #params = request.GET.urlencode() + #return redirect(f"/accounts/orcid/login/callback/?{params}") + return HttpResponse("Send a POST request to see the body.", content_type="text/plain") + + +def success(request): + + return JsonResponse(data={"wow":request.user.email}) + user_redirect_view = UserRedirectView.as_view() + + diff --git a/backoffice/config/settings/base.py b/backoffice/config/settings/base.py index 7f0202f4..0886ac35 100644 --- a/backoffice/config/settings/base.py +++ b/backoffice/config/settings/base.py @@ -95,7 +95,7 @@ "allauth", "allauth.account", "allauth.socialaccount", - "allauth.headless", + #"allauth.headless", "allauth.socialaccount.providers.orcid", "django_celery_beat", "rest_framework", @@ -115,6 +115,7 @@ "SESSION_LOGIN": True, "USE_JWT": True, "JWT_AUTH_COOKIE": "auth", + 'JWT_AUTH_REFRESH_COOKIE': 'my-refresh-token', "JWT_AUTH_HTTPONLY": False, } @@ -137,7 +138,7 @@ # https://docs.djangoproject.com/en/dev/ref/settings/#auth-user-model AUTH_USER_MODEL = "users.User" # https://docs.djangoproject.com/en/dev/ref/settings/#login-redirect-url -LOGIN_REDIRECT_URL = "/accounts/login/success" +LOGIN_REDIRECT_URL = "/accounts/login/success/" # https://docs.djangoproject.com/en/dev/ref/settings/#login-url LOGIN_URL = "account_login" @@ -390,6 +391,7 @@ "client_id": env("ORCID_CLIENT_ID", default=""), "secret": env("ORCID_CLIENT_SECRET", default=""), }, + "BASE_DOMAIN": "sandbox.orcid.org" } } SOCIALACCOUNT_EMAIL_VERIFICATION = False diff --git a/backoffice/config/urls.py b/backoffice/config/urls.py index a1056f91..f323c1cf 100644 --- a/backoffice/config/urls.py +++ b/backoffice/config/urls.py @@ -11,6 +11,7 @@ from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView from backoffice.users.api.views import OrcidConnect, OrcidLogin, OrcidLoginCallback +from backoffice.users.views import success urlpatterns = [ path("", TemplateView.as_view(template_name="pages/home.html"), name="home"), @@ -32,6 +33,7 @@ # API URLS urlpatterns += [ # API base url + path("accounts/login/success/",success), path("api/oauth/authorized/orcid/", OrcidLoginCallback.as_view(), name="orcid_callback"), path("api/", include("config.search_router")), path("api/", include("config.api_router")), @@ -45,10 +47,10 @@ ), path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"), path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"), - path("_allauth/", include("allauth.headless.urls")), + #path("_allauth/", include("allauth.headless.urls")), path("dj-rest-auth/", include("dj_rest_auth.urls")), path("dj-rest-auth/registration/", include("dj_rest_auth.registration.urls")), - path("dj-rest-auth/orcid/", OrcidLogin.as_view(), name="orcid_login"), + path("dj-rest-auth/orcid/", OrcidLogin.as_view(), name="orcid_login2"), path("dj-rest-auth/orcid/connect/", OrcidConnect.as_view(), name="orcid_connect"), ]