-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsso_signedin.py
64 lines (49 loc) · 1.68 KB
/
sso_signedin.py
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
61
62
63
64
from flask import (
request,
session,
redirect,
)
from functools import wraps
from sso_utils import env_var, random_string, jprint
ENVIRONMENT = env_var("ENVIRONMENT", "development")
IS_PROD = ENVIRONMENT.lower().startswith("prod")
DEBUG = not IS_PROD
def get_csrf_session(override_endpoint: str = None):
csrf_value = random_string()
d = {}
if "csrf_values" in session:
d = session["csrf_values"]
d.update({override_endpoint if override_endpoint else request.endpoint: csrf_value})
session["csrf_values"] = d
return csrf_value
def CheckCSRFSession(f):
@wraps(f)
def wrap(*args, **kwds):
valid = True
if "csrf_values" in session and type(session["csrf_values"]) == dict:
ep = request.endpoint
if request.method == "POST" and ep in session["csrf_values"]:
valid = False
try:
from_request = request.form["csrf_form"].strip()
session_value = session["csrf_values"][ep]
if session_value == from_request:
valid = True
session["csrf_values"].pop(ep)
except Exception as e:
print("check_csrf_session:e:", e)
if valid:
return f(*args, **kwds)
else:
return "Forbidden", 403
return wrap
def UserShouldBeSignedIn(f):
@wraps(f)
def wrap(*args, **kwds):
if DEBUG:
jprint({"UserShouldBeSignedIn:session": session})
if "signed_in" in session and session["signed_in"]:
return f(*args, **kwds)
session.clear()
return redirect("/sign-in")
return wrap