This repository was archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathidproxy_server.py
More file actions
165 lines (143 loc) · 6.3 KB
/
idproxy_server.py
File metadata and controls
165 lines (143 loc) · 6.3 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from uuid import uuid4
from idproxy.client.sp.handler import SpHandler
from idproxy.provider.idp.handler import IdPHandler
import server_conf
__author__ = 'haho0032'
#Imports within the project
from dirg_util.log import create_logger
from dirg_util.http_util import HttpHandler, BadRequest
from dirg_util.session import Session
from idproxy.provider.op.handler import OpHandler
#Imports within DIG
#External imports
import importlib
import argparse
from cherrypy import wsgiserver
from cherrypy.wsgiserver import ssl_pyopenssl
from beaker.middleware import SessionMiddleware
from mako.lookup import TemplateLookup
from saml2.s_utils import exception_trace
#Lookup for all mako templates.
LOOKUP = TemplateLookup(directories=['mako/templates', "/opt/dirg/dirg-util/mako/templates", 'mako/htdocs'],
module_directory='mako/modules',
input_encoding='utf-8',
output_encoding='utf-8')
class start_response_intercept(object):
def __init__(self, start_response):
self.start_response = start_response
def __call__(self, status, response_headers, exc_info=None):
self.status = status
self.response_headers = response_headers
self.exc_info = exc_info
self.start_response(status, response_headers, exc_info=None)
def application(environ, start_response):
"""
WSGI application. Handles all requests.
:param environ: WSGI enviroment.
:param start_response: WSGI start response.
:return: Depends on the request. Always a WSGI response where start_response first have to be initialized.
"""
try:
start_response = start_response_intercept(start_response)
session = Session(environ)
http_helper = HttpHandler(environ, start_response, session, logger)
path = http_helper.path()
environ = sphandler.verify_sp_user_validity(session, environ, path)
http_helper.log_request()
response = None
if server_conf.OP_FRONTEND and ophandler.verify_provider_requests(path):
response = ophandler.handle_provider_requests(environ, start_response, path, session)
if server_conf.IDP_FRONTEND and idphandler.verify_provider_requests(path, environ):
response = idphandler.handle_provider_requests(environ, start_response, path)
elif sphandler.verify_sp_requests(path):
response = sphandler.handle_sp_requests(environ, start_response, path, session)
elif http_helper.verify_static(path):
return http_helper.handle_static(path)
if response is None:
response = http_helper.http404()
http_helper.log_response(response)
#Catch all unauthorized attempts and present a better web page.
if start_response.status[0:3] == "401":
mte = LOOKUP.get_template("unauthorized.mako")
message = None
if len(response) == 1:
message = str(response[0])
if message is None or len(message.strip()) == 0:
message = "You are not authorized!"
argv = {
"message": message,
}
return [mte.render(**argv)]
return response
except Exception, excp:
urn = uuid4().urn
logger.error("uuid: " + str(urn) + str(exception_trace(excp)))
argv = {
"log_id": str(urn),
}
mte = LOOKUP.get_template("bad_request.mako")
resp = BadRequest(mte.render(**argv))
return resp(environ, start_response)
if __name__ == '__main__':
#This is equal to a main function in other languages. Handles all setup and starts the server.
#Read arguments.
parser = argparse.ArgumentParser()
parser.add_argument('-va', dest='valid', default="4",
help="How long, in days, the metadata is valid from the time of creation")
parser.add_argument('-c', dest='cert', help='certificate')
parser.add_argument('-isp', dest='id_sp',
help="The ID of the entities descriptor in the metadata for the SP")
parser.add_argument('-idp', dest='id_idp',
help="The ID of the entities descriptor in the metadata for the IdP")
parser.add_argument('-k', dest='keyfile',
help="A file with a key to sign the metadata with")
parser.add_argument('-nsp', dest='name_sp')
parser.add_argument('-nidp', dest='name_idp')
parser.add_argument('-s', dest='sign', action='store_true',
help="sign the metadata")
parser.add_argument('-v', dest='verbose', action='store_true')
parser.add_argument('-d', dest='debug', action='store_true')
parser.add_argument('-t', dest='test', action='store_true')
parser.add_argument(dest="config")
parser.add_argument(dest="idpconfig")
parser.add_argument(dest="spconf")
args = parser.parse_args()
#Application in debug mode if true.
debug = False
test = False
if args.debug:
debug = True
#Application in test mode if true.
if args.test:
test = True
global logger
logger = create_logger(server_conf.LOG_FILE)
global sphandler
sphandler = SpHandler(logger, args)
global ophandler
if server_conf.OP_FRONTEND:
config = importlib.import_module(args.config)
ophandler = OpHandler(logger, config, LOOKUP, sphandler, test, debug)
sphandler.ophandler = ophandler
else:
ophandler = None
sphandler.ophandler = None
global idphandler
if server_conf.IDP_FRONTEND:
idphandler = IdPHandler(args, LOOKUP, sphandler, server_conf.ISSUER)
else:
idphandler = None
global SRV
SRV = wsgiserver.CherryPyWSGIServer(('0.0.0.0', server_conf.PORT), SessionMiddleware(application,
server_conf.SESSION_OPTS))
SRV.stats['Enabled'] = True
#SRV = wsgiserver.CherryPyWSGIServer(('0.0.0.0', config.PORT), application)
if server_conf.HTTPS:
SRV.ssl_adapter = ssl_pyopenssl.pyOpenSSLAdapter(server_conf.SERVER_CERT, server_conf.SERVER_KEY,
server_conf.CERT_CHAIN)
logger.info("Server starting")
print "Server listening on port: %s" % server_conf.PORT
try:
SRV.start()
except KeyboardInterrupt:
SRV.stop()