def entity_descriptor(confd): mycert = None enc_cert = None if confd.cert_file is not None: mycert = [] mycert.append("".join(open(confd.cert_file).readlines()[1:-1])) if confd.additional_cert_files is not None: for _cert_file in confd.additional_cert_files: mycert.append("".join(open(_cert_file).readlines()[1:-1])) if confd.encryption_keypairs is not None: enc_cert = [] for _encryption in confd.encryption_keypairs: enc_cert.append("".join( open(_encryption["cert_file"]).readlines()[1:-1])) entd = md.EntityDescriptor() entd.entity_id = confd.entityid if confd.valid_for: entd.valid_until = in_a_while(hours=int(confd.valid_for)) if confd.organization is not None: entd.organization = do_organization_info(confd.organization) if confd.contact_person is not None: entd.contact_person = do_contact_person_info(confd.contact_person) if confd.entity_category: entd.extensions = md.Extensions() ava = [AttributeValue(text=c) for c in confd.entity_category] attr = Attribute(attribute_value=ava, name="http://macedir.org/entity-category") item = mdattr.EntityAttributes(attribute=attr) entd.extensions.add_extension_element(item) serves = confd.serves if not serves: raise SAMLError( 'No service type ("sp","idp","aa") provided in the configuration') if "sp" in serves: confd.context = "sp" entd.spsso_descriptor = do_spsso_descriptor(confd, mycert, enc_cert) if "idp" in serves: confd.context = "idp" entd.idpsso_descriptor = do_idpsso_descriptor(confd, mycert, enc_cert) if "aa" in serves: confd.context = "aa" entd.attribute_authority_descriptor = do_aa_descriptor( confd, mycert, enc_cert) if "pdp" in serves: confd.context = "pdp" entd.pdp_descriptor = do_pdp_descriptor(confd, mycert, enc_cert) if "aq" in serves: confd.context = "aq" entd.authn_authority_descriptor = do_aq_descriptor( confd, mycert, enc_cert) return entd
def _add_attr_to_entity_attributes(extensions, attribute): try: entity_attributes = next(el for el in extensions.extension_elements if el.tag == mdattr.EntityAttributes.c_tag) except StopIteration: entity_attributes = mdattr.EntityAttributes(attribute=attribute) extensions.add_extension_element(entity_attributes) else: entity_attributes.children.append(attribute)
def handle_metadata_save(self, environ, start_response, qs): """ Takes the input for the page metadata.mako. Encrypts entity id and secret information for the social services. Creates the partial xml to be added to the metadata for the service provider. :param environ: wsgi enviroment :param start_response: wsgi start respons :param qs: Query parameters in a dictionary. :return: wsgi response for the mako file metadatasave.mako. """ resp = Response(mako_template="metadatasave.mako", template_lookup=self.lookup, headers=[]) if "entityId" not in qs or "secret" not in qs: xml = ("Xml could not be generated because no entityId or secret" "has been sent to the service.") _logger.warning(xml) else: try: secret_data = json.dumps({ "entityId": json.loads(qs["entityId"]), "secret": json.loads(qs["secret"]) }) # create a JWE jwe = JWE(secret_data, alg=self.alg, enc=self.enc) secret_data_encrypted = jwe.encrypt([self.key]) val = AttributeValue() val.set_text(secret_data_encrypted) attr = Attribute(name_format=NAME_FORMAT_URI, name="http://social2saml.nordu.net/customer", attribute_value=[val]) eattr = mdattr.EntityAttributes(attribute=[attr]) nspair = { "mdattr": "urn:oasis:names:tc:SAML:metadata:attribute", "samla": "urn:oasis:names:tc:SAML:2.0:assertion", } xml = eattr.to_string(nspair) xml_list = xml.split("\n", 1) if len(xml_list) == 2: xml = xml_list[1] except Exception: _logger.fatal('Unknown error in handle_metadata_save.', exc_info=True) xml = "Xml could not be generated." argv = { "home": CONST_METADATA, "action": CONST_METADATAVERIFY, "xml": xml } return resp(environ, start_response, **argv)
def _add_attr_to_entity_attributes(extensions, attribute): try: entity_attributes = next(filter( lambda el: el.tag == mdattr.EntityAttributes.c_tag, extensions.extension_elements )) except StopIteration: entity_attributes = mdattr.EntityAttributes(attribute=attribute) extensions.add_extension_element(entity_attributes) else: entity_attributes.children.append(attribute)
#!/usr/bin/env python __author__ = 'rohe0002' import sys from saml2.extension import mdattr from saml2.saml import Attribute from saml2.saml import AttributeValue consumer = sys.stdin.read() val = AttributeValue() val.set_text(consumer) attr = Attribute(name_format="urn:oasis:names:tc:SAML:2.0:attrname-format:uri", name="http://social2saml.nordu.net/customer", attribute_value=[val]) eattr = mdattr.EntityAttributes(attribute=[attr]) nspair = { "mdattr": "urn:oasis:names:tc:SAML:metadata:attribute", "samla": "urn:oasis:names:tc:SAML:2.0:assertion" } print eattr.to_string(nspair)