source: TI12-security/trunk/ndg_saml/ndg/saml/utils/__init__.py @ 7143

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_saml/ndg/saml/utils/__init__.py@7143
Revision 7143, 5.5 KB checked in by pjkersha, 10 years ago (diff)

Incomplete - task 2: XACML-Security Integration

  • working query interface middleware for authz decision interface unit tests
  • Property svn:keywords set to Id
Line 
1"""Utilities module for NDG Security SAML implementation
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__ = "10/08/09"
24__copyright__ = "(C) 2009 Science and Technology Facilities Council"
25__license__ = "http://www.apache.org/licenses/LICENSE-2.0"
26__contact__ = "Philip.Kershaw@stfc.ac.uk"
27__revision__ = '$Id$'
28try:
29    from datetime import strptime
30except ImportError:
31    # Allow for Python < 2.5
32    from time import strptime as _strptime
33    strptime = lambda datetimeStr, format: datetime(*(_strptime(datetimeStr, 
34                                                                format)[0:6]))
35from datetime import datetime, timedelta
36
37       
38# Interpret a string as a boolean
39str2Bool = lambda str: str.lower() in ("yes", "true", "t", "1")
40
41     
42class SAMLDateTime(object):
43    """Generic datetime formatting utility for SAML timestamps - XMLSchema
44    Datetime format
45   
46    @cvar DATETIME_FORMAT: date/time format string for SAML timestamps
47    @type DATETIME_FORMAT: string
48    """
49    DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
50   
51    @classmethod
52    def toString(cls, dtValue):
53        """Convert issue instant datetime to correct string type for output
54       
55        @type dtValue: datetime.datetime
56        @param dtValue: issue instance as a datetime
57        @rtype: basestring
58        @return: issue instance as a string
59        """
60        if not isinstance(dtValue, datetime):
61            raise TypeError("Expecting datetime type for string conversion, "
62                            "got %r" % dtValue)
63           
64        # isoformat provides the correct formatting
65#        return dtIssueInstant.strftime(cls.DATETIME_FORMAT)
66        return datetime.isoformat(dtValue)+'Z'
67
68    @classmethod
69    def fromString(cls, strDateTime):
70        """Convert issue instant string to datetime type
71       
72        @type strDateTime: basestring
73        @param strDateTime: issue instance as a string
74        @rtype: datetime.datetime
75        @return: issue instance as a datetime
76        """
77        if not isinstance(strDateTime, basestring):
78            raise TypeError("Expecting basestring derived type for string "
79                            "conversion, got %r" % strDateTime)
80       
81        # Workaround for seconds fraction as strptime doesn't seem able to deal
82        # with this
83        strDateTimeFraction, strSecondsFraction = strDateTime.split('.')
84        dtValue = datetime.strptime(strDateTimeFraction, cls.DATETIME_FORMAT)
85        secondsFraction = float("0." + strSecondsFraction.replace('Z', ''))
86        dtValue += timedelta(seconds=secondsFraction)
87        return dtValue
88
89
90class TypedList(list):
91    """Extend list type to enabled only items of a given type.  Supports
92    any type where the array type in the Standard Library is restricted to
93    only limited set of primitive types
94    """
95   
96    def __init__(self, elementType, *arg, **kw):
97        """
98        @type elementType: type/tuple
99        @param elementType: object type or types which the list is allowed to
100        contain.  If more than one type, pass as a tuple
101        """
102        self.__elementType = elementType
103        super(TypedList, self).__init__(*arg, **kw)
104   
105    def _getElementType(self):
106        """@return: element type for this list
107        @rtype: type
108        """
109        return self.__elementType
110   
111    elementType = property(fget=_getElementType, 
112                           doc="The allowed type or types for list elements")
113     
114    def extend(self, iter):
115        """Extend an existing list with the input iterable
116        @param iter: iterable to extend list with
117        @type iter: iterable
118        """
119        for i in iter:
120            if not isinstance(i, self.__elementType):
121                raise TypeError("List items must be of type %s" % 
122                                (self.__elementType,))
123               
124        return super(TypedList, self).extend(iter)
125       
126    def __iadd__(self, iter):
127        """Extend an existing list with the input iterable with += operator
128       
129        @param iter: iterable to extend list with
130        @type iter: iterable
131        """
132        for i in iter:
133            if not isinstance(i, self.__elementType):
134                raise TypeError("List items must be of type %s" % 
135                                (self.__elementType,))
136                   
137        return super(TypedList, self).__iadd__(iter)
138         
139    def append(self, item):
140        """Append a list with the given item
141       
142        @param item: item to extend list
143        @type item: must agree witj "elementType" attribute of this list
144        """
145        if not isinstance(item, self.__elementType):
146                raise TypeError("List items must be of type %s" % 
147                                (self.__elementType,))
148   
149        return super(TypedList, self).append(item)
Note: See TracBrowser for help on using the repository browser.