Skip to main content

Module api_key

Module api_key 

Source
Expand description

API key authentication for machine-to-machine access.

Provides key generation (prefixed, hashed), a provider trait for storage-agnostic verification, and middleware for route protection.

§Key Format

Keys follow the fe_{env}_{random} pattern (similar to Stripe):

  • fe_live_ prefix for production keys
  • fe_test_ prefix for test/development keys
  • 43 random base62 characters for the secret portion

§Example

use ferro_rs::{ApiKeyMiddleware, generate_api_key};

// Generate a new key (show raw_key once, store prefix + hash)
let key = generate_api_key("live");
println!("API Key: {}", key.raw_key); // show once
// Store key.prefix and key.hashed_key in database

// Protect routes with middleware
group!("/api/v1")
    .middleware(ApiKeyMiddleware::new())
    .routes([...]);

// Require specific scopes
group!("/api/v1/admin")
    .middleware(ApiKeyMiddleware::scopes(&["admin"]))
    .routes([...]);

Structs§

ApiKeyInfo
Information about an authenticated API key, available in request extensions.
ApiKeyMiddleware
Middleware that authenticates requests via API key in the Authorization header.
GeneratedApiKey
Result of generating a new API key.

Traits§

ApiKeyProvider
Storage-agnostic API key verification.

Functions§

generate_api_key
Generate a new API key for the given environment.
hash_api_key
Compute the SHA-256 hex digest of a raw API key.
verify_api_key_hash
Constant-time comparison of a raw key against a stored hash.