source: TI12-security/trunk/ndg_xacml/ndg/xacml/core/attributevalue.py @ 7109

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/core/attributevalue.py@7109
Revision 7109, 9.0 KB checked in by pjkersha, 10 years ago (diff)

Incomplete - task 2: XACML-Security Integration

  • updating epydoc ready for release.
  • Property svn:keywords set to Id
Line 
1"""NDG XACML attribute type definition
2
3NERC DataGrid
4"""
5__author__ = "P J Kershaw"
6__date__ = "25/02/10"
7__copyright__ = "(C) 2010 Science and Technology Facilities Council"
8__contact__ = "Philip.Kershaw@stfc.ac.uk"
9__license__ = "BSD - see LICENSE file in top-level directory"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = "$Id$"
12from datetime import datetime, timedelta
13
14from ndg.xacml.utils import VettedDict
15from ndg.xacml.core.expression import Expression
16   
17
18class AttributeValue(Expression):
19    """XACML Attribute Value type
20   
21    @cvar ELEMENT_LOCAL_NAME: XML local name for this element
22    @type ELEMENT_LOCAL_NAME: string 
23    @cvar CLASS_NAME_SUFFIX: all attribute value classes end with this suffix
24    @type CLASS_NAME_SUFFIX: string
25    @cvar IDENTIFIER_PREFIX: geberic prefix for attribute value URNs
26    @type IDENTIFIER_PREFIX: string
27    @cvar IDENTIFIER: URN for attribute value in derived class
28    @type IDENTIFIER: NoneType - derived classes should set to appropriate
29    string
30    @cvar TYPE_URIS: URIs for all the different supported types
31    @type TYPE_URIS: tuple
32    @cvar TYPE_NAMES: corresponding short names for all the types
33    @type TYPE_NAMES: tuple
34    @cvar NATIVE_TYPES: equivalent python types as implemented
35    @cvar TYPE_MAP: mapping from type names to python types
36    @type TYPE_MAP: dict
37    @cvar TYPE_URI_MAP: mapping from type names to type URIs
38    @type TYPE_URI_MAP: dict
39    @cvar TYPE: type name for derived type - set to None in this parent class
40    @type TYPE: NoneType / string in derived type
41   
42    @ivar __value: setting for this attribute value
43    @type __value: any - constrained in derived classes
44    """
45    ELEMENT_LOCAL_NAME = 'AttributeValue'
46    CLASS_NAME_SUFFIX = 'AttributeValue'
47    IDENTIFIER_PREFIX = 'http://www.w3.org/2001/XMLSchema#'
48 
49    IDENTIFIER = None
50    TYPE_URIS = (
51    'http://www.w3.org/2001/XMLSchema#string',
52    'http://www.w3.org/2001/XMLSchema#anyURI',
53    'http://www.w3.org/2001/XMLSchema#integer',
54    'http://www.w3.org/2001/XMLSchema#boolean',
55    'http://www.w3.org/2001/XMLSchema#double',
56    'http://www.w3.org/2001/XMLSchema#date',
57    'http://www.w3.org/2001/XMLSchema#dateTime',
58    'http://www.w3.org/2001/XMLSchema#time',
59    'http://www.w3.org/TR/2002/WD-xquery-operators-20020816#dayTimeDuration',
60    'http://www.w3.org/TR/2002/WD-xquery-operators-20020816#yearMonthDuration',
61    'urn:oasis:names:tc:xacml:1.0:data-type:x500Name',
62    'urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name',
63    'http://www.w3.org/2001/XMLSchema#hexBinary',
64    'http://www.w3.org/2001/XMLSchema#base64Binary',
65    'urn:oasis:names:tc:xacml:2.0:data-type:ipAddress',
66    'urn:oasis:names:tc:xacml:2.0:data-type:dnsName'
67    )
68    TYPE_NAMES = (
69        'String',
70        'AnyURI',
71        'Integer',
72        'Boolean',
73        'Double',
74        'Date',
75        'DateTime',
76        'Time',
77        'DayTimeDuration',
78        'YearMonthDuration',
79        'X500Name',
80        'Rfc822Name',
81        'HexBinary',
82        'Base64Binary',
83        'IpAddress',
84        'DnsName',
85    )
86    NATIVE_TYPES = (
87        basestring,
88        basestring,
89        int,
90        bool,
91        float,
92        datetime,
93        datetime,
94        datetime,
95        timedelta,
96        timedelta,
97        basestring,
98        basestring,
99        int,
100        NotImplemented,
101        basestring,
102        basestring
103    )
104    TYPE_MAP = dict(zip(TYPE_NAMES, NATIVE_TYPES))
105    TYPE_URI_MAP = dict(zip(TYPE_NAMES, TYPE_URIS))
106    TYPE = None
107   
108    __slots__ = ('__value',) 
109   
110    def __init__(self):
111        """Derived classes must override setting TYPE class variable"""
112       
113        super(AttributeValue, self).__init__()
114        if self.__class__.TYPE is None:
115            raise NotImplementedError('TYPE class variable must be set to a '
116                                      'valid type in a derived class')
117           
118        self.__value = None
119       
120        # Allow derived classes to make an implicit data type setting
121        self.dataType = self.__class__.IDENTIFIER
122
123    def __repr__(self):
124        return "%s = %r " % (super(AttributeValue, self).__repr__(),
125                             self.__value)
126   
127    def _get_value(self):
128        """Get value
129        @return: setting for this attribute value
130        @rtype: any - constrained in derived classes
131        """
132        return self.__value
133
134    def _set_value(self, value):
135        """Set value
136       
137        @param value: setting for this attribute value
138        @type value: any - constrained in derived classes
139        @raise TypeError: if type doesn't match TYPE class variable.  Derived
140        classes should set this
141        """
142        if not isinstance(value, self.__class__.TYPE):
143            raise TypeError('Expecting %r type for "value" '
144                            'attribute; got %r' % (self.__class__.TYPE, 
145                                                   type(value)))
146           
147        self.__value = value 
148
149    value = property(_get_value, _set_value, None, "expression value") 
150   
151    def evaluate(self, context):
152        """Evaluate the result of the expression in a condition.  In the case of
153        an attribute value it's simply itself
154       
155        @param context: the request context
156        @type context: ndg.xacml.core.context.request.Request
157        @return: this attribute value
158        @rtype: AttributeValue 
159        """ 
160        return self
161
162
163class AttributeValueClassMap(VettedDict):
164    """Specialised dictionary to hold mappings of XML attribute type URIs to
165    their equivalent classes
166    """
167   
168    def __init__(self):
169        """Force entries to derive from AttributeValue and IDs to
170        be string type
171        """       
172        # Filters are defined as staticmethods but reference via self here to
173        # enable derived class to override them as standard methods without
174        # needing to redefine this __init__ method           
175        VettedDict.__init__(self, self.keyFilter, self.valueFilter)
176       
177    @staticmethod
178    def keyFilter(key):
179        """Enforce string type keys
180       
181        @param key: URN for attribute
182        @type key: basestring
183        @return: boolean True indicating key is OK
184        @rtype: bool
185        @raise TypeError: incorrect input type
186        """
187        if not isinstance(key, basestring):
188            raise TypeError('Expecting %r type for key; got %r' % 
189                            (basestring, type(key))) 
190        return True 
191   
192    @staticmethod
193    def valueFilter(value):
194        """Enforce AttributeValue derived types for values
195        @param value: attribute value
196        @type value: ndg.xacml.core.attributevalue.AttributeValue derived type
197        @return: boolean True indicating attribute value is correct type
198        @rtype: bool
199        @raise TypeError: incorrect input type
200        """
201        if not issubclass(value, AttributeValue):
202            raise TypeError('Expecting %r derived type for value; got %r' % 
203                            (AttributeValue, type(value))) 
204        return True 
205
206
207# Dynamically Create classes based on AttributeValue for all the XACML primitive
208# types
209_IDENTIFIER2CLASS_MAP = AttributeValueClassMap()
210
211for typeName, _type in AttributeValue.TYPE_MAP.items():
212    identifier = AttributeValue.TYPE_URI_MAP[typeName]
213
214    className = typeName + AttributeValue.CLASS_NAME_SUFFIX               
215    classVars = {'TYPE': _type, 'IDENTIFIER': identifier}
216   
217    attributeValueClass = type(className, (AttributeValue, ), classVars)
218    AttributeValue.register(attributeValueClass)
219    _IDENTIFIER2CLASS_MAP[identifier] = attributeValueClass
220   
221   
222class AttributeValueClassFactory(object):
223    """Create AttributeValue types based on the XML namespace identifier
224   
225    Convenience wrapper for _IDENTIFIER2CLASS_MAP instance of
226    AttributeValueClassMap
227   
228    @ivar __classMap: mapping object to map attribute value URIs to their
229    implementations as classes
230    @type __classMap: ndg.xacml.core.attributevalue.AttributeValueClassMap
231    """
232    __slots__ = ('__classMap',)
233   
234    def __init__(self, classMap=None):
235        """Set a mapping object to map attribute value URIs to their
236        implementations as classes
237       
238        @param classMap: input an alternative to the default class mapping
239        object _IDENTIFIER2CLASS_MAP, if None, it will default to this setting
240        @type classMap: ndg.xacml.core.attributevalue.AttributeValueClassMap
241        """
242        if classMap is None:
243            self.__classMap = _IDENTIFIER2CLASS_MAP
244        elif isinstance(classMap, AttributeValueClassMap):
245            self.__classMap = classMap
246        else:
247            raise TypeError('Expecting %r derived type for "map" input; got %r'
248                            % (AttributeValueClassMap, type(map)))
249           
250    def __call__(self, identifier):
251        """Return <type>AttributeValue class for given identifier URI or None
252        if no match is found
253       
254        @return: attribute value class
255        @rtype: NoneType / ndg.xacml.core.attributevalue.AttributeValue derived
256        type
257        """
258        return self.__classMap.get(identifier)
259       
Note: See TracBrowser for help on using the repository browser.