ToolKit Auth
Authentication infrastructure for Gears / ToolKit.
Overview
The cf-gears-toolkit-auth crate provides:
- JWT / JWKS —
KeyProvidertrait,JwksKeyProviderwith background key refresh,ValidationConfig, standard claim constants - Token validation —
TokenValidatortrait,ClaimsError/AuthErrorerror types - Auth configuration —
AuthConfig(issuers, audiences, leeway, JWKS endpoint) - Outbound OAuth2 client credentials —
Tokenhandle with proactive background refresh and on-demand invalidation,OAuthClientConfig,BearerAuthLayer(tower),BearerAuthAutoRefreshLayer(reactive refresh on 401, port of go-appkit'sAuthBearerRoundTripper),HttpClientBuilderExtfortoolkit-httpintegration - Auth metrics —
AuthMetricstrait withLoggingMetricsandNoOpMetricsimplementations
Outbound OAuth2 quick start
use ;
use HttpClientBuilder;
let token = new
.await?;
let client = new
.with_bearer_auth
.build?;
// Every request gets Authorization: Bearer <token> automatically
let resp = client.get.send.await?;
Reactive refresh on 401
with_bearer_auth covers the proactive case: the background TokenWatcher
refreshes the credential before TTL expires. It does not detect tokens
revoked out-of-band by the issuer — those surface only as a 401 from the
upstream.
with_bearer_auth_auto_refresh adds the reactive half: on a 401 response it
calls Token::invalidate(), re-reads the cached token, and replays the
original request once with the refreshed credential. Behavior is a port of
go-appkit's AuthBearerRoundTripper:
- requests that already carry the configured auth header pass through untouched (no refresh, no retry);
Token::invalidate()is throttled per layer instance — default 15 minutes viatoolkit_auth::oauth2::DEFAULT_MIN_INVALIDATION_INTERVAL— so a burst of 401s will not hammer the token endpoint;- if the refreshed token equals the previous value, or if the invalidate fetch fails, the original 401 is surfaced as-is (no retry loop);
- exactly one retry per call, no backoff. For multi-step retry strategies,
compose with
toolkit_http::RetryLayer.
use ;
use HttpClientBuilder;
use Arc;
use Duration;
let token = new.await?;
// Defaults: Authorization header, retry on 401 only, 15-min throttle.
let client = new
.with_bearer_auth_auto_refresh
.build?;
// Custom predicate / header / throttle:
let opts = BearerAuthAutoRefreshOpts ;
let custom = new
.with_bearer_auth_auto_refresh_opts
.build?;
Cost: auto-refresh keeps a clone of the request body so it can be replayed
on retry. With toolkit-http's default body (Full<Bytes>) this is a
reference-counted bump; with custom B, the type must be Clone. Pick the
plain with_bearer_auth when the upstream is known not to revoke tokens
out-of-band.
See examples/ for more patterns (OIDC discovery, token invalidation, shared token, form auth).
License
Licensed under Apache-2.0.