Authentication & Authorization
Last Updated: 2025-01-16
Primary Source: docs/project_status.md, docs/USER_LEVELS_AND_PERMISSIONS.md
Service File: backend/src/services/auth.service.ts
Middleware: backend/src/middleware/auth.middleware.ts, backend/src/middleware/hybridAuth.middleware.ts
Overview
The Authentication & Authorization system manages user identity, access control, and permissions across the ASO platform. It supports multiple authentication methods (JWT, API keys) and implements role-based access control.
Architecture
Authentication Flow
Authentication Methods
1. JWT Authentication
Purpose: Standard authentication for web portal and native clients.
Process:
- User logs in with email/password
- Backend validates credentials
- Generates JWT access token + refresh token
- Client stores tokens
- Client sends token in
Authorization: Bearer <token>header
Token Payload:
{
id: number; // User ID
role: string; // carbon, silicon, cso
name: string; // User name
accountType: string; // core_yexian, developer, npc, public
}Service: backend/src/services/auth.service.ts
Functions:
getTokenForClient(email, password, clientType)- Direct token request for native clientslogin(email, password)- Standard loginrefreshToken(token)- Refresh access token
2. API Key Authentication
Purpose: Server-to-server authentication (e.g., BoloboloMi backend).
Process:
- External system sends request with
X-API-Keyheader - Backend validates API key
- Grants access to universal state/ingestion endpoints
Middleware: backend/src/middleware/apiKey.middleware.ts
Endpoints:
/api/sync/universal-state- Read ASO state/api/sync/ingest-event- Write timeline events
3. Hybrid Authentication
Purpose: Supports both JWT and API key authentication.
Middleware: backend/src/middleware/hybridAuth.middleware.ts
Process:
- Checks for JWT token first
- Falls back to API key if no JWT
- Grants access based on authentication method
4. Google OAuth
Purpose: Social login for global users (except China).
Process:
- User clicks "Login with Google"
- Redirects to Google OAuth
- Google redirects back with code
- Backend exchanges code for user info
- Creates/updates user account
- Generates JWT tokens
Implementation: backend/src/controllers/auth.controller.ts
User Role System
Two-Dimensional Identity Model
The system uses a two-dimensional identity model:
- Role (
carbon,silicon,cso) - Primary identity classification - Account Type (
core_yexian,developer,npc,public) - Metadata/classification
Role Definitions
Carbon (role: "carbon")
- Identity: Human users (organic beings)
- Default role for new registrations
- Account Types:
public,core_yexian
Silicon (role: "silicon")
- Identity: Internal AI accounts (artificial beings)
- Account Types:
developer,npc
CSO (role: "cso")
- Identity: Carbon-Silicon Organism (Yexian itself)
- Highest level of access
- Meta-observer capabilities
- Account Type: Always
public(public-facing system account)
Access Control
Public Endpoints (No Auth Required)
/api/auth/*- Authentication endpoints- Health checks (if configured)
Authenticated Endpoints (Requires JWT)
/api/user/*- User profile and preferences/api/aso/*- ASO chat and state/api/conversations/*- Conversation management/api/quests/*- Quest system/api/inventory/*- Inventory management (read)/api/tools/*- Tool catalog (read)/api/files/*- File upload and management/api/memories/*- Memory access (own memories)
Admin Endpoints (Requires Admin Access)
Who can access:
- CSO (
role: "cso") - Always - Silicon Developers (
role: "silicon" && accountType: "developer") - Always - Users in
ADMIN_IDSenv variable - Fallback
Endpoints:
/api/admin/*- All admin endpoints/api/admin/health- System health/api/admin/stats- System statistics/api/admin/logs- System logs/api/admin/users- User management/api/admin/prompts/preview- Prompt debugging/api/admin/scheduled-tasks/*- Task management/api/admin/data/*- Data management (NPCs, Quests, Items, etc.)
Permission Matrix
| Feature | Carbon (Public) | Carbon (Core Yexian) | Silicon (Developer) | Silicon (NPC) | CSO |
|---|---|---|---|---|---|
| Chat with ASO | ✅ | ✅ | ✅ | ✅ | ✅ |
| File Upload | ✅ | ✅ | ✅ | ✅ | ✅ |
| Quest System | ✅ | ✅ | ✅ | ✅ | ✅ |
| Inventory | ✅ | ✅ | ✅ | ✅ | ✅ |
| Admin Endpoints | ❌ | ❌ | ✅ | ❌ | ✅ |
| System Statistics | ❌ | ❌ | ✅ | ❌ | ✅ |
| Protocol | PUBLIC | PRIVATE | TECH | NEUTRAL | SYSTEM |
| Observer System | ❌ | ❌ | ❌ | ❌ | ✅ |
Multi-Client Authentication
Client Type Tracking
Service: backend/src/services/clientContext.service.ts
Client Types:
WEB_PORTAL- Web browserDISCORD- Discord botAPI- REST APIUNITY- Unity game (future)MOBILE- Mobile app (future)
Purpose: Track which client is accessing the system for analytics and context.
Universal Login System
Concept
Universal Login enables users to log in once and access the ASO across all platforms (Web, Unity, Mobile, VR/AR) with the same account and synchronized memory.
Implementation
Endpoints:
POST /api/auth/login- Standard loginPOST /api/auth/token- Direct token request (for native clients)GET /api/auth/google- Google OAuth initiationGET /api/auth/google/callback- Google OAuth callback
Features:
- Email/password authentication
- Google OAuth (global, except China)
- JWT token generation
- Refresh token support
- Client type tracking
API Endpoints
Authentication
POST /api/auth/register- Register new userPOST /api/auth/login- Login with email/passwordPOST /api/auth/token- Get token for native clientPOST /api/auth/refresh- Refresh access tokenGET /api/auth/google- Initiate Google OAuthGET /api/auth/google/callback- Google OAuth callbackPOST /api/auth/verify-email- Verify email addressPOST /api/auth/request-password-reset- Request password resetPOST /api/auth/reset-password- Reset password
Source Files
Primary Sources:
backend/src/services/auth.service.ts- Authentication logicbackend/src/middleware/auth.middleware.ts- JWT authenticationbackend/src/middleware/hybridAuth.middleware.ts- Hybrid authbackend/src/middleware/apiKey.middleware.ts- API key authdocs/USER_LEVELS_AND_PERMISSIONS.md- Complete role/permission guide
Related Files:
backend/src/utils/jwt.ts- JWT token generationbackend/src/controllers/auth.controller.ts- Auth endpoints
Related Documentation
- Protocol Routing - Role-based protocol assignment
- 05-INTEGRATIONS/Universal-Login.md - Universal login system details
- 06-ARCHITECTURE/API-Documentation.md - Authentication endpoints
- 00-OVERVIEW.md - Complete system overview