Skip to main content

Crate wabot_addon_auth_api_key

Crate wabot_addon_auth_api_key 

Source
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§

ApiKeyData
Stored form of a key. The secret itself is never in here.
ApiKeyGuard
Guard for Authorization: Api-Key sk_….
DynApiKeyRepository
Container shim, same reasoning as the async feature’s repository shims: a bare trait object can’t be a container key.
InMemoryApiKeyRepository
In-memory store, indexed by secret hash.
IssuedApiKey
A freshly minted key. secret is the only time the plaintext exists — store the ApiKey, hand the secret to the caller.

Enums§

ApiKeyError

Constants§

SECRET_PREFIX
Prefix every secret carries, matching the TS ApiKey.PREFIX.

Traits§

ApiKeyRepository
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 secret against the stored hash.
new_api_key
Mint a new key with a random secret.
register_api_key_repository

Type Aliases§

ApiKey