X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fipsilon.git;a=blobdiff_plain;f=ipsilon%2Fproviders%2Fsaml2idp.py;h=cb2c4a2f2ab219e906bbe339aba2814cbf5a98d6;hp=3dda9e858289e5891b9b16fb8fa69fb0ab424435;hb=62b4656571be6e8671ada295047eac385d330f66;hpb=2926eb00f40d925549b14b86f73f8649df1e9c05 diff --git a/ipsilon/providers/saml2idp.py b/ipsilon/providers/saml2idp.py index 3dda9e8..cb2c4a2 100755 --- a/ipsilon/providers/saml2idp.py +++ b/ipsilon/providers/saml2idp.py @@ -18,8 +18,15 @@ # along with this program. If not, see . from ipsilon.providers.common import ProviderBase, ProviderPageBase +from ipsilon.providers.common import FACILITY from ipsilon.providers.saml2.auth import AuthenticateRequest +from ipsilon.providers.saml2.admin import Saml2AdminPage +from ipsilon.providers.saml2.provider import IdentityProvider +from ipsilon.tools.certs import Certificate +from ipsilon.tools import saml2metadata as metadata +from ipsilon.tools import files from ipsilon.util.user import UserSession +from ipsilon.util.plugin import PluginObject import cherrypy import lasso import os @@ -53,8 +60,8 @@ class Continue(AuthenticateRequest): session = UserSession() user = session.get_user() - session.nuke_data('login', 'Return') - self.stage = session.get_data('saml2', 'stage') + transdata = self.trans.retrieve() + self.stage = transdata['saml2_stage'] if user.is_anonymous: self._debug("User is marked anonymous?!") @@ -63,14 +70,14 @@ class Continue(AuthenticateRequest): self._debug('Continue auth for %s' % user.name) - dump = session.get_data('saml2', 'Request') - if not dump: + if 'saml2_request' not in transdata: self._debug("Couldn't find Request dump?!") # TODO: Return to SP with auth failed error raise cherrypy.HTTPError(400) + dump = transdata['saml2_request'] try: - login = lasso.Login.newFromDump(self.cfg.idp, dump) + login = self.cfg.idp.get_login_handler(dump) except Exception, e: # pylint: disable=broad-except self._debug('Failed to load status from dump: %r' % e) @@ -91,41 +98,21 @@ class SSO(ProviderPageBase): self.Continue = Continue(*args, **kwargs) +class Metadata(ProviderPageBase): + def GET(self, *args, **kwargs): + with open(self.cfg.idp_metadata_file) as m: + body = m.read() + cherrypy.response.headers["Content-Type"] = "text/xml" + cherrypy.response.headers["Content-Disposition"] = \ + 'attachment; filename="metadata.xml"' + return body + + class SAML2(ProviderPageBase): def __init__(self, *args, **kwargs): super(SAML2, self).__init__(*args, **kwargs) - - # Init IDP data - try: - self.cfg.idp = lasso.Server(self.cfg.idp_metadata_file, - self.cfg.idp_key_file, - None, - self.cfg.idp_certificate_file) - self.cfg.idp.role = lasso.PROVIDER_ROLE_IDP - except Exception, e: # pylint: disable=broad-except - self._debug('Failed to enable SAML2 provider: %r' % e) - return - - # Import all known applications - data = self.cfg.get_data() - for idval in data: - if 'type' not in data[idval] or data[idval]['type'] != 'SP': - continue - path = os.path.join(self.cfg.idp_storage_path, str(idval)) - sp = data[idval] - if 'name' in sp: - name = sp['name'] - else: - name = str(idval) - try: - meta = os.path.join(path, 'metadata.xml') - cert = os.path.join(path, 'certificate.pem') - self.cfg.idp.addProvider(lasso.PROVIDER_ROLE_SP, meta, cert) - self._debug('Added SP %s' % name) - except Exception, e: # pylint: disable=broad-except - self._debug('Failed to add SP %s: %r' % (name, e)) - + self.metadata = Metadata(*args, **kwargs) self.SSO = SSO(*args, **kwargs) @@ -133,7 +120,9 @@ class IdpProvider(ProviderBase): def __init__(self): super(IdpProvider, self).__init__('saml2', 'saml2') + self.admin = None self.page = None + self.idp = None self.description = """ Provides SAML 2.0 authentication infrastructure. """ @@ -166,14 +155,26 @@ Provides SAML 2.0 authentication infrastructure. """ 'default allowed nameids': [ """Default Allowed NameIDs for Service Providers. """, 'list', - ['transient', 'email', 'kerberos', 'x509'] + ['persistent', 'transient', 'email', 'kerberos', 'x509'] ], 'default nameid': [ """Default NameID used by Service Providers. """, 'string', - 'email' + 'persistent' + ], + 'default email domain': [ + """Default email domain, for users missing email property.""", + 'string', + 'example.com' ] } + if cherrypy.config.get('debug', False): + import logging + import sys + logger = logging.getLogger('lasso') + lh = logging.StreamHandler(sys.stderr) + logger.addHandler(lh) + logger.setLevel(logging.DEBUG) @property def allow_self_registration(self): @@ -206,6 +207,107 @@ Provides SAML 2.0 authentication infrastructure. """ def default_nameid(self): return self.get_config_value('default nameid') + @property + def default_email_domain(self): + return self.get_config_value('default email domain') + def get_tree(self, site): + self.idp = self.init_idp() self.page = SAML2(site, self) + self.admin = Saml2AdminPage(site, self) return self.page + + def init_idp(self): + idp = None + # Init IDP data + try: + idp = IdentityProvider(self) + except Exception, e: # pylint: disable=broad-except + self._debug('Failed to init SAML2 provider: %r' % e) + return None + + # Import all known applications + data = self.get_data() + for idval in data: + sp = data[idval] + if 'type' not in sp or sp['type'] != 'SP': + continue + if 'name' not in sp or 'metadata' not in sp: + continue + try: + idp.add_provider(sp) + except Exception, e: # pylint: disable=broad-except + self._debug('Failed to add SP %s: %r' % (sp['name'], e)) + + return idp + + def on_enable(self): + self.init_idp() + if hasattr(self, 'admin'): + if self.admin: + self.admin.add_sps() + + +class Installer(object): + + def __init__(self): + self.name = 'saml2' + self.ptype = 'provider' + + def install_args(self, group): + group.add_argument('--saml2', choices=['yes', 'no'], default='yes', + help='Configure SAML2 Provider') + + def configure(self, opts): + if opts['saml2'] != 'yes': + return + + # Check storage path is present or create it + path = os.path.join(opts['data_dir'], 'saml2') + if not os.path.exists(path): + os.makedirs(path, 0700) + + # Use the same cert for signing and ecnryption for now + cert = Certificate(path) + cert.generate('idp', opts['hostname']) + + # Generate Idp Metadata + proto = 'https' + if opts['secure'].lower() == 'no': + proto = 'http' + url = '%s://%s/%s/saml2' % (proto, opts['hostname'], opts['instance']) + meta = metadata.Metadata(metadata.IDP_ROLE) + meta.set_entity_id(url + '/metadata') + meta.add_certs(cert, cert) + meta.add_service(metadata.SAML2_SERVICE_MAP['sso-post'], + url + '/SSO/POST') + meta.add_service(metadata.SAML2_SERVICE_MAP['sso-redirect'], + url + '/SSO/Redirect') + + meta.add_allowed_name_format( + lasso.SAML2_NAME_IDENTIFIER_FORMAT_TRANSIENT) + meta.add_allowed_name_format( + lasso.SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT) + meta.add_allowed_name_format( + lasso.SAML2_NAME_IDENTIFIER_FORMAT_EMAIL) + if 'krb' in opts and opts['krb'] == 'yes': + meta.add_allowed_name_format( + lasso.SAML2_NAME_IDENTIFIER_FORMAT_KERBEROS) + + meta.output(os.path.join(path, 'metadata.xml')) + + # Add configuration data to database + po = PluginObject() + po.name = 'saml2' + po.wipe_data() + + po.wipe_config_values(FACILITY) + config = {'idp storage path': path, + 'idp metadata file': 'metadata.xml', + 'idp certificate file': cert.cert, + 'idp key file': cert.key, + 'enabled': '1'} + po.save_plugin_config(FACILITY, config) + + # Fixup permissions so only the ipsilon user can read these files + files.fix_user_dirs(path, opts['system_user'])