From 7b520855f90e273e04cfcf864ffad0d0ac582d1c Mon Sep 17 00:00:00 2001 From: Michael Mulich Date: Sat, 1 Jun 2013 15:14:31 -0400 Subject: [PATCH] Adds before and after login view call hooks, which are useful for systems using velruse as a plugin. It allows the plugin user to modify any information before or after the login view is called. For example, before or after the login view is hit, one could capture the referrer location so that they can send the authenticated user back to where they came from. --- velruse/events.py | 28 ++++++++++++++++++++++++++++ velruse/providers/openid.py | 2 ++ 2 files changed, 30 insertions(+) create mode 100644 velruse/events.py diff --git a/velruse/events.py b/velruse/events.py new file mode 100644 index 0000000..d5c6c1b --- /dev/null +++ b/velruse/events.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +"""Custom events for identity provider login.""" +import functools +from pyramid.events import NewRequest + +def with_events(before=None, after=None): + """Decorator to create event book ends on a method.""" + def decorator(method): + @functools.wraps(method) + def new_method(obj, request): + request.registry.notify(before(request)) + response = method(obj, request) + request.registry.notify(after(request)) + return response + return new_method + return decorator + + +class BeforeLogin(NewRequest): + """Used to tap into the request before + the identity provider logic is run. + """ + + +class AfterLogin(NewRequest): + """Used to tap into the request after + the identity provider logic has run. + """ diff --git a/velruse/providers/openid.py b/velruse/providers/openid.py index 34e6a4b..c9acf2d 100644 --- a/velruse/providers/openid.py +++ b/velruse/providers/openid.py @@ -17,6 +17,7 @@ AuthenticationDenied, register_provider, ) +from velruse.events import with_events, AfterLogin, BeforeLogin from velruse.exceptions import MissingParameter from velruse.exceptions import ThirdPartyFailure @@ -180,6 +181,7 @@ def _get_access_token(self, request_token): """ + @with_events(before=BeforeLogin, after=AfterLogin) def login(self, request): log.debug('Handling OpenID login')