Skip to content

Add QR login to telegram-mcp - #2

Merged
fdciabdul merged 2 commits into
imtaqin:mainfrom
I3eka:feat/qr-login
Aug 3, 2026
Merged

Add QR login to telegram-mcp#2
fdciabdul merged 2 commits into
imtaqin:mainfrom
I3eka:feat/qr-login

Conversation

@I3eka

@I3eka I3eka commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add telegram-mcp login --qr alongside the existing phone/code login
  • use gotd's QR flow, including automatic refresh of expired login tokens and DC migration handling
  • render the QR directly in the terminal with instructions for Telegram's Settings -> Devices -> Link Desktop Device flow
  • handle SESSION_PASSWORD_NEEDED after QR acceptance with hidden 2FA input, retry invalid passwords, and wipe each plaintext password buffer after SRP
  • keep the Telegram protocol layer reusable so the TUI can adopt the same QR flow separately
  • document the new command and add CLI/auth regression tests

Why

The README currently lists QR login as not implemented. Telegram supports QR authorization through auth.exportLoginToken / auth.importLoginToken, and gotd already provides the token refresh and migration flow: https://core.telegram.org/api/qr-login

This is especially useful when a login code is delivered to a channel that a third-party client cannot surface clearly. The existing telegram-mcp login phone flow remains the default and is unchanged.

Security

  • QR URLs are rendered as QR codes and are not printed as plaintext
  • 2FA input uses term.ReadPassword, so it is not echoed
  • password bytes are passed to gotd's SRP implementation and zeroed after every attempt
  • the QR session continues to use the existing MCP session-file isolation rules

Validation

  • go test -count=1 -timeout=5m ./...
  • go vet ./...
  • go build ./...
  • git diff --check
  • live smoke against Telegram with a separate temporary session: received a real QR token, rendered scan instructions and expiry, waited for acceptance, then cancelled and removed all smoke artifacts

Copilot AI review requested due to automatic review settings August 3, 2026 02:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds QR-based authentication to telegram-mcp, enabling users to authorize via Telegram’s QR login flow while keeping the underlying Telegram protocol logic reusable for other frontends (e.g., the TUI).

Changes:

  • Add a reusable QR login implementation in internal/telegram with optional 2FA password prompting and secret wiping.
  • Extend telegram-mcp login with a --qr mode that renders a scannable terminal QR and prompts for hidden 2FA if needed.
  • Update documentation and add regression tests for the QR/auth command behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
README.md Documents telegram-mcp login --qr and explains QR refresh + 2FA behavior.
internal/telegram/qr.go Introduces reusable QR login flow and 2FA completion helper.
internal/telegram/qr_test.go Adds unit tests for QR fallback-to-2FA behavior and secret wiping on retry.
go.mod Promotes golang.org/x/term to a direct dependency for hidden password input.
cmd/telegram-mcp/main.go Adds --qr parsing and an interactive QR login mode in the CLI.
cmd/telegram-mcp/main_test.go Adds tests covering command parsing and --qr validation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/telegram/qr.go
Comment on lines +148 to +151
secret, err := prompt(ctx, retry)
if err != nil {
return fmt.Errorf("read 2FA password: %w", err)
}
Comment thread cmd/telegram-mcp/main.go
Comment on lines +42 to 46
if err != nil {
fmt.Fprintf(os.Stderr, "telegram-mcp: %v\n", err)
printUsage(os.Stderr)
os.Exit(2)
}

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Suppressed comments (1)

internal/telegram/qr.go:156

  • When the password prompt returns an empty slice (user hit enter), the buffer is discarded without being wiped. This breaks the function's own contract that the returned byte slice is "consumed and wiped" and could leave sensitive bytes in memory for prompt implementations that reuse buffers (e.g. returning buf[:0]).
		if len(secret) == 0 {
			retry = true
			continue
		}

@fdciabdul

Copy link
Copy Markdown
Member

Reviewed the QR implementation — solid work: correct use of gotd's qrlogin flow (token refresh + DC migration), SESSION_PASSWORD_NEEDED handling with retry on ErrPasswordInvalid, password buffers wiped, and x/term was already in the module graph so no new dependency. Happy to merge this.

But it needs a rebase first: the repo was transferred tegal1337imtaqin and the Go module path was renamed to github.com/imtaqin/telegram-cli in e34c971 (touches every import). Please rebase onto current main and update the import paths — the new files (internal/telegram/qr.go, internal/telegram/qr_test.go, cmd/telegram-mcp/main_test.go) still reference the old module, and cmd/telegram-mcp/main.go / go.mod will conflict since main moved on. After rebase we'll merge.

I3eka added 2 commits August 3, 2026 20:32
Wipe partial 2FA password buffers returned alongside prompt errors. Treat flag.ErrHelp as successful usage output instead of an invalid command.
@I3eka

I3eka commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Reviewed the QR implementation — solid work: correct use of gotd's qrlogin flow (token refresh + DC migration), SESSION_PASSWORD_NEEDED handling with retry on ErrPasswordInvalid, password buffers wiped, and x/term was already in the module graph so no new dependency. Happy to merge this.

But it needs a rebase first: the repo was transferred tegal1337imtaqin and the Go module path was renamed to github.com/imtaqin/telegram-cli in e34c971 (touches every import). Please rebase onto current main and update the import paths — the new files (internal/telegram/qr.go, internal/telegram/qr_test.go, cmd/telegram-mcp/main_test.go) still reference the old module, and cmd/telegram-mcp/main.go / go.mod will conflict since main moved on. After rebase we'll merge.

done

@fdciabdul
fdciabdul merged commit c510274 into imtaqin:main Aug 3, 2026
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.

3 participants