Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pyramid SecurityPolicy #9955

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions geoportal/c2cgeoportal_geoportal/lib/security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from pyramid.authentication import AuthTktCookieHelper
from pyramid.authorization import ACLHelper, Authenticated, Everyone
from pyramid.interfaces import ISecurityPolicy

from c2cgeoportal_geoportal.resources import defaultgroupsfinder
from jinja2.runtime import identity
from macaroonbakery.bakery._identity import Identity
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDE imports, remove



class SecurityPolicy(ISecurityPolicy):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@arnaud-morvan arnaud-morvan Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for other interfaces, but pretty sure we do not really need inheritance, just a class and methods with such prototype.
Inheritance can help for typing.
I do not know details about zope.Interface.
This is a draft for discussion, we will discuss, write tests and code.
But interesting catch anyway.

def __init__(
self,
identity_providers: List([Callable[pyramid.request.request], Dict[str, Any]]),
groupfinder: Callable[[str, pyramid.request.Request], List[str]],
):
self.groupfinder = groupfinder or defaultgroupsfinder
self.helper = AuthTktCookieHelper(secret)

def identity(self, request: pyramid.request.Request) -> Dict[str, Any]:
for identity_provider in [self.helper.identify, self.identity_providers]:
identity = identity_provider(request)
if identity is not None:
return Identity
return None

def authenticated_userid(self, request: pyramid.request.Request):
identity = request.identity
if identity is not None:
return identity['userid']

def permits(
self,
request: pyramid.request.Request,
context,
permission,
):
identity = request.identity
principals = set([Everyone])
if identity is not None:
principals.add(Authenticated)
principals.add(identity['userid'])
user = request.user
principals.update(self.groupfinder(user))
return ACLHelper().permits(context, principals, permission)

def remember(self, request, userid, **kw):
return self.helper.remember(request, userid, **kw)

def forget(self, request, **kw):
return self.helper.forget(request, **kw)