1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
from zope.deprecation import deprecated
from zope.interface import implements
from repoze.bfg.interfaces import ISecurityPolicy
from repoze.bfg.interfaces import IAuthorizationPolicy
from repoze.bfg.interfaces import IAuthenticationPolicy
from repoze.bfg.location import lineage
from repoze.bfg.threadlocal import manager
from repoze.bfg.security import Allow
from repoze.bfg.security import Deny
from repoze.bfg.security import ACLAllowed
from repoze.bfg.security import ACLDenied
from repoze.bfg.security import Everyone
from repoze.bfg.security import Authenticated
class ACLSecurityPolicy(object):
implements(ISecurityPolicy)
def __init__(self, get_principals):
self.get_principals = get_principals
def permits(self, context, request, permission):
""" Return ``ACLAllowed`` if the policy permits access,
``ACLDenied`` if not. """
principals = set(self.effective_principals(request))
for location in lineage(context):
try:
acl = location.__acl__
except AttributeError:
continue
for ace in acl:
ace_action, ace_principal, ace_permissions = ace
if ace_principal in principals:
if not hasattr(ace_permissions, '__iter__'):
ace_permissions = [ace_permissions]
if permission in ace_permissions:
if ace_action == Allow:
return ACLAllowed(ace, acl, permission,
principals, location)
else:
return ACLDenied(ace, acl, permission,
principals, location)
# default deny if no ACE matches in the ACL found
result = ACLDenied(None, acl, permission, principals, location)
return result
# default deny if no ACL in lineage at all
return ACLDenied(None, None, permission, principals, context)
def authenticated_userid(self, request):
principals = self.get_principals(request)
if principals:
return principals[0]
def effective_principals(self, request):
effective_principals = [Everyone]
principal_ids = self.get_principals(request)
if principal_ids:
effective_principals.append(Authenticated)
effective_principals.extend(principal_ids)
return effective_principals
def principals_allowed_by_permission(self, context, permission):
for location in lineage(context):
try:
acl = location.__acl__
except AttributeError:
continue
allowed = {}
for ace_action, ace_principal, ace_permissions in acl:
if ace_action == Allow:
if not hasattr(ace_permissions, '__iter__'):
ace_permissions = [ace_permissions]
if permission in ace_permissions:
allowed[ace_principal] = True
return sorted(allowed.keys())
return []
class InheritingACLSecurityPolicy(object):
""" A security policy which uses ACLs in the following ways:
- When checking whether a user is permitted (via the ``permits``
method), the security policy consults the ``context`` for an ACL
first. If no ACL exists on the context, or one does exist but
the ACL does not explicitly allow or deny access for any of the
effective principals, consult the context's parent ACL, and so
on, until the lineage is exhausted or we determine that the
policy permits or denies.
During this processing, if any ``Deny`` ACE is found matching
any effective principal, stop processing by returning an
``ACLDenied`` (equals False) immediately. If any ``Allow`` ACE
is found matching any effective principal, stop processing by
returning an ``ACLAllowed`` (equals True) immediately. If we
exhaust the context's lneage, and no ACE has explicitly
permitted or denied access, return an ``ACLDenied``. This
differs from the non-inheriting security policy (the
``ACLSecurityPolicy``) by virtue of the fact that it does not
stop looking for ACLs in the object lineage after it finds the
first one.
- When computing principals allowed by a permission via the
``principals_allowed_by_permission`` method, we compute the set
of principals that are explicitly granted the ``permission``.
We do this by walking 'up' the object graph *from the root* to
the context. During this walking process, if we find an
explicit ``Allow`` ACE for a principal that matches the
``permission``, the principal is included in the allow list.
However, if later in the walking process that user is mentioned
in any ``Deny`` ACE for the permission, the user is removed from
the allow list. If a ``Deny`` to the principal ``Everyone`` is
encountered during the walking process that matches the
``permission``, the allow list is cleared for all principals
encountered in previous ACLs. The walking process ends after
we've processed the any ACL directly attached to ``context``; a
list of principals is returned.
- Other aspects of this policy are the same as those in the
ACLSecurityPolicy (e.g. ``effective_principals``,
``authenticated_userid``).
"""
implements(ISecurityPolicy)
def __init__(self, get_principals):
self.get_principals = get_principals
def permits(self, context, request, permission):
""" Return ``ACLAllowed`` if the policy permits access,
``ACLDenied`` if not. """
principals = set(self.effective_principals(request))
for location in lineage(context):
try:
acl = location.__acl__
except AttributeError:
continue
for ace in acl:
ace_action, ace_principal, ace_permissions = ace
if ace_principal in principals:
if not hasattr(ace_permissions, '__iter__'):
ace_permissions = [ace_permissions]
if permission in ace_permissions:
if ace_action == Allow:
return ACLAllowed(ace, acl, permission,
principals, location)
else:
return ACLDenied(ace, acl, permission,
principals, location)
# default deny if no ACL in lineage at all
return ACLDenied(None, None, permission, principals, context)
def authenticated_userid(self, request):
principals = self.get_principals(request)
if principals:
return principals[0]
def effective_principals(self, request):
effective_principals = [Everyone]
principal_ids = self.get_principals(request)
if principal_ids:
effective_principals.append(Authenticated)
effective_principals.extend(principal_ids)
return effective_principals
def principals_allowed_by_permission(self, context, permission):
allowed = set()
for location in reversed(list(lineage(context))):
# NB: we're walking *up* the object graph from the root
try:
acl = location.__acl__
except AttributeError:
continue
allowed_here = set()
denied_here = set()
for ace_action, ace_principal, ace_permissions in acl:
if not hasattr(ace_permissions, '__iter__'):
ace_permissions = [ace_permissions]
if ace_action == Allow and permission in ace_permissions:
if not ace_principal in denied_here:
allowed_here.add(ace_principal)
if ace_action == Deny and permission in ace_permissions:
denied_here.add(ace_principal)
if ace_principal == Everyone:
# clear the entire allowed set, as we've hit a
# deny of Everyone ala (Deny, Everyone, ALL)
allowed = set()
break
elif ace_principal in allowed:
allowed.remove(ace_principal)
allowed.update(allowed_here)
return allowed
def get_remoteuser(request):
user_id = request.environ.get('REMOTE_USER')
if user_id:
return [user_id]
return []
def RemoteUserACLSecurityPolicy():
""" A security policy which:
- examines the request.environ for the REMOTE_USER variable and
uses any non-false value as a principal id for this request.
- uses an ACL-based authorization model which attempts to find the
*first* ACL in the context' lineage. It returns ``Allowed`` from
its 'permits' method if the single ACL found grants access to the
current principal. It returns ``Denied`` if permission was not
granted (either explicitly via a deny or implicitly by not finding
a matching ACE action). The *first* ACL found in the context's
lineage is considered canonical; no searching is done for other
security attributes after the first ACL is found in the context'
lineage. Use the 'inheriting' variant of this policy to consider
more than one ACL in the lineage.
An ACL is an ordered sequence of ACE tuples, e.g. ``[(Allow,
Everyone, 'read'), (Deny, 'george', 'write')]``. ACLs stored on
model instance objects as their ``__acl__`` attribute will be used
by the security machinery to grant or deny access.
Enable this security policy by adding the following to your
application's ``configure.zcml``:
.. code-block:: xml
<utility
provides="repoze.bfg.interfaces.ISecurityPolicy"
factory="repoze.bfg.security.RemoteUserACLSecurityPolicy"
/>
"""
return ACLSecurityPolicy(get_remoteuser)
def RemoteUserInheritingACLSecurityPolicy():
""" A security policy which:
- examines the request.environ for the REMOTE_USER variable and
uses any non-false value as a principal id for this request.
- Differs from the non-inheriting security policy variants
(e.g. ``ACLSecurityPolicy``) by virtue of the fact that it does
not stop looking for ACLs in the object lineage after it finds
the first one.
- When checking whether a user is permitted (via the ``permits``
method), the security policy consults the ``context`` for an ACL
first. If no ACL exists on the context, or one does exist but
the ACL does not explicitly allow or deny access for any of the
effective principals, consult the context's parent ACL, and so
on, until the lineage is exhausted or we determine that the
policy permits or denies.
During this processing, if any ``Deny`` ACE is found matching
any effective principal, stop processing by returning an
``ACLDenied`` (equals False) immediately. If any ``Allow`` ACE
is found matching any effective principal, stop processing by
returning an ``ACLAllowed`` (equals True) immediately. If we
exhaust the context's lneage, and no ACE has explicitly
permitted or denied access, return an ``ACLDenied``.
- When computing principals allowed by a permission via the
``principals_allowed_by_permission`` method, we compute the set
of principals that are explicitly granted the ``permission``.
We do this by walking 'up' the object graph *from the root* to
the context. During this walking process, if we find an
explicit ``Allow`` ACE for a principal that matches the
``permission``, the principal is included in the allow list.
However, if later in the walking process that user is mentioned
in any ``Deny`` ACE for the permission, the user is removed from
the allow list. If a ``Deny`` to the principal ``Everyone`` is
encountered during the walking process that matches the
``permission``, the allow list is cleared for all principals
encountered in previous ACLs. The walking process ends after
we've processed the any ACL directly attached to ``context``; a
list of principals is returned.
- Other aspects of this policy are the same as those in the
ACLSecurityPolicy (e.g. ``effective_principals``,
``authenticated_userid``).
Enable this security policy by adding the following to your
application's ``configure.zcml``:
.. code-block:: xml
<utility
provides="repoze.bfg.interfaces.ISecurityPolicy"
factory="repoze.bfg.security.RemoteUserInheritingACLSecurityPolicy"
/>
"""
return InheritingACLSecurityPolicy(get_remoteuser)
def get_who_principals(request):
identity = request.environ.get('repoze.who.identity')
if not identity:
return []
principals = [identity['repoze.who.userid']]
principals.extend(identity.get('groups', []))
return principals
def WhoACLSecurityPolicy():
"""
A security policy which:
- examines the request.environ for the ``repoze.who.identity``
dictionary. If one is found, the principal ids for the request
are composed of ``repoze.who.identity['repoze.who.userid']``
plus ``repoze.who.identity.get('groups', [])``.
- uses an ACL-based authorization model which attempts to find the
*first* ACL in the context' lineage. It returns ``Allowed`` from
its 'permits' method if the single ACL found grants access to the
current principal. It returns ``Denied`` if permission was not
granted (either explicitly via a deny or implicitly by not finding
a matching ACE action). The *first* ACL found in the context's
lineage is considered canonical; no searching is done for other
security attributes after the first ACL is found in the context'
lineage. Use the 'inheriting' variant of this policy to consider
more than one ACL in the lineage.
An ACL is an ordered sequence of ACE tuples, e.g. ``[(Allow,
Everyone, 'read'), (Deny, 'george', 'write')]``. ACLs stored on
model instance objects as their ``__acl__`` attribute will be used
by the security machinery to grant or deny access.
Enable this security policy by adding the following to your
application's ``configure.zcml``:
.. code-block:: xml
<utility
provides="repoze.bfg.interfaces.ISecurityPolicy"
factory="repoze.bfg.security.WhoACLSecurityPolicy"
/>
"""
return ACLSecurityPolicy(get_who_principals)
RepozeWhoIdentityACLSecurityPolicy = WhoACLSecurityPolicy
deprecated('RepozeWhoIdentityACLSecurityPolicy',
'(repoze.bfg.security.RepozeWhoIdentityACLSecurityPolicy '
'should now be imported as '
'repoze.bfg.security.WhoACLSecurityPolicy)',
)
def WhoInheritingACLSecurityPolicy():
""" A security policy which:
- examines the request.environ for the ``repoze.who.identity``
dictionary. If one is found, the principal ids for the request
are composed of ``repoze.who.identity['repoze.who.userid']``
plus ``repoze.who.identity.get('groups', [])``.
- Differs from the non-inheriting security policy variants
(e.g. ``ACLSecurityPolicy``) by virtue of the fact that it does
not stop looking for ACLs in the object lineage after it finds
the first one.
- When checking whether a user is permitted (via the ``permits``
method), the security policy consults the ``context`` for an ACL
first. If no ACL exists on the context, or one does exist but
the ACL does not explicitly allow or deny access for any of the
effective principals, consult the context's parent ACL, and so
on, until the lineage is exhausted or we determine that the
policy permits or denies.
During this processing, if any ``Deny`` ACE is found matching
any effective principal, stop processing by returning an
``ACLDenied`` (equals False) immediately. If any ``Allow`` ACE
is found matching any effective principal, stop processing by
returning an ``ACLAllowed`` (equals True) immediately. If we
exhaust the context's lneage, and no ACE has explicitly
permitted or denied access, return an ``ACLDenied``.
- When computing principals allowed by a permission via the
``principals_allowed_by_permission`` method, we compute the set
of principals that are explicitly granted the ``permission``.
We do this by walking 'up' the object graph *from the root* to
the context. During this walking process, if we find an
explicit ``Allow`` ACE for a principal that matches the
``permission``, the principal is included in the allow list.
However, if later in the walking process that user is mentioned
in any ``Deny`` ACE for the permission, the user is removed from
the allow list. If a ``Deny`` to the principal ``Everyone`` is
encountered during the walking process that matches the
``permission``, the allow list is cleared for all principals
encountered in previous ACLs. The walking process ends after
we've processed the any ACL directly attached to ``context``; a
list of principals is returned.
- Other aspects of this policy are the same as those in the
ACLSecurityPolicy (e.g. ``effective_principals``,
``authenticated_userid``).
Enable this security policy by adding the following to your
application's ``configure.zcml``:
.. code-block:: xml
<utility
provides="repoze.bfg.interfaces.ISecurityPolicy"
factory="repoze.bfg.security.WhoInheritingACLSecurityPolicy"
/>
"""
return InheritingACLSecurityPolicy(get_who_principals)
class SecurityPolicyToAuthorizationPolicyAdapter(object):
""" An adapter registered when an old-style ISecurityPolicy
utility is configured in ZCML instead of an IAuthorizationPolicy
utility """
implements(IAuthorizationPolicy)
def __init__(self, secpol):
self.secpol = secpol
def permits(self, context, principals, permission):
request = manager.get()['request']
return self.secpol.permits(context, request, permission)
def principals_allowed_by_permission(self, context, permission):
return self.secpol.principals_allowed_by_permission(context, permission)
class SecurityPolicyToAuthenticationPolicyAdapter(object):
implements(IAuthenticationPolicy)
def __init__(self, secpol):
self.secpol = secpol
def authenticated_userid(self, request):
return self.secpol.authenticated_userid(request)
def effective_principals(self, request):
return self.secpol.effective_principals(request)
def remember(self, request, principal, **kw):
return []
def forget(self, request):
return []
def registerBBBAuthn(secpol, registry):
# Used when an explicit authentication policy is not defined, and
# an an old-style ISecurityPolicy is registered (via ZCML), turn
# it into separate authorization and authentication utilities
# using adapters
authn = SecurityPolicyToAuthenticationPolicyAdapter(secpol)
authz = SecurityPolicyToAuthorizationPolicyAdapter(secpol)
registry.registerUtility(authn, IAuthenticationPolicy)
registry.registerUtility(authz, IAuthorizationPolicy)
|