Skip to main content

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:
  1. User Lookup - Find employee by email
  2. Status Check - Verify employee status is active
  3. Password Verification - Compare bcrypt hash
  4. 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
Token Properties:
  • Algorithm: HS256 (HMAC with SHA-256)
  • Expiry: 24 hours from issuance
  • Secret: Environment variable JWT_SECRET
The JWT_SECRET environment variable must be set and should be a strong, random string (minimum 32 characters).

Using Tokens

Making Authenticated Requests

Include the JWT token in the Authorization 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

  1. Parse Token - Extract and decode JWT
  2. Verify Signature - Validate HMAC signature with secret
  3. Check Expiry - Ensure token hasn’t expired
  4. 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
Refresh tokens proactively before expiry to avoid forcing users to re-authenticate.

Password Security

Passwords are hashed using bcrypt before storage:
/home/daytona/workspace/source/utils/utils.go:68-78
Security Features:
  • 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.

Authorization

Learn about role-based access control

Multi-Tenancy

Understand how tokens enforce tenant isolation