-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HYP-186 - Tweaked "public or DBMI user" to not always boot users back…
… to login when their JWT expires
- Loading branch information
Showing
4 changed files
with
42 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters