Skip to content

Commit

Permalink
SocialAuthAuthCanceledExceptionMiddleware should only process social … (
Browse files Browse the repository at this point in the history
#5221)

…auth related exceptions

# What this PR does

## Which issue(s) this PR closes

Related to [issue link here]

<!--
*Note*: If you want the issue to be auto-closed once the PR is merged,
change "Related to" to "Closes" in the line above.
If you have more than one GitHub issue that this PR closes, be sure to
preface
each issue link with a [closing
keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue).
This ensures that the issue(s) are auto-closed once the PR has been
merged.
-->

## Checklist

- [ ] Unit, integration, and e2e (if applicable) tests updated
- [ ] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
  • Loading branch information
iskhakov authored Nov 4, 2024
1 parent e9969f4 commit 07d289b
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions engine/apps/social_auth/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@

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

if backend is not None and isinstance(backend, LoginSlackOAuth2V2):
url_builder_function = url_builder.user_profile

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}"))
strategy = getattr(request, "social_strategy", None)
if strategy is None or self.raise_exception(request, exception):
return

if isinstance(exception, exceptions.SocialAuthBaseException):
backend = getattr(exception, "backend", None)
url_builder = UIURLBuilder(request.user.organization)
url_builder_function = url_builder.chatops

if backend is not None and isinstance(backend, LoginSlackOAuth2V2):
url_builder_function = url_builder.user_profile

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}"))

0 comments on commit 07d289b

Please sign in to comment.