Overview
CompanyFlow implements a JWT (JSON Web Token) authentication system that provides stateless, secure authentication for employees across the API. Each token contains user identity, role, and tenant information.Authentication Flow
1. Login Request
Employees authenticate using their email and password:2. Credential Validation
The authentication service performs the following checks:- User Lookup - Find employee by email
- Status Check - Verify employee status is
active - Password Verification - Compare bcrypt hash
- Token Generation - Create JWT with claims
/home/daytona/workspace/source/services/auth_service.go:42-62
3. Login Response
Successful authentication returns a JWT token along with user and company details:JWT Token Structure
Token Claims
Each JWT contains the following claims:/home/daytona/workspace/source/utils/utils.go:80-88
The
CompanyID claim is critical for multi-tenant isolation. See Multi-Tenancy for details.Token Generation
Tokens are signed using HMAC-SHA256 with a secret key:/home/daytona/workspace/source/utils/utils.go:100-113
- Algorithm: HS256 (HMAC with SHA-256)
- Expiry: 24 hours from issuance
- Secret: Environment variable
JWT_SECRET
Using Tokens
Making Authenticated Requests
Include the JWT token in theAuthorization header using the Bearer scheme:
Token Validation
API handlers extract and validate tokens from the Authorization header:/home/daytona/workspace/source/handlers/employee_handler.go:311-334
Validation Steps
- Parse Token - Extract and decode JWT
- Verify Signature - Validate HMAC signature with secret
- Check Expiry - Ensure token hasn’t expired
- Extract Claims - Return AuthClaims for authorization
/home/daytona/workspace/source/utils/utils.go:134-157
Token Refresh
The API supports refreshing tokens before they expire:/home/daytona/workspace/source/utils/utils.go:160-175
Password Security
Passwords are hashed using bcrypt before storage:/home/daytona/workspace/source/utils/utils.go:68-78
- Bcrypt - Adaptive hashing function resistant to brute-force
- Default Cost - Cost factor 10 (2^10 = 1,024 iterations)
- Salted Hashes - Unique salt per password prevents rainbow table attacks
Error Handling
Authentication can fail for several reasons:Best Practices
Secure Token Storage
Store tokens securely on the client side (e.g., httpOnly cookies or secure storage). Never expose tokens in URLs.
Use HTTPS
Always transmit tokens over HTTPS to prevent interception. Never send tokens over unencrypted connections.
Short Expiry Times
The 24-hour expiry balances security and user experience. Consider shorter expiry for sensitive operations.
Rotate Secrets
Periodically rotate the
JWT_SECRET and invalidate old tokens to maintain security.Related Concepts
Authorization
Learn about role-based access control
Multi-Tenancy
Understand how tokens enforce tenant isolation