def _init_app_rest(app_): """Init OAuth rest app.""" FlaskOAuth(app_) InvenioOAuthClientREST(app_) app_.register_blueprint(rest_blueprint) return app_
from invenio_oauthclient import InvenioOAuthClient # Create Flask application app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///app.db') app.config.update( ACCOUNTS_USE_CELERY=False, CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND='memory', CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_RESULT_BACKEND='cache', MAIL_SUPPRESS_SEND=True, SECRET_KEY='CHANGE_ME', SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO', ) FlaskCLI(app) Babel(app) InvenioDB(app) InvenioAccounts(app) FlaskOAuth(app) InvenioOAuthClient(app) InvenioAdmin(app, permission_factory=lambda x: x, view_class_factory=lambda x: x)
def __init__(self, app): """Initialize state.""" self.app = app self.handlers = {} self.disconnect_handlers = {} self.signup_handlers = {} # Connect signal to remove access tokens on logout user_logged_out.connect(oauth_logout_handler) self.oauth = app.extensions.get('oauthlib.client') or FlaskOAuth() # Add remote applications self.oauth.init_app(app) remote_app_class = load_or_import_from_config('OAUTHCLIENT_REMOTE_APP', app, default=OAuthRemoteApp) for remote_app, conf in app.config['OAUTHCLIENT_REMOTE_APPS'].items(): # Prevent double creation problems if remote_app not in self.oauth.remote_apps: # use this app's specific remote app class if there is one. current_remote_app_class = obj_or_import_string( conf.get('remote_app'), default=remote_app_class) # Register the remote app. We are doing this because the # current version of OAuth.remote_app does not allow to specify # the remote app class. Use it once it is fixed. self.oauth.remote_apps[remote_app] = current_remote_app_class( self.oauth, remote_app, **conf['params']) remote = self.oauth.remote_apps[remote_app] # Set token getter for remote remote.tokengetter(make_token_getter(remote)) # Register authorized handler self.handlers[remote_app] = authorized_handler( make_handler( conf.get('authorized_handler', authorized_default_handler), remote), remote.authorized_response) # Register disconnect handler self.disconnect_handlers[remote_app] = make_handler( conf.get('disconnect_handler', disconnect_handler), remote, with_response=False, ) # Register sign-up handlers def dummy_handler(remote, *args, **kargs): pass signup_handler = conf.get('signup_handler', dict()) account_info_handler = make_handler(signup_handler.get( 'info', dummy_handler), remote, with_response=False) account_setup_handler = make_handler(signup_handler.get( 'setup', dummy_handler), remote, with_response=False) account_view_handler = make_handler(signup_handler.get( 'view', dummy_handler), remote, with_response=False) self.signup_handlers[remote_app] = dict( info=account_info_handler, setup=account_setup_handler, view=account_view_handler, )
def test_authorized(base_app, params): """Test login.""" app = base_app handled = {} def test_authorized_handler(resp, remote, *args, **kwargs): """Save configuration.""" handled['resp'] = resp handled['remote'] = remote handled['args'] = args handled['kwargs'] = kwargs return 'TEST' def test_invalid_authorized_handler(resp, remote, *args, **kwargs): """Set wrong configuration.""" handled['resp'] = 1 handled['remote'] = 1 handled['args'] = 1 handled['kwargs'] = 1 base_app.config['OAUTHCLIENT_REMOTE_APPS'].update( dict( test=dict( authorized_handler=test_authorized_handler, params=params('testid'), title='MyLinkedTestAccount', ), test_invalid=dict( authorized_handler=test_invalid_authorized_handler, params=params('test_invalidid'), title='Test Invalid', ), full=dict( params=params('fullid'), title='Full', ), )) FlaskOAuth(app) InvenioOAuthClient(app) base_app.register_blueprint(blueprint_client) base_app.register_blueprint(blueprint_settings) with app.test_client() as client: # Ensure remote apps have been loaded (due to before first # request) client.get(url_for('invenio_oauthclient.login', remote_app='test')) mock_response(app.extensions['oauthlib.client'], 'test') mock_response(app.extensions['oauthlib.client'], 'test_invalid') from invenio_oauthclient.views.client import serializer state = serializer.dumps({ 'app': 'test', 'sid': _create_identifier(), 'next': None, }) resp = client.get( url_for('invenio_oauthclient.authorized', remote_app='test', code='test', state=state)) assert resp.data == b'TEST' assert handled['remote'].name == 'test' assert not handled['args'] assert not handled['kwargs'] assert handled['resp']['access_token'] == 'test_access_token' state = serializer.dumps({ 'app': 'test_invalid', 'sid': _create_identifier(), 'next': None, }) # handler should return something # Flask>1.0 is throwing TypeError and Flask<1.0 ValueError with pytest.raises((ValueError, TypeError)): client.get( url_for( 'invenio_oauthclient.authorized', remote_app='test_invalid', code='test', state=state, ))
def __init__(self, app): """Initialize state.""" self.app = app self.handlers = {} self.disconnect_handlers = {} self.signup_handlers = {} # Connect signal to remove access tokens on logout user_logged_out.connect(oauth_logout_handler) self.oauth = app.extensions.get('oauthlib.client') or FlaskOAuth() # Add remote applications self.oauth.init_app(app) for remote_app, conf in app.config['OAUTHCLIENT_REMOTE_APPS'].items(): # Prevent double creation problems if remote_app not in self.oauth.remote_apps: remote = self.oauth.remote_app(remote_app, **conf['params']) remote = self.oauth.remote_apps[remote_app] # Set token getter for remote remote.tokengetter(make_token_getter(remote)) # Register authorized handler self.handlers[remote_app] = remote.authorized_handler( make_handler( conf.get('authorized_handler', authorized_default_handler), remote, )) # Register disconnect handler self.disconnect_handlers[remote_app] = make_handler( conf.get('disconnect_handler', disconnect_handler), remote, with_response=False, ) # Register sign-up handlers def dummy_handler(remote, *args, **kargs): pass signup_handler = conf.get('signup_handler', dict()) account_info_handler = make_handler(signup_handler.get( 'info', dummy_handler), remote, with_response=False) account_setup_handler = make_handler(signup_handler.get( 'setup', dummy_handler), remote, with_response=False) account_view_handler = make_handler(signup_handler.get( 'view', dummy_handler), remote, with_response=False) self.signup_handlers[remote_app] = dict( info=account_info_handler, setup=account_setup_handler, view=account_view_handler, )