Skip to content

Commit 0e52345

Browse files
committed
Added Globus as an identity provider
* Globus Login work for any provider Globus Auth supports * Added tests, coverage of new code is 100% * Added Sphinx docs
1 parent 2c877c1 commit 0e52345

File tree

7 files changed

+722
-1
lines changed

7 files changed

+722
-1
lines changed

docs/examplesapp.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ CERN
4545
.. include:: ../examples/cern_app.py
4646
:start-after: SPHINX-START
4747
:end-before: SPHINX-END
48+
49+
Globus
50+
------
51+
52+
.. include:: ../examples/globus_app.py
53+
:start-after: SPHINX-START
54+
:end-before: SPHINX-END

docs/usage.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ CERN
4040

4141
.. automodule:: invenio_oauthclient.contrib.cern
4242

43+
Globus
44+
------
45+
.. automodule:: invenio_oauthclient.contrib.globus
46+
4347
Advanced
4448
--------
4549

examples/globus_app.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# This file is part of Invenio.
4+
# Copyright (C) 2015, 2016, 2017 CERN.
5+
#
6+
# Invenio is free software; you can redistribute it
7+
# and/or modify it under the terms of the GNU General Public License as
8+
# published by the Free Software Foundation; either version 2 of the
9+
# License, or (at your option) any later version.
10+
#
11+
# Invenio is distributed in the hope that it will be
12+
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with Invenio; if not, write to the
18+
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19+
# MA 02111-1307, USA.
20+
#
21+
# In applying this license, CERN does not
22+
# waive the privileges and immunities granted to it by virtue of its status
23+
# as an Intergovernmental Organization or submit itself to any jurisdiction.
24+
25+
r"""Minimal Flask application example for development with globus handler.
26+
27+
SPHINX-START
28+
29+
1. Register a Globus application at `https://developers.globus.org/` with the
30+
`Redirect URL` as `http://localhost:5000/oauth/authorized/globus/`. See
31+
here for more documentation:
32+
`https://docs.globus.org/api/auth/developer-guide/#register-app`
33+
34+
35+
2. Grab the *Client ID* and *Client Secret* after registering the application
36+
and add them to your instance configuration as `consumer_key` and
37+
`consumer_secret`.
38+
39+
.. code-block:: console
40+
41+
$ export GLOBUS_APP_CREDENTIALS_KEY=my_globus_client_id
42+
$ export GLOBUS_APP_CREDENTIALS_SECRET=my_globus_client_secret
43+
44+
3. Create database and tables:
45+
46+
.. code-block:: console
47+
48+
$ cdvirtualenv src/invenio-oauthclient
49+
$ pip install -e .[all]
50+
$ cd examples
51+
$ export FLASK_APP=globus_app.py
52+
$ ./app-setup.py
53+
54+
You can find the database in `examples/globus_app.db`.
55+
56+
4. Run the development server:
57+
58+
.. code-block:: console
59+
60+
$ flask run -p 5000 -h '0.0.0.0'
61+
62+
5. Open in a browser the page `http://localhost:5000/globus`.
63+
64+
You will be redirected to globus to authorize the application.
65+
66+
Click on `Allow` and you will be redirected back to
67+
`http://localhost:5000/oauth/signup/globus/`, where you will be able to
68+
finalize the local user registration.
69+
70+
6. To clean up and drop tables:
71+
72+
.. code-block:: console
73+
74+
$ ./app-teardown.sh
75+
76+
SPHINX-END
77+
78+
"""
79+
80+
from __future__ import absolute_import, print_function
81+
82+
import os
83+
84+
from flask import Flask, redirect, url_for
85+
from flask_babelex import Babel
86+
from flask_login import current_user
87+
from flask_menu import Menu as FlaskMenu
88+
from flask_oauthlib.client import OAuth as FlaskOAuth
89+
from invenio_accounts import InvenioAccounts
90+
from invenio_accounts.views import blueprint as blueprint_user
91+
from invenio_db import InvenioDB
92+
from invenio_mail import InvenioMail
93+
from invenio_userprofiles import InvenioUserProfiles
94+
from invenio_userprofiles.views import \
95+
blueprint_ui_init as blueprint_userprofile_init
96+
97+
from invenio_oauthclient import InvenioOAuthClient
98+
from invenio_oauthclient.contrib import globus
99+
from invenio_oauthclient.views.client import blueprint as blueprint_client
100+
from invenio_oauthclient.views.settings import blueprint as blueprint_settings
101+
102+
# [ Configure application credentials ]
103+
GLOBUS_APP_CREDENTIALS = dict(
104+
consumer_key=os.environ.get('GLOBUS_APP_CREDENTIALS_KEY'),
105+
consumer_secret=os.environ.get('GLOBUS_APP_CREDENTIALS_SECRET'),
106+
)
107+
108+
# Create Flask application
109+
app = Flask(__name__)
110+
111+
app.config.update(
112+
SQLALCHEMY_DATABASE_URI=os.environ.get(
113+
'SQLALCHEMY_DATABASE_URI', 'sqlite:///globus_app.db'
114+
),
115+
OAUTHCLIENT_REMOTE_APPS=dict(
116+
globus=globus.REMOTE_APP,
117+
),
118+
GLOBUS_APP_CREDENTIALS=GLOBUS_APP_CREDENTIALS,
119+
DEBUG=True,
120+
SECRET_KEY='TEST',
121+
SQLALCHEMY_ECHO=False,
122+
SECURITY_PASSWORD_SALT='security-password-salt',
123+
MAIL_SUPPRESS_SEND=True,
124+
TESTING=True,
125+
USERPROFILES_EXTEND_SECURITY_FORMS=True,
126+
)
127+
128+
Babel(app)
129+
FlaskMenu(app)
130+
InvenioDB(app)
131+
InvenioAccounts(app)
132+
InvenioUserProfiles(app)
133+
FlaskOAuth(app)
134+
InvenioOAuthClient(app)
135+
InvenioMail(app)
136+
137+
app.register_blueprint(blueprint_user)
138+
app.register_blueprint(blueprint_client)
139+
app.register_blueprint(blueprint_settings)
140+
app.register_blueprint(blueprint_userprofile_init)
141+
142+
143+
@app.route('/')
144+
def index():
145+
"""Homepage."""
146+
return 'Home page (without any restrictions)'
147+
148+
149+
@app.route('/globus')
150+
def globus():
151+
"""Try to print user email or redirect to login with globus."""
152+
if not current_user.is_authenticated:
153+
return redirect(url_for('invenio_oauthclient.login',
154+
remote_app='globus'))
155+
return 'hello {}'.format(current_user.email)

0 commit comments

Comments
 (0)