Prompt
I'm building a SaaS app and want to detect account takeover and token theft, what should I implement?
Latest observation
To detect account takeover (ATO) and token theft in a SaaS app, you want layered detection across login, session, API usage, and privileged actions. The goal is to catch suspicious behavior early and make stolen credentials/tokens less useful.
1) Build a strong event trail
Log security-relevant events with enough context to correlate behavior:
- Login success/failure
- MFA enrollment/changes
- Password changes/reset
- Session creation/refresh/revocation
- New device / new browser / new IP
- API token issuance, use, rotation, and revocation
- Privileged actions:
- role changes
- billing changes
- email changes
- webhook changes
- OAuth app consent
- export/download of large data sets
- Admin actions and impersonation events
Include:
- user ID, org/workspace ID
- session ID / token ID / JWT
jtiif present - IP, ASN, geo, user agent, device fingerprint
- timestamp, request path, action type, status
- authentication method and MFA status
- risk score and reason codes
2) Detect suspicious authentication patterns
Common ATO indicators:
- Impossible travel: logins from distant locations too quickly
- New device + sensitive action shortly after login
- Password reset followed by immediate takeover-like behavior
- Multiple failed logins before a success
- Credential stuffing signals:
- many accounts from same IP/ASN
- many failures across many usernames
- headless/browser automation patterns
- MFA anomalies:
- MFA disabled or changed
- push fatigue patterns
- recovery code usage
- Session churn:
- frequent logins/refreshes from different IPs/devices
- abrupt UA changes
3) Detect token theft / session hijacking
Token theft often shows up as “valid token, wrong context.”
Watch for:
- Same session/token used from multiple IPs/geos/devices in a short period
- Concurrent use of the same token in impossible ways
- Access token used from a context different from the original issuance:
- new ASN/country/device
- different user agent family
- Refresh token reuse after rotation
- Tokens used after suspected compromise events:
- password reset
- MFA reset
- device de-registration
- API token use from unusual automation patterns or new service accounts
- Very old tokens suddenly used after dormancy
If you use JWTs, consider tracking:
jti(unique token ID)iat,exp- subject (
sub) - token type/scopes
- issuing client/app/device
4) Risk-based authentication and step-up
Don’t just detect; respond dynamically.
Implement:
- Step-up MFA for risky logins or actions
- Additional verification for:
- new device
- new geography
- unusual velocity
- privilege changes
- export/download actions
- Session re-authentication for sensitive operations
- Adaptive throttling and temporary lockouts for suspicious activity
5) Session and token hardening
Prevention reduces false positives and impact.
For browser sessions
- Secure, HttpOnly, SameSite cookies
- Short session lifetime
- Idle timeout + absolute timeout
- Rotate session ID after login and privilege elevation
- Bind session to device signals where feasible
- CSRF protection
- Consider WebAuthn/passkeys for stronger auth
For API tokens
- Prefer scoped, short-lived tokens
- Rotate tokens regularly
- Store hashed token identifiers server-side if possible
- Support instant revocation
- Use refresh token rotation with reuse detection
- Separate human vs machine credentials
- Limit scope and audience tightly
6) Model behavior baselines
For each user/org, learn normal patterns:
- typical countries/IP ranges
- common devices/browser versions
- usual login times
- typical endpoints and action sequences
- usual data volume
Then alert on deviations:
- first-time location/device
- unusual time-of-day
- abnormal request volume
- new API usage patterns
- admin actions outside normal workflow
7) Correlate identity and app signals
Best detection comes from combining signals:
- IdP events (Okta/Azure AD/Google Workspace)
- SSO logs
- MFA logs
- endpoint/device posture if available
- email change/password reset flows
- SIEM/EDR feeds if customers have them
If you support enterprise customers, expose webhooks or logs so they can ingest your events into their SIEM.
8) Create a risk engine
A simple approach is a scored rules engine:
Example signals:
- new country: +30
- new device: +25
- impossible travel: +50
- refresh token reuse: +80
- password reset within 10 minutes of new login: +40
- multiple failed logins across accounts from same IP: +35
- same token from 3 IPs in 15 minutes: +60
- MFA disabled: +70
Then:
- 0–29: no action
- 30–59: log and watch
- 60–79: step-up MFA / temporary friction
- 80+: revoke sessions / require password reset / alert user & admin
9) Alerts and user notifications
Notify users and admins about suspicious security events:
- new login from new device/location
- MFA changes
- password/email changes
- new API key created
- token revoked due to suspicious activity
- privileged actions from new context
Include clear actions:
- “This wasn’t me”
- revoke all sessions
- reset password
- re-enroll MFA
- review recent actions
10) Incident response automation
When confidence is high:
- revoke active sessions
- revoke refresh tokens/API keys
- require password reset
- require MFA re-enrollment
- block IP/ASN temporarily
- freeze sensitive operations
- create incident case with timeline of actions
11) Privacy and usability considerations
Avoid being too noisy:
- use multiple signals before hard blocking
- allow trusted devices or risk-based exceptions
- keep geo rules tolerant of VPNs and mobile networks
- provide user-friendly explanations and recovery paths
12) Minimum viable implementation
If you want a practical starting set, implement these first:
- Centralized audit log for auth/session/token events
- Device/IP/ASN/geo capture at auth and API layers
- Refresh token rotation + reuse detection
- Same token used from multiple contexts detection
- Step-up MFA for risky logins and sensitive actions
- Alerts for password/MFA/email/session/token changes
- Privileged-action monitoring and immediate revocation options
Example rules to start with
- Login from a new country + MFA change within 1 hour → alert + step-up
- Same session token used from two countries in 30 minutes → revoke sessions
- Refresh token reused after rotation → revoke family of tokens
- Password reset followed by mass export or role change → high-risk incident
- API token used from new ASN and high-volume export endpoint → alert + throttle
If you want, I can help you design:
- a risk scoring schema
- a database/event schema
- or a real-time detection pipeline for your stack (Postgres, Kafka, Redis, SIEM, etc.).