:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from urllib.parse import parse_qsl, urlencode, urljoin, urlparse, urlunparse
from django.conf import settings
from django.core import signing
from django.db import IntegrityError, transaction
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from posthog.credentials_service.callers import CredentialsCaller
from posthog.credentials_service.errors import CredentialsServiceFailure
from posthog.credentials_service.instance_settings import get_many_or_instance_settings, get_or_instance_setting
from posthog.egress.slack.transport import slack_request
from posthog.helpers.impersonation import is_impersonated
from posthog.models.organization import OrganizationMembership
from posthog.models.team.team import Team
from posthog.models.user import User
from posthog.rate_limit import SupportSlackOAuthCallbackThrottle
from products.conversations.backend.models import TeamConversationsSlackConfig
from products.conversations.backend.permissions import IsConversationsAdmin
from products.conversations.backend.support_slack import clear_supporthog_slack_token, save_supporthog_slack_token
STATE_SALT = "conversations.supporthog.slack.oauth"
STATE_MAX_AGE_SECONDS = 10 * 60
SUPPORTHOG_SLACK_SCOPES = [
"channels:history",
"channels:read",
"chat:write",
"chat:write.customize",
# files:read to download inbound attachments, files:write to upload our replies' images.
# Slack only grants scopes at install time, so installs authorized before these were
# requested keep working without attachment sync until an admin reconnects. See
# SUPPORT_SLACK_FILE_SCOPES for how the settings page detects those installs.
"files:read",
"files:write",
"groups:history",
"groups:read",
# links:read delivers link_shared, links:write posts the unfurl. Slack only fires
# link_shared for domains registered on the app config, so an install that predates
# these scopes simply never unfurls until an admin reconnects.
"links:read",
"links:write",
"reactions:read",
"users:read",
"users:read.email",
]
SUPPORTHOG_SLACK_SCOPE = ",".join(SUPPORTHOG_SLACK_SCOPES)
def _append_query(url: str, params: dict[str, str]) -> str:
parsed = urlparse(url)
current = dict(parse_qsl(parsed.query, keep_blank_values=True))
current.update(params)
return urlunparse(parsed._replace(query=urlencode(current)))
def _get_callback_url() -> str:
return urljoin(settings.SITE_URL.rstrip("/") + "/", "api/conversations/v1/slack/callback")
def _safe_next_path(team_id: int, next_path: str | None) -> str:
default_path = f"/project/{team_id}/settings/support"
if not next_path:
return default_path
if not next_path.startswith("/") or next_path.startswith("//"):
return default_path
return next_path
def _error_response(next_path: str | None, error_message: str, status_code: int) -> HttpResponse:
if next_path:
redirect_url = _append_query(
urljoin(settings.SITE_URL.rstrip("/") + "/", next_path.lstrip("/")), {"error": error_message}
)
return HttpResponseRedirect(redirect_url)
return JsonResponse({"error": error_message}, status=status_code)
class SupportSlackAuthorizeView(APIView):
permission_classes = [IsAuthenticated, IsConversationsAdmin]
def get(self, request: Request, *args, **kwargs) -> Response:
client_id = get_or_instance_setting("SUPPORT_SLACK_APP_CLIENT_ID", CredentialsCaller.CONVERSATIONS)
if not client_id:
return Response({"error": "Support Slack OAuth client ID is not configured"}, status=503)
user = request.user
if not isinstance(user, User) or user.current_team is None:
return Response({"error": "No current team selected"}, status=400)
team_id = user.current_team.id
user_id = getattr(user, "id", None)
if not isinstance(user_id, int):
return Response({"error": "Invalid user id"}, status=400)
next_path = _safe_next_path(team_id, request.query_params.get("next"))
state_payload = {
"team_id": team_id,
"user_id": user_id,
"next": next_path,
}
state = signing.dumps(state_payload, salt=STATE_SALT)
oauth_url = _append_query(
"https://slack.com/oauth/v2/authorize",
{
"client_id": client_id,
"scope": SUPPORTHOG_SLACK_SCOPE,
"redirect_uri": _get_callback_url(),
"state": state,
},
)
return Response({"url": oauth_url})
class SupportSlackDisconnectView(APIView):
permission_classes = [IsAuthenticated, IsConversationsAdmin]
def post(self, request: Request, *args, **kwargs) -> Response:
user = request.user
if not isinstance(user, User) or user.current_team is None:
return Response({"error": "No current team selected"}, status=400)
clear_supporthog_slack_token(
team=user.current_team,
user=user,
is_impersonated_session=is_impersonated(request),
)
return Response({"ok": True})
@csrf_exempt
def support_slack_oauth_callback(request: HttpRequest) -> HttpResponse:
# IP throttle in front of any Slack token exchange — otherwise a stranger
# can loop a valid `state` param and force us to make outbound POSTs.
throttle = SupportSlackOAuthCallbackThrottle()
if not throttle.allow_request(Request(request), view=None): # type: ignore[arg-type]
return JsonResponse({"error": "Too Many Requests"}, status=429)
request_user = getattr(request, "user", None)
request_user_id = getattr(request_user, "id", None)
if not isinstance(request_user, User) or not isinstance(request_user_id, int):
return JsonResponse({"error": "Authentication required"}, status=401)
state_raw = request.GET.get("state")
code = request.GET.get("code")
oauth_error = request.GET.get("error")
state_data: dict
next_path: str | None = None
try:
if not state_raw:
return JsonResponse({"error": "Missing OAuth state"}, status=400)
state_data = signing.loads(state_raw, salt=STATE_SALT, max_age=STATE_MAX_AGE_SECONDS)
next_path = state_data.get("next")
except signing.SignatureExpired:
return JsonResponse({"error": "OAuth state expired"}, status=400)
except signing.BadSignature:
return JsonResponse({"error": "Invalid OAuth state"}, status=400)
if oauth_error:
return _error_response(next_path, oauth_error, 400)
if not code:
return _error_response(next_path, "missing_code", 400)
try:
support_settings = get_many_or_instance_settings(
["SUPPORT_SLACK_APP_CLIENT_ID", "SUPPORT_SLACK_APP_CLIENT_SECRET"], CredentialsCaller.CONVERSATIONS
)
except CredentialsServiceFailure:
return _error_response(next_path, "support_slack_unavailable", 503)
client_id = str(support_settings["SUPPORT_SLACK_APP_CLIENT_ID"])
client_secret = str(support_settings["SUPPORT_SLACK_APP_CLIENT_SECRET"])
if not client_id or not client_secret:
return _error_response(next_path, "support_slack_not_configured", 503)
try:
response = slack_request(
"POST",
"https://slack.com/api/oauth.v2.access",
source="conversations_oauth",
endpoint="oauth.v2.access",
app_id="support",
data={
"client_id": client_id,
"client_secret": client_secret,
"code": code,
"redirect_uri": _get_callback_url(),
},
timeout=15,
)
payload = response.json()
except Exception:
return _error_response(next_path, "oauth_exchange_failed", 502)
if response.status_code != 200 or not payload.get("ok"):
return _error_response(next_path, str(payload.get("error") or "oauth_exchange_failed"), 400)
bot_token = payload.get("access_token")
slack_team_id = payload.get("team", {}).get("id")
granted_scopes = [scope.strip() for scope in str(payload.get("scope") or "").split(",") if scope.strip()]
user_id = state_data.get("user_id")
team_id = state_data.get("team_id")
if not isinstance(bot_token, str) or not bot_token:
return _error_response(next_path, "missing_bot_token", 400)
if not isinstance(slack_team_id, str) or not slack_team_id:
return _error_response(next_path, "missing_slack_team_id", 400)
if not isinstance(user_id, int) or not isinstance(team_id, int):
return _error_response(next_path, "invalid_state_payload", 400)
if request_user_id != user_id:
return _error_response(next_path, "oauth_user_mismatch", 403)
try:
user = User.objects.get(id=user_id)
team = Team.objects.get(id=team_id)
except (User.DoesNotExist, Team.DoesNotExist):
return _error_response(next_path, "team_or_user_not_found", 404)
if not OrganizationMembership.objects.filter(user_id=user.id, organization_id=team.organization_id).exists():
return _error_response(next_path, "forbidden_team_access", 403)
try:
with transaction.atomic():
conflicting_config = (
TeamConversationsSlackConfig.objects.select_for_update()
.filter(slack_team_id=slack_team_id)
.exclude(team_id=team.id)
.first()
)
if conflicting_config:
return _error_response(next_path, "slack_workspace_already_connected", 409)
save_supporthog_slack_token(
team=team,
user=user,
is_impersonated_session=is_impersonated(request),
bot_token=bot_token,
slack_team_id=slack_team_id,
granted_scopes=granted_scopes,
)
except IntegrityError:
return _error_response(next_path, "slack_workspace_already_connected", 409)
redirect_url = _append_query(
urljoin(settings.SITE_URL.rstrip("/") + "/", _safe_next_path(team.id, next_path).lstrip("/")),
{"supportSlackConnected": "1"},
)
return HttpResponseRedirect(redirect_url)