aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/session.py
blob: 39f6959c8afbff2574df5d653f56a0943d640e6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Handle any GN3 sessions."""
from functools import wraps
from datetime import datetime, timezone, timedelta

from flask import flash, request, session, url_for, redirect

__SESSION_KEY__ = "GN::AUTH::session_details"

def __session_expired__():
    """Check whether the session has expired."""
    return datetime.now(tz=timezone.utc) >= session[__SESSION_KEY__]["expires"]

def logged_in(func):
    """Verify the user is logged in."""
    @wraps(func)
    def __logged_in__(*args, **kwargs):
        if bool(session.get(__SESSION_KEY__)) and not __session_expired__():
            return func(*args, **kwargs)
        flash("You need to be logged in to access that page.", "alert-danger")
        return redirect(url_for(
            "oauth2.admin.login", next=request.url_rule.endpoint))
    return __logged_in__

def session_info():
    """Retrieve the session information."""
    return session.get(__SESSION_KEY__, False)

def session_user():
    """Retrieve session user."""
    info = session_info()
    return info and info["user"]

def clear_session_info():
    """Clear any session info."""
    try:
        session.pop(__SESSION_KEY__)
    except KeyError as _keyerr:
        pass

def session_expired() -> bool:
    """
    Check whether the session has expired. Will always return `True` if no
    session currently exists.
    """
    if bool(session.get(__SESSION_KEY__)):
        now = datetime.now(tz=timezone.utc)
        return now >= session[__SESSION_KEY__]["expires"]
    return True

def update_expiry(minutes: int = 10) -> bool:
    """Update the session expiry and return a boolean indicating success."""
    if not session_expired():
        now = datetime.now(tz=timezone.utc)
        session[__SESSION_KEY__]["expires"] = now + timedelta(minutes=minutes)
        return True
    return False

def update_session_info(**info):
    """Update the session information."""
    session[__SESSION_KEY__] = info