-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathviews.py
141 lines (122 loc) · 6.21 KB
/
views.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from django.conf import settings
from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
from django.shortcuts import render
from onelogin.saml2.auth import OneLogin_Saml2_Auth
from onelogin.saml2.settings import OneLogin_Saml2_Settings
from onelogin.saml2.utils import OneLogin_Saml2_Utils
def init_saml_auth(req):
auth = OneLogin_Saml2_Auth(req, custom_base_path=settings.SAML_FOLDER)
return auth
def prepare_django_request(request):
# If server is behind proxys or balancers use the HTTP_X_FORWARDED fields
result = {
"https": "on" if request.is_secure() else "off",
"http_host": request.META["HTTP_HOST"],
"script_name": request.META["PATH_INFO"],
"get_data": request.GET.copy(),
# Uncomment if using ADFS as IdP, https://github.com/onelogin/python-saml/pull/144
# 'lowercase_urlencoding': True,
"post_data": request.POST.copy(),
}
return result
def index(request):
req = prepare_django_request(request)
auth = init_saml_auth(req)
errors = []
error_reason = None
not_auth_warn = False
success_slo = False
attributes = False
paint_logout = False
if "sso" in req["get_data"]:
return HttpResponseRedirect(auth.login())
# If AuthNRequest ID need to be stored in order to later validate it, do instead
# sso_built_url = auth.login()
# request.session['AuthNRequestID'] = auth.get_last_request_id()
# return HttpResponseRedirect(sso_built_url)
elif "sso2" in req["get_data"]:
return_to = OneLogin_Saml2_Utils.get_self_url(req) + reverse("attrs")
return HttpResponseRedirect(auth.login(return_to))
elif "slo" in req["get_data"]:
name_id = session_index = name_id_format = name_id_nq = name_id_spnq = None
if "samlNameId" in request.session:
name_id = request.session["samlNameId"]
if "samlSessionIndex" in request.session:
session_index = request.session["samlSessionIndex"]
if "samlNameIdFormat" in request.session:
name_id_format = request.session["samlNameIdFormat"]
if "samlNameIdNameQualifier" in request.session:
name_id_nq = request.session["samlNameIdNameQualifier"]
if "samlNameIdSPNameQualifier" in request.session:
name_id_spnq = request.session["samlNameIdSPNameQualifier"]
return HttpResponseRedirect(auth.logout(name_id=name_id, session_index=session_index, nq=name_id_nq, name_id_format=name_id_format, spnq=name_id_spnq))
# If LogoutRequest ID need to be stored in order to later validate it, do instead
# slo_built_url = auth.logout(name_id=name_id, session_index=session_index)
# request.session['LogoutRequestID'] = auth.get_last_request_id()
# return HttpResponseRedirect(slo_built_url)
elif "acs" in req["get_data"]:
request_id = None
if "AuthNRequestID" in request.session:
request_id = request.session["AuthNRequestID"]
auth.process_response(request_id=request_id)
errors = auth.get_errors()
not_auth_warn = not auth.is_authenticated()
if not errors:
if "AuthNRequestID" in request.session:
del request.session["AuthNRequestID"]
request.session["samlUserdata"] = auth.get_attributes()
request.session["samlNameId"] = auth.get_nameid()
request.session["samlNameIdFormat"] = auth.get_nameid_format()
request.session["samlNameIdNameQualifier"] = auth.get_nameid_nq()
request.session["samlNameIdSPNameQualifier"] = auth.get_nameid_spnq()
request.session["samlSessionIndex"] = auth.get_session_index()
if "RelayState" in req["post_data"] and OneLogin_Saml2_Utils.get_self_url(req) != req["post_data"]["RelayState"]:
# To avoid 'Open Redirect' attacks, before execute the redirection confirm
# the value of the req['post_data']['RelayState'] is a trusted URL.
return HttpResponseRedirect(auth.redirect_to(req["post_data"]["RelayState"]))
elif auth.get_settings().is_debug_active():
error_reason = auth.get_last_error_reason()
elif "sls" in req["get_data"]:
request_id = None
if "LogoutRequestID" in request.session:
request_id = request.session["LogoutRequestID"]
dscb = lambda: request.session.flush()
url = auth.process_slo(request_id=request_id, delete_session_cb=dscb)
errors = auth.get_errors()
if len(errors) == 0:
if url is not None:
# To avoid 'Open Redirect' attacks, before execute the redirection confirm
# the value of the url is a trusted URL
return HttpResponseRedirect(url)
else:
success_slo = True
elif auth.get_settings().is_debug_active():
error_reason = auth.get_last_error_reason()
if "samlUserdata" in request.session:
paint_logout = True
if len(request.session["samlUserdata"]) > 0:
attributes = request.session["samlUserdata"].items()
return render(
request, "index.html", {"errors": errors, "error_reason": error_reason, "not_auth_warn": not_auth_warn, "success_slo": success_slo, "attributes": attributes, "paint_logout": paint_logout}
)
def attrs(request):
paint_logout = False
attributes = False
if "samlUserdata" in request.session:
paint_logout = True
if len(request.session["samlUserdata"]) > 0:
attributes = request.session["samlUserdata"].items()
return render(request, "attrs.html", {"paint_logout": paint_logout, "attributes": attributes})
def metadata(request):
# req = prepare_django_request(request)
# auth = init_saml_auth(req)
# saml_settings = auth.get_settings()
saml_settings = OneLogin_Saml2_Settings(settings=None, custom_base_path=settings.SAML_FOLDER, sp_validation_only=True)
metadata = saml_settings.get_sp_metadata()
errors = saml_settings.validate_metadata(metadata)
if len(errors) == 0:
resp = HttpResponse(content=metadata, content_type="text/xml")
else:
resp = HttpResponseServerError(content=", ".join(errors))
return resp