torii-core
Core functionality for the Torii authentication framework.
This crate provides the foundational types, traits, and services that power the Torii authentication system. It defines the core abstractions for users, sessions, and authentication methods while providing a flexible service architecture that can be extended with different storage backends.
Features
- User Management: Core user types and management services
- Session Management: Flexible session handling with both opaque and JWT tokens
- Service Architecture: Modular services for different authentication methods
- Storage Abstraction: Database-agnostic storage traits and repository patterns
- Type Safety: Strongly typed IDs and newtype patterns for security
- Async/Await: Fully async operations with tokio support
- Error Handling: Comprehensive error types with structured error handling
Core Types
Users
Users are the foundation of the authentication system. The User struct includes:
| Field | Type | Description |
|---|---|---|
id |
UserId |
The unique identifier for the user |
name |
Option<String> |
The display name of the user |
email |
String |
The email address of the user |
email_verified_at |
Option<DateTime> |
The timestamp when the user's email was verified |
created_at |
DateTime |
The timestamp when the user was created |
updated_at |
DateTime |
The timestamp when the user was last updated |
Sessions
Sessions track user authentication state and can be implemented as either opaque tokens or JWTs:
| Field | Type | Description |
|---|---|---|
token |
SessionToken |
The session token (opaque or JWT) |
user_id |
UserId |
The unique identifier for the user |
user_agent |
Option<String> |
The user agent of the client that created the session |
ip_address |
Option<String> |
The IP address of the client that created the session |
created_at |
DateTime |
The timestamp when the session was created |
updated_at |
DateTime |
The timestamp when the session was last updated |
expires_at |
DateTime |
The timestamp when the session expires |
Service Architecture
Torii uses a service-oriented architecture with the following core services:
UserService
Handles user account management:
- User creation and updates
- Email verification
- User deletion
- User retrieval by ID or email
SessionService
Manages user sessions:
- Session creation with configurable expiration
- Session validation and retrieval
- Session deletion and cleanup
- Multi-device session management
Authentication Services
Specialized services for different authentication methods:
PasswordService- Password-based authenticationOAuthService- OAuth/OpenID Connect integrationPasskeyService- WebAuthn/FIDO2 passkey authenticationMagicLinkService- Passwordless magic link authentication
Storage Abstraction
The crate defines storage traits that can be implemented by different backends:
UserStorage
SessionStorage
Repository Provider
The RepositoryProvider trait allows storage backends to provide all necessary repositories:
Session Providers
Torii supports two session token types:
Opaque Sessions
Traditional session tokens stored in the database:
- Random token generation
- Server-side session validation
- Immediate revocation capability
- Requires database lookup for validation
JWT Sessions
Self-contained JSON Web Tokens:
- Stateless authentication
- Configurable signing algorithms (HS256, HS384, HS512, RS256, etc.)
- Custom claims support
- No database lookup required for validation
Type Safety
The crate uses newtype patterns for enhanced type safety:
// Strongly typed IDs prevent mixing different ID types
;
;
// Builder patterns for safe construction
let user = builder
.id
.email
.build?;
Error Handling
Comprehensive error handling with structured error types:
Usage
This crate is typically used indirectly through the main torii crate, but can be used directly for custom implementations:
use ;
// Create services with your storage backend
let user_service = new;
let session_service = new;
// Use the services
let user = user_service.create_user.await?;
let session = session_service.create_session.await?;
Integration
Storage backends like torii-storage-sqlite, torii-storage-postgres, and torii-storage-seaorm implement the traits defined in this crate to provide concrete storage implementations.