iron_token_manager
User management, API token management, authentication, and rate limiting.
Installation
[]
= { = "../iron_token_manager" }
Token Types
This module manages API tokens for Control Panel authentication.
IC Token (Internal Control Token):
- Purpose: Link agent to budget allocation (Model C architecture)
- Visibility: Developer-visible (JWT format)
- See: docs/protocol/005_budget_control_protocol.md § IC Token Format
API Token:
- Purpose: Authenticate Control Panel REST API requests
- Visibility: Developer-visible (for API access)
- Format: Opaque Base64 string (SHA-256 hashed in storage)
- Lifetime: Long-lived (manually revoked)
Distinction:
- IC Token: For Runtime (agent execution, budget-linked)
- API Token: For Control Panel API (CRUD operations)
Quick Start
User Management
use UserService;
// Create user service with SQLite storage
let service = new?;
// Create a new user account
let user = service.create_user?;
// Suspend user account
service.suspend_user?;
// Change user role
service.change_user_role?;
// Get audit log for user
let audit_entries = service.get_user_audit_log?;
Token Management
use ;
// Create token manager with SQLite storage
let manager = new?;
// Generate new API token
let token = manager.create_token?;
// Verify and track usage
if manager.verify?
// Rate limiting
let limiter = new; // 100 req/sec
if limiter.check?
Quick Start: Fresh Environment
Get a clean, validated development environment in one command:
# Full workflow (reset + seed + validate)
# With full test suite
Database Path Standards
Canonical Development Path: ./iron.db
All development uses this single path:
- Scripts default to
./iron.db - Config:
sqlite:///./iron.db?mode=rwc - Tests use in-memory databases (
sqlite::memory:)
Validation System:
# Run all validators
# Individual validators
Enforcement:
-
Pre-commit hook - Blocks commits with path violations
-
CI/CD validation - GitHub Actions on every PR
-
Makefile integration - Commands include validation
Quick Reference:
Test Tokens for Manual Testing:
Admin: iron_dev_admin_token_001
Developer: iron_dev_pm_token_002
Viewer: iron_dev_viewer_token_003
Detailed Documentation:
- Database Path Standards - Complete guide
- Database Initialization - Schema and migrations
- Configuration - Config file reference
Test Database Infrastructure
This crate uses iron_test_db for standardized test database management with automatic cleanup and seed data support.
Key Features:
- RAII cleanup (no manual TempDir management)
- Shared pool across components (no
/tmpworkarounds) - Automatic migrations
- Seed data population for realistic testing
- In-memory storage for speed
Using v2 Test Helpers
The tests/common/mod.rs module provides v2 helpers using iron_test_db:
use crate;
async
async
async
Seed Data Reference
See tests/fixtures/seed_data_reference.md for complete documentation of seeded entities and their properties.
The seed data validation tests in tests/seed_data_validation.rs ensure the documentation stays in sync with actual seed data.
Migration from v1 to v2 Helpers
Old v1 approach:
let = create_test_db.await;
// Manual pool and TempDir management
New v2 approach:
let db = create_test_db_v2.await;
// Automatic cleanup, cleaner API
Benefits:
- No manual TempDir tracking
- Shared pool across multiple components
- Consistent RAII cleanup pattern
- Better ergonomics
Both approaches are currently supported for backward compatibility.
Responsibilities: Manages user accounts with RBAC (admin/user/viewer roles) and comprehensive audit logging. Handles API token lifecycle with secure generation, SHA-256 hashing, and SQLite storage. Provides JWT authentication, usage tracking, quota enforcement, and token bucket rate limiting for API access control.
In Scope:
- User Management: Account lifecycle (create, suspend, activate, delete with soft delete)
- RBAC: Three roles (admin, user, viewer) with permission-based access control
- Audit Logging: Append-only user_audit_log with immutability guarantees
- Password Security: BCrypt hashing (cost 12), secure password reset
- Cryptographic token generation (Base64, high-entropy)
- SHA-256 token hashing (never store plaintext)
- Token CRUD operations (create, verify, revoke, list)
- Token expiration and deactivation
- Usage tracking per token (requests, tokens, cost)
- Quota enforcement (daily limits, cost caps)
- Token bucket rate limiting (requests per second)
- JWT authentication and validation
- SQLite persistence with proper constraints
Out of Scope:
- OAuth2/OIDC integration (future)
- API key rotation automation (future)
- Multi-tenant token isolation (future)
- Token analytics and reporting (future)
- REST API endpoints (see iron_control_api)
- Dashboard UI (see iron_dashboard)
- Cost calculation (see iron_cost)
- Budget tracking (see iron_cost)
Source Files
| File | Responsibility |
|---|---|
| lib.rs | Token management for LLM API access control |
| agent_budget.rs | Agent Budget Manager |
| budget_request.rs | Budget Request Storage Layer |
| config.rs | Configuration management for token manager |
| cost_calculator.rs | Cost calculation service |
| error.rs | Error types |
| lease_manager.rs | Budget Lease Manager |
| limit_enforcer.rs | Limit enforcement service |
| migrations.rs | Database migration utilities |
| provider_adapter.rs | LLM provider adapter layer |
| provider_key_storage.rs | AI Provider Key storage layer |
| rate_limiter.rs | Rate limiting service |
| seed.rs | Database seeding utilities for development and testing |
| storage.rs | Database storage layer |
| token_generator.rs | Token generation service |
| trace_storage.rs | Trace storage service |
| usage_tracker.rs | Usage tracking service |
| user_service.rs | User management service |
Notes:
- Entries marked 'TBD' require manual documentation
- Entries marked '⚠️ ANTI-PATTERN' should be renamed to specific responsibilities
License
Apache-2.0