Skip to content

Commit baa12f1

Browse files
authored
Merge pull request #80 from cjduffett/bugfix/add-sign-xmlns-xs-patch
Preserve xmlns:xs namespace when signing and serializing responses
2 parents 6505e5c + 0003e7c commit baa12f1

3 files changed

Lines changed: 41 additions & 11 deletions

File tree

src/onelogin/saml2/constants.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ class OneLogin_Saml2_Constants(object):
4747
NS_XENC = 'http://www.w3.org/2001/04/xmlenc#'
4848
NS_DS = 'http://www.w3.org/2000/09/xmldsig#'
4949

50+
# Namespace Prefixes
51+
NS_PREFIX_SAML = 'saml'
52+
NS_PREFIX_SAMLP = 'samlp'
53+
NS_PREFIX_MD = 'md'
54+
NS_PREFIX_XS = 'xs'
55+
NS_PREFIX_XSI = 'xsi'
56+
NS_PREFIX_XENC = 'xenc'
57+
NS_PREFIX_DS = 'ds'
58+
59+
# Prefix:Namespace Mappings
60+
NSMAP = {
61+
NS_PREFIX_SAMLP: NS_SAMLP,
62+
NS_PREFIX_SAML: NS_SAML,
63+
NS_PREFIX_DS: NS_DS,
64+
NS_PREFIX_XENC: NS_XENC,
65+
NS_PREFIX_MD: NS_MD
66+
}
67+
5068
# Bindings
5169
BINDING_HTTP_POST = 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST'
5270
BINDING_HTTP_REDIRECT = 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect'
@@ -76,15 +94,6 @@ class OneLogin_Saml2_Constants(object):
7694
STATUS_PARTIAL_LOGOUT = 'urn:oasis:names:tc:SAML:2.0:status:PartialLogout'
7795
STATUS_PROXY_COUNT_EXCEEDED = 'urn:oasis:names:tc:SAML:2.0:status:ProxyCountExceeded'
7896

79-
# Namespaces
80-
NSMAP = {
81-
'samlp': NS_SAMLP,
82-
'saml': NS_SAML,
83-
'ds': NS_DS,
84-
'xenc': NS_XENC,
85-
'md': NS_MD
86-
}
87-
8897
# Sign & Crypto
8998
SHA1 = 'http://www.w3.org/2000/09/xmldsig#sha1'
9099
SHA256 = 'http://www.w3.org/2001/04/xmlenc#sha256'

src/onelogin/saml2/xml_utils.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class OneLogin_Saml2_XML(object):
3030
dump = staticmethod(etree.dump)
3131
make_root = staticmethod(etree.Element)
3232
make_child = staticmethod(etree.SubElement)
33-
cleanup_namespaces = staticmethod(etree.cleanup_namespaces)
3433

3534
@staticmethod
3635
def to_string(xml, **kwargs):
@@ -123,6 +122,28 @@ def query(dom, query, context=None):
123122
else:
124123
return context.xpath(query, namespaces=OneLogin_Saml2_Constants.NSMAP)
125124

125+
@staticmethod
126+
def cleanup_namespaces(tree_or_element, top_nsmap=None, keep_ns_prefixes=None):
127+
"""
128+
Keeps the xmlns:xs namespace intact when etree.cleanup_namespaces is invoked.
129+
:param tree_or_element: An XML tree or element
130+
:type tree_or_element: etree.Element
131+
:param top_nsmap: A mapping from namespace prefixes to namespace URIs
132+
:type top_nsmap: dict
133+
:param keep_ns_prefixes: List of prefixes that should not be removed as part of the cleanup
134+
:type keep_ns_prefixes: list
135+
:returns: An XML tree or element
136+
:rtype: etree.Element
137+
"""
138+
all_prefixes_to_keep = [
139+
OneLogin_Saml2_Constants.NS_PREFIX_XS
140+
]
141+
142+
if keep_ns_prefixes:
143+
all_prefixes_to_keep = list(set(all_prefixes_to_keep.extend(keep_ns_prefixes)))
144+
145+
return etree.cleanup_namespaces(tree_or_element, keep_ns_prefixes=all_prefixes_to_keep)
146+
126147
@staticmethod
127148
def extract_tag_text(xml, tagname):
128149
open_tag = compat.to_bytes("<%s" % tagname)

tests/pep8.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[pep8]
2-
ignore = E501
2+
ignore = E501,E731
33
max-line-length = 160

0 commit comments

Comments
 (0)