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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.common/ndg/security/common/saml/xml/__init__.py@5514
Revision 5514, 10.7 KB checked in by pjkersha, 10 years ago (diff)

Added OpenSAML credits

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$"
29
30class XMLConstants(object):
31    '''XML related constants.'''
32
33    # XML Tooling
34
35    # Configuration namespace
36    XMLTOOLING_CONFIG_NS = "http:#www.opensaml.org/xmltooling-config"
37
38    # Configuration namespace prefix
39    XMLTOOLING_CONFIG_PREFIX = "xt"
40   
41    # Name of the object provider used for objects that don't have a registered
42    # object provider
43    XMLTOOLING_DEFAULT_OBJECT_PROVIDER = "DEFAULT"
44
45    # Core XML
46
47    # XML core namespace
48    XML_NS = "http:#www.w3.org/XML/1998/namespace"
49   
50    # XML core prefix for xml attributes
51    XML_PREFIX = "xml"
52
53    # XML namespace for xmlns attributes
54    XMLNS_NS = "http://www.w3.org/2000/xmlns/"
55
56    # XML namespace prefix for xmlns attributes
57    XMLNS_PREFIX = "xmlns"
58
59    # XML Schema namespace
60    XSD_NS = "http://www.w3.org/2001/XMLSchema"
61
62    # XML Schema QName prefix
63    XSD_PREFIX = "xs"
64
65    # XML Schema Instance namespace
66    XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"
67
68    # XML Schema Instance QName prefix
69    XSI_PREFIX = "xsi"
70
71    # XML XMLSecSignatureImpl namespace
72    XMLSIG_NS = "http://www.w3.org/2000/09/xmldsig#"
73
74    # XML XMLSecSignatureImpl QName prefix
75    XMLSIG_PREFIX = "ds"
76
77    # XML Encryption namespace
78    XMLENC_NS = "http://www.w3.org/2001/04/xmlenc#"
79
80    # XML Encryption QName prefix
81    XMLENC_PREFIX = "xenc"
82   
83    # Local name of EncryptedData element
84    XMLENC_ENCDATA_LOCAL_NAME = "EncryptedData"
85   
86    # Local name of EncryptedKey element
87    XMLENC_ENCKEY_LOCAL_NAME = "EncryptedKey"
88   
89
90class SAMLConstants(XMLConstants):
91    '''XML related constants used in the SAML specifications.'''
92   
93    # HTTP Constants
94   
95    # HTTP Request Method - POST.
96    POST_METHOD = "POST"
97   
98    # HTTP Method - GET.
99    GET_METHOD = "GET"
100   
101    # OpenSAML 2
102   
103    # Directory, on the classpath, schemas are located in.
104    SCHEMA_DIR = "/schema/"
105   
106    #    Core XML
107   
108    # XML core schema system Id.
109    XML_SCHEMA_LOCATION = SCHEMA_DIR + "xml.xsd"
110   
111    #  XML Signature schema Id.
112    XMLSIG_SCHEMA_LOCATION = SCHEMA_DIR + "xmldsig-core-schema.xsd"
113   
114    # XML Encryption schema Id.
115    XMLENC_SCHEMA_LOCATION = SCHEMA_DIR + "xenc-schema.xsd"
116
117   
118    #    SOAP
119   
120    #  SOAP 1.1 schema Id.
121    SOAP11ENV_SCHEMA_LOCATION = SCHEMA_DIR + SCHEMA_DIR + "soap-envelope.xsd"
122   
123    #  SOAP 1.1 Envelope XML namespace.
124    SOAP11ENV_NS = "http:#schemas.xmlsoap.org/soap/envelope/"
125   
126    #  SOAP 1.1 Envelope QName prefix.
127    SOAP11ENV_PREFIX = "SOAP-ENV"
128   
129    #  Liberty PAOS XML Namespace.
130    PAOS_NS = "urn:liberty:paos:2003-08"
131   
132    #  Liberty PAOS QName prefix.
133    PAOS_PREFIX = "paos"
134
135   
136    #    SAML 1.X
137   
138    # SAML 1.0 Assertion schema system Id.
139    SAML10_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-assertion-01.xsd"
140   
141    # SAML 1.1 Assertion schema system Id.
142    SAML11_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-assertion-1.1.xsd"
143   
144    # SAML 1.X XML namespace.
145    SAML1_NS = "urn:oasis:names:tc:SAML:1.0:assertion"
146   
147    # SAML 1.0 Protocol schema system Id.
148    SAML10P_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-protocol-01.xsd"
149   
150    # SAML 1.1 Protocol schema system Id.
151    SAML11P_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-protocol-1.1.xsd"
152
153    # SAML 1.X protocol XML namespace.
154    SAML10P_NS = "urn:oasis:names:tc:SAML:1.0:protocol"
155   
156    # SAML 1.1 protocol XML namespace, used only in SAML 2 metadata protocol
157    # SupportEnumeration.
158    SAML11P_NS = "urn:oasis:names:tc:SAML:1.1:protocol"
159   
160    # SAML 1.X Protocol QName prefix.
161    SAML1P_PREFIX = "samlp"
162
163    # SAML 1.X Assertion QName prefix.
164    SAML1_PREFIX = "saml"
165   
166    # SAML 1 Metadata extension XML namespace.
167    SAML1MD_NS = "urn:oasis:names:tc:SAML:profiles:v1metadata"
168   
169    # SAML 1 Metadata extension schema system Id.
170    SAML1MD_SCHEMA_LOCATION = SCHEMA_DIR + "sstc-saml1x-metadata.xsd"
171   
172    # SAML 1 Metadata extension namespace prefix.
173    SAML1MD_PREFIX = "saml1md"
174   
175    # URI for SAML 1 Artifact binding.
176    SAML1_ARTIFACT_BINDING_URI = \
177        "urn:oasis:names:tc:SAML:1.0:profiles:artifact-01"
178   
179    # URI for SAML 1 POST binding.
180    SAML1_POST_BINDING_URI = \
181        "urn:oasis:names:tc:SAML:1.0:profiles:browser-post"
182   
183    # URI for SAML 1 SOAP 1.1 binding.
184    SAML1_SOAP11_BINDING_URI = \
185        "urn:oasis:names:tc:SAML:1.0:bindings:SOAP-binding"
186   
187   
188    #    SAML 2.0
189   
190    # SAML 2.0 Assertion schema Id.
191    SAML20_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-assertion-2.0.xsd"
192   
193    # SAML 2.0 Assertion XML Namespace.
194    SAML20_NS = "urn:oasis:names:tc:SAML:2.0:assertion"
195   
196    # SAML 2.0 Assertion QName prefix.
197    SAML20_PREFIX ="saml"
198   
199    # SAML 2.0 Protocol schema Id.
200    SAML20P_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-protocol-2.0.xsd"
201   
202    # SAML 2.0 Protocol XML Namespace.
203    SAML20P_NS = "urn:oasis:names:tc:SAML:2.0:protocol"
204   
205    # SAML 2.0 Protocol QName prefix.
206    SAML20P_PREFIX ="samlp"
207   
208    # SAML 2.0 Protocol Third-party extension schema Id.
209    SAML20PTHRPTY_SCHEMA_LOCATION = SCHEMA_DIR + \
210                                    "sstc-saml-protocol-ext-thirdparty.xsd"
211   
212    # SAML 2.0 Protocol XML Namespace.
213    SAML20PTHRPTY_NS = "urn:oasis:names:tc:SAML:protocol:ext:third-party"
214   
215    # SAML 2.0 Protocol QName prefix.
216    SAML20PTHRPTY_PREFIX ="thrpty"
217   
218    # SAML 2.0 Metadata schema Id.
219    SAML20MD_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-metadata-2.0.xsd"
220   
221    # SAML 2.0 Metadata XML Namespace.
222    SAML20MD_NS ="urn:oasis:names:tc:SAML:2.0:metadata"
223   
224    # SAML 2.0 Standalone Query Metadata extension XML namespace.
225    SAML20MDQUERY_NS = "urn:oasis:names:tc:SAML:metadata:ext:query"
226   
227    # SAML 2.0 Standalone Query Metadata extension schema system Id.
228    SAML20MDQUERY_SCHEMA_LOCATION = SCHEMA_DIR + \
229                                    "sstc-saml-metadata-ext-query.xsd"
230   
231    # SAML 2.0 Standalone Query Metadata extension prefix.
232    SAML20MDQUERY_PREFIX = "query"
233   
234    # SAML 2.0 Metadata QName prefix.
235    SAML20MD_PREFIX = "md"
236   
237    # SAML 2.0 Authentication Context schema Id.
238    SAML20AC_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-authn-context-2.0.xsd"
239   
240    # SAML 2.0 Authentication Context XML Namespace.
241    SAML20AC_NS ="urn:oasis:names:tc:SAML:2.0:ac"
242   
243    # SAML 2.0 Authentication Context QName prefix.
244    SAML20AC_PREFIX = "ac"
245   
246    # SAML 2.0 Enhanced Client/Proxy SSO Profile schema Id.
247    SAML20ECP_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-ecp-2.0.xsd"
248   
249    # SAML 2.0 Enhanced Client/Proxy SSO Profile XML Namespace.
250    SAML20ECP_NS = "urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp"
251   
252    # SAML 2.0 Enhanced Client/Proxy SSO Profile QName prefix.
253    SAML20ECP_PREFIX = "ecp"
254   
255    # SAML 2.0 DCE PAC Attribute Profile schema Id.
256    SAML20DCE_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-dce-2.0.xsd"
257   
258    # SAML 2.0 DCE PAC Attribute Profile XML Namespace.
259    SAML20DCE_NS = "urn:oasis:names:tc:SAML:2.0:profiles:attribute:DCE"
260   
261    # SAML 2.0 DCE PAC Attribute Profile QName prefix.
262    SAML20DCE_PREFIX = "DCE"
263   
264    # SAML 2.0 X.500 Attribute Profile schema Id.
265    SAML20X500_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-x500-2.0.xsd"
266   
267    # SAML 2.0 X.500 Attribute Profile XML Namespace.
268    SAML20X500_NS = "urn:oasis:names:tc:SAML:2.0:profiles:attribute:X500"
269   
270    # SAML 2.0 X.500 Attribute Profile QName prefix.
271    SAML20X500_PREFIX = "x500"
272   
273    # SAML 2.0 XACML Attribute Profile schema Id.
274    SAML20XACML_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-xacml-2.0.xsd"
275   
276    # SAML 2.0 XACML Attribute Profile XML Namespace.
277    SAML20XACML_NS = "urn:oasis:names:tc:SAML:2.0:profiles:attribute:XACML"
278   
279    # SAML 2.0 XACML Attribute Profile QName prefix.
280    SAML20XACML_PREFIX = "xacmlprof"
281   
282    # URI for SAML 2 Artifact binding.
283    SAML2_ARTIFACT_BINDING_URI = \
284                        "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact"
285   
286    # URI for SAML 2 POST binding.
287    SAML2_POST_BINDING_URI = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
288   
289    # URI for SAML 2 POST-SimpleSign binding.
290    SAML2_POST_SIMPLE_SIGN_BINDING_URI = \
291                "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST-SimpleSign"
292   
293    # URI for SAML 2 HTTP redirect binding.
294    SAML2_REDIRECT_BINDING_URI = \
295                "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
296   
297    # URI for SAML 2 SOAP binding.
298    SAML2_SOAP11_BINDING_URI = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
299
300
301class XMLObject(object):
302    """Abstract base class for XML representations of SAML objects"""
303   
304    def create(self, samlObject, makeNsDeclaration=True):
305        """Create an XML representation from the input SAML object"""
306        raise NotImplementedError()
307
308    def parse(self, source):
309        """Parse into XML representation
310        @type source: basestring/file
311        @param source: file object or string content containing XML
312        """
313        raise NotImplementedError()
314   
315    def serialize(self):
316        """Serialize the XML object into a string representation
317        """
318        raise NotImplementedError()
319       
320       
321class AssertionXMLObject(XMLObject):
322    """Specialisation to enable inclusion of datetime formatting for issue
323    instant
324    """
325    issueInstantFmt = "%Y-%m-%dT%H:%M:%SZ"
326   
327    @classmethod
328    def issueInstantDatetime2Str(cls, dtIssueInstant):
329        """Convert issue instant datetime to correct string type for output
330        @type dtIssueInstant: datetime.datetime
331        @param dtIssueInstant: issue instance as a datetime
332        @rtype: basestring
333        @return: issue instance as a string
334        """
335        return dtIssueInstant.strftime(AssertionXMLObject.issueInstantFmt)
336
Note: See TracBrowser for help on using the repository browser.