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

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

Refactored SAML package structure into saml2, core and common sub-packages

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    @classmethod
35    def parse(cls, xmlObject):
36        raise NotImplementedError()
37   
38    @classmethod
39    def create(cls, samlObject):
40        raise NotImplementedError()
41
42
43class SAMLVersion(SAMLObject):
44    """Version helper class"""
45    VERSION_10 = (1, 0)
46    VERSION_11 = (1, 1)
47    VERSION_20 = (2, 0)
48    KNOWN_VERSIONS = (VERSION_10, VERSION_11, VERSION_20)
49   
50    def __init__(self, version):
51        if isinstance(version, basestring):
52            self.__version = SAMLVersion.valueOf(version)
53        elif isinstance(version, (tuple, list)):
54            self.__version = tuple(version)
55        else:
56            raise TypeError("Expecting string, tuple or list type for SAML "
57                            "version initialiser; got %r" % version)
58   
59    def __str__(self):
60        return ".".join([str(i) for i in self.__version])
61   
62    def __eq__(self, version):
63        """Test for equality against an input version string, tuple or list"""
64               
65        if isinstance(version, basestring):
66            return self.__version == SAMLVersion.valueOf(version)
67        elif isinstance(version, (tuple, list)):
68            return self.__version == tuple(version)
69        else:
70            raise TypeError("Expecting string, tuple or list type for SAML "
71                            "version comparison; got %r" % version)
72           
73    def __ne__(self, version):
74        return self.__eq__(version)
75   
76    @staticmethod
77    def valueOf(version):
78        """Parse input string into version tuple
79        @type version: version
80        @param version: SAML version
81        @rtype: tuple
82        @return: SAML version tuple"""
83        return tuple(version.split("."))
Note: See TracBrowser for help on using the repository browser.