X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fipsilon.git;a=blobdiff_plain;f=ipsilon%2Fproviders%2Fsaml2idp.py;h=e89fe0c05f4836d3b4f90aa7195c93a45ed05533;hp=9cf3ed6d5dd85a2b09ef66f9e28398f674208795;hb=ca38224edc22e794c77418d30c2034cdba7ebe67;hpb=ad6e5efc6347639f4edfba94375151ccdbc5f7a8 diff --git a/ipsilon/providers/saml2idp.py b/ipsilon/providers/saml2idp.py index 9cf3ed6..e89fe0c 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 AdminPage +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 @@ -70,7 +77,7 @@ class Continue(AuthenticateRequest): raise cherrypy.HTTPError(400) 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,40 +98,41 @@ 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) + self.metadata = Metadata(*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 + self.cfg.idp = IdentityProvider(self.cfg) except Exception, e: # pylint: disable=broad-except - self._debug('Failed to enable SAML2 provider: %r' % e) + self._debug('Failed to init 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) + if 'type' not in sp or sp['type'] != 'SP': + continue + if 'name' not in sp or 'metadata' not in sp: + continue 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) + self.cfg.idp.add_provider(sp) except Exception, e: # pylint: disable=broad-except - self._debug('Failed to add SP %s: %r' % (name, e)) + self._debug('Failed to add SP %s: %r' % (sp['name'], e)) self.SSO = SSO(*args, **kwargs) @@ -134,6 +142,7 @@ class IdpProvider(ProviderBase): def __init__(self): super(IdpProvider, self).__init__('saml2', 'saml2') self.page = None + self.idp = None self.description = """ Provides SAML 2.0 authentication infrastructure. """ @@ -179,6 +188,13 @@ Provides SAML 2.0 authentication infrastructure. """ '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): @@ -217,4 +233,74 @@ Provides SAML 2.0 authentication infrastructure. """ def get_tree(self, site): self.page = SAML2(site, self) + self.admin = AdminPage(site, self) return self.page + + +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') + group.add_argument('--saml2-secure', + 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['saml2_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.set_config(config) + po.save_plugin_config(FACILITY) + + # Fixup permissions so only the ipsilon user can read these files + files.fix_user_dirs(path, opts['system_user'])