diff --git a/engine/apps/social_auth/middlewares.py b/engine/apps/social_auth/middlewares.py index 19180ebc..09583dcd 100644 --- a/engine/apps/social_auth/middlewares.py +++ b/engine/apps/social_auth/middlewares.py @@ -16,25 +16,30 @@ logger = logging.getLogger(__name__) class SocialAuthAuthCanceledExceptionMiddleware(SocialAuthExceptionMiddleware): def process_exception(self, request, exception): - backend = getattr(exception, "backend", None) - url_builder = UIURLBuilder(request.user.organization) - url_builder_function = url_builder.chatops + strategy = getattr(request, "social_strategy", None) + if strategy is None or self.raise_exception(request, exception): + return - if backend is not None and isinstance(backend, LoginSlackOAuth2V2): - url_builder_function = url_builder.user_profile + if isinstance(exception, exceptions.SocialAuthBaseException): + backend = getattr(exception, "backend", None) + url_builder = UIURLBuilder(request.user.organization) + url_builder_function = url_builder.chatops - if exception: - logger.warning(f"SocialAuthAuthCanceledExceptionMiddleware.process_exception: {exception}") + if backend is not None and isinstance(backend, LoginSlackOAuth2V2): + url_builder_function = url_builder.user_profile - if isinstance(exception, exceptions.AuthCanceled): - # if user canceled authentication, redirect them to the previous page using the same link - # as we used to redirect after auth/install - return redirect(url_builder_function()) - elif isinstance(exception, exceptions.AuthFailed): - # if authentication was failed, redirect user to the plugin page using the same link - # as we used to redirect after auth/install with error flag - return redirect(url_builder_function(f"?slack_error={SLACK_AUTH_FAILED}")) - elif isinstance(exception, KeyError) and REDIRECT_AFTER_SLACK_INSTALL in exception.args: - return HttpResponse(status=status.HTTP_401_UNAUTHORIZED) - elif isinstance(exception, InstallMultiRegionSlackException): - return redirect(url_builder_function(f"?tab=Slack&slack_error={SLACK_REGION_ERROR}")) + if exception: + logger.warning(f"SocialAuthAuthCanceledExceptionMiddleware.process_exception: {exception}") + + if isinstance(exception, exceptions.AuthCanceled): + # if user canceled authentication, redirect them to the previous page using the same link + # as we used to redirect after auth/install + return redirect(url_builder_function()) + elif isinstance(exception, exceptions.AuthFailed): + # if authentication was failed, redirect user to the plugin page using the same link + # as we used to redirect after auth/install with error flag + return redirect(url_builder_function(f"?slack_error={SLACK_AUTH_FAILED}")) + elif isinstance(exception, KeyError) and REDIRECT_AFTER_SLACK_INSTALL in exception.args: + return HttpResponse(status=status.HTTP_401_UNAUTHORIZED) + elif isinstance(exception, InstallMultiRegionSlackException): + return redirect(url_builder_function(f"?tab=Slack&slack_error={SLACK_REGION_ERROR}"))