2018-11-16 12:08:37 +00:00
|
|
|
"""passbook decorators"""
|
|
|
|
from time import time as timestamp
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils.functional import wraps
|
|
|
|
from django.utils.http import urlencode
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
RE_AUTH_KEY = getattr(settings, "RE_AUTH_KEY", "passbook_require_re_auth_done")
|
|
|
|
RE_AUTH_MARGAIN = getattr(settings, "RE_AUTH_MARGAIN", 300)
|
2018-11-16 12:08:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def reauth_required(view_function):
|
|
|
|
"""Decorator to force a re-authentication before continuing"""
|
|
|
|
|
|
|
|
@wraps(view_function)
|
|
|
|
def wrap(*args, **kwargs):
|
|
|
|
"""check if user just authenticated or not"""
|
|
|
|
|
|
|
|
request = args[0] if args else None
|
|
|
|
# Check if user is authenticated at all
|
|
|
|
if not request or not request.user or not request.user.is_authenticated:
|
2019-12-31 11:51:16 +00:00
|
|
|
return redirect(reverse("account-login"))
|
2018-11-16 12:08:37 +00:00
|
|
|
|
|
|
|
now = timestamp()
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
if RE_AUTH_KEY in request.session and request.session[RE_AUTH_KEY] < (
|
|
|
|
now - RE_AUTH_MARGAIN
|
|
|
|
):
|
2018-11-16 12:08:37 +00:00
|
|
|
# Timestamp in session but expired
|
|
|
|
del request.session[RE_AUTH_KEY]
|
|
|
|
|
|
|
|
if RE_AUTH_KEY not in request.session:
|
|
|
|
# Timestamp not in session, force user to reauth
|
2019-12-31 11:51:16 +00:00
|
|
|
return redirect(
|
|
|
|
reverse("account-reauth") + "?" + urlencode({"next": request.path})
|
|
|
|
)
|
|
|
|
|
|
|
|
if (
|
|
|
|
RE_AUTH_KEY in request.session
|
|
|
|
and request.session[RE_AUTH_KEY] >= (now - RE_AUTH_MARGAIN)
|
|
|
|
and request.session[RE_AUTH_KEY] <= now
|
|
|
|
):
|
2018-11-16 12:08:37 +00:00
|
|
|
# Timestamp in session and valid
|
|
|
|
return view_function(*args, **kwargs)
|
|
|
|
|
|
|
|
# This should never be reached, just return False
|
|
|
|
return False # pragma: no cover
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2018-11-16 12:08:37 +00:00
|
|
|
return wrap
|