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

Always trigger event and provide userid #14

Merged
merged 3 commits into from
Feb 11, 2016
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ An event is triggered when one of the multiple policies configured is selected.

# Track policy used, for prefixing user_id and for logging.
def on_policy_selected(event):
print("%s (%s) was selected for request %s" % (event.policy_name,
event.policy,
event.request))
print("%s (%s) authenticated %s for request %s" % (event.policy_name,
event.policy,
event.userid,
event.request))

config.add_subscriber(on_policy_selected, MultiAuthPolicySelected)
9 changes: 7 additions & 2 deletions pyramid_multiauth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ def track_policy(event):
print("We selected policy %s" % event.policy)

"""
def __init__(self, policy, request):
def __init__(self, policy, request, userid=None):
self.policy = policy
self.policy_name = getattr(policy, "_pyramid_multiauth_name", None)
self.request = request
self.userid = userid


@implementer(IAuthenticationPolicy)
Expand Down Expand Up @@ -77,7 +78,8 @@ def authenticated_userid(self, request):
userid = policy.authenticated_userid(request)
if userid is not None:
request.registry.notify(MultiAuthPolicySelected(policy,
request))
request,
userid))

if self._callback is None:
break
Expand Down Expand Up @@ -117,6 +119,9 @@ def effective_principals(self, request):
userid = policy.authenticated_userid(request)
if userid is None:
continue
request.registry.notify(MultiAuthPolicySelected(policy,
request,
userid))
groups = self._callback(userid, request)
if groups is not None:
break
Expand Down
8 changes: 8 additions & 0 deletions pyramid_multiauth/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,17 @@ def track_policy(event):

self.assertEquals(selected_policy[0].policy, policies[0])
self.assertEquals(selected_policy[0].policy_name, "name")
self.assertEquals(selected_policy[0].userid, "test2")
self.assertEquals(selected_policy[0].request, request)
self.assertEquals(len(selected_policy), 1)

# Effective principals also triggers an event when groupfinder
# is provided.
policy_with_group = MultiAuthenticationPolicy(policies,
lambda u, r: ['foo'])
policy_with_group.effective_principals(request)
self.assertEquals(len(selected_policy), 2)

def test_stacking_of_unauthenticated_userid(self):
policies = [TestAuthnPolicy2(), TestAuthnPolicy3()]
policy = MultiAuthenticationPolicy(policies)
Expand Down