83f94b1f09
Add complete documentation structure: - Frontend architecture documentation - Database schema documentation - Deployment guides (Docker, Traefik, Authentik, Environment) - Development guides (Setup, Testing, Contributing, Quality Gates) - Deployment architecture documentation - Updated docs README with complete navigation All new features and APIs are now documented. Quality gates: docs only, no code changes
5.3 KiB
5.3 KiB
Authentik OAuth Configuration
Overview
Headquarter uses Authentik as its OAuth2 provider for authentication. This guide covers setting up Authentik and configuring Headquarter to work with it.
Prerequisites
- Running Authentik instance
- Admin access to Authentik
- Headquarter deployed and accessible
Authentik Setup
Step 1: Create Application
- Log in to Authentik Admin interface
- Navigate to Applications → Applications
- Click Create
- Fill in:
- Name: Headquarter
- Slug:
headquarter-web(or your preferred slug) - Provider: Create new
Step 2: Create OAuth Provider
-
In the provider creation form:
- Name: Headquarter OAuth
- Authentication flow:
default-authentication-flow - Authorization flow:
default-provider-authorization-explicit-consent - Client type: Confidential
- Client ID: Generate or use your own UUID
- Client Secret: Generate strong secret
- Redirect URIs:
https://api.yourdomain.com/auth/callback
-
Advanced protocol settings:
- Signing Key: Select a signing key (required)
- Access Token validity: Minutes (default: 5)
- Refresh Token validity: Days (default: 30)
-
Save provider
Step 3: Configure Application
- Return to Application configuration
- Select the created provider
- Save application
Step 4: Verify URLs
Note these URLs from your Authentik instance:
- Authorize URL:
https://auth.yourdomain.com/application/o/authorize/ - Token URL:
https://auth.yourdomain.com/application/o/token/ - UserInfo URL:
https://auth.yourdomain.com/application/o/userinfo/ - JWKS URL:
https://auth.yourdomain.com/application/o/headquarter-web/jwks/
Headquarter Configuration
Environment Variables
Add to your .env file:
# Authentik Configuration
AUTHENTIK_DOMAIN=auth.yourdomain.com
AUTHENTIK_CLIENT_ID=your-client-id-uuid
AUTHENTIK_CLIENT_SECRET=your-generated-secret
AUTHENTIK_APPLICATION_SLUG=headquarter-web
AUTHENTIK_AUDIENCE=your-client-id-uuid
# Optional: Override default URLs if needed
# AUTHENTIK_AUTHORIZE_URL=https://auth.yourdomain.com/application/o/authorize/
# AUTHENTIK_TOKEN_URL=https://auth.yourdomain.com/application/o/token/
# AUTHENTIK_JWKS_URL=https://auth.yourdomain.com/application/o/headquarter-web/jwks/
# AUTHENTIK_ISSUER=https://auth.yourdomain.com/application/o/headquarter-web/
Important Notes
- AUTHENTIK_CLIENT_ID: The UUID from Authentik (used for OAuth)
- AUTHENTIK_APPLICATION_SLUG: The URL-friendly name (e.g.,
headquarter-web) - AUTHENTIK_AUDIENCE: Usually same as Client ID
User Synchronization
On first login, Headquarter creates a local user record:
user = User(
email="user@example.com",
name="User Name",
authentik_id="authentik-user-id",
)
Synced Fields
| Authentik Field | Headquarter Field |
|---|---|
| name | name |
| sub (user ID) | authentik_id |
| groups | (future: team membership) |
Troubleshooting
Redirect URI Error
Problem: "Redirect URI Error" from Authentik
Solution:
- Check redirect URI in Authentik matches exactly
- Must include protocol:
https://api.yourdomain.com/auth/callback - No trailing slash difference
Invalid Client
Problem: "invalid_client" error
Solution:
- Verify Client ID matches
- Verify Client Secret is correct
- Check application slug in URLs
Missing Refresh Token
Problem: Authentik doesn't return refresh token
Solution: This is normal. Headquarter creates its own session cookies and doesn't need refresh tokens from Authentik.
CORS Errors
Problem: CORS errors in browser
Solution:
- Ensure API domain is in CORS origins
- Check
WEB_BASE_URLenvironment variable - Verify cookies have correct domain
Security Best Practices
- Use HTTPS - Never use HTTP in production
- Strong Client Secret - Use generated secret, don't reuse
- Short Token Lifetime - Keep access tokens short-lived
- Validate State - Always verify state parameter
- Secure Cookies - Use httpOnly, Secure, SameSite
Advanced Configuration
Custom Claims
To add custom claims to the token:
- In Authentik, go to Customization → Property Mappings
- Create new Scope Mapping
- Add custom attributes
- Assign to provider
Group Mapping
For team/organization support:
- Configure group property mapping in Authentik
- Headquarter will sync groups on login
- Use groups for authorization
Multiple Applications
If running multiple environments:
- Create separate applications in Authentik
- Use different client IDs
- Configure environment-specific redirect URIs
Testing
Manual Test
- Visit
https://app.yourdomain.com - Click "Login"
- Should redirect to Authentik
- Login with Authentik credentials
- Should redirect back to app, logged in
API Test
# Check auth endpoint
curl https://api.yourdomain.com/auth/me
# Should return 401 (not authenticated)
# After login, should return user data
curl https://api.yourdomain.com/auth/me --cookie "session=..."