-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.py
executable file
·617 lines (480 loc) · 20.2 KB
/
server.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import sys
import os
import traceback
from exceptions import KeyError
from exceptions import Exception
from exceptions import OSError
from exceptions import IndexError
from exceptions import AttributeError
from exceptions import KeyboardInterrupt
from urlparse import parse_qs
from oic.utils import shelve_wrapper
from oic.utils.authn.javascript_login import JavascriptFormMako
from oic.utils.authn.client import verify_client
from oic.utils.authn.multi_auth import setup_multi_auth
from oic.utils.authn.multi_auth import AuthnIndexedEndpointWrapper
from oic.utils.authn.saml import SAMLAuthnMethod
from oic.utils.authn.user import UsernamePasswordMako
from oic.utils.authz import AuthzHandling
from oic.utils.keyio import keyjar_init
from oic.utils.userinfo import UserInfo
from oic.utils.userinfo.aa_info import AaUserInfo
from oic.utils.webfinger import WebFinger
from oic.utils.webfinger import OIC_ISSUER
from oic.utils.authn.authn_context import AuthnBroker
from oic.utils.authn.authn_context import make_auth_verify
__author__ = 'rohe0002'
import re
from oic.oic.provider import Provider
from oic.oic.provider import EndSessionEndpoint
from oic.utils.http_util import *
from mako.lookup import TemplateLookup
LOGGER = logging.getLogger("")
LOGFILE_NAME = 'oc.log'
hdlr = logging.FileHandler(LOGFILE_NAME)
base_formatter = logging.Formatter(
"%(asctime)s %(name)s:%(levelname)s %(message)s")
CPC = ('%(asctime)s %(name)s:%(levelname)s '
'[%(client)s,%(path)s,%(cid)s] %(message)s')
cpc_formatter = logging.Formatter(CPC)
hdlr.setFormatter(base_formatter)
LOGGER.addHandler(hdlr)
LOGGER.setLevel(logging.DEBUG)
URLMAP = {}
NAME = "pyoic"
OAS = None
PASSWD = {
"diana": "krall",
"babs": "howes",
"upper": "crust"
}
# ----------------------------------------------------------------------------
# noinspection PyUnusedLocal
def safe(environ, start_response, logger):
_oas = environ["oic.oas"]
_srv = _oas.server
_log_info = _oas.logger.info
_log_info("- safe -")
# _log_info("env: %s" % environ)
#_log_info("handle: %s" % (handle,))
try:
authz = environ["HTTP_AUTHORIZATION"]
(typ, code) = authz.split(" ")
assert typ == "Bearer"
except KeyError:
resp = BadRequest("Missing authorization information")
return resp(environ, start_response)
try:
_sinfo = _srv.sdb[code]
except KeyError:
resp = Unauthorized("Not authorized")
return resp(environ, start_response)
info = "'%s' secrets" % _sinfo["sub"]
resp = Response(info)
return resp(environ, start_response)
# noinspection PyUnusedLocal
def css(environ, start_response, logger):
try:
info = open(environ["PATH_INFO"]).read()
resp = Response(info)
except (OSError, IOError):
resp = NotFound(environ["PATH_INFO"])
return resp(environ, start_response)
# ----------------------------------------------------------------------------
#noinspection PyUnusedLocal
def token(environ, start_response, logger):
_oas = environ["oic.oas"]
return wsgi_wrapper(environ, start_response, _oas.token_endpoint,
logger=logger)
#noinspection PyUnusedLocal
def authorization(environ, start_response, logger):
_oas = environ["oic.oas"]
return wsgi_wrapper(environ, start_response, _oas.authorization_endpoint,
logger=logger)
#noinspection PyUnusedLocal
def userinfo(environ, start_response, logger):
_oas = environ["oic.oas"]
return wsgi_wrapper(environ, start_response, _oas.userinfo_endpoint,
logger=logger)
#noinspection PyUnusedLocal
def op_info(environ, start_response, logger):
_oas = environ["oic.oas"]
LOGGER.info("op_info")
return wsgi_wrapper(environ, start_response, _oas.providerinfo_endpoint,
logger=logger)
#noinspection PyUnusedLocal
def registration(environ, start_response, logger):
_oas = environ["oic.oas"]
if environ["REQUEST_METHOD"] == "POST":
return wsgi_wrapper(environ, start_response, _oas.registration_endpoint,
logger=logger)
elif environ["REQUEST_METHOD"] == "GET":
return wsgi_wrapper(environ, start_response, _oas.read_registration,
logger=logger)
else:
resp = ServiceError("Method not supported")
return resp(environ, start_response)
#noinspection PyUnusedLocal
def check_id(environ, start_response, logger):
_oas = environ["oic.oas"]
return wsgi_wrapper(environ, start_response, _oas.check_id_endpoint,
logger=logger)
#noinspection PyUnusedLocal
def swd_info(environ, start_response, logger):
_oas = environ["oic.oas"]
return wsgi_wrapper(environ, start_response, _oas.discovery_endpoint,
logger=logger)
#noinspection PyUnusedLocal
def trace_log(environ, start_response, logger):
_oas = environ["oic.oas"]
return wsgi_wrapper(environ, start_response, _oas.tracelog_endpoint,
logger=logger)
#noinspection PyUnusedLocal
def endsession(environ, start_response, logger):
_oas = environ["oic.oas"]
return wsgi_wrapper(environ, start_response, _oas.endsession_endpoint,
logger=logger)
#noinspection PyUnusedLocal
def meta_info(environ, start_response, logger):
"""
Returns something like this::
{"links":[
{
"rel":"http://openid.net/specs/connect/1.0/issuer",
"href":"https://openidconnect.info/"
}
]}
"""
pass
def webfinger(environ, start_response, _):
query = parse_qs(environ["QUERY_STRING"])
try:
assert query["rel"] == [OIC_ISSUER]
resource = query["resource"][0]
except KeyError:
resp = BadRequest("Missing parameter in request")
else:
wf = WebFinger()
resp = Response(wf.response(subject=resource, base=OAS.baseurl))
return resp(environ, start_response)
def static_file(path):
try:
os.stat(path)
return True
except OSError:
return False
#noinspection PyUnresolvedReferences
def static(environ, start_response, path):
logger.info("[static]sending: %s" % (path,))
try:
text = open(path).read()
if path.endswith(".ico"):
start_response('200 OK', [('Content-Type', "image/x-icon")])
elif path.endswith(".html"):
start_response('200 OK', [('Content-Type', 'text/html')])
elif path.endswith(".json"):
start_response('200 OK', [('Content-Type', 'application/json')])
elif path.endswith(".txt"):
start_response('200 OK', [('Content-Type', 'text/plain')])
elif path.endswith(".css"):
start_response('200 OK', [('Content-Type', 'text/css')])
else:
start_response('200 OK', [('Content-Type', "text/xml")])
return [text]
except IOError:
resp = NotFound()
return resp(environ, start_response)
def check_session_iframe(environ, start_response, logger):
return static(environ, start_response, "htdocs/op_session_iframe.html")
# ----------------------------------------------------------------------------
def key_rollover(environ, start_response, _):
# expects a post containing the necessary information
_jwks = json.loads(get_post(environ))
OAS.do_key_rollover(_jwks, "key_%d_%%d" % int(time.time()))
resp = Response("OK")
return resp(environ, start_response)
def clear_keys(environ, start_response, _):
OAS.remove_inactive_keys()
resp = Response("OK")
return resp(environ, start_response)
# ----------------------------------------------------------------------------
from oic.oic.provider import AuthorizationEndpoint
from oic.oic.provider import TokenEndpoint
from oic.oic.provider import UserinfoEndpoint
from oic.oic.provider import RegistrationEndpoint
ENDPOINTS = [
AuthorizationEndpoint(authorization),
TokenEndpoint(token),
UserinfoEndpoint(userinfo),
RegistrationEndpoint(registration),
EndSessionEndpoint(endsession),
]
URLS = [
(r'^.well-known/openid-configuration', op_info),
(r'^.well-known/simple-web-discovery', swd_info),
(r'^.well-known/host-meta.json', meta_info),
(r'^.well-known/webfinger', webfinger),
# (r'^.well-known/webfinger', webfinger),
(r'.+\.css$', css),
(r'safe', safe),
(r'^keyrollover', key_rollover),
(r'^clearkeys', clear_keys),
(r'^check_session', check_session_iframe)
# (r'tracelog', trace_log),
]
def add_endpoints(extra):
global URLS
for endp in extra:
URLS.append(("^%s" % endp.etype, endp))
# ----------------------------------------------------------------------------
ROOT = './'
LOOKUP = TemplateLookup(directories=[ROOT + 'templates', ROOT + 'htdocs'],
module_directory=ROOT + 'modules',
input_encoding='utf-8', output_encoding='utf-8')
# ----------------------------------------------------------------------------
def application(environ, start_response):
"""
The main WSGI application. Dispatch the current request to
the functions from above and store the regular expression
captures in the WSGI environment as `oic.url_args` so that
the functions from above can access the url placeholders.
If nothing matches call the `not_found` function.
:param environ: The HTTP application environment
:param start_response: The application to run when the handling of the
request is done
:return: The response as a list of lines
"""
global OAS
#user = environ.get("REMOTE_USER", "")
path = environ.get('PATH_INFO', '').lstrip('/')
logger = logging.getLogger('oicServer')
if path == "robots.txt":
return static(environ, start_response, "static/robots.txt")
environ["oic.oas"] = OAS
#remote = environ.get("REMOTE_ADDR")
#kaka = environ.get("HTTP_COOKIE", '')
if path.startswith("static/"):
return static(environ, start_response, path)
for regex, callback in URLS:
match = re.search(regex, path)
if match is not None:
try:
environ['oic.url_args'] = match.groups()[0]
except IndexError:
environ['oic.url_args'] = path
logger.info("callback: %s" % callback)
try:
return callback(environ, start_response, logger)
except Exception as err:
print >> sys.stderr, "%s" % err
message = traceback.format_exception(*sys.exc_info())
print >> sys.stderr, message
logger.exception("%s" % err)
resp = ServiceError("%s" % err)
return resp(environ, start_response)
LOGGER.debug("unknown side: %s" % path)
resp = NotFound("Couldn't find the side you asked for!")
return resp(environ, start_response)
# ----------------------------------------------------------------------------
if __name__ == '__main__':
import argparse
import importlib
from cherrypy import wsgiserver
from cherrypy.wsgiserver import ssl_pyopenssl
from oic.utils.sdb import SessionDB
parser = argparse.ArgumentParser()
parser.add_argument('-v', dest='verbose', action='store_true')
parser.add_argument('-d', dest='debug', action='store_true')
parser.add_argument('-p', dest='port', default=80, type=int)
parser.add_argument('-k', dest='insecure', action='store_true')
parser.add_argument(
'-c', dest='capabilities',
help="A file containing a JSON representation of the capabilities")
parser.add_argument('-b', dest='baseurl', help="base url of the OP")
parser.add_argument(dest="config")
args = parser.parse_args()
# Client data base
cdb = shelve_wrapper.open("client_db", writeback=True)
sys.path.insert(0, ".")
config = importlib.import_module(args.config)
if args.baseurl:
config.baseurl = args.baseurl
config.issuer = config.issuer.format(base=config.baseurl, port=args.port)
config.SERVICE_URL = config.SERVICE_URL.format(issuer=config.issuer)
ac = AuthnBroker()
saml_authn = None
end_points = config.AUTHENTICATION["UserPassword"]["END_POINTS"]
full_end_point_paths = ["%s%s" % (config.issuer, ep) for ep in end_points]
username_password_authn = UsernamePasswordMako(
None, "login.mako", LOOKUP, PASSWD, "%sauthorization" % config.issuer,
None, full_end_point_paths)
for authkey, value in config.AUTHENTICATION.items():
authn = None
if "UserPassword" == authkey:
PASSWORD_END_POINT_INDEX = 0
end_point = config.AUTHENTICATION[authkey]["END_POINTS"][
PASSWORD_END_POINT_INDEX]
authn = AuthnIndexedEndpointWrapper(username_password_authn,
PASSWORD_END_POINT_INDEX)
URLS.append((r'^' + end_point, make_auth_verify(authn.verify)))
# Ensure javascript_login_authn to be defined
try:
javascript_login_authn
except NameError:
javascript_login_authn = None
if "JavascriptLogin" == authkey:
if not javascript_login_authn:
end_points = config.AUTHENTICATION[
"JavascriptLogin"]["END_POINTS"]
full_end_point_paths = [
"%s/%s" % (config.issuer, ep) for ep in end_points]
javascript_login_authn = JavascriptFormMako(
None, "javascript_login.mako", LOOKUP, PASSWD,
"%s/authorization" % config.issuer, None,
full_end_point_paths)
ac.add("", javascript_login_authn, "", "")
JAVASCRIPT_END_POINT_INDEX = 0
end_point = config.AUTHENTICATION[authkey]["END_POINTS"][
JAVASCRIPT_END_POINT_INDEX]
authn = AuthnIndexedEndpointWrapper(javascript_login_authn,
JAVASCRIPT_END_POINT_INDEX)
URLS.append((r'^' + end_point, make_auth_verify(authn.verify)))
if "SAML" == authkey:
from saml2 import BINDING_HTTP_REDIRECT, BINDING_HTTP_POST
if not saml_authn:
saml_authn = SAMLAuthnMethod(
None, LOOKUP, config.SAML, config.SP_CONFIG, config.issuer,
"%s/authorization" % config.issuer,
userinfo=config.USERINFO)
ac.add("", saml_authn, "", "")
SAML_END_POINT_INDEX = 0
end_point = config.AUTHENTICATION[authkey]["END_POINTS"][
SAML_END_POINT_INDEX]
end_point_indexes = {BINDING_HTTP_REDIRECT: 0, BINDING_HTTP_POST: 0,
"disco_end_point_index": 0}
authn = AuthnIndexedEndpointWrapper(saml_authn, end_point_indexes)
URLS.append((r'^' + end_point, make_auth_verify(authn.verify)))
if "SamlPass" == authkey:
from saml2 import BINDING_HTTP_REDIRECT, BINDING_HTTP_POST
if not saml_authn:
saml_authn = SAMLAuthnMethod(
None, LOOKUP, config.SAML, config.SP_CONFIG, config.issuer,
"%s/authorization" % config.issuer,
userinfo=config.USERINFO)
PASSWORD_END_POINT_INDEX = 1
SAML_END_POINT_INDEX = 1
password_end_point = config.AUTHENTICATION["UserPassword"][
"END_POINTS"][PASSWORD_END_POINT_INDEX]
saml_endpoint = config.AUTHENTICATION["SAML"]["END_POINTS"][
SAML_END_POINT_INDEX]
end_point_indexes = {BINDING_HTTP_REDIRECT: 1, BINDING_HTTP_POST: 1,
"disco_end_point_index": 1}
multi_saml = AuthnIndexedEndpointWrapper(saml_authn,
end_point_indexes)
multi_password = AuthnIndexedEndpointWrapper(
username_password_authn, PASSWORD_END_POINT_INDEX)
auth_modules = [(multi_saml, r'^' + saml_endpoint),
(multi_password, r'^' + password_end_point)]
authn = setup_multi_auth(ac, URLS, auth_modules)
if "JavascriptPass" == authkey:
if not javascript_login_authn:
end_points = config.AUTHENTICATION[
"JavascriptLogin"]["END_POINTS"]
full_end_point_paths = [
"%s/%s" % (config.issuer, ep) for ep in end_points]
javascript_login_authn = JavascriptFormMako(
None, "javascript_login.mako", LOOKUP, PASSWD,
"%s/authorization" % config.issuer, None,
full_end_point_paths)
PASSWORD_END_POINT_INDEX = 2
JAVASCRIPT_POINT_INDEX = 1
password_end_point = config.AUTHENTICATION["UserPassword"][
"END_POINTS"][PASSWORD_END_POINT_INDEX]
javascript_end_point = config.AUTHENTICATION["JavascriptLogin"][
"END_POINTS"][JAVASCRIPT_POINT_INDEX]
multi_password = AuthnIndexedEndpointWrapper(
username_password_authn, PASSWORD_END_POINT_INDEX)
multi_javascript = AuthnIndexedEndpointWrapper(
javascript_login_authn, JAVASCRIPT_POINT_INDEX)
auth_modules = [(multi_password, r'^' + password_end_point),
(multi_javascript, r'^' + javascript_end_point)]
authn = setup_multi_auth(ac, URLS, auth_modules)
if authn is not None:
ac.add(config.AUTHENTICATION[authkey]["ACR"], authn,
config.AUTHENTICATION[authkey]["WEIGHT"],
"")
# dealing with authorization
authz = AuthzHandling()
kwargs = {
"template_lookup": LOOKUP,
"template": {"form_post": "form_response.mako"},
#"template_args": {"form_post": {"action": "form_post"}}
}
# Should I care about verifying the certificates used by other entities
if args.insecure:
kwargs["verify_ssl"] = False
else:
kwargs["verify_ssl"] = True
if args.capabilities:
kwargs["capabilities"] = json.loads(open(args.capabilities).read())
else:
pass
OAS = Provider(config.issuer, SessionDB(config.baseurl), cdb, ac, None,
authz, verify_client, config.SYM_KEY, **kwargs)
OAS.baseurl = config.issuer
for authn in ac:
authn.srv = OAS
if config.USERINFO == "SIMPLE":
# User info is a simple dictionary in this case statically defined in
# the configuration file
OAS.userinfo = UserInfo(config.USERDB)
elif config.USERINFO == "SAML":
OAS.userinfo = UserInfo(config.SAML)
elif config.USERINFO == "AA":
OAS.userinfo = AaUserInfo(config.SP_CONFIG, config.issuer, config.SAML)
else:
raise Exception("Unsupported userinfo source")
try:
OAS.cookie_ttl = config.COOKIETTL
except AttributeError:
pass
try:
OAS.cookie_name = config.COOKIENAME
except AttributeError:
pass
#print URLS
if args.debug:
OAS.debug = True
# All endpoints the OpenID Connect Provider should answer on
add_endpoints(ENDPOINTS)
OAS.endpoints = ENDPOINTS
try:
jwks = keyjar_init(OAS, config.keys, kid_template="op%d")
except Exception, err:
LOGGER.error("Key setup failed: %s" % err)
OAS.key_setup("static", sig={"format": "jwk", "alg": "rsa"})
else:
new_name = "static/jwks.json"
f = open(new_name, "w")
f.write(json.dumps(jwks))
f.close()
OAS.jwks_uri.append("%s%s" % (OAS.baseurl, new_name))
for b in OAS.keyjar[""]:
LOGGER.info("OC3 server keys: %s" % b)
# Setup the web server
SRV = wsgiserver.CherryPyWSGIServer(('0.0.0.0', args.port), application)
https = ""
if config.SERVICE_URL.startswith("https"):
https = "using HTTPS"
SRV.ssl_adapter = ssl_pyopenssl.pyOpenSSLAdapter(
config.SERVER_CERT, config.SERVER_KEY, config.CERT_CHAIN)
LOGGER.info("OC server starting listening on port:%s %s" % (args.port,
https))
print "OC server starting listening on port:%s %s" % (args.port, https)
try:
SRV.start()
except KeyboardInterrupt:
SRV.stop()