Add support for OpenID extension.
This work originates from the work of @jeffmiller at https://github.com/mitsuhiko/flask-openid/pull/10
I added on the top of that the possibility to parse the OpenID response with the OpenID
extension of interest.
With this patch one can use flask-openid with an OpenID supporting the team or the CLA
extensions.
diff --git a/flask_openid.py b/flask_openid.py
index a01d998..66974ac 100644
--- a/flask_openid.py
+++ b/flask_openid.py
@@ -141,11 +141,17 @@
class RegLookup(object):
- def __init__(self, resp):
+ def __init__(self, resp, extensions):
sreg_resp = SRegResponse.fromSuccessResponse(resp)
self.sreg = sreg_resp and sreg_resp.data or {}
self.ax_resp = ax.FetchResponse.fromSuccessResponse(resp) or {}
+ # Process the OpenID response with the OpenIDResponse class provided
+ self.ext = {}
+ for extension in extensions:
+ self.ext[extension.__name__] = \
+ extension.fromSuccessResponse(resp)
+
def get(self, name, default=None):
assert name in ALL_KEYS, 'unknown key %r' % name
rv = self.sreg.get(name)
@@ -180,10 +186,10 @@
the :meth:`~OpenID.try_login` function.
"""
- def __init__(self, resp):
+ def __init__(self, resp, extensions):
#: the openid the user used for sign in
self.identity_url = resp.identity_url
- lookup = RegLookup(resp)
+ lookup = RegLookup(resp, extensions)
#: the full name of the user
self.fullname = lookup.get_combined('fullname', FULL_NAME_URIS)
@@ -278,6 +284,10 @@
#: URL to profile image as string
self.image = lookup.get('image')
+ #: Hash of the response object from the OpenID Extension by the
+ # OpenID Extension class name
+ self.extensions = lookup.ext
+
class OpenID(object):
"""Simple helper class for OpenID auth. Has to be created in advance
@@ -312,10 +322,11 @@
if the HTTP referrer is unreliable. By
default the user is redirected back to the
application's index in that case.
+ :param extension_responses: a list of OpenID Extensions Response class.
"""
def __init__(self, app=None, fs_store_path=None, store_factory=None,
- fallback_endpoint=None):
+ fallback_endpoint=None, extension_responses=[]):
# backwards compatibility support
if isinstance(app, basestring):
from warnings import warn
@@ -338,6 +349,8 @@
self.store_factory = store_factory
self.after_login_func = None
self.fallback_endpoint = fallback_endpoint
+ if extension_responses:
+ self.extension_responses = extension_responses
def init_app(self, app):
"""This callback can be used to initialize an application for the
@@ -450,7 +463,8 @@
openid_response = consumer.complete(request.args.to_dict(),
self.get_current_url())
if openid_response.status == SUCCESS:
- return self.after_login_func(OpenIDResponse(openid_response))
+ return self.after_login_func(OpenIDResponse(
+ openid_response, self.extension_responses))
elif openid_response.status == CANCEL:
self.signal_error(u'The request was cancelled')
return redirect(self.get_current_url())