Kotoba Security
Comprehensive authentication and authorization system for the Kotoba graph database. Implements enterprise-grade security with JWT, OAuth2, MFA, and capability-based access control.
๐ฏ Overview
Kotoba Security serves as the complete security foundation for the Kotoba ecosystem, providing:
- Multi-Protocol Authentication: JWT, OAuth2/OpenID Connect, and local authentication
- Advanced Authorization: Capability-based access control with fine-grained permissions
- Multi-Factor Authentication: TOTP-based MFA with modern security standards
- Cryptographic Security: Secure password hashing and token management
- Session Management: Stateless session handling with security best practices
๐๏ธ Architecture
Security Service Architecture
SecurityService - Main Coordinator
// Unified security service combining all components
JWT Service (jwt.rs)
// Standards-compliant JWT token management
OAuth2 Service (oauth2.rs)
// Full OAuth2/OpenID Connect implementation
Capability System (capabilities.rs)
// Fine-grained, object-capability security
;
๐ Quality Metrics
| Metric | Status |
|---|---|
| Compilation | โ Clean (with external dependencies) |
| Tests | โ Comprehensive security test suite |
| Documentation | โ Complete API docs |
| Security | โ Cryptographic best practices |
| Standards | โ JWT, OAuth2, TOTP compliance |
| Performance | โ Optimized for low-latency auth |
๐ง Usage
Complete Security Setup
use ;
use ;
// Configure security components
let security_config = SecurityConfig ;
// Initialize security service
let security = new.await?;
JWT Authentication Flow
// Generate tokens for authenticated user
let token_pair = security.generate_tokens?;
// Validate incoming requests
let claims = security.validate_token?;
println!;
// Refresh expired access tokens
let new_tokens = security.refresh_token?;
OAuth2 Integration
// Start OAuth2 flow
let auth_url = security.start_oauth2_flow?;
// Redirect user to auth_url...
// Complete OAuth2 flow with callback
let auth_result = security.complete_oauth2_flow.await?;
MFA Setup and Verification
// Setup MFA for user
let = security.setup_mfa?;
// Display QR code to user for authenticator app setup...
// Verify MFA codes
let is_valid = security.verify_mfa?;
if is_valid
Capability-Based Authorization
use ;
// Create principal with capabilities
let principal = security.create_principal_with_capabilities;
// Check authorization for resource access
let resource = security.create_resource;
let auth_result = security.check_authorization;
assert!;
Password Security
// Hash passwords securely
let password_hash = security.hash_password?;
// Verify passwords
let is_valid = security.verify_password?;
assert!;
๐ Ecosystem Integration
Kotoba Security is the security foundation for:
| Crate | Purpose | Integration |
|---|---|---|
kotoba-server |
Required | HTTP middleware and API auth |
kotoba-execution |
Required | Query authorization |
kotoba-storage |
Optional | Data access control |
kotoba-graph |
Optional | Graph operation permissions |
๐งช Testing
Test Coverage:
- โ JWT token generation, validation, and refresh
- โ OAuth2 flow initiation and completion
- โ MFA secret generation and TOTP verification
- โ Password hashing and verification
- โ Capability-based authorization
- โ Session management operations
- โ Security configuration validation
- โ Error handling and edge cases
๐ Performance
- Fast Token Operations: Optimized JWT signing/verification
- Efficient Authorization: O(1) capability checks
- Low-Latency MFA: Optimized TOTP verification
- Scalable Sessions: Stateless session management
- Memory Safe: Zero-copy operations where possible
๐ Security
- Cryptographic Standards: JWT with industry-standard algorithms
- Secure Passwords: Argon2/PBKDF2/bcrypt with salt
- OAuth2 Compliance: Full RFC 6749 implementation
- MFA Standards: TOTP per RFC 6238
- Capability Security: Object-capability model prevents privilege escalation
- Audit Trail: Comprehensive security event logging
- TLS Ready: HTTPS enforcement and secure cookie handling
๐ API Reference
Core Security Types
- [
SecurityService] - Main security service coordinator - [
User] - User identity and profile information - [
Principal] - Security principal for authorization - [
Resource] - Protected resource definition - [
Capability] - Fine-grained permission unit - [
AuthResult] / [AuthzResult] - Authentication/authorization results
Security Services
- [
JwtService] - JWT token management - [
OAuth2Service] - OAuth2/OpenID Connect integration - [
MfaService] - Multi-factor authentication - [
PasswordService] - Secure password handling - [
SessionManager] - Session lifecycle management - [
CapabilityService] - Capability-based authorization
Configuration
- [
SecurityConfig] - Main security configuration - [
JwtConfig] - JWT-specific settings - [
OAuth2Config] - OAuth2 provider configuration - [
SessionConfig] - Session management settings
๐ค Contributing
See the main Kotoba repository for contribution guidelines.
๐ License
Licensed under MIT OR Apache-2.0. See LICENSE for details.