Description
ENABLE_TOKEN_AUTH=true activates a TokenAuthProvider that is fully implemented but cannot actually authenticate users through the Telegram bot flow. Three gaps prevent it from working in practice.
Problems
1. No mechanism for users to submit tokens
There is no command (e.g., /auth <token>) or other UX for a Telegram user to present a token. The token generation method (TokenAuthProvider.generate_token()) exists but is never called from any handler.
2. Middleware never passes credentials to the auth manager
The auth middleware (src/bot/middleware/auth.py:65) calls:
authentication_successful = await auth_manager.authenticate_user(user_id)
No credentials dict is passed, so TokenAuthProvider.authenticate() always receives {}, finds no "token" key, and returns False.
3. Token storage is in-memory only
src/main.py initializes InMemoryTokenStorage() with a # TODO: Use database storage comment. Tokens are lost on every restart. The SQLite schema (user_tokens table in src/storage/database.py:103-113) and data model (UserTokenModel in src/storage/models.py:277-314) already exist but are unused.
What already works
TokenAuthProvider — token generation, SHA256 hashing, verification, revocation (src/security/auth.py:142-213)
InMemoryTokenStorage — functional in-memory implementation (src/security/auth.py:111-140)
- Config validation — cross-field check requiring
AUTH_TOKEN_SECRET when enabled (src/config/settings.py:351-354)
- Feature flag —
token_auth_enabled property in FeatureFlags (src/config/features.py:42-47)
- DB schema —
user_tokens table with token_hash, expires_at, last_used, is_active columns
- Tests — comprehensive coverage in
tests/unit/test_security/test_auth.py
Suggested fix
- Add an
/auth <token> command that passes {"token": token} as credentials to auth_manager.authenticate_user(user_id, credentials)
- Add a way for admins to generate tokens (e.g.,
/generate_token <user_id> or a CLI utility)
- Implement
SqliteTokenStorage backed by the existing user_tokens table
- Wire
SqliteTokenStorage into src/main.py in place of InMemoryTokenStorage
Description
ENABLE_TOKEN_AUTH=trueactivates aTokenAuthProviderthat is fully implemented but cannot actually authenticate users through the Telegram bot flow. Three gaps prevent it from working in practice.Problems
1. No mechanism for users to submit tokens
There is no command (e.g.,
/auth <token>) or other UX for a Telegram user to present a token. The token generation method (TokenAuthProvider.generate_token()) exists but is never called from any handler.2. Middleware never passes credentials to the auth manager
The auth middleware (
src/bot/middleware/auth.py:65) calls:authentication_successful = await auth_manager.authenticate_user(user_id)No
credentialsdict is passed, soTokenAuthProvider.authenticate()always receives{}, finds no"token"key, and returnsFalse.3. Token storage is in-memory only
src/main.pyinitializesInMemoryTokenStorage()with a# TODO: Use database storagecomment. Tokens are lost on every restart. The SQLite schema (user_tokenstable insrc/storage/database.py:103-113) and data model (UserTokenModelinsrc/storage/models.py:277-314) already exist but are unused.What already works
TokenAuthProvider— token generation, SHA256 hashing, verification, revocation (src/security/auth.py:142-213)InMemoryTokenStorage— functional in-memory implementation (src/security/auth.py:111-140)AUTH_TOKEN_SECRETwhen enabled (src/config/settings.py:351-354)token_auth_enabledproperty inFeatureFlags(src/config/features.py:42-47)user_tokenstable withtoken_hash,expires_at,last_used,is_activecolumnstests/unit/test_security/test_auth.pySuggested fix
/auth <token>command that passes{"token": token}as credentials toauth_manager.authenticate_user(user_id, credentials)/generate_token <user_id>or a CLI utility)SqliteTokenStoragebacked by the existinguser_tokenstableSqliteTokenStorageintosrc/main.pyin place ofInMemoryTokenStorage