Skip to content

Commit 44e8b90

Browse files
committed
HYP-186 - Tweaked "public or DBMI user" to not always boot users back to login when their JWT expires
1 parent 3207b69 commit 44e8b90

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

app/contact/views.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
from django.core.mail import EmailMultiAlternatives
1515
from django.shortcuts import render
1616
from django.template.loader import render_to_string
17+
from hypatio.dbmiauthn_services import DBMIAuthn
1718

1819
# Get an instance of a logger
1920
logger = logging.getLogger(__name__)
2021

21-
@public_user_auth_and_jwt
22+
@DBMIAuthn.public_user_auth_and_jwt
2223
def contact_form(request, project_key=None):
2324

2425
# If this is a POST request we need to process the form data.

app/hypatio/dbmiauthn_services.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from functools import wraps
2+
from pyauth0jwt.auth0authenticate import validate_request, jwt_login
3+
from django.conf import settings
4+
from django.contrib import auth
5+
import logging
6+
logger = logging.getLogger(__name__)
7+
8+
9+
class DBMIAuthn:
10+
11+
def public_user_auth_and_jwt(function):
12+
13+
@wraps(function)
14+
def wrap(request, *args, **kwargs):
15+
"""
16+
Here we see if the user is logged in but let them stay on the page if they aren't.
17+
"""
18+
19+
# Validates the JWT and returns its payload if valid.
20+
jwt_payload = validate_request(request)
21+
22+
# If user is logged in, make sure they have a valid JWT
23+
if request.user.is_authenticated and jwt_payload is None:
24+
logger.debug('User ' + request.user.email + ' is authenticated but does not have a valid JWT. Logging them out.')
25+
auth.logout(request)
26+
27+
# User has a JWT session open but not a Django session. Try to start a Django session and continue the request.
28+
if not request.user.is_authenticated and jwt_payload is not None:
29+
jwt_login(request, jwt_payload)
30+
31+
return function(request, *args, **kwargs)
32+
33+
return wrap

app/hypatio/views.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from django.shortcuts import render
22

3-
from pyauth0jwt.auth0authenticate import public_user_auth_and_jwt
3+
from hypatio.dbmiauthn_services import DBMIAuthn
44

5-
6-
@public_user_auth_and_jwt
5+
@DBMIAuthn.public_user_auth_and_jwt
76
def index(request, template_name='index.html'):
87
"""
98
Homepage for the DBMI Portal

app/projects/views.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from profile.forms import RegistrationForm
1919

20-
from pyauth0jwt.auth0authenticate import public_user_auth_and_jwt
20+
from hypatio.dbmiauthn_services import DBMIAuthn
2121
from pyauth0jwt.auth0authenticate import user_auth_and_jwt
2222

2323
from projects.models import AGREEMENT_FORM_TYPE_EXTERNAL_LINK
@@ -72,7 +72,7 @@ def signed_agreement_form(request):
7272
return HttpResponse(403)
7373

7474

75-
@public_user_auth_and_jwt
75+
@DBMIAuthn.public_user_auth_and_jwt
7676
def list_data_projects(request, template_name='projects/list-data-projects.html'):
7777
"""
7878
Displays all visible data projects.
@@ -84,7 +84,7 @@ def list_data_projects(request, template_name='projects/list-data-projects.html'
8484
return render(request, template_name, context=context)
8585

8686

87-
@public_user_auth_and_jwt
87+
@DBMIAuthn.public_user_auth_and_jwt
8888
def list_data_challenges(request, template_name='projects/list-data-challenges.html'):
8989
"""
9090
Displays all visible data challenges.
@@ -96,7 +96,7 @@ def list_data_challenges(request, template_name='projects/list-data-challenges.h
9696
return render(request, template_name, context=context)
9797

9898

99-
@public_user_auth_and_jwt
99+
@DBMIAuthn.public_user_auth_and_jwt
100100
def list_software_projects(request, template_name='projects/list-software-projects.html'):
101101
"""
102102
Displays all visible software projects.
@@ -108,7 +108,7 @@ def list_software_projects(request, template_name='projects/list-software-projec
108108
return render(request, template_name, context=context)
109109

110110

111-
@method_decorator(public_user_auth_and_jwt, name='dispatch')
111+
@method_decorator(DBMIAuthn.public_user_auth_and_jwt, name='dispatch')
112112
class DataProjectView(TemplateView):
113113
"""
114114
Builds and renders screens related to DataProject signup and participation.

0 commit comments

Comments
 (0)