source: TI12-security/trunk/ndg_security_saml/saml/common/__init__.py @ 6547

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

Working ElementTree serialisation/deserialisation for AuthzDecisionQuery?

Line 
1"""SAML 2.0 common package
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__ = "11/08/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.common.xml import SAMLConstants, QName
32 
33
34class SAMLObject(object):
35    """Base class for all SAML types"""
36    DEFAULT_ELEMENT_LOCAL_NAME = None
37    __slots__ = ('__qname',)
38   
39    def __init__(self,
40                 namespaceURI=SAMLConstants.SAML20_NS, 
41                 elementLocalName=None, 
42                 namespacePrefix=SAMLConstants.SAML20_PREFIX):
43        '''@param namespaceURI: the namespace the element is in
44        @param elementLocalName: the local name of the XML element this Object
45        represents
46        @param namespacePrefix: the prefix for the given namespace
47        '''
48        if elementLocalName is None:
49            elementLocalName = self.__class__.DEFAULT_ELEMENT_LOCAL_NAME
50           
51        self.__qname = QName(namespaceURI, 
52                             elementLocalName, 
53                             namespacePrefix)
54   
55    @property
56    def qname(self):
57        "Qualified Name for this type"
58        return self.__qname
59           
60    @classmethod
61    def fromXML(cls, xmlObject):
62        '''Parse from an XML representation into a SAML object
63        @type: XML class e.g. ElementTree or 4Suite XML
64        @param: XML representation of SAML Object
65        @rtype: saml.saml2.common.SAMLObject derived type
66        @return: SAML object
67        '''
68        raise NotImplementedError()
69   
70    @classmethod
71    def toXML(cls, samlObject):
72        '''Convert the input SAML object into an XML representation
73        @type: saml.saml2.common.SAMLObject derived type
74        @param: SAML object
75        @rtype: XML class e.g. ElementTree or 4Suite XML
76        @return: XML representation of SAML Object
77        '''
78        raise NotImplementedError()
79
80
81class SAMLVersion(SAMLObject):
82    """Version helper class"""
83   
84    VERSION_10 = (1, 0)
85    VERSION_11 = (1, 1)
86    VERSION_20 = (2, 0)
87    KNOWN_VERSIONS = (VERSION_10, VERSION_11, VERSION_20)
88   
89    def __init__(self, version):
90        if isinstance(version, basestring):
91            self.__version = SAMLVersion.valueOf(version)
92        elif isinstance(version, (tuple, list)):
93            self.__version = tuple(version)
94        else:
95            raise TypeError("Expecting string, tuple or list type for SAML "
96                            "version initialiser; got %r" % version)
97   
98    def __str__(self):
99        return ".".join([str(i) for i in self.__version])
100   
101    def __eq__(self, version):
102        """Test for equality against an input version string, tuple or list"""
103               
104        if isinstance(version, basestring):
105            return self.__version == SAMLVersion.valueOf(version)
106        elif isinstance(version, (tuple, list)):
107            return self.__version == tuple(version)
108        else:
109            raise TypeError("Expecting string, tuple or list type for SAML "
110                            "version comparison; got %r" % version)
111           
112    def __ne__(self, version):
113        return not self.__eq__(version)
114           
115    def __gt__(self, version):               
116        if isinstance(version, basestring):
117            return self.__version > SAMLVersion.valueOf(version)
118        elif isinstance(version, (tuple, list)):
119            return self.__version > tuple(version)
120        else:
121            raise TypeError("Expecting string, tuple or list type for SAML "
122                            "version comparison; got %r" % version)
123           
124    def __lt__(self, version):
125        if isinstance(version, basestring):
126            return self.__version < SAMLVersion.valueOf(version)
127        elif isinstance(version, (tuple, list)):
128            return self.__version < tuple(version)
129        else:
130            raise TypeError("Expecting string, tuple or list type for SAML "
131                            "version comparison; got %r" % version)
132           
133    def __ge__(self, version):               
134        if isinstance(version, basestring):
135            return self.__version >= SAMLVersion.valueOf(version)
136        elif isinstance(version, (tuple, list)):
137            return self.__version >= tuple(version)
138        else:
139            raise TypeError("Expecting string, tuple or list type for SAML "
140                            "version comparison; got %r" % version)
141           
142    def __le__(self, version):               
143        if isinstance(version, basestring):
144            return self.__version <= SAMLVersion.valueOf(version)
145        elif isinstance(version, (tuple, list)):
146            return self.__version <= tuple(version)
147        else:
148            raise TypeError("Expecting string, tuple or list type for SAML "
149                            "version comparison; got %r" % version)
150   
151    @staticmethod
152    def valueOf(version):
153        """Parse input string into version tuple
154        @type version: version
155        @param version: SAML version
156        @rtype: tuple
157        @return: SAML version tuple"""
158        return tuple([int(i) for i in version.split(".")])
Note: See TracBrowser for help on using the repository browser.