Skip to content

Commit bb83ce0

Browse files
author
Roland Hedberg
committed
Made a special idp version compatible with uwsgi/gunicorn usage.
1 parent 87e51d6 commit bb83ce0

File tree

3 files changed

+1126
-72
lines changed

3 files changed

+1126
-72
lines changed

example/idp2/idp.py

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
#!/usr/bin/env python
2-
import importlib
32
import argparse
43
import base64
5-
import re
4+
import importlib
65
import logging
6+
import os
7+
import re
8+
import socket
79
import time
8-
from hashlib import sha1
910

10-
from urlparse import parse_qs
1111
from Cookie import SimpleCookie
12-
import os
13-
from saml2.profile import ecp
12+
from hashlib import sha1
13+
from urlparse import parse_qs
1414

15-
from saml2 import server
1615
from saml2 import BINDING_HTTP_ARTIFACT
1716
from saml2 import BINDING_URI
1817
from saml2 import BINDING_PAOS
1918
from saml2 import BINDING_SOAP
2019
from saml2 import BINDING_HTTP_REDIRECT
2120
from saml2 import BINDING_HTTP_POST
21+
from saml2 import server
2222
from saml2 import time_util
2323

2424
from saml2.authn_context import AuthnBroker
@@ -35,6 +35,7 @@
3535
from saml2.httputil import ServiceError
3636
from saml2.ident import Unknown
3737
from saml2.metadata import create_metadata_string
38+
from saml2.profile import ecp
3839
from saml2.s_utils import rndstr
3940
from saml2.s_utils import exception_trace
4041
from saml2.s_utils import UnknownPrincipal
@@ -43,6 +44,10 @@
4344
from saml2.sigver import verify_redirect_signature
4445
from saml2.sigver import encrypt_cert_from_item
4546

47+
from idp_user import USERS
48+
from idp_user import EXTRA
49+
from mako.lookup import TemplateLookup
50+
4651
logger = logging.getLogger("saml2.idp")
4752

4853

@@ -716,7 +721,7 @@ def do(self, request, binding, relay_state="", encrypt_cert=None):
716721
name_id = _query.subject.name_id
717722
uid = name_id.text
718723
logger.debug("Local uid: %s" % uid)
719-
identity = EXTRA[uid]
724+
identity = EXTRA[self.user]
720725

721726
# Comes in over SOAP so only need to construct the response
722727
args = IDP.response_args(_query, [BINDING_SOAP])
@@ -948,32 +953,6 @@ def application(environ, start_response):
948953

949954
# ----------------------------------------------------------------------------
950955

951-
# allow uwsgi or gunicorn mount
952-
# by moving some initialization out of __name__ == '__main__' section.
953-
# uwsgi -s 0.0.0.0:8088 --protocol http --callable application --module idp
954-
955-
args = type('Config', (object,), { })
956-
args.config = 'idp_conf'
957-
args.mako_root = './'
958-
args.path = None
959-
960-
import socket
961-
from idp_user import USERS
962-
from idp_user import EXTRA
963-
from mako.lookup import TemplateLookup
964-
965-
AUTHN_BROKER = AuthnBroker()
966-
AUTHN_BROKER.add(authn_context_class_ref(PASSWORD),
967-
username_password_authn, 10,
968-
"http://%s" % socket.gethostname())
969-
AUTHN_BROKER.add(authn_context_class_ref(UNSPECIFIED),
970-
"", 0, "http://%s" % socket.gethostname())
971-
CONFIG = importlib.import_module(args.config)
972-
IDP = server.Server(args.config, cache=Cache())
973-
IDP.ticket = {}
974-
975-
# ----------------------------------------------------------------------------
976-
977956
if __name__ == '__main__':
978957
from wsgiref.simple_server import make_server
979958

@@ -993,6 +972,16 @@ def application(environ, start_response):
993972
parser.add_argument(dest="config")
994973
args = parser.parse_args()
995974

975+
AUTHN_BROKER = AuthnBroker()
976+
AUTHN_BROKER.add(authn_context_class_ref(PASSWORD),
977+
username_password_authn, 10,
978+
"http://%s" % socket.gethostname())
979+
AUTHN_BROKER.add(authn_context_class_ref(UNSPECIFIED),
980+
"", 0, "http://%s" % socket.gethostname())
981+
CONFIG = importlib.import_module(args.config)
982+
IDP = server.Server(args.config, cache=Cache())
983+
IDP.ticket = {}
984+
996985
_rot = args.mako_root
997986
LOOKUP = TemplateLookup(directories=[_rot + 'templates', _rot + 'htdocs'],
998987
module_directory=_rot + 'modules',
@@ -1004,8 +993,3 @@ def application(environ, start_response):
1004993
SRV = make_server(HOST, PORT, application)
1005994
print "IdP listening on %s:%s" % (HOST, PORT)
1006995
SRV.serve_forever()
1007-
else:
1008-
_rot = args.mako_root
1009-
LOOKUP = TemplateLookup(directories=[_rot + 'templates', _rot + 'htdocs'],
1010-
module_directory=_rot + 'modules',
1011-
input_encoding='utf-8', output_encoding='utf-8')

0 commit comments

Comments
 (0)