Skip to main content

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
Format: <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:
When a refresh token is used, its hash becomes the “parent” hash. If the parent token is used again, it indicates token theft, and all sessions for that user are revoked.

Refresh Process

From Session.java:543-650:
  1. Verify refresh token and extract session handle
  2. Check anti-CSRF token if enabled
  3. Database transaction:
    • Get session info from database
    • Check if refreshTokenHash2 matches 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 token

Key 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
The anti-CSRF token is validated on:
  • Session verification (if doAntiCsrfCheck is true)
  • Session refresh

Token Theft Detection

SuperTokens detects token theft when:
  1. A parent refresh token is reused after its child was already used
  2. The system throws TokenTheftDetectedException
  3. All sessions for that user are automatically revoked
From Session.java:652-654:

Session Blacklisting

Optional database check during verification:

Session Data Management

JWT Payload (Client-Accessible)

Stored in access token, available on client:
JWT data is visible to the client. Never store sensitive information in the JWT payload.

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 tId claim
  • Refresh tokens store tenant ID in encrypted payload
  • Session operations are scoped to tenant storage
From Session.java:141-144:

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