π Authentication & Authorization Overview
This document explains how user authentication and access control work within the application.
It covers admin behavior, user tokens, and partition-level permissions.
1. Authentication Activation
Section titled β1. Authentication ActivationβAUTH_TOKEN
Section titled βAUTH_TOKENβ- The presence of the environment variable
AUTH_TOKENactivates authentication. - If
AUTH_TOKENis absent, the middleware bypasses all authentication checks, allowing open access (useful for local or testing environments).
2. Admin Bootstrapping
Section titled β2. Admin BootstrappingβWhen AUTH_TOKEN is set:
- On startup, the application checks whether an admin user already exists in the database.
- If not, it creates one automatically:
display_name:"Admin"is_admin:Truetoken: SHA-256 hash of theAUTH_TOKENvalue
This admin user serves as the global entry point for bootstrapping the system.
3. Token Management
Section titled β3. Token ManagementβGeneration
Section titled βGenerationβ- Each new user is assigned a token at creation time (format:
or-<random hex>). - The app returns the raw token to the API caller once (e.g.,
POST /usersresponse).
Storage
Section titled βStorageβ- Only a SHA-256 hash of the token is stored in PostgreSQL.
- The raw token is never persisted, ensuring that leaked database contents cannot reveal user credentials.
Validation
Section titled βValidationβ- When an API request includes an
Authorization: Bearer <token>header:- The middleware extracts the token.
- The hash of this token is computed.
- The hash is compared against the stored value in the
userstable.
4. User Roles
Section titled β4. User Rolesβπ Admin
Section titled βπ Adminβ- Full access to all API routes, including:
- User management
- Actor management
- Queue and system information
- Can also create other users and assign privileges.
- Admins can use the app as regular users (own partitions, files, etc.).
- By default, an admin cannot view other usersβ data.
π§ Super Admin Mode
Section titled βπ§ Super Admin Modeβ- Controlled by the environment variable
SUPER_ADMIN_MODE. - When
SUPER_ADMIN_MODE=true:- The admin can access all partitions and data across users.
- Partition-level access restrictions are ignored.
- When
SUPER_ADMIN_MODE=false:- Admin privileges are limited to admin-only operations (user creation, actor management, etc.).
- Data-level access (partitions/files) requires using a normal user account.
5. Regular Users
Section titled β5. Regular Usersβ- Created by an admin via the
/usersendpoint. - Receive a personal API token (returned once upon creation).
- Can authenticate using
Authorization: Bearer <token>.
Users can:
- Create and manage their own partitions and files.
- Access shared partitions based on assigned roles.
6. Partition Access Roles
Section titled β6. Partition Access RolesβAccess control is handled through the partition_memberships table.
Each userβpartition relationship defines a role:
| Role | Description | Capabilities |
|---|---|---|
| owner | Partition creator or owner | Full access β can delete the partition, manage members, edit files, etc. |
| editor | Collaborator | Can read and write files within the partition |
| viewer | Read-only member | Can view content and perform semantic search or chat but not modify data |
Role-based restrictions are enforced via dependency guards:
require_partition_ownerrequire_partition_editorrequire_partition_viewer
7. Authorization Flow Summary
Section titled β7. Authorization Flow Summaryβ- Request arrives with optional
Authorization: Bearer <token>. - If
AUTH_TOKENis unset, authentication is skipped (open mode). - If set:
- Middleware hashes the token.
- Looks up the user by hash.
- Loads their partition memberships.
- User info and memberships are attached to
request.state. - Role-based dependencies ensure the user has proper privileges before executing the endpoint logic.
8. Summary Diagram
Section titled β8. Summary Diagramβββββββββββββββββββββββββββ Incoming Request ββ Authorization: Bearer βββββββββββββββ¬βββββββββββ β βΌββββββββββββββββββββββββββββββ AuthMiddleware ββ - Hash token (SHA-256) ββ - Lookup user in DB ββ - Load memberships ββ - Attach to request.state βββββββββββββββ¬βββββββββββββββ β βΌβββββββββββββββββββββββββββββββββ Endpoint Dependency Checks ββ (e.g., require_partition_*) βββββββββββββββ¬ββββββββββββββββββ β βΌβββββββββββββββββββββββββββββββββ Route Logic Executes ββ with validated user context βββββββββββββββββββββββββββββββββ9. Security Highlights
Section titled β9. Security Highlightsβ- No plaintext tokens stored in database.
- SHA-256 hashing for authentication.
- Partition-based role hierarchy for fine-grained access control.
- Admin privileges separated from regular user data access.
- Configurable
SUPER_ADMIN_MODEfor system-wide debugging or admin override.