Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HYP-186 - Tweaked "public or DBMI user" to not always boot users back to login when their JWT expires #383

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/contact/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
from django.core.mail import EmailMultiAlternatives
from django.shortcuts import render
from django.template.loader import render_to_string
from hypatio.dbmiauthn_services import DBMIAuthn

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

@public_user_auth_and_jwt
@DBMIAuthn.public_user_auth_and_jwt
def contact_form(request, project_key=None):

# If this is a POST request we need to process the form data.
Expand Down
33 changes: 33 additions & 0 deletions app/hypatio/dbmiauthn_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from functools import wraps
from pyauth0jwt.auth0authenticate import validate_request, jwt_login
from django.conf import settings
from django.contrib import auth
import logging
logger = logging.getLogger(__name__)


class DBMIAuthn:

def public_user_auth_and_jwt(function):

@wraps(function)
def wrap(request, *args, **kwargs):
"""
Here we see if the user is logged in but let them stay on the page if they aren't.
"""

# Validates the JWT and returns its payload if valid.
jwt_payload = validate_request(request)

# If user is logged in, make sure they have a valid JWT
if request.user.is_authenticated and jwt_payload is None:
logger.debug('User ' + request.user.email + ' is authenticated but does not have a valid JWT. Logging them out.')
auth.logout(request)

# User has a JWT session open but not a Django session. Try to start a Django session and continue the request.
if not request.user.is_authenticated and jwt_payload is not None:
jwt_login(request, jwt_payload)

return function(request, *args, **kwargs)

return wrap
5 changes: 2 additions & 3 deletions app/hypatio/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.shortcuts import render

from pyauth0jwt.auth0authenticate import public_user_auth_and_jwt
from hypatio.dbmiauthn_services import DBMIAuthn


@public_user_auth_and_jwt
@DBMIAuthn.public_user_auth_and_jwt
def index(request, template_name='index.html'):
"""
Homepage for the DBMI Portal
Expand Down
10 changes: 5 additions & 5 deletions app/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from profile.forms import RegistrationForm

from pyauth0jwt.auth0authenticate import public_user_auth_and_jwt
from hypatio.dbmiauthn_services import DBMIAuthn
from pyauth0jwt.auth0authenticate import user_auth_and_jwt

from projects.models import AGREEMENT_FORM_TYPE_EXTERNAL_LINK
Expand Down Expand Up @@ -72,7 +72,7 @@ def signed_agreement_form(request):
return HttpResponse(403)


@public_user_auth_and_jwt
@DBMIAuthn.public_user_auth_and_jwt
def list_data_projects(request, template_name='projects/list-data-projects.html'):
"""
Displays all visible data projects.
Expand All @@ -84,7 +84,7 @@ def list_data_projects(request, template_name='projects/list-data-projects.html'
return render(request, template_name, context=context)


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


@public_user_auth_and_jwt
@DBMIAuthn.public_user_auth_and_jwt
def list_software_projects(request, template_name='projects/list-software-projects.html'):
"""
Displays all visible software projects.
Expand All @@ -108,7 +108,7 @@ def list_software_projects(request, template_name='projects/list-software-projec
return render(request, template_name, context=context)


@method_decorator(public_user_auth_and_jwt, name='dispatch')
@method_decorator(DBMIAuthn.public_user_auth_and_jwt, name='dispatch')
class DataProjectView(TemplateView):
"""
Builds and renders screens related to DataProject signup and participation.
Expand Down