MoosicBox Authentication
Basic authentication utilities for client registration and token management in the MoosicBox ecosystem.
Overview
The MoosicBox Auth package provides:
- Client Registration: Register clients and manage access tokens
- Signature Token Fetching: Retrieve signature tokens for secure operations
- Request Authorization: Basic request authorization middleware
- Database Integration: Store and retrieve client credentials
- API Endpoints: Optional REST API for magic token management (requires
apifeature)
Features
Core Authentication Functions
- Client ID Generation: Generate unique client identifiers as UUIDs
- Token Management: Store and retrieve client access tokens from database
- Signature Token Access: Fetch signature tokens for secure operations
Request Authorization
- Non-Tunnel Authorization: Validate that requests are not from tunnel services
- Header-Based Auth: Check user agent headers for authorization
API Endpoints (requires api feature, enabled by default)
- GET /magic-token: Retrieve credentials associated with a magic token
- POST /magic-token: Create a new magic token for authentication flows
- Magic tokens expire after 1 day and are single-use (deleted after retrieval)
Installation
Add this to your Cargo.toml:
[]
= "0.1.4"
Available Features
api(default): Enables API endpoints for magic token managementopenapi(default): Enables OpenAPI/utoipa documentation supportfail-on-warnings: Enables strict compilation warnings
To use without API endpoints:
[]
= { = "0.1.4", = false }
Usage
Client Registration and Token Management
use ;
use ConfigDatabase;
async
Using API Endpoints
The package provides REST API endpoints when the api feature is enabled (default). To use these endpoints in your Actix Web application:
use ;
use bind_services;
async
This enables:
GET /auth/magic-token?magicToken=<token>- Retrieve credentials for a magic tokenPOST /auth/magic-token?host=<host>- Create a new magic token
Signature Token Retrieval
use fetch_signature_token;
async
Request Authorization Middleware
use NonTunnelRequestAuthorized;
use ;
// Handler that requires non-tunnel authorization
async
// The middleware automatically checks the User-Agent header
// and rejects requests from "MOOSICBOX_TUNNEL"
Database Schema
The package uses the following database tables:
client_access_tokens
client_id: String - The unique client identifiertoken: String - The access token for the clientexpires: Optional timestamp - Token expiration timeupdated: Timestamp - Last update time
magic_tokens (requires api feature)
magic_token: String - The magic token UUIDclient_id: String - Associated client identifieraccess_token: String - Associated access tokenexpires: Timestamp - Expiration time (1 day from creation)
HTTP Integration
- Actix Web request extractor for authorization
- User-Agent based request filtering
- Header validation and parsing
Error Handling
The package provides comprehensive error handling through the AuthError enum:
DatabaseFetch: Database operation errorsParse: JSON parsing errorsHttp: HTTP request errorsRegisterClient: Client registration failuresUnauthorized: Authorization failures
Environment Variables
TUNNEL_ACCESS_TOKEN: Required for client registration with tunnel services
Security Considerations
- Client IDs are generated as UUIDs for uniqueness
- Magic tokens are UUIDs for temporary authentication (when using API endpoints)
- Magic tokens expire after 1 day and are single-use (deleted upon retrieval)
- Access tokens are managed securely in the database
- Request filtering prevents tunnel service abuse via User-Agent checking
- The
NonTunnelRequestAuthorizedextractor blocks requests with User-Agent "MOOSICBOX_TUNNEL"