Overview
Orquestra uses JWT (JSON Web Token) for stateless authentication. After logging in via GitHub OAuth, you receive a JWT that must be included in subsequent API requests.Token Format
JWTs are composed of three parts separated by dots:Header
- Algorithm: HMAC-SHA256 (symmetric signing)
- Type: JWT
Payload (Claims)
Subject - the user ID
GitHub username
Issued At - Unix timestamp when the token was created
Expiration - Unix timestamp when the token expires (7 days from issue)
Signature
The signature is computed using HMAC-SHA256:JWT_SECRET environment variable.
Using JWT Tokens
Authorization Header
Include the JWT in theAuthorization header with the Bearer scheme:
Example Requests
Token Expiration
Default Expiration
JWT tokens expire 7 days after issuance.Checking Expiration
You can decode the JWT client-side to check expiration:Handling Expired Tokens
When a token expires, the API returns a401 Unauthorized error:
- Detect the 401 error
- Clear the stored token
- Redirect the user to the login page
Token Refresh
To minimize user disruption:- Tokens have a 7-day lifespan
- Users can stay logged in for a week of continuous use
- Consider warning users when their token is about to expire (e.g., within 24 hours)
Security Best Practices
Store tokens securely
Store tokens securely
- Frontend: Use
localStorageorsessionStorage(not cookies if you need cross-domain access) - Mobile: Use secure storage (Keychain on iOS, Keystore on Android)
- CLI: Store in a secure file with restricted permissions (
chmod 600) - Never commit tokens to version control
Protect against XSS
Protect against XSS
- Sanitize all user input
- Use Content Security Policy (CSP) headers
- Avoid
eval()and inline scripts - If using cookies, set
HttpOnlyandSecureflags
Use HTTPS
Use HTTPS
Always use HTTPS to prevent token interception. Never send tokens over HTTP.
Validate tokens server-side
Validate tokens server-side
- Always validate the signature
- Check expiration time
- Verify the issuer (if applicable)
- Orquestra handles this automatically for all protected endpoints
Handle token exposure
Handle token exposure
If a token is compromised:
- User should log in again to get a new token
- Old token will expire after 7 days
- Consider implementing a token blocklist for immediate revocation (not currently supported)
Authentication Middleware
Orquestra uses two middleware functions for JWT authentication:Required Authentication
Endpoints protected byauthMiddleware require a valid JWT:
/auth/me- Get current user/api/projects/mine- List user’s projects/api/projects/:projectId- Update/delete projects/api/projects/:projectId/keys- Manage API keys- All owner-only endpoints
401 Unauthorized.
Optional Authentication
Endpoints usingoptionalAuthMiddleware work with or without a token:
/api/projects- List projects (shows private projects if authenticated)/api/:projectId/docs- Get documentation (shows private docs if owner)/api/:projectId/addresses- List known addresses
Error Responses
401 Unauthorized
Missing Authorization Header
401 Unauthorized
Invalid Token Format
401 Unauthorized
Expired Token
401 Unauthorized
Invalid Signature
Implementation Reference
The JWT service is implemented in/packages/worker/src/services/jwt.ts using the Web Crypto API (compatible with Cloudflare Workers).
Token Generation
Token Verification
The implementation uses HMAC-SHA256 with Base64URL encoding, following the JWT standard (RFC 7519).
Comparison with API Keys
| Feature | JWT | API Key |
|---|---|---|
| Use Case | User authentication | Programmatic access |
| Lifetime | 7 days | Configurable or permanent |
| Scope | Full user access | Project-specific |
| Revocation | Cannot revoke (expires naturally) | Can delete anytime |
| Header | Authorization: Bearer <token> | X-API-Key: <key> |
| Best For | Web/mobile apps | CI/CD, bots, integrations |
Related Endpoints
- GitHub OAuth - Obtain a JWT token
- API Keys - Alternative authentication method
- Get Current User - Verify token validity