13 Commits

Author SHA1 Message Date
Thigazhezhilan J
98ef7701d1 fix: auto-refresh expired broker session on server startup
Every time pm2 restarts, the Zerodha token may be in EXPIRED state
(knocked out by the engine between midnight and 6:05 AM auto-login).
Now on startup we check each auto-login user's broker auth_state and
immediately re-login anyone who is expired/disconnected, so the broker
shows as connected the moment the user opens the website.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-04 09:34:17 +05:30
Thigazhezhilan J
6215910c9c fix: set auth_state to VALID (not CONNECTED) after auto-login reconnect
Frontend checks authState === 'VALID' to clear the session-expired
banner. Using 'CONNECTED' left the banner stuck even after a successful
token refresh.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-02 22:32:47 +05:30
Thigazhezhilan J
18d07c1910 Remove debug logging from auto-login (feature working)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:58:29 +05:30
Thigazhezhilan J
bec31b9f9d Add OAuth completion step after twofa
After twofa verifies identity, re-visit connect/login as authenticated user.
Zerodha then redirects to the registered callback URL with request_token.
This mirrors what the browser JS does to complete the OAuth flow.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:55:19 +05:30
Thigazhezhilan J
9d1126b84d Fix auto-login: pass sess_id to twofa to link request to OAuth app
The sess_id from the connect/login redirect is Zerodha's OAuth session
identifier. Without it in the twofa POST, Zerodha returns profile:{}
(regular web login) instead of redirect_url with request_token.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:49:28 +05:30
Thigazhezhilan J
e447a39912 Fix twofa returning profile instead of redirect_url
Following the connect/login redirect chain overwrites kf_session with a
plain web session, stripping the OAuth context. Stop at the first 302
so kf_session retains the api_key OAuth context through the twofa step.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:39:18 +05:30
Thigazhezhilan J
dc663a129c Fix auto-login: remove Referer header, add Accept/Origin headers
Referer on POST was causing Zerodha to reject login with Invalid username.
Add Accept, Accept-Language, Origin headers for proper browser-like requests.
Keep redirect_url body check for twofa. Add login step debug logging.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:28:09 +05:30
Thigazhezhilan J
4922ea69e8 Fix auto-login OAuth context not being established
Root cause: X-Kite-Version header on web login endpoints caused Zerodha
to return plain profile response instead of OAuth redirect_url.

Changes:
- Remove X-Kite-Version from session headers (only valid for Kite Connect API)
- Use allow_redirects=False on connect/login GET to preserve OAuth cookie
- Add Referer header to login/twofa POSTs
- Check data.redirect_url in twofa JSON body (modern Zerodha SPA behavior)
- Keep Location header fallback for legacy behavior

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-25 21:42:25 +05:30
Thigazhezhilan J
a1b19b7431 Add debug logging to auto-login twofa step
Prints twofa response status, Location header, and body so we can see
exactly what Zerodha returns after TOTP submission.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-25 21:30:03 +05:30
Thigazhezhilan J
df137afcbd Fix auto-login: initialize Zerodha OAuth session before credentials
Without first GETting the connect/login URL with the api_key,
Zerodha doesn't know which app is logging in and never returns
a request_token after TOTP — causing the redirect loop to fail.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-25 20:59:18 +05:30
Thigazhezhilan J
1b3be0437d Gracefully handle missing pyotp so backend always starts 2026-05-02 15:34:09 +05:30
Thigazhezhilan J
f5762de2b4 Fix startup crash by importing pyotp lazily inside function 2026-05-02 15:31:13 +05:30
Thigazhezhilan J
94f175668a Add automated daily Zerodha token refresh (auto-login)
- New auto_login_service.py: stores encrypted credentials (login ID,
  password, TOTP secret), performs headless Zerodha login via pyotp,
  and refreshes the session daily at 6:05 AM IST via background thread
- New auto_login router: setup, status, remove, and manual trigger endpoints
- Scheduler started at app boot alongside existing daemons
- Added pyotp==2.9.0 dependency

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-02 12:47:21 +05:30