This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Fix distinction and optimize (un)authenticated_userid #641
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,20 @@ | |
from cliquet import utils | ||
|
||
|
||
def prefixed_userid(request): | ||
"""In Cliquet users ids are prefixed with the policy name that is | ||
contained in Pyramid Multiauth. | ||
If a custom authn policy is used, without authn_type, this method returns | ||
the user id without prefix. | ||
""" | ||
# If pyramid_multiauth is used, a ``authn_type`` is set on request | ||
# when a policy succesfully authenticates a user. | ||
# (see :func:`cliquet.initializatio.setup_authentication`) | ||
authn_type = getattr(request, 'authn_type', None) | ||
if authn_type is not None: | ||
return authn_type + ':' + request.selected_userid | ||
|
||
|
||
class BasicAuthAuthenticationPolicy(base_auth.BasicAuthAuthenticationPolicy): | ||
"""Basic auth implementation. | ||
|
||
|
@@ -17,6 +31,13 @@ def noop_check(*a): | |
*args, | ||
**kwargs) | ||
|
||
def effective_principals(self, request): | ||
"""Bypass default Pyramid construction of principals because | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: should probably be comments? |
||
Pyramid multiauth already adds userid, Authenticated and Everyone | ||
principals. | ||
""" | ||
return [] | ||
|
||
def unauthenticated_userid(self, request): | ||
settings = request.registry.settings | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import functools | ||
|
||
from pyramid.settings import aslist | ||
from pyramid.security import IAuthorizationPolicy, Authenticated | ||
from zope.interface import implementer | ||
|
||
from cliquet import utils | ||
from cliquet.storage import exceptions as storage_exceptions | ||
from cliquet.authentication import prefixed_userid | ||
|
||
# A permission is called "dynamic" when it's computed at request time. | ||
DYNAMIC = 'dynamic' | ||
|
@@ -23,15 +25,13 @@ def groupfinder(userid, request): | |
if not backend: | ||
return [] | ||
|
||
# Anonymous safety check. | ||
if not hasattr(request, 'prefixed_userid'): | ||
return [] | ||
if request.prefixed_userid: | ||
userid = request.prefixed_userid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have cases where the userid is not prefixed here? If so which ones? |
||
|
||
# Query the permission backend only once per request (e.g. batch). | ||
|
||
reify_key = request.prefixed_userid + '_principals' | ||
reify_key = userid + '_principals' | ||
if reify_key not in request.bound_data: | ||
principals = backend.user_principals(request.prefixed_userid) | ||
principals = backend.user_principals(userid) | ||
request.bound_data[reify_key] = principals | ||
|
||
return request.bound_data[reify_key] | ||
|
@@ -48,9 +48,10 @@ def permits(self, context, principals, permission): | |
return Authenticated in principals | ||
|
||
# Add prefixed user id to principals. | ||
if context.prefixed_userid: | ||
principals = principals + [context.prefixed_userid] | ||
prefix, user_id = context.prefixed_userid.split(':', 1) | ||
prefixed_userid = context.get_prefixed_userid() | ||
if prefixed_userid and ':' in prefixed_userid: | ||
principals = principals + [prefixed_userid] | ||
prefix, user_id = prefixed_userid.split(':', 1) | ||
# Remove unprefixed user id to avoid conflicts. | ||
# (it is added via Pyramid Authn policy effective principals) | ||
if user_id in principals: | ||
|
@@ -110,7 +111,7 @@ class RouteFactory(object): | |
|
||
def __init__(self, request): | ||
# Make it available for the authorization policy. | ||
self.prefixed_userid = getattr(request, "prefixed_userid", None) | ||
self.get_prefixed_userid = functools.partial(prefixed_userid, request) | ||
|
||
self._check_permission = request.registry.permission.check_permission | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: typo.