-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviews.py
143 lines (120 loc) · 5.12 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
142
143
import tornado.ioloop
import tornado.web
import Settings
import tornado.httpserver
import tornado.httputil
from onelogin.saml2.auth import OneLogin_Saml2_Auth
from onelogin.saml2.settings import OneLogin_Saml2_Settings
from onelogin.saml2.utils import OneLogin_Saml2_Utils
##Global session info
session = {}
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", IndexHandler),
(r"/attrs", AttrsHandler),
(r"/metadata",MetadataHandler),
]
settings = {
"template_path": Settings.TEMPLATE_PATH,
"saml_path": Settings.SAML_PATH,
}
tornado.web.Application.__init__(self, handlers, **settings)
class IndexHandler(tornado.web.RequestHandler):
def post(self):
req = prepare_tornado_request(self.request)
auth = init_saml_auth(req)
attributes = False
paint_logout = False
auth.process_response()
errors = auth.get_errors()
not_auth_warn = not auth.is_authenticated()
if len(errors) == 0:
session['samlUserdata'] = auth.get_attributes()
session['samlNameId'] = auth.get_nameid()
session['samlSessionIndex'] = auth.get_session_index()
self_url = OneLogin_Saml2_Utils.get_self_url(req)
if 'RelayState' in request.forms and self_url != request.forms['RelayState']:
return redirect(request.forms['RelayState'])
if 'samlUserdata' in session:
paint_logout = True
if len(session['samlUserdata']) > 0:
attributes = session['samlUserdata'].items()
self.render('index.html',errors=errors,not_auth_warn=not_auth_warn,attributes=attributes,paint_logout=paint_logout)
def get(self):
req = prepare_tornado_request(self.request)
auth = init_saml_auth(req)
errors = []
not_auth_warn = False
success_slo = False
attributes = False
paint_logout = False
if 'sso' in req['get_data']:
return self.redirect(auth.login())
elif 'sso2' in req['get_data']:
return_to = '%s/attrs' % self.request.host
#return_to = OneLogin_Saml2_Utils.get_self_url(req) + reverse('attrs')
return self.redirect(auth.login(return_to))
elif 'slo' in req['get_data']:
name_id = None
session_index = None
if 'samlNameId' in session:
name_id = session['samlNameId']
if 'samlSessionIndex' in session:
session_index = session['samlSessionIndex']
return self.redirect(auth.logout(name_id=name_id, session_index=session_index))
elif 'sls' in req['get_data']:
dscb = lambda: session.clear() ## clear out the session
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:
return self.redirect(url)
else:
success_slo = True
if 'samlUserdata' in session:
paint_logout = True
if len(session['samlUserdata']) > 0:
attributes = session['samlUserdata'].items()
self.render('index.html',errors=errors,not_auth_warn=not_auth_warn,success_slo=success_slo,attributes=attributes,paint_logout=paint_logout)
class AttrsHandler(tornado.web.RequestHandler):
def get(self):
paint_logout = False
attributes = False
if 'samlUserdata' in session:
paint_logout = True
if len(session['samlUserdata']) > 0:
attributes = session['samlUserdata'].items()
self.render('attrs.html',paint_logout=paint_logout,attributes=attributes)
class MetadataHandler(tornado.web.RequestHandler):
def get(self):
req = prepare_tornado_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
def prepare_tornado_request(request):
result = {
'https': 'on' if request == 'https' else 'off',
'http_host': tornado.httputil.split_host_and_port(request.host)[0],
'script_name': request.path,
'server_port': tornado.httputil.split_host_and_port(request.host)[1],
'get_data': request.arguments,
'post_data': request.arguments,
'query_string': request.query
}
return result
def init_saml_auth(req):
auth = OneLogin_Saml2_Auth(req, custom_base_path=Settings.SAML_PATH)
return auth
if __name__ == "__main__":
app = Application()
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(8000)
tornado.ioloop.IOLoop.instance().start()