Overview
SuperTokens Core implements a secure session management system with automatic token rotation, blacklisting support, and anti-CSRF protection. Sessions use JWT-based access tokens and encrypted refresh tokens.Session Architecture
Token Types
SuperTokens uses three types of tokens:Access Token
Short-lived JWT containing session data and user information
Refresh Token
Long-lived encrypted token used to generate new access tokens
ID Refresh Token
Client-side indicator for refresh token validity
Access Token Structure
Access tokens are JWTs signed with RS256 containing:Access tokens support multiple versions (V1-V5) for backwards compatibility. Version 5 includes both primary user ID (
sub) and recipe user ID (rsub) for account linking.Refresh Token Format
Refresh tokens are encrypted and contain:- Session handle
- User ID
- Parent refresh token hash (for rotation)
- Anti-CSRF token
- Nonce for encryption
- Tenant ID
<encrypted-payload>.<nonce>.V2
Creating Sessions
Sessions are created with:1
Generate Session Handle
Create a unique UUID for the session, appending tenant ID if not default:
2
Create Tokens
Generate refresh token, then create access token with refresh token hash:
3
Store Session in Database
Persist session with double-hashed refresh token:
Token Rotation
How Token Rotation Works
SuperTokens implements automatic token rotation to detect token theft:Refresh Process
From Session.java:543-650:- Verify refresh token and extract session handle
- Check anti-CSRF token if enabled
- Database transaction:
- Get session info from database
- Check if
refreshTokenHash2matches double-hashed input token - If match: Create new tokens with current token as parent
- If parent hash matches: Promote child token (update database)
- Otherwise: Token theft detected → throw exception
Verifying Sessions
Access Token Verification
The verification process:1
JWT Signature Verification
Verify JWT signature using public keys from signing key rotation
2
Check Expiry
Validate token hasn’t expired
3
Anti-CSRF Check
Compare anti-CSRF token from header with token in JWT
4
Database Check (Optional)
Verify session hasn’t been blacklisted or revoked
5
Token Promotion
If
parentRefreshTokenHash1 exists, promote to current tokenKey Verification Code
From AccessToken.java:62-169:Session Revocation
Revoke by Session Handle
Revoke All User Sessions
Session Deletion Process
From Session.java:819-857:1
Parse Tenant ID
Extract tenant ID from session handle (format:
uuid_tenantId)2
Group by Tenant
Organize session handles by their tenants
3
Delete from Storage
Remove sessions from database for each tenant
4
Return Revoked Sessions
Return list of successfully revoked session handles
Security Features
Anti-CSRF Protection
boolean
default:"true"
When enabled, generates a random CSRF token stored in both access and refresh tokens
- Session verification (if
doAntiCsrfCheckis true) - Session refresh
Token Theft Detection
SuperTokens detects token theft when:- A parent refresh token is reused after its child was already used
- The system throws
TokenTheftDetectedException - All sessions for that user are automatically revoked
Session Blacklisting
Optional database check during verification:Session Data Management
JWT Payload (Client-Accessible)
Stored in access token, available on client:Database Session Data (Server-Only)
Stored in database, never sent to client:Updating Session Data
Configuration
number
default:"3600"
Access token lifetime in seconds (default: 1 hour)
number
default:"144000"
Refresh token lifetime in minutes (default: 100 days)
number
default:"168"
Hours between signing key rotations (default: 7 days)
Multi-Tenancy Support
Sessions are tenant-aware:- Session handles include tenant ID:
{uuid}_{tenantId} - Access tokens contain
tIdclaim - Refresh tokens store tenant ID in encrypted payload
- Session operations are scoped to tenant storage
Best Practices
Always Use HTTPS
Never transmit tokens over insecure connections
Enable Anti-CSRF
Protect against cross-site attacks for web applications
Use Static Keys Sparingly
Dynamic keys provide better security through rotation
Monitor Token Theft
Log and alert on TokenTheftDetectedException events