|
17 | 17 | provides methods and functions to convert SAML classes to and from strings. |
18 | 18 | """ |
19 | 19 |
|
| 20 | +import copy |
20 | 21 | import logging |
21 | 22 |
|
22 | 23 | import six |
|
58 | 59 | DS_NAMESPACE = 'http://www.w3.org/2000/09/xmldsig#' |
59 | 60 | MD_NAMESPACE = "urn:oasis:names:tc:SAML:2.0:metadata" |
60 | 61 | MDUI_NAMESPACE = "urn:oasis:names:tc:SAML:metadata:ui" |
61 | | -DEFAULT_NS_PREFIXES = {'saml': NAMESPACE, 'samlp': SAMLP_NAMESPACE, |
62 | | - 'ds': DS_NAMESPACE, 'xsi': XSI_NAMESPACE, |
63 | | - 'xs': XS_NAMESPACE, |
64 | | - 'mdui': MDUI_NAMESPACE, |
65 | | - 'md': MD_NAMESPACE, |
66 | | - # 'alg': TODO: algsupport.DIGEST_METHODS|SIGNING_METHODS shoulb be moved before mapping them here |
67 | | - # TODO: <ns1:EntityAttributes> |
68 | | - } |
| 62 | +XENC_NAMESPACE = "http://www.w3.org/2001/04/xmlenc#" |
| 63 | + |
| 64 | +# this should be configurable by users |
| 65 | +OASIS_DEFAULT_NS_PREFIXES = {'saml': NAMESPACE, 'samlp': SAMLP_NAMESPACE, |
| 66 | + 'ds': DS_NAMESPACE, 'xsi': XSI_NAMESPACE, |
| 67 | + 'xs': XS_NAMESPACE, |
| 68 | + 'mdui': MDUI_NAMESPACE, |
| 69 | + 'md': MD_NAMESPACE, |
| 70 | + 'xenc': XENC_NAMESPACE, |
| 71 | + # mdattr: <ns1:EntityAttributes> |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +# make DEFAULT_NS_PREFIXES as default without register ns in every entities |
| 76 | +for prefix, uri in OASIS_DEFAULT_NS_PREFIXES.items(): |
| 77 | + try: |
| 78 | + ElementTree.register_namespace(prefix, uri) |
| 79 | + except AttributeError: |
| 80 | + # Backwards compatibility with ET < 1.3 |
| 81 | + ElementTree._namespace_map[uri] = prefix |
| 82 | + except ValueError: |
| 83 | + pass |
69 | 84 |
|
70 | 85 |
|
71 | 86 | NAMEID_FORMAT_EMAILADDRESS = ( |
|
90 | 105 | BINDING_HTTP_ARTIFACT = 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact' |
91 | 106 | BINDING_URI = 'urn:oasis:names:tc:SAML:2.0:bindings:URI' |
92 | 107 |
|
| 108 | +def replace_ns_prefixes(value, ns): |
| 109 | + """function to adapt ns to user's customs |
| 110 | + """ |
| 111 | + if not SWAPPED_NS_PREFIXES: |
| 112 | + return value |
| 113 | + return value.replace(DEFAULT_SWAPPED_NS_PREFIXES[ns], |
| 114 | + SWAPPED_NS_PREFIXES[ns]) |
93 | 115 |
|
94 | 116 | def class_name(instance): |
95 | 117 | return "%s:%s" % (instance.c_namespace, instance.c_tag) |
@@ -705,19 +727,19 @@ def to_string_force_namespace(self, nspair): |
705 | 727 |
|
706 | 728 | return ElementTree.tostring(elem, encoding="UTF-8") |
707 | 729 |
|
708 | | - def to_string(self, nspair=DEFAULT_NS_PREFIXES): |
| 730 | + def to_string(self, nspair=None): |
709 | 731 | """Converts the Saml object to a string containing XML. |
710 | 732 |
|
711 | 733 | :param nspair: A dictionary of prefixes and uris to use when |
712 | 734 | constructing the text representation. |
713 | 735 | :return: String representation of the object |
714 | 736 | """ |
715 | | - if not nspair and self.c_ns_prefix: |
| 737 | + if self.c_ns_prefix: |
716 | 738 | nspair = self.c_ns_prefix |
717 | 739 |
|
718 | 740 | if nspair: |
719 | 741 | self.register_prefix(nspair) |
720 | | - |
| 742 | + |
721 | 743 | return ElementTree.tostring(self._to_element_tree(), encoding="UTF-8") |
722 | 744 |
|
723 | 745 | def __str__(self): |
|
0 commit comments