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

contrib: removal of dependency on Invenio-Groups #19

Merged
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
6 changes: 6 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ Forms
.. automodule:: invenio_oauthclient.forms
:members:

Signals
-------

.. automodule:: invenio_oauthclient.signals
:members:

Utils
-----

Expand Down
22 changes: 2 additions & 20 deletions invenio_oauthclient/contrib/cern.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@
import copy
import re

import requests
from flask import current_app
from flask_login import current_user

#: Tunable list of groups to be hidden.
CFG_EXTERNAL_AUTH_HIDDEN_GROUPS = (
Expand Down Expand Up @@ -201,22 +199,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()
pass
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
15 changes: 13 additions & 2 deletions invenio_oauthclient/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .forms import EmailSignUpForm
from .models import RemoteAccount, RemoteToken
from .proxies import current_oauthclient
from .signals import account_info_received, account_setup_received
from .utils import oauth_authenticate, oauth_get_user, oauth_register


Expand Down Expand Up @@ -217,6 +218,9 @@ def authorized_signup_handler(resp, remote, *args, **kwargs):
# ---------------
if not current_user.is_authenticated:
account_info = handlers['info'](resp)
account_info_received.send(
remote, token=token, response=resp, account_info=account_info
)

user = oauth_get_user(
remote.consumer_key,
Expand Down Expand Up @@ -257,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, token=token, response=resp, account_setup=account_setup
)

# Redirect to next
next_url = get_session_next_url(remote.name)
Expand Down Expand Up @@ -337,7 +344,11 @@ 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)
account_setup = handlers['setup'](token, response)
account_setup_received.send(
remote, token=token, response=response,
account_setup=account_setup
)

# Remove account info from session
session.pop(session_prefix + '_account_info', None)
Expand Down
65 changes: 65 additions & 0 deletions invenio_oauthclient/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Signals used together with various handlers."""

from blinker import Namespace

_signals = Namespace()

account_info_received = _signals.signal('oauthclient-account-info-received')
"""Signal is sent after account info handler response.

Example subscriber:

.. code-block:: python

from invenio_oauthclient.signals import account_info_received

# During overlay initialization.
@account_info_received.connect
def load_extra_information(remote, token=None, 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, token=None, 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