diff --git a/apps/authentication/models.py b/apps/authentication/models.py index 4e589a547..e69833ea5 100644 --- a/apps/authentication/models.py +++ b/apps/authentication/models.py @@ -149,6 +149,7 @@ class OnlineUser(AbstractUser): ) bio = models.TextField(_("bio"), max_length=2048, blank=True) # NTNU credentials + # Only set if the user signs in with FEIDE, or by admins ntnu_username = models.CharField( _("NTNU-brukernavn"), max_length=50, blank=True, null=True, unique=True ) diff --git a/apps/dataporten/study/tasks.py b/apps/dataporten/study/tasks.py index 17a3fb6da..35eea94db 100644 --- a/apps/dataporten/study/tasks.py +++ b/apps/dataporten/study/tasks.py @@ -112,8 +112,3 @@ def find_user_study_and_update(user, groups): membership.save() return True, study_name, study_year - - -def set_ntnu_username(user, username): - user.ntnu_username = username - user.save() diff --git a/apps/dataporten/study/tests/task_tests.py b/apps/dataporten/study/tests/task_tests.py index e91969995..5c4cc1b5f 100644 --- a/apps/dataporten/study/tests/task_tests.py +++ b/apps/dataporten/study/tests/task_tests.py @@ -10,7 +10,6 @@ from apps.dataporten.study.tasks import ( fetch_groups_information, find_user_study_and_update, - set_ntnu_username, ) from .course_test_data import ( @@ -74,10 +73,3 @@ def test_find_user_study_and_update_5th_grader(self): self.assertTrue(application.processed) self.assertEqual(len(mail.outbox), 0) - - -class UserUpdatingTestCase(TestCase): - def test_set_ntnu_username(self): - user = G(OnlineUser) - set_ntnu_username(user, "testname") - self.assertEqual(user.ntnu_username, "testname") diff --git a/apps/dataporten/views.py b/apps/dataporten/views.py index d0c52c843..8d10a15a3 100644 --- a/apps/dataporten/views.py +++ b/apps/dataporten/views.py @@ -1,6 +1,7 @@ import logging from urllib.parse import urlparse +import requests from django.conf import settings from django.contrib import messages from django.contrib.auth.decorators import login_required @@ -9,10 +10,10 @@ from oic import rndstr from oic.oauth2 import AuthorizationResponse, ResponseError +from apps.authentication.models import OnlineUser from apps.dataporten.study.tasks import ( fetch_groups_information, find_user_study_and_update, - set_ntnu_username, ) from .client import client_setup @@ -146,12 +147,24 @@ def study_callback(request): # noqa: C901 pass # @ToDo: Register email address. Maybe store it, but ask user to confirm? -> resend auth email + extended_userinfo = requests.get( + "https://api.dataporten.no/userinfo/v1/userinfo", + headers={"Authorization": f"Bearer {access_token}"}, + ).json() + + given_name = extended_userinfo["givenName"][0] + family_name = extended_userinfo["sn"][0] + + user: OnlineUser = request.user + user.ntnu_username = ntnu_username_dataporten + user.first_name = given_name + user.last_name = family_name + user.save() + # Getting information about study of the user groups = fetch_groups_information(access_token) try: - if not request.user.ntnu_username: - set_ntnu_username(request.user, ntnu_username_dataporten) studies_info = find_user_study_and_update(request.user, groups) if not studies_info: diff --git a/onlineweb4/settings/dataporten.py b/onlineweb4/settings/dataporten.py index 286dadc5f..dc8d2f08e 100644 --- a/onlineweb4/settings/dataporten.py +++ b/onlineweb4/settings/dataporten.py @@ -7,6 +7,14 @@ "CLIENT_SECRET": config("OW4_DP_STUDY_CLIENT_SECRET", default=""), "REDIRECT_URI": config("OW4_DP_STUDY_REDIRECT_URI", default=""), "PROVIDER_URL": "https://auth.dataporten.no/oauth/token", - "SCOPES": ["openid", "userid-feide", "profile", "groups", "email"], + # userid-name gives access to extended userinfo https://docs.feide.no/reference/apis/extended_userinfo.html#example + "SCOPES": [ + "openid", + "userid-feide", + "userid-name", + "profile", + "groups", + "email", + ], } } diff --git a/onlineweb4/settings/zappa.py b/onlineweb4/settings/zappa.py index 3501f92dc..2e55d4c03 100644 --- a/onlineweb4/settings/zappa.py +++ b/onlineweb4/settings/zappa.py @@ -36,7 +36,14 @@ "CLIENT_SECRET": env["DP_STUDY_CLIENT_SECRET"], "REDIRECT_URI": config("OW4_DP_STUDY_REDIRECT_URI", default=""), "PROVIDER_URL": "https://auth.dataporten.no/oauth/token", - "SCOPES": ["openid", "userid-feide", "profile", "groups", "email"], + "SCOPES": [ + "openid", + "userid-feide", + "userid-name", + "profile", + "groups", + "email", + ], } } diff --git a/templates/profiles/membership.html b/templates/profiles/membership.html index a15ca975e..cfb971f93 100644 --- a/templates/profiles/membership.html +++ b/templates/profiles/membership.html @@ -40,7 +40,7 @@
For å aktivere ditt medlemskap kreves det at du har knyttet din studentkonto til din profil.
-Gå til Epost-innstillinger og registrer din @stud.ntnu.no epost.
+Dette kan gjøres ved å søke om automatisk medlemskap, uavhengig av om du får innvilget det automatisk.