Overview
The Soft-Bee API uses JSON Web Tokens (JWT) for authentication and authorization. JWT configuration controls token generation, validation, and expiration.The API uses two types of tokens: access tokens for API authentication and reset tokens for password reset flows.
JWT Environment Variables
Secret Key
Secret key used to sign and verify JWT tokens
secret-key-default (development only)
Algorithm
Algorithm used for signing JWT tokensSupported algorithms:
HS256(HMAC with SHA-256) - Recommended for most use casesHS384(HMAC with SHA-384)HS512(HMAC with SHA-512)RS256(RSA with SHA-256) - Requires public/private key pair
HS256
Access Token Expiration
Expiration time for access tokens in minutesDefault:
1440 minutes (24 hours)15- 15 minutes (high security)60- 1 hour (balanced)1440- 24 hours (default, user convenience)10080- 7 days (long-lived sessions)
Shorter expiration times are more secure but require users to log in more frequently. Balance security with user experience.
Reset Token Expiration
Expiration time for password reset tokens in minutesDefault:
30 minutes15- 15 minutes (high security)30- 30 minutes (default, balanced)60- 1 hour (user convenience)
Configuration in Code
The JWT configuration is loaded from environment variables inconfig.py:
config.py
Environment-Specific Examples
Generating Secure Keys
For production deployments, generate cryptographically secure JWT keys:Using OpenSSL
Using Python
Using Node.js
Token Types
Access Tokens (Session Tokens)
Purpose: Used for authenticating API requests Expiration: Controlled byEXPIRES_TOKEN_SESSION (default: 24 hours)
Usage:
- Included in
Authorizationheader asBearer {token} - Used for all authenticated API endpoints
- Returned upon successful login
Reset Tokens (Email Tokens)
Purpose: Used for password reset flows Expiration: Controlled byEXPIRES_TOKEN_EMAIL (default: 30 minutes)
Usage:
- Sent via email for password reset
- One-time use only
- Shorter expiration for security
Production Validation
In production mode, the application validates that critical JWT configuration is present:config.py
Security Best Practices
Use strong, random keys - Generate keys using cryptographically secure methods
Different keys per environment - Use unique JWT keys for each environment
Separate from SECRET_KEY - JWT_KEY should be different from Flask’s SECRET_KEY
Shorter expiration in production - Consider shorter token lifetimes for production
Secure key storage - Use environment variables or secret managers, never hardcode
Rotate keys periodically - Implement key rotation for long-running production systems
Use HTTPS only - Always transmit tokens over encrypted connections
Token Expiration Recommendations
Access Tokens
High Security
15-60 minutesBest for: Financial apps, healthcare, sensitive data
Balanced
1-24 hoursBest for: Most web applications (default)
User Convenience
7-30 daysBest for: Mobile apps, trusted devices
Reset Tokens
High Security
10-15 minutesShort window, forces quick action
Balanced
30 minutesRecommended default setting
User Convenience
1-2 hoursMore time for users to reset
Troubleshooting
Token Expired Error
Token Expired Error
Cause: The access token has exceeded its expiration time.Solution:
- Request a new token by logging in again
- Consider increasing
EXPIRES_TOKEN_SESSIONfor better UX - Implement token refresh mechanism
Invalid Token Error
Invalid Token Error
Cause: Token signature verification failed.Solutions:
- Ensure
JWT_KEYis the same across all app instances - Check that the token hasn’t been tampered with
- Verify the algorithm matches (
ALGORITHM)
Production Startup Failure
Production Startup Failure
Cause: Missing
JWT_KEY in production environment.Solution:Token Not Working After Key Change
Token Not Working After Key Change
Cause: Existing tokens were signed with old key.Solution:
- All users must log in again to get new tokens
- This is expected behavior when rotating keys
- Consider implementing graceful key rotation
Integration Example
Here’s how to use JWT tokens with the API:Next Steps
Environment Configuration
Configure different environments
Database Configuration
Set up database connections