source: TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/saml_utils/esgf/xml/etree.py @ 7698

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg-security/TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/saml_utils/esgf/xml/etree.py@7698
Revision 7698, 7.3 KB checked in by pjkersha, 10 years ago (diff)

Integrated SAML ESGF Group/Role? attribute value type into SAML Attribute Authority client unit tests.

  • Property svn:keywords set to Id
Line 
1"""SAML 2.0 Earth System Grid Group/Role ElementTree representation
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "09/11/09"
7__copyright__ = "(C) 2009 Science and Technology Facilities Council"
8__license__ = "BSD - see LICENSE file in top-level directory"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__revision__ = '$Id$'
11import logging
12log = logging.getLogger(__name__)
13
14from xml.etree import ElementTree
15
16from ndg.saml.xml import XMLTypeParseError, UnknownAttrProfile
17from ndg.saml.xml.etree import (AttributeValueElementTreeBase, 
18                                ResponseElementTree,
19                                QName)
20
21from ndg.security.common.saml_utils.esgf import ESGFGroupRoleAttributeValue
22
23
24class ESGFGroupRoleAttributeValueElementTree(AttributeValueElementTreeBase,
25                                             ESGFGroupRoleAttributeValue):
26    """ElementTree XML representation of Earth System Grid custom Group/Role
27    Attribute Value""" 
28
29    @classmethod
30    def toXML(cls, attributeValue):
31        """Create an XML representation of the input SAML ESG Group/Role type
32        Attribute Value
33       
34        @type attributeValue: ndg.security.common.saml_utils.esgf.ESGFGroupRoleAttributeValue
35        @param attributeValue: Group/Role Attribute Value to be represented as
36        an ElementTree Element
37        @rtype: ElementTree.Element
38        @return: ElementTree Element
39        """
40        elem = AttributeValueElementTreeBase.toXML(attributeValue)
41       
42        if not isinstance(attributeValue, ESGFGroupRoleAttributeValue):
43            raise TypeError("Expecting %r type; got: %r" % 
44                            (ESGFGroupRoleAttributeValue, type(attributeValue)))
45           
46        ElementTree._namespace_map[attributeValue.namespaceURI
47                                   ] = attributeValue.namespacePrefix
48                                   
49        tag = str(QName.fromGeneric(cls.TYPE_NAME))   
50        groupRoleElem = ElementTree.Element(tag)
51        ElementTree._namespace_map[cls.DEFAULT_ELEMENT_NAME.namespaceURI
52                                   ] = cls.DEFAULT_ELEMENT_NAME.prefix
53       
54        groupRoleElem.set(cls.GROUP_ATTRIB_NAME, attributeValue.group)
55        groupRoleElem.set(cls.ROLE_ATTRIB_NAME, attributeValue.role)
56
57        elem.append(groupRoleElem)
58       
59        return elem
60
61    @classmethod
62    def fromXML(cls, elem):
63        """Parse ElementTree ESG Group/Role attribute element into a SAML
64        ESGFGroupRoleAttributeValue object
65       
66        @type elem: ElementTree.Element
67        @param elem: Attribute value as ElementTree XML element
68        @rtype: saml.saml2.core.ESGFGroupRoleAttributeValue
69        @return: SAML ESG Group/Role Attribute value
70        """
71       
72        # Update namespace map for the Group/Role type referenced. 
73        ElementTree._namespace_map[cls.DEFAULT_NS] = cls.DEFAULT_PREFIX
74       
75        if not ElementTree.iselement(elem):
76            raise TypeError("Expecting %r input type for parsing; got %r" %
77                            (ElementTree.Element, elem))
78
79        localName = QName.getLocalPart(elem.tag)
80        if localName != cls.DEFAULT_ELEMENT_LOCAL_NAME:
81            raise XMLTypeParseError("No \"%s\" element found" %
82                                    cls.DEFAULT_ELEMENT_LOCAL_NAME)
83                                   
84        # Check for group/role child element
85        if len(elem) == 0:
86            raise XMLTypeParseError('Expecting "%s" child element to "%s" '
87                                    'element' % (cls.TYPE_LOCAL_NAME,
88                                               cls.DEFAULT_ELEMENT_LOCAL_NAME))
89       
90        childElem = elem[0]
91        childLocalName = QName.getLocalPart(childElem.tag)
92        if childLocalName != cls.TYPE_LOCAL_NAME:
93            raise XMLTypeParseError("No \"%s\" element found" %
94                                    cls.TYPE_LOCAL_NAME)
95
96                                     
97        attributeValue = ESGFGroupRoleAttributeValue()
98        groupName = childElem.attrib.get(cls.GROUP_ATTRIB_NAME)
99        if groupName is None:
100            raise XMLTypeParseError('No "%s" attribute found in Group/Role '
101                                    'attribute element' % 
102                                    cls.GROUP_ATTRIB_NAME)
103        attributeValue.group = groupName
104       
105        roleName = childElem.attrib.get(cls.ROLE_ATTRIB_NAME)
106        if roleName is None:
107            roleName = cls.DEFAULT_ROLE_NAME
108           
109        attributeValue.role = roleName
110
111        return attributeValue
112   
113    @classmethod
114    def factoryMatchFunc(cls, elem):
115        """Match function used by AttributeValueElementTreeFactory to
116        determine whether the given attribute is ESGFGroupRoleAttributeValue
117        type
118       
119        @type elem: ElementTree.Element
120        @param elem: Attribute value as ElementTree XML element
121        @rtype: ndg.security.common.saml_utils.etree.ESGFGroupRoleAttributeValue
122        or None
123        @return: SAML ESGF Group/Role Attribute Value class if elem is an
124        Group/role type element or None if if doesn't match this type
125        """
126       
127        # Group/role element is a child of the AttributeValue element
128        if len(elem) == 0:
129            return None
130       
131        childLocalName = QName.getLocalPart(elem[0].tag)
132        if childLocalName != cls.TYPE_LOCAL_NAME:
133            raise XMLTypeParseError('No "%s" child element found in '
134                                    'AttributeValue' % cls.TYPE_LOCAL_NAME)
135               
136        if cls.GROUP_ATTRIB_NAME in elem[0].attrib and \
137           cls.ROLE_ATTRIB_NAME in elem[0].attrib:
138            return cls
139
140        return None
141
142
143class ESGFResponseElementTree(ResponseElementTree):
144    """Extend ResponseElementTree type for Attribute Query Response to include
145    ESG custom Group/Role Attribute support"""
146   
147    @classmethod
148    def toXML(cls, response, **kw):
149        """Extend base method adding mapping for ESG Group/Role Attribute Value
150        to enable ElementTree Attribute Value factory to render the XML output
151       
152        @type response: ndg.security.common.saml_utils.etree.ESGFGroupRoleAttributeValue
153        @param response: ESGF Group/Role attribute value
154        @rtype: ElementTree.Element
155        @return: ESGF Group/Role attribute value as ElementTree.Element
156        """
157        toXMLTypeMap = kw.get('customToXMLTypeMap', {})
158        toXMLTypeMap[ESGFGroupRoleAttributeValue
159                     ] = ESGFGroupRoleAttributeValueElementTree
160       
161        kw['customToXMLTypeMap'] = toXMLTypeMap
162       
163        # Convert to ElementTree representation to enable attachment to SOAP
164        # response body
165        return ResponseElementTree.toXML(response, **kw)
166   
167    @classmethod
168    def fromXML(cls, elem, **kw):
169        """Extend base method adding mapping for ESG Group/Role Attribute Value
170         
171        @type elem: ElementTree.Element
172        @param elem: ESGF Group/Role attribute value as ElementTree.Element
173        @rtype: ndg.security.common.saml_utils.etree.ESGFGroupRoleAttributeValue
174        @return: ESGF Group/Role attribute value
175        """
176        toSAMLTypeMap = kw.get('customToSAMLTypeMap', [])
177        toSAMLTypeMap.append(
178                        ESGFGroupRoleAttributeValueElementTree.factoryMatchFunc)
179        kw['customToSAMLTypeMap'] = toSAMLTypeMap
180       
181        return ResponseElementTree.fromXML(elem, **kw)
Note: See TracBrowser for help on using the repository browser.