Skip to content

Commit 0dde360

Browse files
author
Roland Hedberg
committed
Fixed problem with signing metadata.
1 parent f684c4a commit 0dde360

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

src/saml2/config.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,17 @@
116116

117117
AQ_ARGS = ["endpoints"]
118118

119+
AA_ARGS = ["attribute", "attribute_profile"]
120+
119121
COMPLEX_ARGS = ["attribute_converters", "metadata", "policy"]
120-
ALL = set(COMMON_ARGS + SP_ARGS + AA_IDP_ARGS + PDP_ARGS + COMPLEX_ARGS)
122+
ALL = set(COMMON_ARGS + SP_ARGS + AA_IDP_ARGS + PDP_ARGS + COMPLEX_ARGS +
123+
AA_ARGS)
121124

122125
SPEC = {
123126
"": COMMON_ARGS + COMPLEX_ARGS,
124127
"sp": COMMON_ARGS + COMPLEX_ARGS + SP_ARGS,
125128
"idp": COMMON_ARGS + COMPLEX_ARGS + AA_IDP_ARGS,
126-
"aa": COMMON_ARGS + COMPLEX_ARGS + AA_IDP_ARGS,
129+
"aa": COMMON_ARGS + COMPLEX_ARGS + AA_IDP_ARGS + AA_ARGS,
127130
"pdp": COMMON_ARGS + COMPLEX_ARGS + PDP_ARGS,
128131
"aq": COMMON_ARGS + COMPLEX_ARGS + AQ_ARGS,
129132
}
@@ -222,6 +225,8 @@ def __init__(self, homedir="."):
222225
self.tmp_key_file = None
223226
self.validate_certificate = None
224227
self.extensions = {}
228+
self.attribute = []
229+
self.attribute_profile = []
225230

226231
def setattr(self, context, attr, val):
227232
if context == "":

src/saml2/metadata.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
from saml2.md import AttributeProfile, entity_descriptor_from_string
23
from saml2.sigver import security_context
34
from saml2.config import Config
45
from saml2.validate import valid_instance
@@ -52,11 +53,13 @@
5253
"organization_url": ("url", md.OrganizationURL)
5354
}
5455

56+
MDNS = '"urn:oasis:names:tc:SAML:2.0:metadata"'
57+
XMLNSXS = " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
5558

56-
def metadata_tostring_fix(desc, nspair):
57-
MDNS = '"urn:oasis:names:tc:SAML:2.0:metadata"'
58-
XMLNSXS = " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
59-
xmlstring = desc.to_string(nspair)
59+
60+
def metadata_tostring_fix(desc, nspair, xmlstring=""):
61+
if not xmlstring:
62+
xmlstring = desc.to_string(nspair)
6063
if "\"xs:string\"" in xmlstring and XMLNSXS not in xmlstring:
6164
xmlstring = xmlstring.replace(MDNS, MDNS+XMLNSXS)
6265
return xmlstring
@@ -94,13 +97,15 @@ def create_metadata_string(configfile, config, valid, cert, keyfile, mid, name,
9497

9598
return metadata_tostring_fix(desc, nspair)
9699
else:
97-
for eid in eds:
98-
if sign:
99-
desc = sign_entity_descriptor(eid, mid, secc)
100-
else:
101-
desc = eid
102-
valid_instance(desc)
103-
return metadata_tostring_fix(desc, nspair)
100+
eid = eds[0]
101+
if sign:
102+
eid, xmldoc = sign_entity_descriptor(eid, mid, secc)
103+
else:
104+
xmldoc = None
105+
106+
valid_instance(eid)
107+
xmldoc = metadata_tostring_fix(eid, nspair, xmldoc)
108+
return xmldoc
104109

105110

106111
def _localized_name(val, klass):
@@ -598,6 +603,16 @@ def do_aa_descriptor(conf, cert):
598603
if cert:
599604
aad.key_descriptor = do_key_descriptor(cert)
600605

606+
attributes = conf.getattr("attribute", "aa")
607+
if attributes:
608+
for attribute in attributes:
609+
aad.attribute.append(Attribute(text=attribute))
610+
611+
attribute_profiles = conf.getattr("attribute_profile", "aa")
612+
if attribute_profiles:
613+
for attribute_profile in attribute_profiles:
614+
aad.attribute.append(AttributeProfile(text=attribute_profile))
615+
601616
return aad
602617

603618

@@ -712,14 +727,26 @@ def entities_descriptor(eds, valid_for, name, ident, sign, secc):
712727
entities.id = ident
713728
xmldoc = secc.sign_statement("%s" % entities, class_name(entities))
714729
entities = md.entities_descriptor_from_string(xmldoc)
715-
return entities
730+
else:
731+
xmldoc = None
732+
733+
return entities, xmldoc
716734

717735

718736
def sign_entity_descriptor(edesc, ident, secc):
737+
"""
738+
739+
:param edesc: EntityDescriptor instance
740+
:param ident: EntityDescriptor identifier
741+
:param secc: Security context
742+
:return: Tuple with EntityDescriptor instance and Signed XML document
743+
"""
744+
719745
if not ident:
720746
ident = sid()
721747

722748
edesc.signature = pre_signature_part(ident, secc.my_cert, 1)
723749
edesc.id = ident
724750
xmldoc = secc.sign_statement("%s" % edesc, class_name(edesc))
725-
return md.entity_descriptor_from_string(xmldoc)
751+
edesc = md.entity_descriptor_from_string(xmldoc)
752+
return edesc, xmldoc

tools/make_metadata.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import argparse
33
import os
44
import sys
5-
from saml2.metadata import entity_descriptor
5+
from saml2.s_utils import rndstr
6+
from saml2.metadata import entity_descriptor, metadata_tostring_fix
67
from saml2.metadata import entities_descriptor
78
from saml2.metadata import sign_entity_descriptor
89

@@ -71,9 +72,12 @@
7172
else:
7273
for eid in eds:
7374
if args.sign:
74-
desc = sign_entity_descriptor(eid, id, secc)
75+
assert conf.key_file
76+
assert conf.cert_file
77+
eid, xmldoc = sign_entity_descriptor(eid, args.id, secc)
7578
else:
76-
desc = eid
77-
valid_instance(desc)
78-
print desc.to_string(nspair)
79+
xmldoc = None
7980

81+
valid_instance(eid)
82+
xmldoc = metadata_tostring_fix(eid, nspair, xmldoc)
83+
print xmldoc

0 commit comments

Comments
 (0)