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

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

Working SAML Assertion code with ESG specific Group/Role? attributes

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