Skip to main content

DataCloudTokenProvider

Struct DataCloudTokenProvider 

Source
pub struct DataCloudTokenProvider { /* private fields */ }
Expand description

DC JWT provider.

Handles the full token flow for Salesforce Data Cloud:

  1. Authenticates with Salesforce using the configured auth mode to obtain an OAuth Access Token
  2. Exchanges the OAuth Access Token for a DC JWT
  3. Caches both tokens and refreshes them independently:
    • The OAuth Access Token is refreshed only when genuinely expired (to avoid unnecessary OAuth Refresh Token rotation)
    • The DC JWT is refreshed whenever it is expired or requested

On DC JWT exchange failure, the provider retries once with a force-refreshed OAuth Access Token (Step 2a), matching the behavior described in the GenieOAuthManagement documentation.

§Example

use hyperdb_api_salesforce::{SalesforceAuthConfig, AuthMode, DataCloudTokenProvider};

let config = SalesforceAuthConfig::new(
    "https://login.salesforce.com",
    "your-client-id",
)?
.auth_mode(AuthMode::private_key("user@example.com", &private_key_pem)?);

let mut provider = DataCloudTokenProvider::new(config)?;

// Get a valid DC JWT (automatically handles the full token flow)
let token = provider.get_token().await?;
println!("Authorization: {}", token.bearer_token());

Implementations§

Source§

impl DataCloudTokenProvider

Source

pub fn new(config: SalesforceAuthConfig) -> SalesforceAuthResult<Self>

Creates a new DC JWT provider with the given configuration.

§Errors

Returns an error if the configuration is invalid.

Source

pub fn config(&self) -> &SalesforceAuthConfig

Returns the configuration.

Source

pub async fn get_token(&mut self) -> SalesforceAuthResult<&DataCloudToken>

Gets a valid DC JWT.

If a cached DC JWT exists and is still valid, it is returned. Otherwise, a new DC JWT is obtained through the full token flow.

§Errors

Propagates any error from Self::fetch_dc_jwt — typically SalesforceAuthError::Http, SalesforceAuthError::Authorization, SalesforceAuthError::Jwt, SalesforceAuthError::TokenExchange, or SalesforceAuthError::TokenParse depending on where the three-step refresh cycle (OAuth Access Token → DC JWT) fails.

§Panics

Does not panic in practice. The trailing unwrap() on self.cached_dc_jwt is guarded by the preceding cache-population logic: either the cache was already populated with a valid token, or Self::fetch_dc_jwt just filled it.

Source

pub async fn force_refresh(&mut self) -> SalesforceAuthResult<&DataCloudToken>

Forces a full token refresh (both OAuth Access Token and DC JWT), even if the cached tokens are still valid.

§Errors

Propagates any error from Self::get_token (same failure modes as the full token-flow refresh).

Source

pub async fn refresh_token(&mut self) -> SalesforceAuthResult<&DataCloudToken>

Forces a DC JWT refresh while allowing the OAuth Access Token to be reused if still valid.

This is the preferred refresh method during normal operation: it re-exchanges the (possibly cached) OAuth Access Token for a fresh DC JWT without unnecessarily rotating the OAuth Refresh Token.

§Errors

Propagates any error from Self::get_token (HTTP, authorization, JWT signing, or token-parse failures during the DC JWT exchange).

Source

pub fn clear_cache(&mut self)

Clears all cached tokens (both OAuth Access Token and DC JWT).

Source

pub fn bearer_token(&self) -> Option<String>

Returns the DC JWT bearer token string if a valid DC JWT is cached.

Convenience method for getting the Authorization header value without an async call. Returns None if no valid DC JWT is cached.

Source

pub fn tenant_url(&self) -> Option<&str>

Returns the tenant URL if a valid DC JWT is cached.

Source

pub fn lakehouse_name(&self) -> SalesforceAuthResult<Option<String>>

Returns the lakehouse name for Hyper connection.

§Errors

Propagates SalesforceAuthError::TokenParse from DataCloudToken::lakehouse_name if the cached DC JWT’s tenant URL cannot be parsed into a valid lakehouse identifier.

Trait Implementations§

Source§

impl Debug for DataCloudTokenProvider

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more