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

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

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

Line 
1"""Implementation of SAML 2.0 for NDG Security - XML package
2
3NERC DataGrid Project
4
5This implementation is adapted from the Java OpenSAML implementation.  The
6copyright and licence information are included here:
7
8Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
9
10Licensed under the Apache License, Version 2.0 (the "License");
11you may not use this file except in compliance with the License.
12You may obtain a copy of the License at
13
14http://www.apache.org/licenses/LICENSE-2.0
15
16Unless required by applicable law or agreed to in writing, software
17distributed under the License is distributed on an "AS IS" BASIS,
18WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19See the License for the specific language governing permissions and
20limitations under the License.
21"""
22__author__ = "P J Kershaw"
23__date__ = "23/07/09"
24__copyright__ = "(C) 2009 Science and Technology Facilities Council"
25__contact__ = "Philip.Kershaw@stfc.ac.uk"
26__license__ = "BSD - see LICENSE file in top-level directory"
27__contact__ = "Philip.Kershaw@stfc.ac.uk"
28__revision__ = "$Id$"
29try:
30    from datetime import strptime
31except ImportError:
32    # Allow for Python < 2.5
33    from time import strptime as _strptime
34    strptime = lambda datetimeStr, format: datetime(*(_strptime(datetimeStr, 
35                                                                format)[0:6]))
36from datetime import datetime
37
38class XMLObjectError(Exception):
39    pass
40
41class XMLObjectParseError(Exception):
42    pass
43
44class XMLObject(object):
45    """Abstract base class for XML representations of SAML objects"""
46   
47    def create(self, samlObject):
48        """Create an XML representation from the input SAML object
49        @type samlObject: SAMLObject
50        param samlObject: SAML object to render into XML
51        """
52        raise NotImplementedError()
53
54    def parse(self, elem):
55        """Parse into XML representation
56        @type elem: object
57        @param elem: XML object - type depends on XML class representation
58        @rtype: SAMLObject
59        @return: equivalent SAML object
60        @raise XMLObjectParsingError: error parsing content into SAML
61        representation
62        """
63        raise NotImplementedError()
64   
65    def serialize(self):
66        """Serialize the XML object into a string representation
67        """
68        raise NotImplementedError()
69       
70       
71class IssueInstantXMLObject(XMLObject):
72    """Specialisation to enable inclusion of datetime formatting for issue
73    instant
74    """
75    issueInstantFmt = "%Y-%m-%dT%H:%M:%SZ"
76   
77    @classmethod
78    def datetime2Str(cls, dtIssueInstant):
79        """Convert issue instant datetime to correct string type for output
80        @type dtIssueInstant: datetime.datetime
81        @param dtIssueInstant: issue instance as a datetime
82        @rtype: basestring
83        @return: issue instance as a string
84        """
85        if not isinstance(dtIssueInstant, datetime):
86            raise TypeError("Expecting datetime type for string conversion, "
87                            "got %r" % dtIssueInstant)
88           
89        return dtIssueInstant.strftime(IssueInstantXMLObject.issueInstantFmt)
90
91    @classmethod
92    def str2Datetime(cls, issueInstant):
93        """Convert issue instant string to datetime type
94        @type issueInstant: basestring
95        @param issueInstant: issue instance as a string
96        @rtype: datetime.datetime
97        @return: issue instance as a datetime
98        """
99        if not isinstance(issueInstant, basestring):
100            raise TypeError("Expecting basestring derived type for string "
101                            "conversion, got %r" % issueInstant)
102           
103        return datetime.strptime(issueInstant, 
104                                 IssueInstantXMLObject.issueInstantFmt)
105       
106   
107class QName(object):
108    """XML Qualified Name""" 
109
110    def __init__(self, namespaceURI, localPart, prefix):
111        self.namespaceURI = namespaceURI
112        self.localPart = localPart
113        self.prefix = prefix
114   
115    def _getPrefix(self):
116        return self.__prefix
117
118    def _setPrefix(self, value):
119        if not isinstance(value, basestring):
120            raise TypeError('Expected string type for "prefix"; got %r' %
121                            type(value))
122        self.__prefix = value
123   
124    prefix = property(_getPrefix, _setPrefix, None, "Prefix")
125
126    def _getLocalPart(self):
127        return self.__localPart
128   
129    def _setLocalPart(self, value):
130        if not isinstance(value, basestring):
131            raise TypeError('Expected string type for "localPart"; got %r' %
132                            type(value))
133        self.__localPart = value
134       
135    localPart = property(_getLocalPart, _setLocalPart, None, "LocalPart")
136
137    def _getNamespaceURI(self):
138        return self.__namespaceURI
139
140    def _setNamespaceURI(self, value):
141        if not isinstance(value, basestring):
142            raise TypeError('Expected string type for "namespaceURI"; got %r' %
143                            type(value))
144        self.__namespaceURI = value
145 
146    namespaceURI = property(_getNamespaceURI, _setNamespaceURI, None, 
147                            "Namespace URI'")
Note: See TracBrowser for help on using the repository browser.