1 """SAML 2.0 common package
2
3 Implementation of SAML 2.0 for NDG Security
4
5 NERC DataGrid Project
6
7 This implementation is adapted from the Java OpenSAML implementation. The
8 copyright and licence information are included here:
9
10 Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
11
12 Licensed under the Apache License, Version 2.0 (the "License");
13 you may not use this file except in compliance with the License.
14 You may obtain a copy of the License at
15
16 http://www.apache.org/licenses/LICENSE-2.0
17
18 Unless required by applicable law or agreed to in writing, software
19 distributed under the License is distributed on an "AS IS" BASIS,
20 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 See the License for the specific language governing permissions and
22 limitations 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__ = "http://www.apache.org/licenses/LICENSE-2.0"
29 __contact__ = "Philip.Kershaw@stfc.ac.uk"
30 __revision__ = "$Id: __init__.py 7130 2010-06-30 13:33:07Z pjkersha $"
31 from ndg.saml.common.xml import SAMLConstants, QName
35 """Base class for all SAML types
36
37 @cvar DEFAULT_ELEMENT_LOCAL_NAME: default XML element name - derived classes
38 must specify
39 @type DEFAULT_ELEMENT_LOCAL_NAME: None
40 @ivar __qname: qualified name for XML element
41 @type __qname: ndg.saml.common.xml.QName
42 """
43 DEFAULT_ELEMENT_LOCAL_NAME = None
44 __slots__ = ('__qname',)
45
50 '''
51 @param namespaceURI: the namespace the element is in
52 @type namespaceURI: basestring
53 @param elementLocalName: the local name of the XML element this Object
54 represents, defaults to DEFAULT_ELEMENT_LOCAL_NAME. Ensure that this
55 is set to a valid string in derived classes rather the None base class
56 setting
57 @type elementLocalName: NoneType/basestring
58 @param namespacePrefix: the prefix for the given namespace
59 @type namespacePrefix: basestring
60 '''
61 if elementLocalName is None:
62 elementLocalName = self.__class__.DEFAULT_ELEMENT_LOCAL_NAME
63
64 self.__qname = QName(namespaceURI,
65 elementLocalName,
66 namespacePrefix)
67
68 @property
70 """Qualified Name for this type
71
72 @return: qualified name
73 @rtype: ndg.saml.common.xml.QName
74 """
75 return self.__qname
76
77 @classmethod
79 '''Parse from an XML representation into a SAML object. Abstract method
80 - derived types should implement
81
82 @type xmlObject: XML class e.g. ElementTree or 4Suite XML type
83 @param xmlObject: XML representation of SAML Object
84 @rtype: saml.saml2.common.SAMLObject derived type
85 @return: SAML object
86 '''
87 raise NotImplementedError()
88
89 @classmethod
90 - def toXML(cls, samlObject):
91 '''Convert the input SAML object into an XML representation. Abstract
92 method - derived types should implement
93 @type samlObject: saml.saml2.common.SAMLObject derived type
94 @param samlObject: SAML object
95 @rtype: XML class e.g. ElementTree or 4Suite XML
96 @return: XML representation of SAML Object
97 '''
98 raise NotImplementedError()
99
101 '''Enable pickling
102
103 @return: object's attribute dictionary
104 @rtype: dict
105 '''
106 _dict = {}
107 for attrName in SAMLObject.__slots__:
108
109
110 if attrName.startswith('__'):
111 attrName = "_SAMLObject" + attrName
112
113 try:
114 _dict[attrName] = getattr(self, attrName)
115 except:
116 pass
117
118 return _dict
119
121 '''Enable pickling
122
123 @param attrDict: object's attribute dictionary
124 @type attrDict: dict
125 '''
126 for attrName, val in attrDict.items():
127 setattr(self, attrName, val)
128
131 """Version helper class
132
133 @cvar VERSION_10: SAML Version 1.0 identifier
134 @type VERSION_10: tuple
135 @cvar VERSION_11: SAML Version 1.1 identifier
136 @type VERSION_11: tuple
137 @cvar VERSION_20: SAML Version 2.0 identifier
138 @type VERSION_20: tuple
139 @cvar KNOWN_VERSIONS: list of known SAML version identifiers
140 @type KNOWN_VERSIONS: tuple
141 @ivar __version: SAML version for the given class instance
142 @type __version: tuple
143 """
144
145 VERSION_10 = (1, 0)
146 VERSION_11 = (1, 1)
147 VERSION_20 = (2, 0)
148 KNOWN_VERSIONS = (VERSION_10, VERSION_11, VERSION_20)
149
150 __slots__ = ('__version', )
151
153 """Instantiate from a given input version
154 @param version: SAML version to set
155 @type version: basestring or tuple or list
156 @raise TypeError: unexpected type for version input
157 """
158 if isinstance(version, basestring):
159 self.__version = SAMLVersion.valueOf(version)
160 elif isinstance(version, (tuple, list)):
161 self.__version = tuple(version)
162 else:
163 raise TypeError("Expecting string, tuple or list type for SAML "
164 "version initialiser; got %r" % version)
165
167 '''Enable pickling
168
169 @return: object's attribute dictionary
170 @rtype: dict
171 '''
172 _dict = {}
173 for attrName in SAMLVersion.__slots__:
174
175
176 if attrName.startswith('__'):
177 attrName = "_SAMLVersion" + attrName
178
179 _dict[attrName] = getattr(self, attrName)
180
181 return _dict
182
184 '''Enable pickling
185
186 @param attrDict: object's attribute dictionary
187 @type attrDict: dict
188 '''
189 for attrName, val in attrDict.items():
190 setattr(self, attrName, val)
191
193 """
194 @return: string representation of SAML version
195 @rtype: string
196 """
197 return ".".join([str(i) for i in self.__version])
198
200 """Test for equality against an input version string, tuple or list
201
202 @param version: SAML version to test
203 @type version: SAMLVersion, basestring, tuple or list
204 @return: True if input and this object match
205 @rtype: bool
206 @raise TypeError: unexpected type for version input
207 """
208 if isinstance(version, SAMLVersion):
209 return str(self) == str(version)
210
211 elif isinstance(version, basestring):
212 return self.__version == SAMLVersion.valueOf(version)
213
214 elif isinstance(version, (tuple, list)):
215 return self.__version == tuple(version)
216 else:
217 raise TypeError("Expecting string, tuple or list type for SAML "
218 "version comparison; got %r" % version)
219
221 """Test True for this instance version not equal to input version
222
223 @param version: SAML version to test
224 @type version: SAMLVersion, basestring, tuple or list
225 @return: True if input and this object don't match
226 @rtype: bool
227 @raise TypeError: unexpected type for version input
228 """
229 return not self.__eq__(version)
230
232 """Test True for this instance version greater than input version
233
234 @param version: SAML version to test
235 @type version: SAMLVersion, basestring, tuple or list
236 @return: True if this instance version greater than input version
237 @rtype: bool
238 @raise TypeError: unexpected type for version input
239 """
240 if isinstance(version, basestring):
241 return self.__version > SAMLVersion.valueOf(version)
242 elif isinstance(version, (tuple, list)):
243 return self.__version > tuple(version)
244 else:
245 raise TypeError("Expecting string, tuple or list type for SAML "
246 "version comparison; got %r" % version)
247
249 """Test True for this instance version less than input version
250
251 @param version: SAML version to test
252 @type version: SAMLVersion, basestring, tuple or list
253 @return: True if this instance version less than input version
254 @rtype: bool
255 @raise TypeError: unexpected type for version input
256 """
257 if isinstance(version, basestring):
258 return self.__version < SAMLVersion.valueOf(version)
259 elif isinstance(version, (tuple, list)):
260 return self.__version < tuple(version)
261 else:
262 raise TypeError("Expecting string, tuple or list type for SAML "
263 "version comparison; got %r" % version)
264
266 """Test True for this instance version greater or equal to the input
267 version
268
269 @param version: SAML version to test
270 @type version: SAMLVersion, basestring, tuple or list
271 @return: True if this instance version greater than or equal to input
272 version
273 @rtype: bool
274 @raise TypeError: unexpected type for version input
275 """
276 if isinstance(version, basestring):
277 return self.__version >= SAMLVersion.valueOf(version)
278 elif isinstance(version, (tuple, list)):
279 return self.__version >= tuple(version)
280 else:
281 raise TypeError("Expecting string, tuple or list type for SAML "
282 "version comparison; got %r" % version)
283
285 """Test True for this instance version less than or equal to input
286 version
287
288 @param version: SAML version to test
289 @type version: SAMLVersion, basestring, tuple or list
290 @return: True if this instance version less than or equal to input
291 version
292 @rtype: bool
293 @raise TypeError: unexpected type for version input
294 """
295 if isinstance(version, basestring):
296 return self.__version <= SAMLVersion.valueOf(version)
297 elif isinstance(version, (tuple, list)):
298 return self.__version <= tuple(version)
299 else:
300 raise TypeError("Expecting string, tuple or list type for SAML "
301 "version comparison; got %r" % version)
302
303 @staticmethod
305 """Parse input string into version tuple
306 @type version: basestring
307 @param version: SAML version
308 @rtype: tuple
309 @return: SAML version tuple"""
310 return tuple([int(i) for i in version.split(".")])
311