JWT Authentication
This guide explains how to configure Palpo to authenticate users using JSON Web Tokens (JWT).
Overview
JWT authentication allows external systems to authenticate users to Palpo without requiring them to enter Matrix credentials. This is useful for:
- Integrating Matrix into existing applications with their own authentication
- Single sign-on (SSO) scenarios
- Automated systems that need to act as users
- Migration from Synapse or other homeservers using JWT authentication
Prerequisites
- A system capable of generating valid JWTs
- A shared secret (for HMAC) or public key (for ECDSA)
- Understanding of JWT claims and structure
Basic Configuration
Add the following section to your palpo.toml configuration file:
Configuration Options
Key Formats
HMAC (Default)
Use a plaintext shared secret:
Generate a secure secret:
B64HMAC
Use a base64-encoded secret (useful for binary keys):
Generate a base64 secret:
ECDSA
Use a PEM-encoded public key for asymmetric verification:
JWT Token Structure
The JWT token must contain the following claim:
The sub (subject) claim is used as the Matrix user ID (localpart).
Optional Claims
Token Validation
Configure validation requirements:
Login Flow
To authenticate with JWT:
- Your application generates a JWT with the user's identity
- The client makes a login request to Palpo:
- Palpo validates the token and creates/authenticates the user
- Client receives access token for Matrix API calls
Example: Generating JWT in Different Languages
Python
Node.js
Rust
Security Considerations
- Keep secrets secure: Never expose HMAC secrets in client-side code or logs
- Use strong secrets: At least 256 bits (32 bytes) for HMAC algorithms
- Set expiration: Always include
expclaim and setrequire_exp = true - Validate issuer: Set
issuerto prevent tokens from untrusted sources - Use HTTPS: Always transmit tokens over encrypted connections
- Consider ECDSA: For public-facing systems, asymmetric keys (ECDSA) are more secure as the verification key can be shared without compromising token generation
Synapse Migration
If migrating from Synapse with JWT authentication:
Troubleshooting
"Invalid token" Error
- Verify the secret matches between token generator and Palpo
- Check the algorithm matches (
HS256,ES256, etc.) - Ensure the token hasn't expired
"User not found" Error
- If
register_user = false, the user must already exist - Check the
subclaim contains a valid username
"Invalid audience/issuer" Error
- Ensure token claims match the configured
audienceandissuerarrays - Check for case sensitivity and exact string matching
Signature Verification Failed
- For HMAC: ensure secrets are identical (watch for extra whitespace)
- For ECDSA: ensure the public key is correctly formatted (PEM format)
- Verify the algorithm in token header matches configuration