pub struct BearerAuth { /* private fields */ }Expand description
Bearer-token authentication (Authorization: Bearer <token>).
§Drop-path zeroization
The cached header string is wrapped in zeroize::Zeroizing so its
buffer is overwritten with zeros before being returned to the allocator
on drop. This defends against credential recovery from process core
dumps, /proc/PID/mem inspection, and post-drop heap re-use across
tenants in long-running multi-user JMAP clients (bd:JMAP-6r7c.59).
Callers that hold the original token string SHOULD also store it in a
Zeroizing<String> or equivalent — the zeroization here is bounded by
what this type owns.
§Do not move validation from construction to per-request (bd:JMAP-6r7c.18)
A future contributor may suggest “just store the token field and call
HeaderValue::from_str in auth_header on each request”. This is the
wrong simplification for both BearerAuth and BasicAuth. Five
reasons:
- Fail-fast at auth setup. Validation at construction means
invalid credentials surface at
BearerAuth::new()return value — the caller fails near the bug source (their auth-setup code). Per-request validation pushes failures to the firstJmapClient::call()orfetch_session(), far from the bug and harder to debug. - Hot-path performance.
auth_headeris called on every HTTP request and every WebSocket connection.HeaderValue::from_strwalks the string and rejects on the first non-VCHAR/SP/HTAB octet (RFC 7230 §3.2.6) — non-trivial work for a hot path. Pre-validation moves that work out of every request. - Infallible accessor signature. Pre-validation lets
auth_headerkeep the signaturefn auth_header(&self) -> Option<AuthHeader<'_>>— infallible. Per-request validation would requireResult<Option<(&str, &str)>, ClientError>, propagating an extra error layer through every call site (HTTPcall, blob upload/download, WebSocket connect, session fetch). - Borrow simplicity. Storing as
Zeroizing<String>letsauth_headerreturn borrows directly without ownership tricks (Cow,Box<str>, etc.). The borrow checker stays simple, the call sites stay readable. - Debug-redaction tripwire compatibility. The manual
Debugimpls onBearerAuthandBasicAuth(auth.rs further below) target the stored field. A future contributor adding#[derive(Debug)]instead of the manual impl is caught immediately by the existing canary testsbearer_auth_debug_does_not_leak_tokenandbasic_auth_debug_does_not_leak_credentials(bd:JMAP-sc1b.79). Moving to per-request validation requires the field shape to change in a way that re-derives the canary contract — extra surface area for review without buying anything.
This is the same pre-validate-at-construction pattern rustls and
reqwest use for their own type designs. It is not over-engineering.
Implementations§
Source§impl BearerAuth
impl BearerAuth
Sourcepub fn new(token: &str) -> Result<Self, ClientError>
pub fn new(token: &str) -> Result<Self, ClientError>
Construct a BearerAuth from a Bearer token string.
§Errors
ClientError::InvalidArgumentiftokenis empty or contains whitespace (RFC 6750 §2.1 bearer tokens must not contain whitespace).ClientError::InvalidHeaderValueiftokencontains characters that are not valid in an HTTP header value (non-visible-ASCII octets).
Trait Implementations§
Source§impl AuthProvider for BearerAuth
impl AuthProvider for BearerAuth
Source§fn auth_header(&self) -> Option<AuthHeader<'_>>
fn auth_header(&self) -> Option<AuthHeader<'_>>
AuthHeader to attach to every request. Read moreSource§impl Clone for BearerAuth
impl Clone for BearerAuth
Source§fn clone(&self) -> BearerAuth
fn clone(&self) -> BearerAuth
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more