source: TI12-security/trunk/python/ndg.security.common/ndg/security/common/saml/xml/__init__.py @ 5554

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.common/ndg/security/common/saml/xml/__init__.py@5554
Revision 5554, 12.1 KB checked in by pjkersha, 11 years ago (diff)
  • Started adding ElementTree based parsers for SAML classes in ndg.security.common.saml.xml.etree.
  • ndg.security.common.utils.prettyPrint needs a bug fix for namespace declarations
Line 
1"""Implementation of SAML 2.0 for NDG Security - XML package
2
3NERC DataGrid Project
4
5This implementation is adapted from the Java OpenSAML implementation.  The
6copyright and licence information are included here:
7
8Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
9
10Licensed under the Apache License, Version 2.0 (the "License");
11you may not use this file except in compliance with the License.
12You may obtain a copy of the License at
13
14http://www.apache.org/licenses/LICENSE-2.0
15
16Unless required by applicable law or agreed to in writing, software
17distributed under the License is distributed on an "AS IS" BASIS,
18WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19See the License for the specific language governing permissions and
20limitations under the License.
21"""
22__author__ = "P J Kershaw"
23__date__ = "23/07/09"
24__copyright__ = "(C) 2009 Science and Technology Facilities Council"
25__contact__ = "Philip.Kershaw@stfc.ac.uk"
26__license__ = "BSD - see LICENSE file in top-level directory"
27__contact__ = "Philip.Kershaw@stfc.ac.uk"
28__revision__ = "$Id$"
29from datetime import datetime
30try:
31    from datetime import strptime
32except ImportError:
33    # Allow for Python < 2.5
34    from time import strptime as _strptime
35    strptime = lambda datetimeStr, format: datetime(*(_strptime(datetimeStr, 
36                                                                format)[0:6]))
37
38class XMLConstants(object):
39    '''XML related constants.'''
40
41    # XML Tooling
42
43    # Configuration namespace
44    XMLTOOLING_CONFIG_NS = "http:#www.opensaml.org/xmltooling-config"
45
46    # Configuration namespace prefix
47    XMLTOOLING_CONFIG_PREFIX = "xt"
48   
49    # Name of the object provider used for objects that don't have a registered
50    # object provider
51    XMLTOOLING_DEFAULT_OBJECT_PROVIDER = "DEFAULT"
52
53    # Core XML
54
55    # XML core namespace
56    XML_NS = "http:#www.w3.org/XML/1998/namespace"
57   
58    # XML core prefix for xml attributes
59    XML_PREFIX = "xml"
60
61    # XML namespace for xmlns attributes
62    XMLNS_NS = "http://www.w3.org/2000/xmlns/"
63
64    # XML namespace prefix for xmlns attributes
65    XMLNS_PREFIX = "xmlns"
66
67    # XML Schema namespace
68    XSD_NS = "http://www.w3.org/2001/XMLSchema"
69
70    # XML Schema QName prefix
71    XSD_PREFIX = "xs"
72
73    # XML Schema Instance namespace
74    XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"
75
76    # XML Schema Instance QName prefix
77    XSI_PREFIX = "xsi"
78
79    # XML XMLSecSignatureImpl namespace
80    XMLSIG_NS = "http://www.w3.org/2000/09/xmldsig#"
81
82    # XML XMLSecSignatureImpl QName prefix
83    XMLSIG_PREFIX = "ds"
84
85    # XML Encryption namespace
86    XMLENC_NS = "http://www.w3.org/2001/04/xmlenc#"
87
88    # XML Encryption QName prefix
89    XMLENC_PREFIX = "xenc"
90   
91    # Local name of EncryptedData element
92    XMLENC_ENCDATA_LOCAL_NAME = "EncryptedData"
93   
94    # Local name of EncryptedKey element
95    XMLENC_ENCKEY_LOCAL_NAME = "EncryptedKey"
96   
97
98class SAMLConstants(XMLConstants):
99    '''XML related constants used in the SAML specifications.'''
100   
101    # HTTP Constants
102   
103    # HTTP Request Method - POST.
104    POST_METHOD = "POST"
105   
106    # HTTP Method - GET.
107    GET_METHOD = "GET"
108   
109    # OpenSAML 2
110   
111    # Directory, on the classpath, schemas are located in.
112    SCHEMA_DIR = "/schema/"
113   
114    #    Core XML
115   
116    # XML core schema system Id.
117    XML_SCHEMA_LOCATION = SCHEMA_DIR + "xml.xsd"
118   
119    #  XML Signature schema Id.
120    XMLSIG_SCHEMA_LOCATION = SCHEMA_DIR + "xmldsig-core-schema.xsd"
121   
122    # XML Encryption schema Id.
123    XMLENC_SCHEMA_LOCATION = SCHEMA_DIR + "xenc-schema.xsd"
124
125   
126    #    SOAP
127   
128    #  SOAP 1.1 schema Id.
129    SOAP11ENV_SCHEMA_LOCATION = SCHEMA_DIR + SCHEMA_DIR + "soap-envelope.xsd"
130   
131    #  SOAP 1.1 Envelope XML namespace.
132    SOAP11ENV_NS = "http://schemas.xmlsoap.org/soap/envelope/"
133   
134    #  SOAP 1.1 Envelope QName prefix.
135    SOAP11ENV_PREFIX = "SOAP-ENV"
136   
137    #  Liberty PAOS XML Namespace.
138    PAOS_NS = "urn:liberty:paos:2003-08"
139   
140    #  Liberty PAOS QName prefix.
141    PAOS_PREFIX = "paos"
142   
143    #    SAML 1.X
144   
145    # SAML 1.0 Assertion schema system Id.
146    SAML10_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-assertion-01.xsd"
147   
148    # SAML 1.1 Assertion schema system Id.
149    SAML11_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-assertion-1.1.xsd"
150   
151    # SAML 1.X XML namespace.
152    SAML1_NS = "urn:oasis:names:tc:SAML:1.0:assertion"
153   
154    # SAML 1.0 Protocol schema system Id.
155    SAML10P_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-protocol-01.xsd"
156   
157    # SAML 1.1 Protocol schema system Id.
158    SAML11P_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-protocol-1.1.xsd"
159
160    # SAML 1.X protocol XML namespace.
161    SAML10P_NS = "urn:oasis:names:tc:SAML:1.0:protocol"
162   
163    # SAML 1.1 protocol XML namespace, used only in SAML 2 metadata protocol
164    # SupportEnumeration.
165    SAML11P_NS = "urn:oasis:names:tc:SAML:1.1:protocol"
166   
167    # SAML 1.X Protocol QName prefix.
168    SAML1P_PREFIX = "samlp"
169
170    # SAML 1.X Assertion QName prefix.
171    SAML1_PREFIX = "saml"
172   
173    # SAML 1 Metadata extension XML namespace.
174    SAML1MD_NS = "urn:oasis:names:tc:SAML:profiles:v1metadata"
175   
176    # SAML 1 Metadata extension schema system Id.
177    SAML1MD_SCHEMA_LOCATION = SCHEMA_DIR + "sstc-saml1x-metadata.xsd"
178   
179    # SAML 1 Metadata extension namespace prefix.
180    SAML1MD_PREFIX = "saml1md"
181   
182    # URI for SAML 1 Artifact binding.
183    SAML1_ARTIFACT_BINDING_URI = \
184        "urn:oasis:names:tc:SAML:1.0:profiles:artifact-01"
185   
186    # URI for SAML 1 POST binding.
187    SAML1_POST_BINDING_URI = \
188        "urn:oasis:names:tc:SAML:1.0:profiles:browser-post"
189   
190    # URI for SAML 1 SOAP 1.1 binding.
191    SAML1_SOAP11_BINDING_URI = \
192        "urn:oasis:names:tc:SAML:1.0:bindings:SOAP-binding"
193   
194    #    SAML 2.0
195   
196    # SAML 2.0 Assertion schema Id.
197    SAML20_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-assertion-2.0.xsd"
198   
199    # SAML 2.0 Assertion XML Namespace.
200    SAML20_NS = "urn:oasis:names:tc:SAML:2.0:assertion"
201   
202    # SAML 2.0 Assertion QName prefix.
203    SAML20_PREFIX ="saml"
204   
205    # SAML 2.0 Protocol schema Id.
206    SAML20P_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-protocol-2.0.xsd"
207   
208    # SAML 2.0 Protocol XML Namespace.
209    SAML20P_NS = "urn:oasis:names:tc:SAML:2.0:protocol"
210   
211    # SAML 2.0 Protocol QName prefix.
212    SAML20P_PREFIX ="samlp"
213   
214    # SAML 2.0 Protocol Third-party extension schema Id.
215    SAML20PTHRPTY_SCHEMA_LOCATION = SCHEMA_DIR + \
216                                    "sstc-saml-protocol-ext-thirdparty.xsd"
217   
218    # SAML 2.0 Protocol XML Namespace.
219    SAML20PTHRPTY_NS = "urn:oasis:names:tc:SAML:protocol:ext:third-party"
220   
221    # SAML 2.0 Protocol QName prefix.
222    SAML20PTHRPTY_PREFIX ="thrpty"
223   
224    # SAML 2.0 Metadata schema Id.
225    SAML20MD_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-metadata-2.0.xsd"
226   
227    # SAML 2.0 Metadata XML Namespace.
228    SAML20MD_NS ="urn:oasis:names:tc:SAML:2.0:metadata"
229   
230    # SAML 2.0 Standalone Query Metadata extension XML namespace.
231    SAML20MDQUERY_NS = "urn:oasis:names:tc:SAML:metadata:ext:query"
232   
233    # SAML 2.0 Standalone Query Metadata extension schema system Id.
234    SAML20MDQUERY_SCHEMA_LOCATION = SCHEMA_DIR + \
235                                    "sstc-saml-metadata-ext-query.xsd"
236   
237    # SAML 2.0 Standalone Query Metadata extension prefix.
238    SAML20MDQUERY_PREFIX = "query"
239   
240    # SAML 2.0 Metadata QName prefix.
241    SAML20MD_PREFIX = "md"
242   
243    # SAML 2.0 Authentication Context schema Id.
244    SAML20AC_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-authn-context-2.0.xsd"
245   
246    # SAML 2.0 Authentication Context XML Namespace.
247    SAML20AC_NS ="urn:oasis:names:tc:SAML:2.0:ac"
248   
249    # SAML 2.0 Authentication Context QName prefix.
250    SAML20AC_PREFIX = "ac"
251   
252    # SAML 2.0 Enhanced Client/Proxy SSO Profile schema Id.
253    SAML20ECP_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-ecp-2.0.xsd"
254   
255    # SAML 2.0 Enhanced Client/Proxy SSO Profile XML Namespace.
256    SAML20ECP_NS = "urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp"
257   
258    # SAML 2.0 Enhanced Client/Proxy SSO Profile QName prefix.
259    SAML20ECP_PREFIX = "ecp"
260   
261    # SAML 2.0 DCE PAC Attribute Profile schema Id.
262    SAML20DCE_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-dce-2.0.xsd"
263   
264    # SAML 2.0 DCE PAC Attribute Profile XML Namespace.
265    SAML20DCE_NS = "urn:oasis:names:tc:SAML:2.0:profiles:attribute:DCE"
266   
267    # SAML 2.0 DCE PAC Attribute Profile QName prefix.
268    SAML20DCE_PREFIX = "DCE"
269   
270    # SAML 2.0 X.500 Attribute Profile schema Id.
271    SAML20X500_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-x500-2.0.xsd"
272   
273    # SAML 2.0 X.500 Attribute Profile XML Namespace.
274    SAML20X500_NS = "urn:oasis:names:tc:SAML:2.0:profiles:attribute:X500"
275   
276    # SAML 2.0 X.500 Attribute Profile QName prefix.
277    SAML20X500_PREFIX = "x500"
278   
279    # SAML 2.0 XACML Attribute Profile schema Id.
280    SAML20XACML_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-xacml-2.0.xsd"
281   
282    # SAML 2.0 XACML Attribute Profile XML Namespace.
283    SAML20XACML_NS = "urn:oasis:names:tc:SAML:2.0:profiles:attribute:XACML"
284   
285    # SAML 2.0 XACML Attribute Profile QName prefix.
286    SAML20XACML_PREFIX = "xacmlprof"
287   
288    # URI for SAML 2 Artifact binding.
289    SAML2_ARTIFACT_BINDING_URI = \
290                        "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact"
291   
292    # URI for SAML 2 POST binding.
293    SAML2_POST_BINDING_URI = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
294   
295    # URI for SAML 2 POST-SimpleSign binding.
296    SAML2_POST_SIMPLE_SIGN_BINDING_URI = \
297                "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST-SimpleSign"
298   
299    # URI for SAML 2 HTTP redirect binding.
300    SAML2_REDIRECT_BINDING_URI = \
301                "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
302   
303    # URI for SAML 2 SOAP binding.
304    SAML2_SOAP11_BINDING_URI = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
305
306class XMLObjectError(Exception):
307    pass
308
309class XMLObjectParsingError(Exception):
310    pass
311
312class XMLObject(object):
313    """Abstract base class for XML representations of SAML objects"""
314   
315    def create(self, samlObject):
316        """Create an XML representation from the input SAML object
317        @type samlObject: SAMLObject
318        param samlObject: SAML object to render into XML
319        """
320        raise NotImplementedError()
321
322    def parse(self, elem):
323        """Parse into XML representation
324        @type elem: object
325        @param elem: XML object - type depends on XML class representation
326        @rtype: SAMLObject
327        @return: equivalent SAML object
328        @raise XMLObjectParsingError: error parsing content into SAML
329        representation
330        """
331        raise NotImplementedError()
332   
333    def serialize(self):
334        """Serialize the XML object into a string representation
335        """
336        raise NotImplementedError()
337       
338       
339class IssueInstantXMLObject(XMLObject):
340    """Specialisation to enable inclusion of datetime formatting for issue
341    instant
342    """
343    issueInstantFmt = "%Y-%m-%dT%H:%M:%SZ"
344   
345    @classmethod
346    def datetime2Str(cls, dtIssueInstant):
347        """Convert issue instant datetime to correct string type for output
348        @type dtIssueInstant: datetime.datetime
349        @param dtIssueInstant: issue instance as a datetime
350        @rtype: basestring
351        @return: issue instance as a string
352        """
353        if not isinstance(dtIssueInstant, datetime):
354            raise TypeError("Expecting datetime type for string conversion, "
355                            "got %r" % dtIssueInstant)
356           
357        return dtIssueInstant.strftime(IssueInstantXMLObject.issueInstantFmt)
358
359    @classmethod
360    def str2Datetime(cls, issueInstant):
361        """Convert issue instant string to datetime type
362        @type issueInstant: basestring
363        @param issueInstant: issue instance as a string
364        @rtype: datetime.datetime
365        @return: issue instance as a datetime
366        """
367        if not isinstance(issueInstant, basestring):
368            raise TypeError("Expecting basestring derived type for string "
369                            "conversion, got %r" % issueInstant)
370           
371        return datetime.strptime(issueInstant, 
372                                 IssueInstantXMLObject.issueInstantFmt)
Note: See TracBrowser for help on using the repository browser.