source: TI12-security/trunk/python/ndg_security_saml/saml/common/xml.py @ 6069

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

Re-release as rc1

Line 
1"""SAML Common XML module
2
3Implementation of SAML 2.0 for NDG Security
4
5NERC DataGrid Project
6
7This implementation is adapted from the Java OpenSAML implementation.  The
8copyright and licence information are included here:
9
10Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
11
12Licensed under the Apache License, Version 2.0 (the "License");
13you may not use this file except in compliance with the License.
14You may obtain a copy of the License at
15
16http://www.apache.org/licenses/LICENSE-2.0
17
18Unless required by applicable law or agreed to in writing, software
19distributed under the License is distributed on an "AS IS" BASIS,
20WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21See the License for the specific language governing permissions and
22limitations under the License.
23"""
24__author__ = "P J Kershaw"
25__date__ = "23/07/09"
26__copyright__ = "(C) 2009 Science and Technology Facilities Council"
27__contact__ = "Philip.Kershaw@stfc.ac.uk"
28__license__ = "BSD - see LICENSE file in top-level directory"
29__contact__ = "Philip.Kershaw@stfc.ac.uk"
30__revision__ = "$Id: $"
31from saml.xml import XMLConstants   
32
33class SAMLConstants(XMLConstants):
34    '''XML related constants used in the SAML specifications.'''
35   
36    # HTTP Constants
37   
38    # HTTP Request Method - POST.
39    POST_METHOD = "POST"
40   
41    # HTTP Method - GET.
42    GET_METHOD = "GET"
43   
44    # OpenSAML 2
45   
46    # Directory, on the classpath, schemas are located in.
47    SCHEMA_DIR = "/schema/"
48   
49    #    Core XML
50   
51    # XML core schema system Id.
52    XML_SCHEMA_LOCATION = SCHEMA_DIR + "xml.xsd"
53   
54    #  XML Signature schema Id.
55    XMLSIG_SCHEMA_LOCATION = SCHEMA_DIR + "xmldsig-core-schema.xsd"
56   
57    # XML Encryption schema Id.
58    XMLENC_SCHEMA_LOCATION = SCHEMA_DIR + "xenc-schema.xsd"
59
60   
61    #    SOAP
62   
63    #  SOAP 1.1 schema Id.
64    SOAP11ENV_SCHEMA_LOCATION = SCHEMA_DIR + SCHEMA_DIR + "soap-envelope.xsd"
65   
66    #  SOAP 1.1 Envelope XML namespace.
67    SOAP11ENV_NS = "http://schemas.xmlsoap.org/soap/envelope/"
68   
69    #  SOAP 1.1 Envelope QName prefix.
70    SOAP11ENV_PREFIX = "SOAP-ENV"
71   
72    #  Liberty PAOS XML Namespace.
73    PAOS_NS = "urn:liberty:paos:2003-08"
74   
75    #  Liberty PAOS QName prefix.
76    PAOS_PREFIX = "paos"
77   
78    #    SAML 1.X
79   
80    # SAML 1.0 Assertion schema system Id.
81    SAML10_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-assertion-01.xsd"
82   
83    # SAML 1.1 Assertion schema system Id.
84    SAML11_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-assertion-1.1.xsd"
85   
86    # SAML 1.X XML namespace.
87    SAML1_NS = "urn:oasis:names:tc:SAML:1.0:assertion"
88   
89    # SAML 1.0 Protocol schema system Id.
90    SAML10P_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-protocol-01.xsd"
91   
92    # SAML 1.1 Protocol schema system Id.
93    SAML11P_SCHEMA_LOCATION = SCHEMA_DIR + "cs-sstc-schema-protocol-1.1.xsd"
94
95    # SAML 1.X protocol XML namespace.
96    SAML10P_NS = "urn:oasis:names:tc:SAML:1.0:protocol"
97   
98    # SAML 1.1 protocol XML namespace, used only in SAML 2 metadata protocol
99    # SupportEnumeration.
100    SAML11P_NS = "urn:oasis:names:tc:SAML:1.1:protocol"
101   
102    # SAML 1.X Protocol QName prefix.
103    SAML1P_PREFIX = "samlp"
104
105    # SAML 1.X Assertion QName prefix.
106    SAML1_PREFIX = "saml"
107   
108    # SAML 1 Metadata extension XML namespace.
109    SAML1MD_NS = "urn:oasis:names:tc:SAML:profiles:v1metadata"
110   
111    # SAML 1 Metadata extension schema system Id.
112    SAML1MD_SCHEMA_LOCATION = SCHEMA_DIR + "sstc-saml1x-metadata.xsd"
113   
114    # SAML 1 Metadata extension namespace prefix.
115    SAML1MD_PREFIX = "saml1md"
116   
117    # URI for SAML 1 Artifact binding.
118    SAML1_ARTIFACT_BINDING_URI = \
119        "urn:oasis:names:tc:SAML:1.0:profiles:artifact-01"
120   
121    # URI for SAML 1 POST binding.
122    SAML1_POST_BINDING_URI = \
123        "urn:oasis:names:tc:SAML:1.0:profiles:browser-post"
124   
125    # URI for SAML 1 SOAP 1.1 binding.
126    SAML1_SOAP11_BINDING_URI = \
127        "urn:oasis:names:tc:SAML:1.0:bindings:SOAP-binding"
128   
129    #    SAML 2.0
130   
131    # SAML 2.0 Assertion schema Id.
132    SAML20_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-assertion-2.0.xsd"
133   
134    # SAML 2.0 Assertion XML Namespace.
135    SAML20_NS = "urn:oasis:names:tc:SAML:2.0:assertion"
136   
137    # SAML 2.0 Assertion QName prefix.
138    SAML20_PREFIX ="saml"
139   
140    # SAML 2.0 Protocol schema Id.
141    SAML20P_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-protocol-2.0.xsd"
142   
143    # SAML 2.0 Protocol XML Namespace.
144    SAML20P_NS = "urn:oasis:names:tc:SAML:2.0:protocol"
145   
146    # SAML 2.0 Protocol QName prefix.
147    SAML20P_PREFIX ="samlp"
148   
149    # SAML 2.0 Protocol Third-party extension schema Id.
150    SAML20PTHRPTY_SCHEMA_LOCATION = SCHEMA_DIR + \
151                                    "sstc-saml-protocol-ext-thirdparty.xsd"
152   
153    # SAML 2.0 Protocol XML Namespace.
154    SAML20PTHRPTY_NS = "urn:oasis:names:tc:SAML:protocol:ext:third-party"
155   
156    # SAML 2.0 Protocol QName prefix.
157    SAML20PTHRPTY_PREFIX ="thrpty"
158   
159    # SAML 2.0 Metadata schema Id.
160    SAML20MD_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-metadata-2.0.xsd"
161   
162    # SAML 2.0 Metadata XML Namespace.
163    SAML20MD_NS ="urn:oasis:names:tc:SAML:2.0:metadata"
164   
165    # SAML 2.0 Standalone Query Metadata extension XML namespace.
166    SAML20MDQUERY_NS = "urn:oasis:names:tc:SAML:metadata:ext:query"
167   
168    # SAML 2.0 Standalone Query Metadata extension schema system Id.
169    SAML20MDQUERY_SCHEMA_LOCATION = SCHEMA_DIR + \
170                                    "sstc-saml-metadata-ext-query.xsd"
171   
172    # SAML 2.0 Standalone Query Metadata extension prefix.
173    SAML20MDQUERY_PREFIX = "query"
174   
175    # SAML 2.0 Metadata QName prefix.
176    SAML20MD_PREFIX = "md"
177   
178    # SAML 2.0 Authentication Context schema Id.
179    SAML20AC_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-authn-context-2.0.xsd"
180   
181    # SAML 2.0 Authentication Context XML Namespace.
182    SAML20AC_NS ="urn:oasis:names:tc:SAML:2.0:ac"
183   
184    # SAML 2.0 Authentication Context QName prefix.
185    SAML20AC_PREFIX = "ac"
186   
187    # SAML 2.0 Enhanced Client/Proxy SSO Profile schema Id.
188    SAML20ECP_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-ecp-2.0.xsd"
189   
190    # SAML 2.0 Enhanced Client/Proxy SSO Profile XML Namespace.
191    SAML20ECP_NS = "urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp"
192   
193    # SAML 2.0 Enhanced Client/Proxy SSO Profile QName prefix.
194    SAML20ECP_PREFIX = "ecp"
195   
196    # SAML 2.0 DCE PAC Attribute Profile schema Id.
197    SAML20DCE_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-dce-2.0.xsd"
198   
199    # SAML 2.0 DCE PAC Attribute Profile XML Namespace.
200    SAML20DCE_NS = "urn:oasis:names:tc:SAML:2.0:profiles:attribute:DCE"
201   
202    # SAML 2.0 DCE PAC Attribute Profile QName prefix.
203    SAML20DCE_PREFIX = "DCE"
204   
205    # SAML 2.0 X.500 Attribute Profile schema Id.
206    SAML20X500_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-x500-2.0.xsd"
207   
208    # SAML 2.0 X.500 Attribute Profile XML Namespace.
209    SAML20X500_NS = "urn:oasis:names:tc:SAML:2.0:profiles:attribute:X500"
210   
211    # SAML 2.0 X.500 Attribute Profile QName prefix.
212    SAML20X500_PREFIX = "x500"
213   
214    # SAML 2.0 XACML Attribute Profile schema Id.
215    SAML20XACML_SCHEMA_LOCATION = SCHEMA_DIR + "saml-schema-xacml-2.0.xsd"
216   
217    # SAML 2.0 XACML Attribute Profile XML Namespace.
218    SAML20XACML_NS = "urn:oasis:names:tc:SAML:2.0:profiles:attribute:XACML"
219   
220    # SAML 2.0 XACML Attribute Profile QName prefix.
221    SAML20XACML_PREFIX = "xacmlprof"
222   
223    # URI for SAML 2 Artifact binding.
224    SAML2_ARTIFACT_BINDING_URI = \
225                        "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact"
226   
227    # URI for SAML 2 POST binding.
228    SAML2_POST_BINDING_URI = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
229   
230    # URI for SAML 2 POST-SimpleSign binding.
231    SAML2_POST_SIMPLE_SIGN_BINDING_URI = \
232                "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST-SimpleSign"
233   
234    # URI for SAML 2 HTTP redirect binding.
235    SAML2_REDIRECT_BINDING_URI = \
236                "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
237   
238    # URI for SAML 2 SOAP binding.
239    SAML2_SOAP11_BINDING_URI = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
240 
241   
242class QName(object):
243    """XML Qualified Name""" 
244
245    def __init__(self, namespaceURI, localPart, prefix):
246        self.namespaceURI = namespaceURI
247        self.localPart = localPart
248        self.prefix = prefix
249   
250    def _getPrefix(self):
251        return self.__prefix
252
253    def _setPrefix(self, value):
254        if not isinstance(value, basestring):
255            raise TypeError('Expected string type for "prefix"; got %r' %
256                            type(value))
257        self.__prefix = value
258   
259    prefix = property(_getPrefix, _setPrefix, None, "Namespace Prefix")
260
261    def _getLocalPart(self):
262        return self.__localPart
263   
264    def _setLocalPart(self, value):
265        if not isinstance(value, basestring):
266            raise TypeError('Expected string type for "localPart"; got %r' %
267                            type(value))
268        self.__localPart = value
269       
270    localPart = property(_getLocalPart, _setLocalPart, None, "LocalPart")
271
272    def _getNamespaceURI(self):
273        return self.__namespaceURI
274
275    def _setNamespaceURI(self, value):
276        if not isinstance(value, basestring):
277            raise TypeError('Expected string type for "namespaceURI"; got %r' %
278                            type(value))
279        self.__namespaceURI = value
280 
281    namespaceURI = property(_getNamespaceURI, _setNamespaceURI, None, 
282                            "Namespace URI")
283
284    def __eq__(self, qname):
285        """Enable equality check for QName
286        @type qname: saml.common.xml.QName
287        @param qname: Qualified Name to compare with self
288        """
289        if not isinstance(qname, QName):
290            raise TypeError('Expecting %r; got %r' % (QName, type(qname)))
291                           
292        return (self.prefix, self.namespaceURI, self.localPart) == \
293               (qname.prefix, qname.namespaceURI, qname.localPart)
294
295    def __ne__(self, qname):
296        """Enable equality check for QName
297        @type qname: saml.common.xml.QName
298        @param qname: Qualified Name to compare with self
299        """
300        return not self.__eq__(qname)
Note: See TracBrowser for help on using the repository browser.