We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
OW doesnt support use of employee mails
**Describe the bug**
Apperantly OW4 doesnt support use of employee mails, which means the user has issues with verifying their membership if their main email is their employee mail

**Expected behavior**
An user should be able to verify their membership and use OW as long as they are a student too.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `apps/dataporten/views.py`
Content:
```
1 import logging
2 
3 from django.conf import settings
4 from django.contrib import messages
5 from django.contrib.auth.decorators import login_required
6 from django.db import IntegrityError
7 from django.shortcuts import redirect
8 from oic import rndstr
9 from oic.oauth2 import AuthorizationResponse, ResponseError
10 
11 from apps.dataporten.study.tasks import (fetch_groups_information, find_user_study_and_update,
12                                          set_ntnu_username)
13 
14 from .client import client_setup
15 
16 logger = logging.getLogger(__name__)
17 
18 DATAPORTEN_CLIENT_ID = settings.DATAPORTEN.get('STUDY', {}).get('CLIENT_ID')
19 DATAPORTEN_CLIENT_SECRET = settings.DATAPORTEN.get('STUDY', {}).get('CLIENT_SECRET')
20 DATAPORTEN_REDIRECT_URI = settings.DATAPORTEN.get('STUDY', {}).get('REDIRECT_URI')
21 DATAPORTEN_SCOPES = settings.DATAPORTEN.get('STUDY', {}).get('SCOPES')
22 
23 
24 @login_required()
25 def study(request):
26     """This view redirects the user to Dataporten to request authorization for fetching information about the
27     user's groups membership, which can be used to verify eligibility for membership of Online."""
28 
29     # If the user already is a member we can return early. However, if we're in testing, we want to skip the check.
30     if settings.DATAPORTEN.get('STUDY').get('ENABLED') and request.user.is_member:
31         messages.info(request, 'Du er allerede registrert som medlem.')
32         return redirect('profiles_active', active_tab='membership')
33 
34     logger.debug(
35         '{} wants to automatically confirm study programme through Dataporten.'.format(request.user),
36         extra={'user': request.user}
37     )
38 
39     client = client_setup(DATAPORTEN_CLIENT_ID, DATAPORTEN_CLIENT_SECRET)
40 
41     # Generate random values used to verify that it's the same user when in the callback.
42     state = rndstr()
43     nonce = rndstr()
44 
45     request.session['dataporten_study_state'] = state
46     request.session['dataporten_study_nonce'] = nonce
47 
48     args = {
49         'client_id': DATAPORTEN_CLIENT_ID,
50         'response_type': 'code',
51         'scope': DATAPORTEN_SCOPES,
52         'redirect_uri': DATAPORTEN_REDIRECT_URI,
53         'nonce': nonce,
54         'state': state,
55     }
56 
57     logger.debug(
58         'Constructing authorization request and redirecting user to authorize through Dataporten.',
59         extra={'user': request.user}
60     )
61 
62     auth_req = client.construct_AuthorizationRequest(request_args=args)
63     login_url = auth_req.request(client.authorization_endpoint)
64 
65     return redirect(login_url)
66 
67 
68 @login_required()  # noqa: C901
69 def study_callback(request):
70     """This view fetches information from Dataporten to verify the eligibility. This is done by fetching
71     the /me/groups-API from Dataporten and further processing the fetched groups to find group membership.
72 
73     Dataporten Groups API: https://docs.dataporten.no/docs/groups/"""
74     logger.debug('Fetching study programme for user {}'.format(request.user), extra={'user': request.user})
75 
76     client = client_setup(DATAPORTEN_CLIENT_ID, DATAPORTEN_CLIENT_SECRET)
77 
78     queryparams = request.GET.urlencode()
79 
80     try:
81         auth_resp = client.parse_response(AuthorizationResponse, info=queryparams, sformat='urlencoded')
82     except ResponseError:
83         messages.error(request, 'Forespørselen mangler påkrevde felter, vennligst prøv igjen.')
84         return redirect('profiles_active', active_tab='membership')
85 
86     if not request.session.get('dataporten_study_state', '') or \
87             request.session['dataporten_study_state'] != auth_resp['state']:
88         logger.warning('Dataporten state did not equal the one in session!')
89         messages.error(request, 'Verifisering av forespørselen feilet. Vennligst prøv igjen.')
90         return redirect('profiles_active', active_tab='membership')
91 
92     args = {
93         'code': auth_resp['code'],
94         'redirect_uri': DATAPORTEN_REDIRECT_URI,
95     }
96 
97     token_request = client.do_access_token_request(
98         state=auth_resp['state'], request_args=args, authn_method='client_secret_basic',
99     )
100 
101     access_token = token_request.get('access_token')
102 
103     # Do user info request
104     userinfo = client.do_user_info_request(state=auth_resp['state'], behavior='use_authorization_header')
105     ntnu_username_dataporten = userinfo.get('email').split('@')[0]
106     if request.user.ntnu_username and request.user.ntnu_username != ntnu_username_dataporten:
107         logger.warning(
108             '{} tried to authorize, but the registered ntnu_username and the one received from Dataporten differ.'
109             .format(request.user),
110             extra={
111                 'user': request.user,
112                 'ntnu_username__ow4': request.user.ntnu_username,
113                 'ntnu_username__dataporten': ntnu_username_dataporten
114             }
115         )
116         messages.error(
117             request,
118             'Brukernavnet for brukerkontoen brukt til verifisering i Dataporten stemmer ikke overens med '
119             'kontoen du er logget inn med hos Online. Pass på at du er logget inn på din egen konto begge '
120             'steder og prøv igjen.'
121         )
122         return redirect('profiles_active', active_tab='membership')
123     elif not request.user.ntnu_username:
124         pass
125         # @ToDo: Register email address. Maybe store it, but ask user to confirm? -> resend auth email
126 
127     # Getting information about study of the user
128     groups = fetch_groups_information(access_token)
129 
130     try:
131         if not request.user.ntnu_username:
132             set_ntnu_username(request.user, ntnu_username_dataporten)
133         studies_info = find_user_study_and_update(request.user, groups)
134 
135         if not studies_info:
136             logger.warning(
137                 'Dataporten groups do not match groups for informatics',
138                 extra={
139                     'user': request.user,
140                     'groups': groups,
141                 }
142             )
143             messages.error(
144                 request,
145                 'Studieretningen du studerer ved gir ikke medlemskap i Online. ',
146                 'Hvis du mener dette er en feil; ta vennligst kontakt Dotkom slik at vi kan feilsøke prosessen.'
147             )
148             return redirect('profiles_active', active_tab='membership')
149 
150         studies_informatics, study_name, study_year = studies_info
151     except IntegrityError:
152         messages.error(
153             request,
154             'En bruker er allerede knyttet til denne NTNU-kontoen. '
155             'Dersom du har glemt passordet til din andre bruker kan du bruke "glemt passord"-funksjonen.'
156         )
157         return redirect('profiles_active', active_tab='membership')
158 
159     if studies_informatics:
160         messages.success(
161             request,
162             'Bekreftet studieretning som {} i {}. klasse. Dersom dette er feil, '
163             'kontakt dotkom slik at vi kan rette opp og finne ut hva som gikk galt.'
164             .format(study_name, study_year)
165         )
166     else:
167         messages.error(
168             request,
169             'Det ser ikke ut som du tar informatikkfag. Dersom du mener dette er galt kan du sende inn en søknad '
170             'manuelt. Ta gjerne kontakt med dotkom slik at vi kan feilsøke prosessen.'
171         )
172 
173     return redirect('profiles_active', active_tab='membership')
174
```

Path: `apps/dataporten/settings.py`
Content:
```
1 from decouple import config
2 
3 DATAPORTEN = {
4     'STUDY': {
5         'ENABLED': config('OW4_DP_STUDY_ENABLED', cast=bool, default=False),
6         'TESTING': config('OW4_DP_STUDY_TESTING', cast=bool, default=True),
7         'CLIENT_ID': config('OW4_DP_STUDY_CLIENT_ID', default=''),
8         'CLIENT_SECRET': config('OW4_DP_STUDY_CLIENT_SECRET', default=''),
9         'REDIRECT_URI': config('OW4_DP_STUDY_REDIRECT_URI', default=''),
10         'PROVIDER_URL': 'https://auth.dataporten.no/oauth/token',
11         'SCOPES': ['openid', 'userid', 'profile', 'groups', 'email'],
12     }
13 }
14
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/apps/dataporten/settings.py b/apps/dataporten/settings.py
--- a/apps/dataporten/settings.py
+++ b/apps/dataporten/settings.py
@@ -8,6 +8,6 @@
         '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', 'profile', 'groups', 'email'],
+        'SCOPES': ['openid', 'userid-feide', 'profile', 'groups', 'email'],
     }
 }
diff --git a/apps/dataporten/views.py b/apps/dataporten/views.py
--- a/apps/dataporten/views.py
+++ b/apps/dataporten/views.py
@@ -102,7 +102,8 @@
 
     # Do user info request
     userinfo = client.do_user_info_request(state=auth_resp['state'], behavior='use_authorization_header')
-    ntnu_username_dataporten = userinfo.get('email').split('@')[0]
+    # connect-userid_sec format is array with "feide:username@ntnu.no"
+    ntnu_username_dataporten = userinfo.get('connect-userid_sec')[0].split(':')[1].split('@')[0]
     if request.user.ntnu_username and request.user.ntnu_username != ntnu_username_dataporten:
         logger.warning(
             '{} tried to authorize, but the registered ntnu_username and the one received from Dataporten differ.'

{"golden_diff": "diff --git a/apps/dataporten/settings.py b/apps/dataporten/settings.py\n--- a/apps/dataporten/settings.py\n+++ b/apps/dataporten/settings.py\n@@ -8,6 +8,6 @@\n         'CLIENT_SECRET': config('OW4_DP_STUDY_CLIENT_SECRET', default=''),\n         'REDIRECT_URI': config('OW4_DP_STUDY_REDIRECT_URI', default=''),\n         'PROVIDER_URL': 'https://auth.dataporten.no/oauth/token',\n-        'SCOPES': ['openid', 'userid', 'profile', 'groups', 'email'],\n+        'SCOPES': ['openid', 'userid-feide', 'profile', 'groups', 'email'],\n     }\n }\ndiff --git a/apps/dataporten/views.py b/apps/dataporten/views.py\n--- a/apps/dataporten/views.py\n+++ b/apps/dataporten/views.py\n@@ -102,7 +102,8 @@\n \n     # Do user info request\n     userinfo = client.do_user_info_request(state=auth_resp['state'], behavior='use_authorization_header')\n-    ntnu_username_dataporten = userinfo.get('email').split('@')[0]\n+    # connect-userid_sec format is array with \"feide:username@ntnu.no\"\n+    ntnu_username_dataporten = userinfo.get('connect-userid_sec')[0].split(':')[1].split('@')[0]\n     if request.user.ntnu_username and request.user.ntnu_username != ntnu_username_dataporten:\n         logger.warning(\n             '{} tried to authorize, but the registered ntnu_username and the one received from Dataporten differ.'\n", "issue": "OW doesnt support use of employee mails\n**Describe the bug**\r\nApperantly OW4 doesnt support use of employee mails, which means the user has issues with verifying their membership if their main email is their employee mail\r\n\r\n**Expected behavior**\r\nAn user should be able to verify their membership and use OW as long as they are a student too.\r\n\n", "before_files": [{"content": "import logging\n\nfrom django.conf import settings\nfrom django.contrib import messages\nfrom django.contrib.auth.decorators import login_required\nfrom django.db import IntegrityError\nfrom django.shortcuts import redirect\nfrom oic import rndstr\nfrom oic.oauth2 import AuthorizationResponse, ResponseError\n\nfrom apps.dataporten.study.tasks import (fetch_groups_information, find_user_study_and_update,\n                                         set_ntnu_username)\n\nfrom .client import client_setup\n\nlogger = logging.getLogger(__name__)\n\nDATAPORTEN_CLIENT_ID = settings.DATAPORTEN.get('STUDY', {}).get('CLIENT_ID')\nDATAPORTEN_CLIENT_SECRET = settings.DATAPORTEN.get('STUDY', {}).get('CLIENT_SECRET')\nDATAPORTEN_REDIRECT_URI = settings.DATAPORTEN.get('STUDY', {}).get('REDIRECT_URI')\nDATAPORTEN_SCOPES = settings.DATAPORTEN.get('STUDY', {}).get('SCOPES')\n\n\n@login_required()\ndef study(request):\n    \"\"\"This view redirects the user to Dataporten to request authorization for fetching information about the\n    user's groups membership, which can be used to verify eligibility for membership of Online.\"\"\"\n\n    # If the user already is a member we can return early. However, if we're in testing, we want to skip the check.\n    if settings.DATAPORTEN.get('STUDY').get('ENABLED') and request.user.is_member:\n        messages.info(request, 'Du er allerede registrert som medlem.')\n        return redirect('profiles_active', active_tab='membership')\n\n    logger.debug(\n        '{} wants to automatically confirm study programme through Dataporten.'.format(request.user),\n        extra={'user': request.user}\n    )\n\n    client = client_setup(DATAPORTEN_CLIENT_ID, DATAPORTEN_CLIENT_SECRET)\n\n    # Generate random values used to verify that it's the same user when in the callback.\n    state = rndstr()\n    nonce = rndstr()\n\n    request.session['dataporten_study_state'] = state\n    request.session['dataporten_study_nonce'] = nonce\n\n    args = {\n        'client_id': DATAPORTEN_CLIENT_ID,\n        'response_type': 'code',\n        'scope': DATAPORTEN_SCOPES,\n        'redirect_uri': DATAPORTEN_REDIRECT_URI,\n        'nonce': nonce,\n        'state': state,\n    }\n\n    logger.debug(\n        'Constructing authorization request and redirecting user to authorize through Dataporten.',\n        extra={'user': request.user}\n    )\n\n    auth_req = client.construct_AuthorizationRequest(request_args=args)\n    login_url = auth_req.request(client.authorization_endpoint)\n\n    return redirect(login_url)\n\n\n@login_required()  # noqa: C901\ndef study_callback(request):\n    \"\"\"This view fetches information from Dataporten to verify the eligibility. This is done by fetching\n    the /me/groups-API from Dataporten and further processing the fetched groups to find group membership.\n\n    Dataporten Groups API: https://docs.dataporten.no/docs/groups/\"\"\"\n    logger.debug('Fetching study programme for user {}'.format(request.user), extra={'user': request.user})\n\n    client = client_setup(DATAPORTEN_CLIENT_ID, DATAPORTEN_CLIENT_SECRET)\n\n    queryparams = request.GET.urlencode()\n\n    try:\n        auth_resp = client.parse_response(AuthorizationResponse, info=queryparams, sformat='urlencoded')\n    except ResponseError:\n        messages.error(request, 'Foresp\u00f8rselen mangler p\u00e5krevde felter, vennligst pr\u00f8v igjen.')\n        return redirect('profiles_active', active_tab='membership')\n\n    if not request.session.get('dataporten_study_state', '') or \\\n            request.session['dataporten_study_state'] != auth_resp['state']:\n        logger.warning('Dataporten state did not equal the one in session!')\n        messages.error(request, 'Verifisering av foresp\u00f8rselen feilet. Vennligst pr\u00f8v igjen.')\n        return redirect('profiles_active', active_tab='membership')\n\n    args = {\n        'code': auth_resp['code'],\n        'redirect_uri': DATAPORTEN_REDIRECT_URI,\n    }\n\n    token_request = client.do_access_token_request(\n        state=auth_resp['state'], request_args=args, authn_method='client_secret_basic',\n    )\n\n    access_token = token_request.get('access_token')\n\n    # Do user info request\n    userinfo = client.do_user_info_request(state=auth_resp['state'], behavior='use_authorization_header')\n    ntnu_username_dataporten = userinfo.get('email').split('@')[0]\n    if request.user.ntnu_username and request.user.ntnu_username != ntnu_username_dataporten:\n        logger.warning(\n            '{} tried to authorize, but the registered ntnu_username and the one received from Dataporten differ.'\n            .format(request.user),\n            extra={\n                'user': request.user,\n                'ntnu_username__ow4': request.user.ntnu_username,\n                'ntnu_username__dataporten': ntnu_username_dataporten\n            }\n        )\n        messages.error(\n            request,\n            'Brukernavnet for brukerkontoen brukt til verifisering i Dataporten stemmer ikke overens med '\n            'kontoen du er logget inn med hos Online. Pass p\u00e5 at du er logget inn p\u00e5 din egen konto begge '\n            'steder og pr\u00f8v igjen.'\n        )\n        return redirect('profiles_active', active_tab='membership')\n    elif not request.user.ntnu_username:\n        pass\n        # @ToDo: Register email address. Maybe store it, but ask user to confirm? -> resend auth email\n\n    # Getting information about study of the user\n    groups = fetch_groups_information(access_token)\n\n    try:\n        if not request.user.ntnu_username:\n            set_ntnu_username(request.user, ntnu_username_dataporten)\n        studies_info = find_user_study_and_update(request.user, groups)\n\n        if not studies_info:\n            logger.warning(\n                'Dataporten groups do not match groups for informatics',\n                extra={\n                    'user': request.user,\n                    'groups': groups,\n                }\n            )\n            messages.error(\n                request,\n                'Studieretningen du studerer ved gir ikke medlemskap i Online. ',\n                'Hvis du mener dette er en feil; ta vennligst kontakt Dotkom slik at vi kan feils\u00f8ke prosessen.'\n            )\n            return redirect('profiles_active', active_tab='membership')\n\n        studies_informatics, study_name, study_year = studies_info\n    except IntegrityError:\n        messages.error(\n            request,\n            'En bruker er allerede knyttet til denne NTNU-kontoen. '\n            'Dersom du har glemt passordet til din andre bruker kan du bruke \"glemt passord\"-funksjonen.'\n        )\n        return redirect('profiles_active', active_tab='membership')\n\n    if studies_informatics:\n        messages.success(\n            request,\n            'Bekreftet studieretning som {} i {}. klasse. Dersom dette er feil, '\n            'kontakt dotkom slik at vi kan rette opp og finne ut hva som gikk galt.'\n            .format(study_name, study_year)\n        )\n    else:\n        messages.error(\n            request,\n            'Det ser ikke ut som du tar informatikkfag. Dersom du mener dette er galt kan du sende inn en s\u00f8knad '\n            'manuelt. Ta gjerne kontakt med dotkom slik at vi kan feils\u00f8ke prosessen.'\n        )\n\n    return redirect('profiles_active', active_tab='membership')\n", "path": "apps/dataporten/views.py"}, {"content": "from decouple import config\n\nDATAPORTEN = {\n    'STUDY': {\n        'ENABLED': config('OW4_DP_STUDY_ENABLED', cast=bool, default=False),\n        'TESTING': config('OW4_DP_STUDY_TESTING', cast=bool, default=True),\n        'CLIENT_ID': config('OW4_DP_STUDY_CLIENT_ID', default=''),\n        'CLIENT_SECRET': config('OW4_DP_STUDY_CLIENT_SECRET', default=''),\n        'REDIRECT_URI': config('OW4_DP_STUDY_REDIRECT_URI', default=''),\n        'PROVIDER_URL': 'https://auth.dataporten.no/oauth/token',\n        'SCOPES': ['openid', 'userid', 'profile', 'groups', 'email'],\n    }\n}\n", "path": "apps/dataporten/settings.py"}], "after_files": [{"content": "import logging\n\nfrom django.conf import settings\nfrom django.contrib import messages\nfrom django.contrib.auth.decorators import login_required\nfrom django.db import IntegrityError\nfrom django.shortcuts import redirect\nfrom oic import rndstr\nfrom oic.oauth2 import AuthorizationResponse, ResponseError\n\nfrom apps.dataporten.study.tasks import (fetch_groups_information, find_user_study_and_update,\n                                         set_ntnu_username)\n\nfrom .client import client_setup\n\nlogger = logging.getLogger(__name__)\n\nDATAPORTEN_CLIENT_ID = settings.DATAPORTEN.get('STUDY', {}).get('CLIENT_ID')\nDATAPORTEN_CLIENT_SECRET = settings.DATAPORTEN.get('STUDY', {}).get('CLIENT_SECRET')\nDATAPORTEN_REDIRECT_URI = settings.DATAPORTEN.get('STUDY', {}).get('REDIRECT_URI')\nDATAPORTEN_SCOPES = settings.DATAPORTEN.get('STUDY', {}).get('SCOPES')\n\n\n@login_required()\ndef study(request):\n    \"\"\"This view redirects the user to Dataporten to request authorization for fetching information about the\n    user's groups membership, which can be used to verify eligibility for membership of Online.\"\"\"\n\n    # If the user already is a member we can return early. However, if we're in testing, we want to skip the check.\n    if settings.DATAPORTEN.get('STUDY').get('ENABLED') and request.user.is_member:\n        messages.info(request, 'Du er allerede registrert som medlem.')\n        return redirect('profiles_active', active_tab='membership')\n\n    logger.debug(\n        '{} wants to automatically confirm study programme through Dataporten.'.format(request.user),\n        extra={'user': request.user}\n    )\n\n    client = client_setup(DATAPORTEN_CLIENT_ID, DATAPORTEN_CLIENT_SECRET)\n\n    # Generate random values used to verify that it's the same user when in the callback.\n    state = rndstr()\n    nonce = rndstr()\n\n    request.session['dataporten_study_state'] = state\n    request.session['dataporten_study_nonce'] = nonce\n\n    args = {\n        'client_id': DATAPORTEN_CLIENT_ID,\n        'response_type': 'code',\n        'scope': DATAPORTEN_SCOPES,\n        'redirect_uri': DATAPORTEN_REDIRECT_URI,\n        'nonce': nonce,\n        'state': state,\n    }\n\n    logger.debug(\n        'Constructing authorization request and redirecting user to authorize through Dataporten.',\n        extra={'user': request.user}\n    )\n\n    auth_req = client.construct_AuthorizationRequest(request_args=args)\n    login_url = auth_req.request(client.authorization_endpoint)\n\n    return redirect(login_url)\n\n\n@login_required()  # noqa: C901\ndef study_callback(request):\n    \"\"\"This view fetches information from Dataporten to verify the eligibility. This is done by fetching\n    the /me/groups-API from Dataporten and further processing the fetched groups to find group membership.\n\n    Dataporten Groups API: https://docs.dataporten.no/docs/groups/\"\"\"\n    logger.debug('Fetching study programme for user {}'.format(request.user), extra={'user': request.user})\n\n    client = client_setup(DATAPORTEN_CLIENT_ID, DATAPORTEN_CLIENT_SECRET)\n\n    queryparams = request.GET.urlencode()\n\n    try:\n        auth_resp = client.parse_response(AuthorizationResponse, info=queryparams, sformat='urlencoded')\n    except ResponseError:\n        messages.error(request, 'Foresp\u00f8rselen mangler p\u00e5krevde felter, vennligst pr\u00f8v igjen.')\n        return redirect('profiles_active', active_tab='membership')\n\n    if not request.session.get('dataporten_study_state', '') or \\\n            request.session['dataporten_study_state'] != auth_resp['state']:\n        logger.warning('Dataporten state did not equal the one in session!')\n        messages.error(request, 'Verifisering av foresp\u00f8rselen feilet. Vennligst pr\u00f8v igjen.')\n        return redirect('profiles_active', active_tab='membership')\n\n    args = {\n        'code': auth_resp['code'],\n        'redirect_uri': DATAPORTEN_REDIRECT_URI,\n    }\n\n    token_request = client.do_access_token_request(\n        state=auth_resp['state'], request_args=args, authn_method='client_secret_basic',\n    )\n\n    access_token = token_request.get('access_token')\n\n    # Do user info request\n    userinfo = client.do_user_info_request(state=auth_resp['state'], behavior='use_authorization_header')\n    # connect-userid_sec format is array with \"feide:username@ntnu.no\"\n    ntnu_username_dataporten = userinfo.get('connect-userid_sec')[0].split(':')[1].split('@')[0]\n    if request.user.ntnu_username and request.user.ntnu_username != ntnu_username_dataporten:\n        logger.warning(\n            '{} tried to authorize, but the registered ntnu_username and the one received from Dataporten differ.'\n            .format(request.user),\n            extra={\n                'user': request.user,\n                'ntnu_username__ow4': request.user.ntnu_username,\n                'ntnu_username__dataporten': ntnu_username_dataporten\n            }\n        )\n        messages.error(\n            request,\n            'Brukernavnet for brukerkontoen brukt til verifisering i Dataporten stemmer ikke overens med '\n            'kontoen du er logget inn med hos Online. Pass p\u00e5 at du er logget inn p\u00e5 din egen konto begge '\n            'steder og pr\u00f8v igjen.'\n        )\n        return redirect('profiles_active', active_tab='membership')\n    elif not request.user.ntnu_username:\n        pass\n        # @ToDo: Register email address. Maybe store it, but ask user to confirm? -> resend auth email\n\n    # Getting information about study of the user\n    groups = fetch_groups_information(access_token)\n\n    try:\n        if not request.user.ntnu_username:\n            set_ntnu_username(request.user, ntnu_username_dataporten)\n        studies_info = find_user_study_and_update(request.user, groups)\n\n        if not studies_info:\n            logger.warning(\n                'Dataporten groups do not match groups for informatics',\n                extra={\n                    'user': request.user,\n                    'groups': groups,\n                }\n            )\n            messages.error(\n                request,\n                'Studieretningen du studerer ved gir ikke medlemskap i Online. ',\n                'Hvis du mener dette er en feil; ta vennligst kontakt Dotkom slik at vi kan feils\u00f8ke prosessen.'\n            )\n            return redirect('profiles_active', active_tab='membership')\n\n        studies_informatics, study_name, study_year = studies_info\n    except IntegrityError:\n        messages.error(\n            request,\n            'En bruker er allerede knyttet til denne NTNU-kontoen. '\n            'Dersom du har glemt passordet til din andre bruker kan du bruke \"glemt passord\"-funksjonen.'\n        )\n        return redirect('profiles_active', active_tab='membership')\n\n    if studies_informatics:\n        messages.success(\n            request,\n            'Bekreftet studieretning som {} i {}. klasse. Dersom dette er feil, '\n            'kontakt dotkom slik at vi kan rette opp og finne ut hva som gikk galt.'\n            .format(study_name, study_year)\n        )\n    else:\n        messages.error(\n            request,\n            'Det ser ikke ut som du tar informatikkfag. Dersom du mener dette er galt kan du sende inn en s\u00f8knad '\n            'manuelt. Ta gjerne kontakt med dotkom slik at vi kan feils\u00f8ke prosessen.'\n        )\n\n    return redirect('profiles_active', active_tab='membership')\n", "path": "apps/dataporten/views.py"}, {"content": "from decouple import config\n\nDATAPORTEN = {\n    'STUDY': {\n        'ENABLED': config('OW4_DP_STUDY_ENABLED', cast=bool, default=False),\n        'TESTING': config('OW4_DP_STUDY_TESTING', cast=bool, default=True),\n        'CLIENT_ID': config('OW4_DP_STUDY_CLIENT_ID', default=''),\n        'CLIENT_SECRET': config('OW4_DP_STUDY_CLIENT_SECRET', default=''),\n        'REDIRECT_URI': config('OW4_DP_STUDY_REDIRECT_URI', default=''),\n        'PROVIDER_URL': 'https://auth.dataporten.no/oauth/token',\n        'SCOPES': ['openid', 'userid-feide', 'profile', 'groups', 'email'],\n    }\n}\n", "path": "apps/dataporten/settings.py"}]}