Overview
Torn uses JWT (JSON Web Token) authentication for securing API endpoints. The authentication flow involves:- Login with email and password
- Receive a JWT access token
- Include the token in subsequent requests
- Optionally validate the session
Token Configuration
JWT signing algorithm
Access tokens expire after 12 hours (720 minutes)
OAuth2 bearer token format
Authentication Endpoints
POST /auth/login
JSON-based login endpoint that accepts email and password credentials.User’s email address
User’s password
JWT access token for authentication
Token type (always “bearer”)
List of tenants (companies) the user has access to
Tenant ID
Company name
Chilean tax ID (RUT)
User’s role in this tenant (e.g., “ADMINISTRADOR”, “VENDEDOR”)
Whether the tenant is active
Maximum number of users allowed for this tenant
Role-based permissions for this tenant
POST /auth/token
OAuth2-compatible token endpoint using form data (alternative to/auth/login).
User’s email address (sent as “username” for OAuth2 compatibility)
User’s password
/auth/login.
GET /auth/users/me
Retrieve the current authenticated user’s profile. Headers:Bearer token:
Bearer YOUR_ACCESS_TOKENUser ID
User’s email address
User’s full name
Account status
Superuser status
GET /auth/validate
Validate the current session and refresh the list of available tenants. Headers:Bearer token:
Bearer YOUR_ACCESS_TOKENUsing Authentication Tokens
After obtaining an access token, include it in theAuthorization header for all authenticated requests:
Token Format
The JWT token contains the following claims:- sub: User’s email address (subject)
- exp: Token expiration timestamp (UTC)
Security Implementation
Password Hashing
Passwords are hashed using bcrypt algorithm via thepasslib library:
- Scheme:
bcrypt - Deprecated schemes are automatically handled
- Plain passwords are never stored
app/utils/security.py:15-21
Token Generation
Tokens are generated using thepython-jose library:
app/utils/security.py:23-31
Token Validation
Token validation is handled by theget_current_global_user dependency:
- Extract token from Authorization header
- Decode JWT using SECRET_KEY
- Extract user email from “sub” claim
- Query database for user
- Return user object or raise 401 error
app/dependencies/tenant.py:34-56
Multi-Tenant Authentication Flow
Torn’s authentication works across two levels:1. Global Authentication
First, authenticate at the SaaS level to get your access token:2. Tenant Access
Use the token withX-Tenant-ID header to access tenant-specific resources:
- Token is valid and not expired
- User has access to the specified tenant
- User’s role has appropriate permissions
app/dependencies/tenant.py:59-82
Superusers can access any tenant, even without explicit membership. For regular users, access is validated through the
TenantUser relationship.Common Authentication Errors
Environment Variables
Authentication relies on the following environment variables:JWT signing secret key (MUST be changed in production)
Best Practices
- Store tokens securely: Keep access tokens in secure storage (httpOnly cookies, secure local storage)
- Handle token expiration: Implement token refresh logic or re-authentication after 12 hours
- Use HTTPS: Always use HTTPS in production to prevent token interception
- Validate permissions: Check user roles and permissions before performing operations
- Rotate secrets: Periodically rotate the SECRET_KEY in production environments
Next Steps
- Explore Tenant Management to learn about multi-tenant operations
- Review User Management for managing users within tenants
- Check Roles & Permissions for access control details