Skip to content

: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.

feat(integrations): route GitHub OAuth callback to mobile app deep link (#59410)

Tom Owersb7e82910e37b
diff --git a/posthog/api/test/test_user_integration.py b/posthog/api/test/test_user_integration.py
index 1b86472fd59e..cfa7c7cd84c4 100644
--- a/posthog/api/test/test_user_integration.py
+++ b/posthog/api/test/test_user_integration.py
@@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch
from django.core.cache import cache
from django.test import override_settings
+from parameterized import parameterized
from rest_framework import status
from posthog.models import User
@@ -450,14 +451,25 @@ class TestUserIntegrationEndpoints(APIBaseTest):
self.assertEqual(integration.sensitive_config["user_access_token"], "gho_access")
self.assertEqual(integration.sensitive_config["access_token"], "ghs_install_token")
+ @parameterized.expand(
+ [
+ ("posthog_code", "/account-connected/github-integration"),
+ ("posthog_mobile", "posthog://github/callback"),
+ ]
+ )
@override_settings(GITHUB_APP_CLIENT_ID="client_id", GITHUB_APP_CLIENT_SECRET="client_secret")
@patch("posthog.api.user_integration.requests.get")
@patch("posthog.models.integration.GitHubIntegration.client_request")
@patch("posthog.models.integration.GitHubIntegration.github_user_from_code")
- def test_github_link_redirects_to_account_integration_connected_when_posthog_code(
- self, mock_user_from_code, mock_client_request, mock_verify_get
+ def test_github_link_redirects_to_client_destination_on_success(
+ self,
+ connect_from,
+ expected_destination,
+ mock_user_from_code,
+ mock_client_request,
+ mock_verify_get,
):
- """PostHog Code passes ``connect_from`` via start payload → cache; success uses return-to-app page."""
+ """First-party clients pass ``connect_from`` via start payload → cache; success redirects to their destination."""
mock_verify_get.return_value = MagicMock(status_code=200)
mock_user_from_code.return_value = _authorization()
mock_install_info = MagicMock()
@@ -472,10 +484,10 @@ class TestUserIntegrationEndpoints(APIBaseTest):
}
mock_client_request.side_effect = [mock_install_info, mock_access_token]
- state = "test_state_posthog_code"
+ state = f"test_state_{connect_from}"
cache.set(
f"github_user_install_state:{state}",
- {"user_id": self.user.id, "connect_from": "posthog_code"},
+ {"user_id": self.user.id, "connect_from": connect_from},
timeout=600,
)
@@ -486,8 +498,29 @@ class TestUserIntegrationEndpoints(APIBaseTest):
self.assertEqual(response.status_code, 302)
loc = response["Location"]
- self.assertIn("/account-connected/github-integration", loc)
+ self.assertIn(expected_destination, loc)
+ self.assertIn("provider=github", loc)
+ self.assertNotIn("error=", loc)
+
+ def test_github_link_redirects_to_mobile_deep_link_with_error(self):
+ """When GitHub returns an error, the mobile deep link still carries provider + error."""
+ state = "test_state_posthog_mobile_error"
+ cache.set(
+ f"github_user_install_state:{state}",
+ {"user_id": self.user.id, "connect_from": "posthog_mobile"},
+ timeout=600,
+ )
+
+ response = self.client.get(
+ "/complete/github-link/",
+ {"error": "access_denied", "state": state},
+ )
+
+ self.assertEqual(response.status_code, 302)
+ loc = response["Location"]
+ self.assertIn("posthog://github/callback", loc)
self.assertIn("provider=github", loc)
+ self.assertIn("error=access_denied", loc)
def test_github_link_callback_rejects_mismatched_state(self):
cache.set("github_user_install_state:valid_state", {"user_id": self.user.id}, timeout=600)
diff --git a/posthog/api/user_integration.py b/posthog/api/user_integration.py
index 9920fce0e6b0..d9f435bdca9f 100644
--- a/posthog/api/user_integration.py
+++ b/posthog/api/user_integration.py
@@ -67,6 +67,48 @@ logger = structlog.get_logger(__name__)
PERSONAL_INTEGRATIONS_SETTINGS_PATH = "/settings/user-personal-integrations"
ACCOUNT_CONNECTED_GITHUB_INTEGRATION_PATH = "/account-connected/github-integration"
+# Native deep link the mobile app (apps/mobile) registers as the OAuth return
+# URL. The in-app browser (ASWebAuthenticationSession / Custom Tabs) closes and
+# returns control to the app when the callback 302s here.
+MOBILE_GITHUB_CALLBACK_URL = "posthog://github/callback"
+# ``connect_from`` values for first-party clients that use the lightweight app
+# linking flow (OAuth-only when the team already has the GitHub App installed,
+# otherwise discover/install) and return to a client-specific destination.
+APP_CONNECT_FROM_VALUES = ("posthog_code", "posthog_mobile")
+
+
+class _AppDeepLinkRedirect(HttpResponseRedirect):
+ """Redirect that also permits the mobile app's custom ``posthog://`` scheme.
+
+ Django's default ``HttpResponseRedirect`` rejects non-web schemes as unsafe
+ (``DisallowedRedirect``). The target here is a hardcoded first-party deep
+ link, not user input, so allowing the extra scheme is safe.
+ """
+
+ allowed_schemes = [*HttpResponseRedirect.allowed_schemes, "posthog"]
+
+
+def _final_github_redirect(connect_from: str | None, *, error: str | None = None) -> HttpResponseRedirect:
+ """Pick the post-OAuth destination based on which client started the flow.
+
+ - ``posthog_mobile`` → the app's ``posthog://`` deep link so the in-app
+ browser auto-closes.
+ - ``posthog_code`` → the web ``/account-connected`` page that the desktop app
+ intercepts via its own deep link.
+ - anything else (web UI) → the personal integrations settings page.
+ """
+ app_base_urls: dict[str, str] = {
+ "posthog_mobile": MOBILE_GITHUB_CALLBACK_URL,
+ "posthog_code": ACCOUNT_CONNECTED_GITHUB_INTEGRATION_PATH,
+ }
+ if connect_from in app_base_urls:
+ params = {"provider": "github"}
+ if error:
+ params["error"] = error
+ return _AppDeepLinkRedirect(f"{app_base_urls[connect_from]}?{urlencode(params)}")
+ if error:
+ return redirect(f"{PERSONAL_INTEGRATIONS_SETTINGS_PATH}?github_link_error={error}")
+ return redirect(f"{PERSONAL_INTEGRATIONS_SETTINGS_PATH}?github_link_success=1")
def _github_oauth_authorize_url(state: str) -> str:
@@ -381,11 +423,12 @@ class UserIntegrationViewSet(viewsets.GenericViewSet):
page handles both cases: orgs where the app is installed show "Configure"
(no admin needed), orgs where it isn't show "Install" (needs admin).
- **PostHog Code fast path:** when ``connect_from`` is ``"posthog_code"``,
+ **First-party app fast path:** when ``connect_from`` is one of
+ ``APP_CONNECT_FROM_VALUES`` (e.g. ``"posthog_code"`` or ``"posthog_mobile"``),
the current project already has a team-level GitHub installation, and the
user has no ``UserIntegration`` for that installation yet, we skip the org
picker and redirect straight to ``/login/oauth/authorize`` so the user
- only authorizes themselves and returns to PostHog Code immediately.
+ only authorizes themselves and returns to the originating client immediately.
In both cases the response key is ``install_url`` for compatibility with callers.
"""
@@ -397,8 +440,8 @@ class UserIntegrationViewSet(viewsets.GenericViewSet):
team = _resolve_team_for_github_start(user, self.request)
connect_from = request.data.get("connect_from")
- if connect_from == "posthog_code":
- if fast_path_response := _attempt_posthog_code_oauth_fast_path(user, team, token, state):
+ if connect_from in APP_CONNECT_FROM_VALUES:
+ if fast_path_response := _attempt_app_oauth_fast_path(user, team, token, state, connect_from):
return fast_path_response
if _team_github_installation_id(team) is None:
cache.set(
@@ -433,19 +476,20 @@ class UserIntegrationViewSet(viewsets.GenericViewSet):
)
-def _posthog_code_flow_from_state_query(request: HttpRequest) -> bool:
- """Best-effort: OAuth error callbacks may still include ``state``; read cache (do not delete)."""
+def _app_connect_from_from_state_query(request: HttpRequest) -> str | None:
+ """Best-effort: OAuth error callbacks may still include ``state``; read cache
+ (do not delete) to recover which first-party client started the flow so the
+ error redirect returns to the right place."""
state_raw = request.GET.get("state")
if not state_raw:
- return False
+ return None
token = _github_state_token(state_raw)
cache_key = f"{GITHUB_INSTALL_STATE_CACHE_PREFIX}{token}"
state_payload = cache.get(cache_key)
- return bool(
- state_payload
- and state_payload.get("user_id") == request.user.id
- and state_payload.get("connect_from") == "posthog_code"
- )
+ if not state_payload or state_payload.get("user_id") != request.user.id:
+ return None
+ connect_from = state_payload.get("connect_from")
+ return connect_from if connect_from in APP_CONNECT_FROM_VALUES else None
@require_http_methods(["GET"])
@@ -466,14 +510,13 @@ def github_link_complete(request: HttpRequest) -> HttpResponseRedirect:
user = cast(User, request.user)
- posthog_code_flow = False
+ # Which first-party client (if any) started this flow; controls the final
+ # redirect destination. Resolved from validated server-side state below.
+ connect_from_value: str | None = None
def _error(reason: str) -> HttpResponseRedirect:
logger.warning("github_link: redirecting with error", reason=reason, user_id=user.id)
- if posthog_code_flow:
- q = urlencode({"provider": "github", "error": reason})
- return redirect(f"{ACCOUNT_CONNECTED_GITHUB_INTEGRATION_PATH}?{q}")
- return redirect(f"{PERSONAL_INTEGRATIONS_SETTINGS_PATH}?github_link_error={reason}")
+ return _final_github_redirect(connect_from_value, error=reason)
# GitHub appends ?error=... when the user denied consent.
if github_error := request.GET.get("error"):
@@ -483,8 +526,7 @@ def github_link_complete(request: HttpRequest) -> HttpResponseRedirect:
description=request.GET.get("error_description"),
user_id=user.id,
)
- if _posthog_code_flow_from_state_query(request):
- posthog_code_flow = True
+ connect_from_value = _app_connect_from_from_state_query(request)
return _error(github_error if github_error == "access_denied" else "github_oauth_error")
code = request.GET.get("code")
@@ -503,8 +545,7 @@ def github_link_complete(request: HttpRequest) -> HttpResponseRedirect:
state_payload = cache.get(cache_key)
if not state_payload or state_payload.get("user_id") != user.id:
return _error("invalid_state")
- if state_payload.get("connect_from") == "posthog_code":
- posthog_code_flow = True
+ connect_from_value = state_payload.get("connect_from")
cache.delete(cache_key)
flow = state_payload.get("flow")
@@ -562,10 +603,7 @@ def github_link_complete(request: HttpRequest) -> HttpResponseRedirect:
except requests.RequestException:
return _error("installation_fetch_failed")
if not installation_ids:
- return _redirect_to_github_app_install(
- user,
- "posthog_code" if posthog_code_flow else cast(str | None, state_payload.get("connect_from")),
- )
+ return _redirect_to_github_app_install(user, connect_from_value)
for installation_id in installation_ids:
# installation_id must be a plain positive integer (GitHub App IDs always are).
@@ -660,9 +698,7 @@ def github_link_complete(request: HttpRequest) -> HttpResponseRedirect:
joiner = "&" if "?" in target else "?"
return redirect(f"{target}{joiner}{urlencode(forwarded_params)}")
- if posthog_code_flow:
- return redirect(f"{ACCOUNT_CONNECTED_GITHUB_INTEGRATION_PATH}?{urlencode({'provider': 'github'})}")
- return redirect(f"{PERSONAL_INTEGRATIONS_SETTINGS_PATH}?github_link_success=1")
+ return _final_github_redirect(connect_from_value)
def _resolve_team_for_github_start(user: User, request: Request):
@@ -748,10 +784,14 @@ def _team_github_installation_id(team: Any) -> str | None:
return str(team_row.integration_id) if team_row is not None and team_row.integration_id else None
-def _attempt_posthog_code_oauth_fast_path(user: User, team: Any, token: str, state: str) -> Response | None:
+def _attempt_app_oauth_fast_path(
+ user: User, team: Any, token: str, state: str, connect_from: str | None
+) -> Response | None:
"""If the team has a GitHub installation the user hasn't linked yet, return
- an OAuth-only ``/login/oauth/authorize`` redirect so PostHog Code users
- authorize and return immediately — no org picker needed.
+ an OAuth-only ``/login/oauth/authorize`` redirect so first-party app users
+ (PostHog Code, mobile) authorize and return immediately — no org picker
+ needed. ``connect_from`` is preserved so the callback returns to the right
+ client.
Returns ``None`` when the fast path doesn't apply (no team integration,
user already linked, or missing config).
@@ -767,7 +807,7 @@ def _attempt_posthog_code_oauth_fast_path(user: User, team: Any, token: str, sta
"user_id": user.id,
"installation_id": team_installation_id,
"flow": "oauth_authorize",
- "connect_from": "posthog_code",
+ "connect_from": connect_from,
},
timeout=GITHUB_INSTALL_STATE_TTL_SECONDS,
)
diff --git a/products/integrations/frontend/generated/api.ts b/products/integrations/frontend/generated/api.ts
index 735295adf94c..efb0fdabb68e 100644
--- a/products/integrations/frontend/generated/api.ts
+++ b/products/integrations/frontend/generated/api.ts
@@ -855,11 +855,12 @@ user can pick any GitHub org (new or already connected). GitHub's install
page handles both cases: orgs where the app is installed show "Configure"
(no admin needed), orgs where it isn't show "Install" (needs admin).
-**PostHog Code fast path:** when ``connect_from`` is ``"posthog_code"``,
+**First-party app fast path:** when ``connect_from`` is one of
+``APP_CONNECT_FROM_VALUES`` (e.g. ``"posthog_code"`` or ``"posthog_mobile"``),
the current project already has a team-level GitHub installation, and the
user has no ``UserIntegration`` for that installation yet, we skip the org
picker and redirect straight to ``/login/oauth/authorize`` so the user
-only authorizes themselves and returns to PostHog Code immediately.
+only authorizes themselves and returns to the originating client immediately.
In both cases the response key is ``install_url`` for compatibility with callers.
* @summary Start GitHub personal integration linking
diff --git a/products/integrations/frontend/generated/api.zod.ts b/products/integrations/frontend/generated/api.zod.ts
index 0ac6d3668123..02255a45e8e0 100644
--- a/products/integrations/frontend/generated/api.zod.ts
+++ b/products/integrations/frontend/generated/api.zod.ts
@@ -366,11 +366,12 @@ user can pick any GitHub org (new or already connected). GitHub's install
page handles both cases: orgs where the app is installed show "Configure"
(no admin needed), orgs where it isn't show "Install" (needs admin).
-**PostHog Code fast path:** when ``connect_from`` is ``"posthog_code"``,
+**First-party app fast path:** when ``connect_from`` is one of
+``APP_CONNECT_FROM_VALUES`` (e.g. ``"posthog_code"`` or ``"posthog_mobile"``),
the current project already has a team-level GitHub installation, and the
user has no ``UserIntegration`` for that installation yet, we skip the org
picker and redirect straight to ``/login/oauth/authorize`` so the user
-only authorizes themselves and returns to PostHog Code immediately.
+only authorizes themselves and returns to the originating client immediately.
In both cases the response key is ``install_url`` for compatibility with callers.
* @summary Start GitHub personal integration linking