Overview
JWT (JSON Web Token) authentication provides a stateless, scalable authentication mechanism for Ant Media Server. Unlike traditional tokens, JWTs are self-contained and cryptographically signed, eliminating the need for server-side token storage.
JWT Filter
The JWTFilter class (filter/JWTFilter.java:31) handles JWT validation for REST API endpoints and streaming requests.
How JWT Filter Works
Enabling JWT Authentication
Via REST API
Enable for Streaming
For stream-level JWT authentication:
JWT Validation Methods
Ant Media Server supports two JWT validation approaches:
1. Secret Key Validation (HMAC-SHA256)
Validate JWTs signed with a shared secret:
See filter/JWTFilter.java:105-121 for the implementation.
2. JWKS Validation (RSA-256)
Validate JWTs using JSON Web Key Sets (JWKS) with public key cryptography:
See filter/JWTFilter.java:85-102 for the implementation.
Configuration
Using Secret Key
Configure a shared secret for HMAC signing:
Using JWKS URL
Configure a JWKS endpoint for RSA validation:
If both jwksURL and jwtSecretKey are configured, JWKS validation takes precedence. The filter checks JWKS first, then falls back to secret key validation.
Generating JWT Tokens
Basic JWT Token
Generate a JWT with expiration:
See filter/JWTFilter.java:193-213 for implementation.
JWT with Issuer
Include an issuer claim:
Validate with issuer check:
See filter/JWTFilter.java:125-142.
JWT with Custom Claims
Add custom claims for fine-grained control:
Example with subscriber ID:
Validate with claim:
See filter/JWTFilter.java:156-172 for claim validation.
Using JWT Tokens
REST API Requests
Include JWT in the Authorization header:
Stream Playback
Pass JWT as a query parameter:
WebRTC Publishing
Creating JWT Tokens via REST API
Stream-Specific JWT
Create a JWT token for a specific stream:
Response:
Using ITokenService
Programmatically create JWT tokens:
See security/ITokenService.java:111 for the interface definition.
JWT Structure
A typical Ant Media Server JWT contains:
Payload
Signature
Cluster Communication
In cluster mode, nodes use JWT for internal communication:
This allows edge nodes to authenticate with origin nodes (see filter/JWTFilter.java:60-62).
Security Best Practices
Use a strong secret key of at least 32 characters. Weak secrets can be brute-forced, compromising all your tokens.
Always validate the JWT expiration claim. Expired tokens should be rejected to prevent replay attacks.
Use HTTPS for all communications. JWTs transmitted over HTTP can be intercepted and used by attackers.
Rotate your JWT secret keys periodically. Key rotation limits the impact of potential key compromise.
For production environments, prefer JWKS with RSA-256 over shared secrets. Public key cryptography provides better security for distributed systems.
Advantages Over Simple Tokens
Stateless Authentication
JWTs don’t require server-side storage:
- No database lookups for validation
- Better scalability in cluster deployments
- Reduced server memory usage
JWTs can carry claims about the user:
- Subscriber ID
- Permissions
- Custom metadata
Cryptographic Security
JWTs are cryptographically signed:
- Tampering detection
- Authenticity verification
- Support for multiple algorithms
Industry Standard
JWT is a widely adopted standard:
- Interoperability with OAuth 2.0 / OpenID Connect
- Extensive library support
- Well-documented security considerations
Integration with Third-Party Auth
JWT authentication allows integration with external identity providers:
Auth0 Integration
- Configure Auth0 JWKS URL:
- Include Auth0 token in requests:
Keycloak Integration
- Configure Keycloak JWKS:
- Use Keycloak-issued tokens for API access
Troubleshooting
Invalid JWT Token Errors
Common causes:
- Expired Token: Check the
exp claim
- Wrong Secret: Verify
jwtSecretKey matches
- Malformed Token: Ensure proper JWT format (header.payload.signature)
- Algorithm Mismatch: Verify the signing algorithm
JWKS Validation Failures
- Invalid JWKS URL: Verify the URL is accessible
- Key ID Mismatch: Ensure JWT
kid matches JWKS keys
- Network Issues: Check connectivity to JWKS endpoint
- Certificate Problems: Verify SSL certificates are valid
Ensure the header format is correct:
Not:
Next Steps