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

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

Added new types to support AuthzDecisionQuery? - the client query object for the authorisation service.

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: $"
31 
32class SAMLObject(object):
33    """Base class for all SAML types"""
34    __slots__ = ()
35   
36    @classmethod
37    def fromXML(cls, xmlObject):
38        '''Parse from an XML representation into a SAML object
39        @type: XML class e.g. ElementTree or 4Suite XML
40        @param: XML representation of SAML Object
41        @rtype: saml.saml2.common.SAMLObject derived type
42        @return: SAML object
43        '''
44        raise NotImplementedError()
45   
46    @classmethod
47    def toXML(cls, samlObject):
48        '''Convert the input SAML object into an XML representation
49        @type: saml.saml2.common.SAMLObject derived type
50        @param: SAML object
51        @rtype: XML class e.g. ElementTree or 4Suite XML
52        @return: XML representation of SAML Object
53        '''
54        raise NotImplementedError()
55
56
57class SAMLVersion(SAMLObject):
58    """Version helper class"""
59   
60    VERSION_10 = (1, 0)
61    VERSION_11 = (1, 1)
62    VERSION_20 = (2, 0)
63    KNOWN_VERSIONS = (VERSION_10, VERSION_11, VERSION_20)
64   
65    def __init__(self, version):
66        if isinstance(version, basestring):
67            self.__version = SAMLVersion.valueOf(version)
68        elif isinstance(version, (tuple, list)):
69            self.__version = tuple(version)
70        else:
71            raise TypeError("Expecting string, tuple or list type for SAML "
72                            "version initialiser; got %r" % version)
73   
74    def __str__(self):
75        return ".".join([str(i) for i in self.__version])
76   
77    def __eq__(self, version):
78        """Test for equality against an input version string, tuple or list"""
79               
80        if isinstance(version, basestring):
81            return self.__version == SAMLVersion.valueOf(version)
82        elif isinstance(version, (tuple, list)):
83            return self.__version == tuple(version)
84        else:
85            raise TypeError("Expecting string, tuple or list type for SAML "
86                            "version comparison; got %r" % version)
87           
88    def __ne__(self, version):
89        return not self.__eq__(version)
90           
91    def __gt__(self, version):               
92        if isinstance(version, basestring):
93            return self.__version > SAMLVersion.valueOf(version)
94        elif isinstance(version, (tuple, list)):
95            return self.__version > tuple(version)
96        else:
97            raise TypeError("Expecting string, tuple or list type for SAML "
98                            "version comparison; got %r" % version)
99           
100    def __lt__(self, version):
101        if isinstance(version, basestring):
102            return self.__version < SAMLVersion.valueOf(version)
103        elif isinstance(version, (tuple, list)):
104            return self.__version < tuple(version)
105        else:
106            raise TypeError("Expecting string, tuple or list type for SAML "
107                            "version comparison; got %r" % version)
108           
109    def __ge__(self, version):               
110        if isinstance(version, basestring):
111            return self.__version >= SAMLVersion.valueOf(version)
112        elif isinstance(version, (tuple, list)):
113            return self.__version >= tuple(version)
114        else:
115            raise TypeError("Expecting string, tuple or list type for SAML "
116                            "version comparison; got %r" % version)
117           
118    def __le__(self, version):               
119        if isinstance(version, basestring):
120            return self.__version <= SAMLVersion.valueOf(version)
121        elif isinstance(version, (tuple, list)):
122            return self.__version <= tuple(version)
123        else:
124            raise TypeError("Expecting string, tuple or list type for SAML "
125                            "version comparison; got %r" % version)
126   
127    @staticmethod
128    def valueOf(version):
129        """Parse input string into version tuple
130        @type version: version
131        @param version: SAML version
132        @rtype: tuple
133        @return: SAML version tuple"""
134        return tuple([int(i) for i in version.split(".")])
Note: See TracBrowser for help on using the repository browser.