Expand description
Product-neutral local session authentication mechanics.
aster_forge_auth owns JWT token contracts, validation policy, token-pair issuance, and the
refresh-rotation coordinator. Product repositories own account state, database transactions,
session tables, cookies, permissions, audit events, and error presentation. Persistence is
accessed through RefreshSessionStore, so this crate does not depend on SeaORM or a product
schema.
Select exactly one Cargo backend feature: aws-lc-rs (default) or rust-crypto.
The product entrypoint must call initialize_jwt_crypto_provider before any JWT use,
including direct jsonwebtoken calls. This installs a process-wide provider, not an
instance-specific backend. Signing algorithms remain a runtime JwtPolicy choice.
use aster_forge_auth::{AuthTokenType, JwtKey, JwtPolicy, JwtSigner, JwtVerifier,
Restriction, SessionId, SessionVersion, Subject};
aster_forge_auth::initialize_jwt_crypto_provider()?;
let key = JwtKey::hmac([42; 32])?; // Product loads a dedicated random signing key.
let policy = JwtPolicy::new("product", "local-web", 900, 604_800)?;
let signer = JwtSigner::with_system_clock(key.clone(), policy.clone());
let verifier = JwtVerifier::with_system_clock(key, policy);
let pair = signer.issue(Subject::new("42")?, SessionId::new("session-1")?,
SessionVersion::new(1), Restriction::None)?;
let verified = verifier.verify(&pair.access_token, AuthTokenType::Access)?;
assert_eq!(verified.claims().sub.as_str(), "42");
// Product persists the session before publishing pair, and checks current authorization.Structs§
- Auth
Token Pair - Candidate credentials; commit their refresh identity before releasing token strings.
- Client
Context - Untrusted client metadata used only for stale-retry classification, never device authentication.
- Fixed
Clock - Fixed clock for deterministic tests and product integration tests.
- JwtKey
- HMAC key wrapper. Products own entropy, loading, rotation, and isolation from AEAD keys.
- JwtPolicy
- Issuance lifetimes and strict namespace. Namespace/session checks cannot be disabled. Old JWT formats require reauthentication rather than an implicit fallback parser.
- JwtSigner
- Product-configured signer with injectable clock and ID source.
- JwtVerifier
- Strict verifier: only signature-verified claims cross the validation boundary.
- Password
Hash Runtime - Instance-owned blocking executor with a memory/CPU concurrency budget.
- Random
IdSource - Production ID source using the upstream UUID v4 implementation.
- Refresh
Coordinator - Instance resource shared by all refresh requests. No global singleton or historical-JTI leak. The finite budget includes running requests and same-JTI waiters; saturation rejects promptly. It complements, never replaces, the product’s authoritative cross-instance database CAS.
- Refresh
Jti - Single-use refresh identity. Debug output is always redacted.
- Refresh
Rotation Request - Candidate mutation submitted to the product’s writer transaction. It contains IDs and expiry,
never token strings. Stores must recheck current account state, version, restriction and session
ownership, then use
classify_refreshbefore conditional rotation or atomic reuse revocation. - Refresh
Session - Authoritative product session snapshot, read under the transaction used for the decision.
- Refresh
Token Service - Refresh workflow combining strict JWT checks, a shared instance coordinator, and product CAS. The coordinator can be reused while products rebuild signers for dynamic lifetime configuration.
- Session
Id - Server-side session identity, distinct from its single-use refresh JTI.
- Session
Version - Account-wide generation; revocation must fail on overflow rather than reuse a version.
- Subject
- Stable product subject; integer IDs and UUIDs use their string representation.
- System
Clock - Production clock backed by
SystemTime. - Token
Claims - Required local web-session claims. No product entities or permissions are embedded. Claims are signed, not encrypted, and must never carry secrets.
- Verified
Token - Signature/policy-verified claims. Private construction prevents labeling unchecked JSON as verified. Current account, session, and permissions still require product checks.
Enums§
- Auth
Error - Errors produced by the product-neutral authentication mechanism.
- Auth
Token Type - Local session credential purpose; OAuth access and OIDC ID tokens use other contracts.
- JwtAlgorithm
- Supported HMAC algorithms; headers cannot select a different key family.
- Password
Work Error - Redacted async password-work errors. Products map these at their own boundary.
- Refresh
Decision - Pure state-machine decision; products apply its effects atomically in their own transaction.
- Refresh
Error - Refresh failure separates mechanism failures from the product’s unmodified persistence error.
- Refresh
Outcome - Post-commit result. Only the successful branch contains usable replacement credentials.
- Refresh
Policy - Explicit replay policy. Both variants reject old credentials; neither grants a second refresh.
- Refresh
Rejection - Authentication rejection, distinct from persistence/commit failure and never containing tokens.
- Refresh
Store Outcome - Transaction result. A store must not return success or reuse until all required writes commit.
- Restriction
- Authentication restriction. Products decide which operations a restricted session may perform.
Constants§
- DEFAULT_
PASSWORD_ HASH_ MAX_ CONCURRENCY - Default number of concurrent memory-hard password jobs.
Traits§
- Clock
- Time source used by token issuance and validation.
- IdSource
- Supplies unpredictable refresh IDs in production and deterministic IDs in tests.
- Refresh
Session Store - Product persistence boundary. Forge never opens a database transaction or maps product errors.
Functions§
- classify_
refresh - Classifies authoritative session state without accessing product entities or performing effects.
- initialize_
jwt_ crypto_ provider - Installs the backend selected by this crate’s Cargo features for the entire process.
Type Aliases§
- Result
- Result returned by authentication primitives.