|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Code to manage fetching and storing the metadata of IdPs. |
| 4 | +""" |
| 5 | +#pylint: disable=no-member |
| 6 | +from celery.task import task # pylint: disable=import-error,no-name-in-module |
| 7 | +import datetime |
| 8 | +import dateutil.parser |
| 9 | +import logging |
| 10 | +from lxml import etree |
| 11 | +import requests |
| 12 | +from onelogin.saml2.utils import OneLogin_Saml2_Utils |
| 13 | +from third_party_auth.models import SAMLConfiguration, SAMLProviderConfig, SAMLProviderData |
| 14 | + |
| 15 | +log = logging.getLogger(__name__) |
| 16 | + |
| 17 | +SAML_XML_NS = 'urn:oasis:names:tc:SAML:2.0:metadata' # The SAML Metadata XML namespace |
| 18 | + |
| 19 | + |
| 20 | +class MetadataParseError(Exception): |
| 21 | + """ An error occurred while parsing the SAML metadata from an IdP """ |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +@task(name='third_party_auth.fetch_saml_metadata') |
| 26 | +def fetch_saml_metadata(): |
| 27 | + """ |
| 28 | + Fetch and store/update the metadata of all IdPs |
| 29 | +
|
| 30 | + This task should be run on a daily basis. |
| 31 | + It's OK to run this whether or not SAML is enabled. |
| 32 | +
|
| 33 | + Return value: |
| 34 | + tuple(num_changed, num_failed, num_total) |
| 35 | + num_changed: Number of providers that are either new or whose metadata has changed |
| 36 | + num_failed: Number of providers that could not be updated |
| 37 | + num_total: Total number of providers whose metadata was fetched |
| 38 | + """ |
| 39 | + if not SAMLConfiguration.is_enabled(): |
| 40 | + return (0, 0, 0) # Nothing to do until SAML is enabled. |
| 41 | + |
| 42 | + num_changed, num_failed = 0, 0 |
| 43 | + |
| 44 | + # First make a list of all the metadata XML URLs: |
| 45 | + url_map = {} |
| 46 | + for idp_slug in SAMLProviderConfig.key_values('idp_slug', flat=True): |
| 47 | + config = SAMLProviderConfig.current(idp_slug) |
| 48 | + if not config.enabled: |
| 49 | + continue |
| 50 | + url = config.metadata_source |
| 51 | + if url not in url_map: |
| 52 | + url_map[url] = [] |
| 53 | + if config.entity_id not in url_map[url]: |
| 54 | + url_map[url].append(config.entity_id) |
| 55 | + # Now fetch the metadata: |
| 56 | + for url, entity_ids in url_map.items(): |
| 57 | + try: |
| 58 | + log.info("Fetching %s", url) |
| 59 | + if not url.lower().startswith('https'): |
| 60 | + log.warning("This SAML metadata URL is not secure! It should use HTTPS. (%s)", url) |
| 61 | + response = requests.get(url, verify=True) # May raise HTTPError or SSLError or ConnectionError |
| 62 | + response.raise_for_status() # May raise an HTTPError |
| 63 | + |
| 64 | + try: |
| 65 | + parser = etree.XMLParser(remove_comments=True) |
| 66 | + xml = etree.fromstring(response.text, parser) |
| 67 | + except etree.XMLSyntaxError: |
| 68 | + raise |
| 69 | + # TODO: Can use OneLogin_Saml2_Utils to validate signed XML if anyone is using that |
| 70 | + |
| 71 | + for entity_id in entity_ids: |
| 72 | + log.info(u"Processing IdP with entityID %s", entity_id) |
| 73 | + public_key, sso_url, expires_at = _parse_metadata_xml(xml, entity_id) |
| 74 | + changed = _update_data(entity_id, public_key, sso_url, expires_at) |
| 75 | + if changed: |
| 76 | + log.info(u"→ Created new record for SAMLProviderData") |
| 77 | + num_changed += 1 |
| 78 | + else: |
| 79 | + log.info(u"→ Updated existing SAMLProviderData. Nothing has changed.") |
| 80 | + except Exception as err: # pylint: disable=broad-except |
| 81 | + log.exception(err.message) |
| 82 | + num_failed += 1 |
| 83 | + return (num_changed, num_failed, len(url_map)) |
| 84 | + |
| 85 | + |
| 86 | +def _parse_metadata_xml(xml, entity_id): |
| 87 | + """ |
| 88 | + Given an XML document containing SAML 2.0 metadata, parse it and return a tuple of |
| 89 | + (public_key, sso_url, expires_at) for the specified entityID. |
| 90 | +
|
| 91 | + Raises MetadataParseError if anything is wrong. |
| 92 | + """ |
| 93 | + if xml.tag == etree.QName(SAML_XML_NS, 'EntityDescriptor'): |
| 94 | + entity_desc = xml |
| 95 | + else: |
| 96 | + if xml.tag != etree.QName(SAML_XML_NS, 'EntitiesDescriptor'): |
| 97 | + raise MetadataParseError("Expected root element to be <EntitiesDescriptor>, not {}".format(xml.tag)) |
| 98 | + entity_desc = xml.find( |
| 99 | + ".//{}[@entityID='{}']".format(etree.QName(SAML_XML_NS, 'EntityDescriptor'), entity_id) |
| 100 | + ) |
| 101 | + if not entity_desc: |
| 102 | + raise MetadataParseError("Can't find EntityDescriptor for entityID {}".format(entity_id)) |
| 103 | + |
| 104 | + expires_at = None |
| 105 | + if "validUntil" in xml.attrib: |
| 106 | + expires_at = dateutil.parser.parse(xml.attrib["validUntil"]) |
| 107 | + if "cacheDuration" in xml.attrib: |
| 108 | + cache_expires = OneLogin_Saml2_Utils.parse_duration(xml.attrib["cacheDuration"]) |
| 109 | + if expires_at is None or cache_expires < expires_at: |
| 110 | + expires_at = cache_expires |
| 111 | + |
| 112 | + sso_desc = entity_desc.find(etree.QName(SAML_XML_NS, "IDPSSODescriptor")) |
| 113 | + if not sso_desc: |
| 114 | + raise MetadataParseError("IDPSSODescriptor missing") |
| 115 | + if 'urn:oasis:names:tc:SAML:2.0:protocol' not in sso_desc.get("protocolSupportEnumeration"): |
| 116 | + raise MetadataParseError("This IdP does not support SAML 2.0") |
| 117 | + |
| 118 | + # Now we just need to get the public_key and sso_url |
| 119 | + public_key = sso_desc.findtext("./{}//{}".format( |
| 120 | + etree.QName(SAML_XML_NS, "KeyDescriptor"), "{http://www.w3.org/2000/09/xmldsig#}X509Certificate" |
| 121 | + )) |
| 122 | + if not public_key: |
| 123 | + raise MetadataParseError("Public Key missing. Expected an <X509Certificate>") |
| 124 | + public_key = public_key.replace(" ", "") |
| 125 | + binding_elements = sso_desc.iterfind("./{}".format(etree.QName(SAML_XML_NS, "SingleSignOnService"))) |
| 126 | + sso_bindings = {element.get('Binding'): element.get('Location') for element in binding_elements} |
| 127 | + try: |
| 128 | + # The only binding supported by python-saml and python-social-auth is HTTP-Redirect: |
| 129 | + sso_url = sso_bindings['urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect'] |
| 130 | + except KeyError: |
| 131 | + raise MetadataParseError("Unable to find SSO URL with HTTP-Redirect binding.") |
| 132 | + return public_key, sso_url, expires_at |
| 133 | + |
| 134 | + |
| 135 | +def _update_data(entity_id, public_key, sso_url, expires_at): |
| 136 | + """ |
| 137 | + Update/Create the SAMLProviderData for the given entity ID. |
| 138 | + Return value: |
| 139 | + False if nothing has changed and existing data's "fetched at" timestamp is just updated. |
| 140 | + True if a new record was created. (Either this is a new provider or something changed.) |
| 141 | + """ |
| 142 | + data_obj = SAMLProviderData.current(entity_id) |
| 143 | + fetched_at = datetime.datetime.now() |
| 144 | + if data_obj and (data_obj.public_key == public_key and data_obj.sso_url == sso_url): |
| 145 | + data_obj.expires_at = expires_at |
| 146 | + data_obj.fetched_at = fetched_at |
| 147 | + data_obj.save() |
| 148 | + return False |
| 149 | + else: |
| 150 | + SAMLProviderData.objects.create( |
| 151 | + entity_id=entity_id, |
| 152 | + fetched_at=fetched_at, |
| 153 | + expires_at=expires_at, |
| 154 | + sso_url=sso_url, |
| 155 | + public_key=public_key, |
| 156 | + ) |
| 157 | + return True |
0 commit comments