| Crates.io | moosicbox_auth |
| lib.rs | moosicbox_auth |
| version | 0.1.4 |
| created_at | 2024-10-04 15:29:51.867857+00 |
| updated_at | 2025-07-21 19:24:42.89041+00 |
| description | MoosicBox authentication package |
| homepage | |
| repository | https://github.com/MoosicBox/MoosicBox |
| max_upload_size | |
| id | 1396707 |
| size | 80,760 |
Basic authentication utilities for client registration and token management in the MoosicBox ecosystem.
The MoosicBox Auth package provides:
Add this to your Cargo.toml:
[dependencies]
moosicbox_auth = "0.1.1"
use moosicbox_auth::{get_client_id_and_access_token, AuthError};
use switchy_database::config::ConfigDatabase;
#[tokio::main]
async fn main() -> Result<(), AuthError> {
let db = ConfigDatabase::new().await?;
let host = "https://api.example.com";
// Get or create client credentials
let (client_id, access_token) = get_client_id_and_access_token(&db, host).await?;
println!("Client ID: {}", client_id);
println!("Access Token: {}", access_token);
Ok(())
}
use moosicbox_auth::create_magic_token;
use switchy_database::config::ConfigDatabase;
async fn magic_token_example() -> Result<(), Box<dyn std::error::Error>> {
let db = ConfigDatabase::new().await?;
let tunnel_host = Some("https://tunnel.example.com".to_string());
// Create a magic token for authentication flow
let magic_token = create_magic_token(&db, tunnel_host).await?;
println!("Magic token: {}", magic_token);
Ok(())
}
use moosicbox_auth::fetch_signature_token;
async fn get_signature_token() -> Result<(), Box<dyn std::error::Error>> {
let host = "https://api.example.com";
let client_id = "your-client-id";
let access_token = "your-access-token";
// Fetch signature token for secure operations
match fetch_signature_token(host, client_id, access_token).await? {
Some(signature_token) => {
println!("Signature token: {}", signature_token);
}
None => {
println!("No signature token available");
}
}
Ok(())
}
use moosicbox_auth::NonTunnelRequestAuthorized;
use actix_web::{web, HttpResponse, Result};
// Handler that requires non-tunnel authorization
async fn protected_handler(_auth: NonTunnelRequestAuthorized) -> Result<HttpResponse> {
Ok(HttpResponse::Ok().json("Access granted"))
}
// The middleware automatically checks the User-Agent header
// and rejects requests from "MOOSICBOX_TUNNEL"
The package provides comprehensive error handling through the AuthError enum:
DatabaseFetch: Database operation errorsParse: JSON parsing errorsHttp: HTTP request errorsRegisterClient: Client registration failuresUnauthorized: Authorization failuresTUNNEL_ACCESS_TOKEN: Required for client registration with tunnel services