Skip to main content

SalesforceCredentials

Struct SalesforceCredentials 

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

Standard Salesforce credentials implementation.

Sensitive fields (access_token, refresh_token) are redacted in Debug output to prevent accidental exposure in logs.

Implementations§

Source§

impl SalesforceCredentials

Source

pub fn new( instance_url: impl Into<String>, access_token: impl Into<String>, api_version: impl Into<String>, ) -> SalesforceCredentials

Create new credentials with the given values.

Source

pub fn with_refresh_token( self, refresh_token: impl Into<String>, ) -> SalesforceCredentials

Create credentials with a refresh token.

Source

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

Get the refresh token if available.

Source

pub fn set_access_token(&mut self, token: impl Into<String>)

Set a new access token (e.g., after refresh).

Source

pub async fn revoke_session( &self, revoke_refresh: bool, login_url: &str, ) -> Result<(), Error>

Revoke the current session by invalidating the access token or refresh token.

This convenience method creates an OAuthClient and calls revoke_token() with the current credentials. You can choose to revoke either the access token or the refresh token (if available).

§Token Type Behavior
  • Revoking refresh token (revoke_refresh: true): Invalidates the refresh token AND all associated access tokens. Use this for complete session termination. Requires a refresh token to be present in the credentials.
  • Revoking access token (revoke_refresh: false): Invalidates only the current access token. The refresh token remains valid and can be used to obtain a new access token.
§Arguments
§Errors

Returns an error if:

  • revoke_refresh is true but no refresh token is available
  • The HTTP request to the revocation endpoint fails
  • The Salesforce server returns an error response
§Example
let creds = SalesforceCredentials::new(
    "https://na1.salesforce.com",
    "access_token",
    "62.0"
).with_refresh_token("refresh_token");

// Revoke the entire session (refresh token + all access tokens)
creds.revoke_session(true, PRODUCTION_LOGIN_URL).await?;

// Or revoke just the access token
creds.revoke_session(false, PRODUCTION_LOGIN_URL).await?;
Source

pub fn from_env() -> Result<SalesforceCredentials, Error>

Load credentials from environment variables.

Required environment variables:

  • SF_INSTANCE_URL or SALESFORCE_INSTANCE_URL
  • SF_ACCESS_TOKEN or SALESFORCE_ACCESS_TOKEN

Optional:

  • SF_API_VERSION or SALESFORCE_API_VERSION (default: “62.0”)
  • SF_REFRESH_TOKEN or SALESFORCE_REFRESH_TOKEN
Source

pub async fn from_sfdx_alias( alias_or_username: &str, ) -> Result<SalesforceCredentials, Error>

Load credentials from SFDX CLI using an org alias or username.

Requires the sf CLI to be installed and the org to be authenticated.

Source

pub async fn from_sfdx_auth_url( auth_url: &str, ) -> Result<SalesforceCredentials, Error>

Load credentials from an SFDX auth URL.

The SFDX auth URL format is:

  • force://<client_id>:<client_secret>:<refresh_token>@<instance_url>
  • force://<client_id>::<refresh_token>@<instance_url> (empty client_secret)
  • force://<client_id>:<client_secret>:<refresh_token>:<username>@<instance_url> (with username)

The client_secret can be empty (indicated by ::) for the default Salesforce CLI connected app. The username field is optional.

This method will parse the auth URL and use the refresh token to obtain an access token from Salesforce.

§Example
let auth_url = std::env::var("SF_AUTH_URL")?;
let creds = SalesforceCredentials::from_sfdx_auth_url(&auth_url).await?;
Source

pub fn with_api_version( self, version: impl Into<String>, ) -> SalesforceCredentials

Change the API version.

Source

pub fn rest_api_url(&self) -> String

Get the base REST API URL for this org.

Source

pub fn tooling_api_url(&self) -> String

Get the Tooling API URL for this org.

Source

pub fn metadata_api_url(&self) -> String

Get the Metadata API URL for this org.

Source

pub fn bulk_api_url(&self) -> String

Get the Bulk API 2.0 URL for this org.

Trait Implementations§

Source§

impl Clone for SalesforceCredentials

Source§

fn clone(&self) -> SalesforceCredentials

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Credentials for SalesforceCredentials

Source§

fn instance_url(&self) -> &str

Get the Salesforce instance URL.
Source§

fn access_token(&self) -> &str

Get the access token.
Source§

fn api_version(&self) -> &str

Get the API version (e.g., “62.0”).
Source§

fn is_valid(&self) -> bool

Returns true if the credentials appear to be valid (non-empty).
Source§

impl Debug for SalesforceCredentials

Source§

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

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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: Sized + 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: Sized + 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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