← Back to PRs

#19492: feat(telegram): add TOTP 2FA verification for Telegram DMs

by moltizmo open 2026-02-17 21:41 View on GitHub →
channel: telegram cli size: L
## Summary Adds time-based one-time password (TOTP) support for Telegram channels, providing an additional authentication layer beyond the existing DM allowlist. ## Motivation OpenClaw's Telegram channel currently relies on `allowFrom` (numeric user ID allowlist) for DM access control. While effective, this is a single factor — if a Telegram account is compromised, the attacker gains full access to the assistant. TOTP adds a second factor: users must enter a 6-digit code when starting a new session. This was built and battle-tested in production since Feb 9, 2026, handling daily sessions without issues. ## What's included | File | Purpose | |------|---------| | `src/totp/totp.ts` | Core TOTP library (RFC 6238, SHA-1, 6-digit, 30s window) | | `src/totp/totp-store.ts` | Persistent store for enrollment, verification, sessions | | `src/totp/totp-tool-gate.ts` | Restricts tool access until TOTP verified | | `src/telegram/totp-gate.ts` | Telegram DM middleware for TOTP challenge/response | | `src/config/types.telegram.ts` | `TelegramTotpConfig` type definition | | `src/config/zod-schema.providers-core.ts` | Zod schema for config validation | ## Configuration ```json { "channels": { "telegram": { "totp": { "enabled": true, "sessionDurationSeconds": 86400, "maxAttempts": 5, "rateLimitWindowSeconds": 300 } } } } ``` ## Security features - **RFC 6238 compliant** — standard TOTP algorithm, compatible with Google Authenticator, Authy, 1Password, etc. - **Rate limiting** — locks out after configurable max failed attempts - **Session duration** — verified sessions expire after configurable period (default 24h) - **Clock drift tolerance** — ±1 window (±30s) - **Secure storage** — enrollment data stored in `~/.openclaw/credentials/`, not in config - **Enrollment flow** — generates secret + QR code URI for authenticator app setup ## How it works 1. User sends first message → TOTP gate intercepts, prompts for 6-digit code 2. User enters code → verified against enrolled secret 3. Session marked as verified for `sessionDurationSeconds` 4. Tool gate blocks tool execution until session is verified 5. After session expires, user must re-verify on next message ## Testing Tested in production for 8 days with daily Telegram sessions. Handles: - Fresh enrollment - Session persistence across gateway restarts - Rate limiting on failed attempts - Graceful degradation when TOTP is disabled Co-authored-by: Bharadwaz Kari <zendizmo@gmail.com> <!-- greptile_comment --> <h3>Greptile Summary</h3> Adds TOTP 2FA infrastructure for Telegram DMs with RFC 6238-compliant implementation, rate limiting, and session management. The core cryptographic implementation is solid, but the PR has critical integration issues that prevent it from functioning: - TOTP gate middleware (`checkTotpGate`) is not integrated into Telegram message handlers - Tool gate wrapper (`wrapToolsWithTotpGate`) is not connected to tool execution flow - No enrollment mechanism exists (no CLI commands or bot handlers to call `enrollTotpUser`) - `totp-tool-gate.ts` references undefined `protectedToolGroups` config property The TOTP library itself (RFC 6238, base32 encoding, HMAC-SHA1) is correctly implemented with proper security features (rate limiting, file locking, secure storage). However, without integration into the Telegram provider, this code is currently non-functional. <h3>Confidence Score: 1/5</h3> - Critical integration issues prevent the TOTP feature from functioning - Core TOTP implementation is solid and secure, but three critical logical errors make this feature completely non-functional: (1) gate middleware never called, (2) tool wrapper never applied, (3) no enrollment mechanism exists. Additionally, one syntax error references undefined config property. - All files require integration work - `src/telegram/totp-gate.ts` and `src/totp/totp-tool-gate.ts` must be wired into Telegram handlers, enrollment commands must be added, and `TelegramTotpConfig` type needs `protectedToolGroups` property <sub>Last reviewed commit: a36e7c0</sub> <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> <!-- /greptile_comment -->

Most Similar PRs