diff --git a/invenio_oauthclient/contrib/cern.py b/invenio_oauthclient/contrib/cern.py index d6d2923a..fcbd1659 100644 --- a/invenio_oauthclient/contrib/cern.py +++ b/invenio_oauthclient/contrib/cern.py @@ -87,6 +87,7 @@ import re import requests +from blinker import signal from flask import current_app from flask_login import current_user @@ -159,6 +160,9 @@ REMOTE_APP_RESOURCE_SCHEMA = "http://schemas.xmlsoap.org/claims/" +oauth_cern_response_received = signal('oauth-cern-response-received') + + def fetch_groups(groups): """Prepare list of allowed group names.""" hidden_groups = current_app.config.get( @@ -201,22 +205,6 @@ def account_info(remote, resp): return dict(email=email.lower(), nickname=common_name) -def account_setup(remote, token): +def account_setup(remote, token, resp): """Perform additional setup after user have been logged in.""" - from invenio_db import db - - response = remote.get(REMOTE_APP_RESOURCE_API_URL) - user = token.remote_account.user - - if response.status == requests.codes.ok: - res = get_dict_from_response(response) - current_user.info['group'] = fetch_groups(res['Group']) - current_user.modified = True - current_user.save() - - if user and not any([user.family_name, user.given_names]): - user.family_name = res['Lastname'][0] - user.given_names = res['Firstname'][0] - - db.session.add(user) - current_user.reload() + return None diff --git a/invenio_oauthclient/contrib/github.py b/invenio_oauthclient/contrib/github.py index 4fe699b1..f1fbcd75 100644 --- a/invenio_oauthclient/contrib/github.py +++ b/invenio_oauthclient/contrib/github.py @@ -120,6 +120,6 @@ def account_info(remote, resp): external_method='github') -def account_setup(remote, token): +def account_setup(remote, token, resp): """Perform additional setup after user have been logged in.""" pass diff --git a/invenio_oauthclient/contrib/orcid.py b/invenio_oauthclient/contrib/orcid.py index aa7ae06f..70c0eb60 100644 --- a/invenio_oauthclient/contrib/orcid.py +++ b/invenio_oauthclient/contrib/orcid.py @@ -78,6 +78,7 @@ import copy +from blinker import signal from flask import current_app, redirect, url_for from flask_login import current_user from invenio_db import db @@ -119,9 +120,13 @@ )) +oauth_orcid_response_received = signal('oauth-orcid-response-received') + + def account_info(remote, resp): """Retrieve remote account information used to find local user.""" orcid = resp.get("orcid") + oauth_orcid_response_received.send(resp) return dict( external_id=orcid, external_method="orcid", @@ -166,6 +171,8 @@ def account_setup(remote, token, resp): # Create user <-> external id link. oauth_link_external_id(user, dict(id=orcid, method="orcid")) + oauth_orcid_response_received.send(resp) + # FIXME put these data in user profile! # Fill user full name if not already set # if user and not any([user.given_names, user.family_name]): diff --git a/invenio_oauthclient/handlers.py b/invenio_oauthclient/handlers.py index 71e26778..b8a7a54d 100644 --- a/invenio_oauthclient/handlers.py +++ b/invenio_oauthclient/handlers.py @@ -36,7 +36,7 @@ from .forms import EmailSignUpForm from .models import RemoteAccount, RemoteToken from .proxies import current_oauthclient -from .signals import account_info_received, account_set +from .signals import account_info_received, account_setup_received from .utils import oauth_authenticate, oauth_get_user, oauth_register @@ -261,7 +261,10 @@ def authorized_signup_handler(resp, remote, *args, **kwargs): # Setup account # ------------- if not token.remote_account.extra_data: - handlers['setup'](token, resp) + account_setup = handlers['setup'](token, resp) + account_setup_received.send( + remote, response=resp, account_setup=account_setup + ) # Redirect to next next_url = get_session_next_url(remote.name) @@ -341,8 +344,10 @@ def signup_handler(remote, *args, **kwargs): raise OAuthError("Could not create token for user.", remote) if not token.remote_account.extra_data: - handlers['setup'](token, response) - # TODO add signal for account setup + account_setup = handlers['setup'](token, response) + account_setup_received.send( + remote, response=response, account_setup=account_setup + ) # Remove account info from session session.pop(session_prefix + '_account_info', None) diff --git a/invenio_oauthclient/signals.py b/invenio_oauthclient/signals.py index 46b7114e..5337c467 100644 --- a/invenio_oauthclient/signals.py +++ b/invenio_oauthclient/signals.py @@ -38,11 +38,26 @@ from invenio_oauthclient.signals import account_info_received # During overlay initialization. - @account_info_received.connect_via( - sender=app.extensions['invenio-oauthclient'].handlers['orcid'] - ) + @account_info_received.connect def load_extra_information(remote, response=None, account_info=None): response = remote.get('https://example.org/api/resource') # process response """ + +account_setup_received = _signals.signal('oauthclient-account-setup-received') +"""Signal is sent after account info handler response. + +Example subscriber: + +.. code-block:: python + + from invenio_oauthclient.signals import account_setup_received + + # During overlay initialization. + @account_setup_received.connect + def load_extra_information(remote, response=None, account_setup=None): + response = remote.get('https://example.org/api/resource') + # process response + +""" diff --git a/setup.py b/setup.py index 8d417ddd..73f7c407 100644 --- a/setup.py +++ b/setup.py @@ -67,6 +67,7 @@ ] install_requires = [ + 'blinker>=1.4', 'cryptography>=0.6', # sqlalchemy-utils dependency 'Flask>=0.10.1', 'Flask-BabelEx>=0.9.2',