|
| 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