2022-06-03 08:09:47 -06:00
|
|
|
|
import logging
|
|
|
|
|
|
|
2023-03-21 16:15:35 +08:00
|
|
|
|
from django.conf import settings
|
2022-06-03 08:09:47 -06:00
|
|
|
|
from django.contrib.auth import REDIRECT_FIELD_NAME
|
2023-07-18 10:31:11 -03:00
|
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2022-06-03 08:09:47 -06:00
|
|
|
|
from django.views.decorators.cache import never_cache
|
|
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
from rest_framework.decorators import api_view, authentication_classes
|
2024-04-02 14:59:03 -04:00
|
|
|
|
from rest_framework.request import Request
|
2022-06-03 08:09:47 -06:00
|
|
|
|
from rest_framework.response import Response
|
2024-04-02 14:59:03 -04:00
|
|
|
|
from social_core.actions import do_auth, do_complete, do_disconnect
|
|
|
|
|
|
from social_core.backends.google import GoogleOAuth2
|
2022-06-03 08:09:47 -06:00
|
|
|
|
from social_django.utils import psa
|
|
|
|
|
|
from social_django.views import _do_login
|
|
|
|
|
|
|
2025-04-21 14:23:37 -03:00
|
|
|
|
from apps.auth_token.auth import (
|
|
|
|
|
|
GoogleTokenAuthentication,
|
|
|
|
|
|
MattermostTokenAuthentication,
|
|
|
|
|
|
PluginAuthentication,
|
|
|
|
|
|
SlackTokenAuthentication,
|
|
|
|
|
|
)
|
2024-06-03 17:07:10 +08:00
|
|
|
|
from apps.chatops_proxy.utils import (
|
|
|
|
|
|
get_installation_link_from_chatops_proxy,
|
|
|
|
|
|
get_slack_oauth_response_from_chatops_proxy,
|
|
|
|
|
|
)
|
2024-10-09 08:55:10 -04:00
|
|
|
|
from apps.grafana_plugin.ui_url_builder import UIURLBuilder
|
2024-06-03 17:07:10 +08:00
|
|
|
|
from apps.slack.installation import install_slack_integration
|
2025-04-21 14:23:37 -03:00
|
|
|
|
from apps.social_auth.backends import SLACK_INSTALLATION_BACKEND, LoginMattermostOAuth2, LoginSlackOAuth2V2
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(["GET"])
|
|
|
|
|
|
@authentication_classes([PluginAuthentication])
|
|
|
|
|
|
@never_cache
|
|
|
|
|
|
@psa("social:complete")
|
2024-04-02 14:59:03 -04:00
|
|
|
|
def overridden_login_social_auth(request: Request, backend: str) -> Response:
|
2024-06-03 17:07:10 +08:00
|
|
|
|
"""
|
|
|
|
|
|
overridden_login_social_auth starts the installation of integration which uses OAuth flow.
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
# We can't just redirect frontend here because we need to make a API call and pass tokens to this view from JS.
|
|
|
|
|
|
# So frontend can't follow our redirect.
|
|
|
|
|
|
# So wrapping and returning URL to redirect as a string.
|
2024-04-02 14:59:03 -04:00
|
|
|
|
if "slack" in backend and settings.SLACK_INTEGRATION_MAINTENANCE_ENABLED:
|
2023-03-21 16:15:35 +08:00
|
|
|
|
return Response(
|
|
|
|
|
|
"Grafana OnCall is temporary unable to connect your slack account or install OnCall to your slack workspace",
|
|
|
|
|
|
status=400,
|
|
|
|
|
|
)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
2024-06-03 17:07:10 +08:00
|
|
|
|
if backend == SLACK_INSTALLATION_BACKEND and settings.UNIFIED_SLACK_APP_ENABLED:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Install unified slack integration via chatops-proxy.
|
|
|
|
|
|
1. Get installation link from chatops-proxy
|
|
|
|
|
|
2. If link is not None – slack installation already exists on Chatops-Proxy - install using it's oauth response.
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
link = get_installation_link_from_chatops_proxy(request.user)
|
|
|
|
|
|
if link is not None:
|
|
|
|
|
|
return Response(link, 200)
|
|
|
|
|
|
else:
|
|
|
|
|
|
slack_oauth_response = get_slack_oauth_response_from_chatops_proxy(request.user.organization.stack_id)
|
|
|
|
|
|
install_slack_integration(request.user.organization, request.user, slack_oauth_response)
|
|
|
|
|
|
return Response("slack integration installed", 201)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.exception("overridden_login_social_auth: Failed to install slack via chatops-proxy: %s", e)
|
|
|
|
|
|
return Response({"error": "something went wrong, try again later"}, 500)
|
|
|
|
|
|
else:
|
|
|
|
|
|
# Otherwise use social-auth.
|
|
|
|
|
|
url_to_redirect_to = do_auth(request.backend, redirect_name=REDIRECT_FIELD_NAME).url
|
2022-06-03 08:09:47 -06:00
|
|
|
|
return Response(url_to_redirect_to, 200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(["GET"])
|
2025-04-21 14:23:37 -03:00
|
|
|
|
@authentication_classes([GoogleTokenAuthentication, SlackTokenAuthentication, MattermostTokenAuthentication])
|
2022-06-03 08:09:47 -06:00
|
|
|
|
@never_cache
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
|
@psa("social:complete")
|
2024-04-02 14:59:03 -04:00
|
|
|
|
def overridden_complete_social_auth(request: Request, backend: str, *args, **kwargs) -> Response:
|
2022-06-03 08:09:47 -06:00
|
|
|
|
"""Authentication complete view"""
|
2023-11-29 12:04:48 -03:00
|
|
|
|
kwargs.update(
|
2022-06-03 08:09:47 -06:00
|
|
|
|
user=request.user,
|
|
|
|
|
|
redirect_name=REDIRECT_FIELD_NAME,
|
|
|
|
|
|
request=request,
|
2023-11-29 12:04:48 -03:00
|
|
|
|
)
|
|
|
|
|
|
result = do_complete(
|
|
|
|
|
|
request.backend,
|
|
|
|
|
|
_do_login,
|
2022-06-03 08:09:47 -06:00
|
|
|
|
*args,
|
|
|
|
|
|
**kwargs,
|
|
|
|
|
|
)
|
2023-02-08 09:08:18 -03:00
|
|
|
|
|
2023-07-18 10:31:11 -03:00
|
|
|
|
# handle potential errors in the strategy pipeline
|
|
|
|
|
|
return_to = None
|
|
|
|
|
|
if isinstance(result, HttpResponse):
|
|
|
|
|
|
# check if there was a redirect set in the session
|
|
|
|
|
|
return_to = request.backend.strategy.session.get(REDIRECT_FIELD_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
if return_to is None:
|
2024-10-09 08:55:10 -04:00
|
|
|
|
url_builder = UIURLBuilder(request.user.organization)
|
|
|
|
|
|
|
|
|
|
|
|
# if this was a user login/linking account, redirect to profile (ie. users/me)
|
|
|
|
|
|
# otherwise it pertains to the InstallSlackOAuth2V2 backend, and we should redirect to the chat-ops page
|
|
|
|
|
|
return_to = (
|
|
|
|
|
|
url_builder.user_profile()
|
2025-04-21 14:23:37 -03:00
|
|
|
|
if isinstance(request.backend, (LoginMattermostOAuth2, LoginSlackOAuth2V2, GoogleOAuth2))
|
2024-10-09 08:55:10 -04:00
|
|
|
|
else url_builder.chatops()
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
return HttpResponseRedirect(return_to)
|
2024-04-02 14:59:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(["GET"])
|
|
|
|
|
|
@authentication_classes([PluginAuthentication])
|
|
|
|
|
|
@never_cache
|
|
|
|
|
|
@psa("social:disconnect")
|
|
|
|
|
|
def overridden_disconnect_social_auth(request: Request, backend: str) -> Response:
|
|
|
|
|
|
if backend == "google-oauth2":
|
|
|
|
|
|
do_disconnect(request.backend, request.user)
|
|
|
|
|
|
return Response("ok", 200)
|