pub struct ApiKeyCredential { /* private fields */ }Expand description
Exchanges an application API key for short-lived Dex access tokens.
Use this when an application holds a long-lived API key and Dex requires bearer tokens. Use
crate::FileCredential when another process writes ready-to-use tokens to a file. Unlike
other Credential implementations, construction is async: ApiKeyCredentialBuilder::build
performs the first exchange so an invalid key fails before Client or Worker creation. The
synchronous Credential::token reads only the cache and never performs network I/O.
A background Tokio task refreshes at 80% of each token’s lifetime. Concurrent callers use the
same cached token and only one exchange runs at a time. After a failed refresh, the current
token remains usable until expiry and refresh is retried no more often than every five seconds.
An expired token fails calls until a refresh succeeds. A unary Dex call rejected with gRPC
Unauthenticated discards that exact token and retries once after a fresh exchange. Dex rejects
unauthenticated calls before execution, so the retry is safe. Worker poll streams discard
rejected tokens and reconnect once immediately. A second rejection uses the normal backoff.
The credential is safe to share across threads. Its background task is aborted when the last
owner drops it. Client and Worker release their references when dropped; applications holding
another Arc should drop that reference to stop refreshing. Keep the Tokio runtime used for
construction alive and driving tasks while the credential is in use. A multi-thread runtime
does this while it remains alive; a current-thread runtime must be continuously driven.
Debug output and exchange errors omit the API key and access tokens.
use std::{sync::Arc, time::Duration};
use dex_sdk::{ApiKeyCredential, ClientOptions, WorkerOptions};
let runtime = tokio::runtime::Runtime::new()?;
let credential = Arc::new(runtime.block_on(ApiKeyCredential::builder(
"https://auth.example.com/token", "my-dex-server", "application-api-key",
).exchange_timeout(Duration::from_secs(10)).build())?);
let client_options = ClientOptions::new().credential(credential.clone());
let worker_options = WorkerOptions::new().credential(credential);Implementations§
Source§impl ApiKeyCredential
impl ApiKeyCredential
Sourcepub fn builder(
token_url: impl Into<String>,
server_id: impl Into<String>,
api_key: impl Into<String>,
) -> ApiKeyCredentialBuilder
pub fn builder( token_url: impl Into<String>, server_id: impl Into<String>, api_key: impl Into<String>, ) -> ApiKeyCredentialBuilder
Starts configuration of an API-key credential.
token_url identifies the exchange endpoint. server_id scopes its access token, and
api_key is the application secret. All three values are required. Validation and the
first exchange occur in ApiKeyCredentialBuilder::build.
Trait Implementations§
Source§impl Credential for ApiKeyCredential
impl Credential for ApiKeyCredential
Source§impl Debug for ApiKeyCredential
impl Debug for ApiKeyCredential
Source§impl Drop for ApiKeyCredential
impl Drop for ApiKeyCredential
Auto Trait Implementations§
impl !RefUnwindSafe for ApiKeyCredential
impl !UnwindSafe for ApiKeyCredential
impl Freeze for ApiKeyCredential
impl Send for ApiKeyCredential
impl Sync for ApiKeyCredential
impl Unpin for ApiKeyCredential
impl UnsafeUnpin for ApiKeyCredential
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request