pub enum Credential {
Bearer(SecretString),
Basic {
username: String,
password: SecretString,
},
ApiKey(SecretString),
Provider(TokenProvider),
Refreshable {
token: TokenProvider,
is_rejection: RejectionCheck,
on_rejected: TokenRefresher,
},
}Expand description
A per-scheme credential supplied at client construction: a static secret or a token provider for rotation.
Registration checks nothing: a credential is stored under whatever securitySchemes key it is
given, and is matched against that scheme’s kind only when an operation that requires
the scheme is called. Every failure below is therefore an error of that call, raised as a
request-construction error before anything is sent — never a 401 from the server:
- no non-empty alternative of the operation’s requirement has every scheme registered, and
either the requirement lists no empty
{}alternative or a credential is registered for some scheme of its non-empty alternatives:super::RequestError::MissingCredential; - the selected alternative has a credential of a variant its scheme cannot carry:
super::RequestError::CredentialMismatch; - the selected alternative’s
http basicscheme has aCredential::Basicwhose username holds:, which RFC 7617 §2 forbids:super::RequestError::InvalidBasicUsername; - the selected alternative’s
Credential::Provider, or the token provider of itsCredential::Refreshable, fails:super::RequestError::CredentialProvider.
Which variant a scheme accepts:
http basicaccepts onlyCredential::Basic;- every scheme that carries a token —
http bearer,oauth2andopenIdConnect(all three sent as a bearer token) andapiKeyin a header, query or cookie — acceptsCredential::Bearer,Credential::ApiKey,Credential::ProviderandCredential::Refreshable, and rejectsCredential::Basic; mutualTLSis satisfied by the transport and never reads a registered credential, so one registered under it is ignored.
Variants§
Bearer(SecretString)
Authorization: Bearer <token>.
Basic
HTTP basic auth.
ApiKey(SecretString)
An apiKey value.
Provider(TokenProvider)
A rotating token supplied on demand.
The provider yields one secret, so it stands in for Credential::Bearer or
Credential::ApiKey: it is accepted under every scheme that carries a token (http bearer, oauth2, openIdConnect, and apiKey in a header, query or cookie) and is asked
for a fresh token each time a call attaches it. A provider that fails fails that call as
super::RequestError::CredentialProvider, with its AuthError as the source.
It is not accepted under an http basic scheme, which needs a username and password
(Credential::Basic). There the call fails as
super::RequestError::CredentialMismatch with required: SchemeCarrier::HttpBasic
and registered: CredentialKind::Provider, and the provider is never called. Under a
mutualTLS scheme it is ignored and never called, since the transport’s client
certificate satisfies the scheme.
Refreshable
A token the runtime refreshes and replays once when the server rejects it.
It attaches like Credential::Provider: token is asked for a token each time a call
attaches it, under the same schemes, with the same failures. What it adds is the reactive
case. When the call’s response is not a success and is_rejection reads it as a rejection
of the token, on_rejected is given the token that was sent and yields a fresh one, and the
call is sent once more with it. token is not asked again for that replay; a refresh that
should outlive it is on_rejected’s to store where token reads it.
The replay happens at most once per call, and only when the request’s body could be cloned
before it was sent (a streaming upload cannot be). The call then returns the replay’s
response, unless the replay is rejected again. In every other case — a body that cannot be
cloned, on_rejected failing, the replay failing to send, or the replay rejected again —
the call returns the original response, which decodes as the operation’s typed error as
it would have without a refresh. A streaming response’s reconnect is signed afresh, as a
call is: token is asked for a token again, and a rejected reconnect is refreshed and
replayed once in the same way.
Fields
token: TokenProviderYields the token each call attaches.
is_rejection: RejectionCheckWhether a failed response means the token it carried is stale.
on_rejected: TokenRefresherGiven the stale token, yields a fresh one.
Implementations§
Source§impl Credential
impl Credential
Sourcepub fn refreshable<P, R, Fut>(
token: TokenProvider,
is_rejection: P,
on_rejected: R,
) -> Self
pub fn refreshable<P, R, Fut>( token: TokenProvider, is_rejection: P, on_rejected: R, ) -> Self
A Credential::Refreshable from closures: token as for token_provider,
is_rejection reading a failed response, and on_rejected an async closure given the
rejected token.
Trait Implementations§
Source§impl Clone for Credential
impl Clone for Credential
Source§fn clone(&self) -> Credential
fn clone(&self) -> Credential
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for Credential
impl !UnwindSafe for Credential
impl Freeze for Credential
impl Send for Credential
impl Sync for Credential
impl Unpin for Credential
impl UnsafeUnpin for Credential
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<T, UT> HandleAlloc<UT> for T
impl<T, UT> HandleAlloc<UT> for T
§fn new_handle(value: Arc<T>) -> Handle
fn new_handle(value: Arc<T>) -> Handle
§unsafe fn clone_handle(handle: Handle) -> Handle
unsafe fn clone_handle(handle: Handle) -> Handle
§unsafe fn consume_handle(handle: Handle) -> Arc<T> ⓘ
unsafe fn consume_handle(handle: Handle) -> Arc<T> ⓘ
Arc<> Read more§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more