Skip to content

Fix Google OAuth in Telegram's in-app browser#104

Merged
priyanshujain merged 8 commits intomasterfrom
fix-telegram-webview-auth
Mar 19, 2026
Merged

Fix Google OAuth in Telegram's in-app browser#104
priyanshujain merged 8 commits intomasterfrom
fix-telegram-webview-auth

Conversation

@priyanshujain
Copy link
Copy Markdown
Collaborator

@priyanshujain priyanshujain commented Mar 19, 2026

Summary

  • Adds an auth redirect trampoline page (GET /auth/redirect) that detects Telegram's WebView and shows a "Continue with Google" button with target="_blank", forcing the system browser (Safari/Chrome) to open. In real browsers, it auto-redirects immediately.
  • Wraps the Google OAuth URL through the trampoline when AuthRedirectURL is configured on GWSToolConfig, so Telegram users can actually sign in with their Google accounts (WebView lacks cookie/passkey support).
  • Derives the trampoline URL automatically from the existing GWS callback URL config — no new config fields needed.

Test plan

  • go test ./internal/server/ -run TestHandleAuthRedirect -v — missing URL → 400, non-Google URL → 400, HTTP → 400, valid URL → 200 with correct HTML, ampersand escaping
  • go test ./agent/tools/ -run TestGWSExecute_AuthRedirect -v — wraps URL when set, passes through directly when empty
  • go test ./channel/telegram/ -run TestAuthRedirectURL -v — derives correct URL from callback config, returns empty when unconfigured
  • All existing tests in ./internal/server/, ./agent/tools/, ./channel/telegram/ pass
  • Manual E2E: start server with ngrok, trigger GWS command needing new scopes in Telegram, verify trampoline appears, tap "Continue with Google", verify Safari opens with Google accounts visible

Serves an HTML page that detects Telegram's WebView and shows a
"Continue with Google" button with target="_blank" to force the
system browser. In non-Telegram browsers, auto-redirects immediately.
Validates that the URL is a Google OAuth URL to prevent open redirects.
Adds AuthRedirectURL field to GWSToolConfig. When set, requestConsent
wraps the Google OAuth URL inside the trampoline URL so Telegram's
WebView opens the trampoline page instead of Google directly.
Derives the trampoline URL from the existing GWS callback URL and
passes it to GWSToolConfig so OAuth links in Telegram open via the
trampoline page, allowing users to sign in with their system browser.
The user agent string in Telegram's iOS WebView doesn't contain
"Telegram", so the auto-redirect fired incorrectly. Check for
window.TelegramWebviewProxy (injected by Telegram) in addition
to the user agent regex.
requestConsent now sends the auth link and returns immediately with
errAuthPending instead of blocking the agent loop on scopeWaiter.Await.
The waiter entry is cleaned up by a background goroutine. Account
discovery is moved to the start of Execute so retries after auth work.
This stops the "typing..." indicator from hanging while waiting for
the user to complete OAuth.
Include the Telegram Mini App JS SDK and call openLink() on button
click to open the Google OAuth URL in the system browser (Safari).
Falls back to normal link behavior in non-Telegram contexts.
Telegram's in-app browser ignores target="_blank" on iOS. Auth
redirect URLs now use a web_app inline keyboard button, which opens
the page as a Mini App where Telegram.WebApp.openLink() can open
the system browser (Safari). Regular URLs still use url buttons.
@priyanshujain priyanshujain merged commit d2a0564 into master Mar 19, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant