Skip to content

Commit 327fb4b

Browse files
author
Roland Hedberg
committed
Merge branch 'master' of github.com:rohe/pysaml2
2 parents f915605 + f7f3b7c commit 327fb4b

File tree

5 files changed

+71
-20
lines changed

5 files changed

+71
-20
lines changed

example/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
idp/idp_conf.py
2+
idp2/idp.subject
3+
idp2/idp_conf.py
4+
sp/outstanding
5+
sp/sp_conf.py
6+
server.crt
7+
server.csr
8+
server.key
9+
server.key.org

example/idp2/idp.py

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def unpack_either(self):
120120

121121
def operation(self, _dict, binding):
122122
logger.debug("_operation: %s" % _dict)
123-
if not _dict:
123+
if not _dict or not 'SAMLRequest' in _dict:
124124
resp = BadRequest('Error parsing request or no request')
125125
return resp(self.environ, self.start_response)
126126
else:
@@ -335,8 +335,13 @@ def redirect(self):
335335
self.req_info = _info["req_info"]
336336
del IDP.ticket[_key]
337337
except KeyError:
338-
self.req_info = IDP.parse_authn_request(_info["SAMLRequest"],
339-
BINDING_HTTP_REDIRECT)
338+
try:
339+
self.req_info = IDP.parse_authn_request(_info["SAMLRequest"],
340+
BINDING_HTTP_REDIRECT)
341+
except KeyError:
342+
resp = BadRequest("Message signature verification failure")
343+
return resp(self.environ, self.start_response)
344+
340345
_req = self.req_info.message
341346

342347
if "SigAlg" in _info and "Signature" in _info: # Signed request
@@ -547,8 +552,11 @@ def do(self, request, binding, relay_state=""):
547552
if msg.name_id:
548553
lid = IDP.ident.find_local_id(msg.name_id)
549554
logger.info("local identifier: %s" % lid)
550-
del IDP.cache.uid2user[IDP.cache.user2uid[lid]]
551-
del IDP.cache.user2uid[lid]
555+
if lid in IDP.cache.user2uid:
556+
uid = IDP.cache.user2uid[lid]
557+
if uid in IDP.cache.uid2user:
558+
del IDP.cache.uid2user[uid]
559+
del IDP.cache.user2uid[lid]
552560
# remove the authentication
553561
try:
554562
IDP.session_db.remove_authn_statements(msg.name_id)
@@ -843,6 +851,19 @@ def metadata(environ, start_response):
843851
logger.error("An error occured while creating metadata:" + ex.message)
844852
return not_found(environ, start_response)
845853

854+
def staticfile(environ, start_response):
855+
try:
856+
path = args.path
857+
if path is None or len(path) == 0:
858+
path = os.path.dirname(os.path.abspath(__file__))
859+
if path[-1] != "/":
860+
path += "/"
861+
path += environ.get('PATH_INFO', '').lstrip('/')
862+
start_response('200 OK', [('Content-Type', "text/xml")])
863+
return open(path, 'r').read()
864+
except Exception as ex:
865+
logger.error("An error occured while creating metadata:" + ex.message)
866+
return not_found(environ, start_response)
846867

847868
def application(environ, start_response):
848869
"""
@@ -900,19 +921,40 @@ def application(environ, start_response):
900921
return func()
901922
return callback(environ, start_response, user)
902923

924+
if re.search(r'static/.*', path) is not None:
925+
return staticfile(environ, start_response)
903926
return not_found(environ, start_response)
904927

905928
# ----------------------------------------------------------------------------
906929

930+
# allow uwsgi or gunicorn mount
931+
# by moving some initialization out of __name__ == '__main__' section.
932+
# uwsgi -s 0.0.0.0:8088 --protocol http --callable application --module idp
933+
934+
args = type('Config', (object,), { })
935+
args.config = 'idp_conf'
936+
args.mako_root = './'
937+
args.path = None
938+
939+
import socket
940+
from idp_user import USERS
941+
from idp_user import EXTRA
942+
from mako.lookup import TemplateLookup
943+
944+
AUTHN_BROKER = AuthnBroker()
945+
AUTHN_BROKER.add(authn_context_class_ref(PASSWORD),
946+
username_password_authn, 10,
947+
"http://%s" % socket.gethostname())
948+
AUTHN_BROKER.add(authn_context_class_ref(UNSPECIFIED),
949+
"", 0, "http://%s" % socket.gethostname())
950+
951+
IDP = server.Server(args.config, cache=Cache())
952+
IDP.ticket = {}
907953

908954
# ----------------------------------------------------------------------------
909955

910956
if __name__ == '__main__':
911-
import socket
912-
from idp_user import USERS
913-
from idp_user import EXTRA
914957
from wsgiref.simple_server import make_server
915-
from mako.lookup import TemplateLookup
916958

917959
parser = argparse.ArgumentParser()
918960
parser.add_argument('-p', dest='path', help='Path to configuration file.')
@@ -937,16 +979,11 @@ def application(environ, start_response):
937979

938980
PORT = 8088
939981

940-
AUTHN_BROKER = AuthnBroker()
941-
AUTHN_BROKER.add(authn_context_class_ref(PASSWORD),
942-
username_password_authn, 10,
943-
"http://%s" % socket.gethostname())
944-
AUTHN_BROKER.add(authn_context_class_ref(UNSPECIFIED),
945-
"", 0, "http://%s" % socket.gethostname())
946-
947-
IDP = server.Server(args.config, cache=Cache())
948-
IDP.ticket = {}
949-
950982
SRV = make_server('', PORT, application)
951983
print "IdP listening on port: %s" % PORT
952984
SRV.serve_forever()
985+
else:
986+
_rot = args.mako_root
987+
LOOKUP = TemplateLookup(directories=[_rot + 'templates', _rot + 'htdocs'],
988+
module_directory=_rot + 'modules',
989+
input_encoding='utf-8', output_encoding='utf-8')

example/idp2/static/css/main.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* Sample css file */
2+

example/idp2/templates/root.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<% self.seen_css.add(path) %>
77
</%def>
88
<%def name="css()" filter="trim">
9-
${css_link('/css/main.css', 'screen')}
9+
${css_link('/static/css/main.css', 'screen')}
1010
</%def>
1111
<%def name="pre()" filter="trim">
1212
<div class="header">

example/sp-repoze/sp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ def application(environ, start_response):
270270
# ----------------------------------------------------------------------------
271271
PORT = 8087
272272

273+
# allow uwsgi or gunicorn mount
274+
# by moving some initialization out of __name__ == '__main__' section.
275+
# uwsgi -s 0.0.0.0:8087 --protocol http --callable app_with_auth --module idp
273276

274277
if __name__ == '__main__':
275278
#make_metadata arguments

0 commit comments

Comments
 (0)