From a12838032fe24c766280d31d65e4174918af1b51 Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Fri, 25 Aug 2017 11:47:40 +0200 Subject: [PATCH] Introducing exception handling in the application code --- pillar/auth/oauth.py | 9 +++++++-- pillar/web/users/routes.py | 20 +++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pillar/auth/oauth.py b/pillar/auth/oauth.py index e5a082a3..7130a8d2 100644 --- a/pillar/auth/oauth.py +++ b/pillar/auth/oauth.py @@ -93,8 +93,13 @@ class OAuthSignIn(metaclass=abc.ABCMeta): cls._providers = {} # TODO convert to the new __init_subclass__ for provider_class in cls.__subclasses__(): - provider = provider_class() - cls._providers[provider.provider_name] = provider + try: + provider = provider_class() + except ProviderConfigurationMissing: + # TODO: log this at info level + pass + else: + cls._providers[provider.provider_name] = provider try: return cls._providers[provider_name] except KeyError: diff --git a/pillar/web/users/routes.py b/pillar/web/users/routes.py index 14033ad2..66ca0441 100644 --- a/pillar/web/users/routes.py +++ b/pillar/web/users/routes.py @@ -15,7 +15,8 @@ from pillar.web import system_util from pillar.api.local_auth import generate_and_store_token, get_local_user from pillar.api.utils.authentication import find_user_in_db, upsert_user from pillar.api.blender_cloud.subscription import update_subscription -from pillar.auth.oauth import OAuthSignIn +from pillar.auth.oauth import OAuthSignIn, ProviderConfigurationMissing, ProviderNotImplemented, \ + OAuthCodeNotProvided from . import forms log = logging.getLogger(__name__) @@ -31,7 +32,16 @@ def check_oauth_provider(provider): def oauth_authorize(provider): if not current_user.is_anonymous: return redirect(url_for('main.homepage')) - oauth = OAuthSignIn.get_provider(provider) + + try: + oauth = OAuthSignIn.get_provider(provider) + except ProviderConfigurationMissing as e: + log.error('Login with OAuth failed: %s', e) + raise wz_exceptions.NotFound() + except ProviderNotImplemented as e: + log.error('Login with OAuth failed: %s', e) + raise wz_exceptions.NotFound() + return oauth.authorize() @@ -40,7 +50,11 @@ def oauth_callback(provider): if not current_user.is_anonymous: return redirect(url_for('main.homepage')) oauth = OAuthSignIn.get_provider(provider) - oauth_user = oauth.callback() + try: + oauth_user = oauth.callback() + except OAuthCodeNotProvided as e: + log.error(e) + raise wz_exceptions.Forbidden() if oauth_user.id is None: log.debug('Authentication failed for user with {}'.format(provider)) return redirect(url_for('main.homepage'))