Skip to content

Commit

Permalink
contrib: removal of dependency on Invenio-Groups
Browse files Browse the repository at this point in the history
* Removes the dependency on Invenio-Groups reducing the number of fields
  parsed inside CERN or ORCID modules and sending the response to a
  subscribable signal. (closes #18)

Signed-off-by: Javier Delgado <javier.delgado.fernandez@cern.ch>
  • Loading branch information
JavierDelgadoFernandez committed Dec 3, 2015
1 parent 176295c commit 93a11d4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
24 changes: 6 additions & 18 deletions invenio_oauthclient/contrib/cern.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import re

import requests
from blinker import signal
from flask import current_app
from flask_login import current_user

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion invenio_oauthclient/contrib/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions invenio_oauthclient/contrib/orcid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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]):
Expand Down
15 changes: 9 additions & 6 deletions invenio_oauthclient/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

from __future__ import absolute_import

import warnings
from functools import partial, wraps

import six
Expand All @@ -36,7 +35,7 @@
from .forms import EmailSignUpForm
from .models import RemoteAccount, RemoteToken
from .proxies import current_oauthclient
from .signals import account_info_received
from .signals import account_info_received, account_setup_received
from .utils import oauth_authenticate, oauth_get_user, oauth_register


Expand Down Expand Up @@ -260,8 +259,10 @@ def authorized_signup_handler(resp, remote, *args, **kwargs):
# Setup account
# -------------
if not token.remote_account.extra_data:
handlers['setup'](token, resp)
# TODO add signal for account setup
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)
Expand Down Expand Up @@ -341,8 +342,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)
Expand Down
21 changes: 18 additions & 3 deletions invenio_oauthclient/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
]

install_requires = [
'blinker>=1.4',
'cryptography>=0.6', # sqlalchemy-utils dependency
'Flask>=0.10.1',
'Flask-BabelEx>=0.9.2',
Expand Down

0 comments on commit 93a11d4

Please sign in to comment.