1 | """NDG Security authorisation package - contains code for Gatekeeper (PEP) |
---|
2 | and authorisation interfaces (PDP) |
---|
3 | |
---|
4 | NERC DataGrid Project |
---|
5 | """ |
---|
6 | __author__ = "P J Kershaw" |
---|
7 | __date__ = "04/04/08" |
---|
8 | __copyright__ = "(C) 2009 Science and Technology Facilities Council" |
---|
9 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
10 | __license__ = "BSD - see LICENSE file in top-level directory" |
---|
11 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
12 | __revision__ = "$Id: __init__.py 3755 2008-04-04 09:11:44Z pjkersha $" |
---|
13 | |
---|
14 | |
---|
15 | class _AttrDict(dict): |
---|
16 | """Utility class for holding a constrained list of attributes governed |
---|
17 | by a namespace list""" |
---|
18 | namespaces = () |
---|
19 | def __init__(self, **attributes): |
---|
20 | invalidAttributes = [attr for attr in attributes |
---|
21 | if attr not in self.__class__.namespaces] |
---|
22 | if len(invalidAttributes) > 0: |
---|
23 | raise TypeError("The following attribute namespace(s) are not " |
---|
24 | "recognised: %s" % invalidAttributes) |
---|
25 | |
---|
26 | self.update(attributes) |
---|
27 | |
---|
28 | def __setitem__(self, key, val): |
---|
29 | if key not in self.__class__.namespaces: |
---|
30 | raise KeyError('Namespace "%s" not recognised. Valid namespaces ' |
---|
31 | 'are: %s' % self.__class__.namespaces) |
---|
32 | |
---|
33 | dict.__setitem__(self, key, val) |
---|
34 | |
---|
35 | |
---|
36 | def update(self, d, **kw): |
---|
37 | for dictArg in (d, kw): |
---|
38 | for k in dictArg: |
---|
39 | if k not in self.__class__.namespaces: |
---|
40 | raise KeyError('Namespace "%s" not recognised. Valid ' |
---|
41 | 'namespaces are: %s' % |
---|
42 | self.__class__.namespaces) |
---|
43 | |
---|
44 | dict.update(self, d, **kw) |
---|
45 | |
---|
46 | |
---|
47 | class SubjectRetrievalError(Exception): |
---|
48 | """Generic exception class for errors related to information about the |
---|
49 | subject""" |
---|
50 | |
---|
51 | |
---|
52 | class SubjectBase(object): |
---|
53 | '''Base class Subject designator''' |
---|
54 | namespaces = ("urn:ndg:security:authz:1.0:attr:subject:roles", ) |
---|
55 | (ROLES_NS,) = namespaces |
---|