Skip to content

Add Telegram browser login for user credential extraction#54

Open
joshalbrecht wants to merge 5 commits intomainfrom
mngr/telegram-init
Open

Add Telegram browser login for user credential extraction#54
joshalbrecht wants to merge 5 commits intomainfrom
mngr/telegram-init

Conversation

@joshalbrecht
Copy link
Copy Markdown

@joshalbrecht joshalbrecht commented Apr 7, 2026

Summary

  • Adds TelegramUserCredentials type that stores MTProto auth_key data extracted from web.telegram.org localStorage after browser-based login
  • latchkey auth browser telegram now opens a browser, waits for the user to log in, then extracts the auth_key and persists it in the encrypted credential store as TelegramUserCredentials
  • Adds latchkey auth get <service_name> command that outputs stored credentials as JSON, enabling programmatic access from external scripts
  • The existing latchkey auth set-nocurl telegram <bot-token> flow for bot tokens is unchanged

Changes

  • New TelegramUserCredentials class storing dcId, authKeyHex, userId, firstName
  • New TelegramBrowserSession that overrides login() with a manual "press Enter after login" flow (Telegram Web A's SPA makes response-based auto-detection unreliable)
  • TelegramUserCredentialsSchema registered in apiCredentialsSerialization.ts
  • Telegram.checkApiCredentials() returns Unknown for user credentials (MTProto can't be checked via curl)
  • New auth get CLI command for programmatic credential retrieval
  • Registry test updated: uses AWS instead of Telegram as example of service without getSession

Test plan

  • npm run build passes
  • npm run lint passes
  • npm run format:check passes
  • npm test passes (265 tests pass; 2 timeout-only failures on lint/typecheck test wrappers that pass when run directly)
  • Manual test: ran latchkey auth browser telegram, logged in, verified credentials extracted and persisted
  • Manual test: used extracted credentials with Telethon to connect and send messages to BotFather
  • Manual test: latchkey auth get telegram outputs stored credentials as JSON

Generated with Claude Code

joshalbrecht and others added 4 commits April 7, 2026 07:41
Extends the Telegram service to support `latchkey auth browser telegram`.
Instead of extracting final credentials, this exploration session dumps
all localStorage, sessionStorage, cookies, and captured network requests
to /tmp/latchkey-telegram-dump.json for inspection. This lets us see what
auth tokens web.telegram.org makes available after login.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…tect

The previous approach tried to auto-detect login completion from network
responses, but the Telegram Web A SPA loads its full bundle (>50KB) before
login, causing false-positive detection. Also, BrowserFollowupServiceSession
opens a spinner in a second tab.

Now overrides login() directly with a simpler flow:
- Opens browser to web.telegram.org/a/
- Prompts user to press Enter after logging in
- Dumps localStorage, sessionStorage, IndexedDB structure, cookies,
  and all captured network requests to /tmp/latchkey-telegram-dump.json
- Prints a summary of what was found

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Two scripts that confirm the full auth extraction chain works:

- test_telegram_session.py: Reads auth_key from browser dump, converts
  to Telethon StringSession, connects and verifies identity via get_me()
- test_botfather.py: Sends /mybots to BotFather and reads the response,
  confirming we can programmatically interact with BotFather

Both tested successfully: authenticated as the user and received
BotFather responses.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Adds TelegramUserCredentials type that stores MTProto auth_key data
extracted from web.telegram.org localStorage after browser-based login.
This enables programmatic Telegram user API access via libraries like
Telethon without requiring a separate phone/OTP flow.

Changes:
- Add TelegramUserCredentials (dcId, authKeyHex, userId, firstName)
- Add TelegramBrowserSession that opens web.telegram.org, waits for
  user login, then extracts auth_key from localStorage
- Register TelegramUserCredentials in serialization system
- Telegram service now supports both `auth browser` (user credentials)
  and `auth set-nocurl` (bot token)
- Update registry test: use AWS instead of Telegram as example of
  service without getSession (Telegram now has it)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vet found 1 issue.


🔴 Vet Issue test_coverage severity: 3/5, confidence: 0.90

The diff introduces significant new functionality (TelegramUserCredentials class, TelegramBrowserSession, serialization/deserialization of TelegramUserCredentials) but no unit tests are added for the new credential type's serialization, deserialization, toJSON/fromJSON, or injectIntoCurlCall behavior. The existing test files (e.g., tests/apiCredentials.test.ts) should be updated with tests for TelegramUserCredentials.

Adds `latchkey auth get <service_name>` which outputs the stored
credentials as JSON. This enables programmatic access to stored
credentials (e.g., for the Telegram bot creation script to read
the extracted auth_key).

Also fixes prettier formatting on telegram.ts that was causing CI
format check failures.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vet found 1 issue.


🔴 Vet Issue test_coverage severity: 3/5, confidence: 0.90

The diff introduces significant new functionality: TelegramUserCredentials class, TelegramBrowserSession class, the 'auth get' CLI command, and serialization/deserialization support for TelegramUserCredentials. However, no new unit tests are added for any of these. The only test change is modifying an existing test to use AWS instead of Telegram. Tests should be added for TelegramUserCredentials serialization/deserialization, the 'auth get' command, and the checkApiCredentials override.

Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vet found 1 issue.


🔴 Vet Issue test_coverage severity: 3/5, confidence: 0.85

The diff introduces significant new functionality: TelegramUserCredentials class, TelegramBrowserSession, serialization/deserialization of user credentials, the auth get CLI command, and checkApiCredentials override. No unit tests are added for any of these. At minimum, tests for TelegramUserCredentials serialization/deserialization roundtrip and the checkApiCredentials override returning Unknown should be added.

@hynek-urban
Copy link
Copy Markdown
Collaborator

hynek-urban commented Apr 8, 2026

Thanks, Josh :) I appreciate this! Still, I think most of this functionality is probably out of scope for Latchkey:

  • latchkey auth get is missing intentionally. Latchkey is rather specialized - it injects API credentials to curl-encoded HTTP requests. In other words, it is not a generic credentials management tool. It's meant to be used by agents and we don't want those to be able to retrieve arbitrary credentials in plaintext. (Ideally, there should exist better guarantees than just a missing method in the interface but even so, the direction we'd like to go is the opposite of auth get :) )
  • The newly introduced auth browser telegram runs an interactive session in the terminal (waiting for keypresses) which makes it different from all other auth browser invocations and unsuitable e.g. as an llm tool.
  • The implementation returns credentials that can only be used with the MTProto protocol, not HTTP, making it out of scope for a "HTTP credentials injection" tool.

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.

2 participants