Expand description
§wabot-addon-auth-api-key
API-key credentials and the guard that checks them. Port of
wabot-ts/src/addon/auth/api-key (ApiKey, ApiKeyRepository,
ApiKeyGuardMiddleware).
ⓘ
let keys = Arc::new(InMemoryApiKeyRepository::new());
register_api_key_repository(&c, keys.clone());
register_singletons!(&c, ApiKeyGuard);
// Mint one — the secret is returned exactly once.
let issued = keys.generate("ci-deploy", json!({ "sub": "svc-ci" }), Default::default()).await?;
println!("give this to the caller: {}", issued.secret);Callers then send Authorization: Api-Key sk_…, and the guard fills
the request’s Auth with the key’s stored claims.
§How secrets are stored
Only a SHA-256 hash is persisted, so a database leak doesn’t hand
over working credentials, and the comparison is constant-time so a
caller can’t recover a hash byte-by-byte from response timing. The
plaintext exists only in the IssuedApiKey returned by
ApiKeyRepository::generate — there is deliberately no way to
read it back later.
Structs§
- ApiKey
Data - Stored form of a key. The secret itself is never in here.
- ApiKey
Guard - Guard for
Authorization: Api-Key sk_…. - DynApi
KeyRepository - Container shim, same reasoning as the async feature’s repository shims: a bare trait object can’t be a container key.
- InMemory
ApiKey Repository - In-memory store, indexed by secret hash.
- Issued
ApiKey - A freshly minted key.
secretis the only time the plaintext exists — store theApiKey, hand the secret to the caller.
Enums§
Constants§
- SECRET_
PREFIX - Prefix every secret carries, matching the TS
ApiKey.PREFIX.
Traits§
- ApiKey
Repository - Storage for API keys. Implement against a real database; the in-memory one below is for tests and single-process deployments.
Functions§
- generate_
secret sk_+ 32 bytes of CSPRNG output, hex-encoded.- hash_
secret - SHA-256, hex-encoded. Matches TS’s
ApiKey.hashSecret. - is_
active - True when the key is neither revoked nor past its expiry.
- is_
valid_ secret - Constant-time check of
secretagainst the stored hash. - new_
api_ key - Mint a new key with a random secret.
- register_
api_ key_ repository