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
impl SalesforceCredentials
Sourcepub fn new(
instance_url: impl Into<String>,
access_token: impl Into<String>,
api_version: impl Into<String>,
) -> SalesforceCredentials
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.
Sourcepub fn with_refresh_token(
self,
refresh_token: impl Into<String>,
) -> SalesforceCredentials
pub fn with_refresh_token( self, refresh_token: impl Into<String>, ) -> SalesforceCredentials
Create credentials with a refresh token.
Sourcepub fn refresh_token(&self) -> Option<&str>
pub fn refresh_token(&self) -> Option<&str>
Get the refresh token if available.
Sourcepub fn set_access_token(&mut self, token: impl Into<String>)
pub fn set_access_token(&mut self, token: impl Into<String>)
Set a new access token (e.g., after refresh).
Sourcepub async fn revoke_session(
&self,
revoke_refresh: bool,
login_url: &str,
) -> Result<(), Error>
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
revoke_refresh- If true, revokes the refresh token (and all access tokens). If false, revokes only the access token.login_url- The Salesforce login URL (e.g., https://login.salesforce.com for production or https://test.salesforce.com for sandbox).
§Errors
Returns an error if:
revoke_refreshis 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?;Sourcepub fn from_env() -> Result<SalesforceCredentials, Error>
pub fn from_env() -> Result<SalesforceCredentials, Error>
Load credentials from environment variables.
Required environment variables:
SF_INSTANCE_URLorSALESFORCE_INSTANCE_URLSF_ACCESS_TOKENorSALESFORCE_ACCESS_TOKEN
Optional:
SF_API_VERSIONorSALESFORCE_API_VERSION(default: “62.0”)SF_REFRESH_TOKENorSALESFORCE_REFRESH_TOKEN
Sourcepub async fn from_sfdx_alias(
alias_or_username: &str,
) -> Result<SalesforceCredentials, Error>
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.
Sourcepub async fn from_sfdx_auth_url(
auth_url: &str,
) -> Result<SalesforceCredentials, Error>
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?;Sourcepub fn with_api_version(
self,
version: impl Into<String>,
) -> SalesforceCredentials
pub fn with_api_version( self, version: impl Into<String>, ) -> SalesforceCredentials
Change the API version.
Sourcepub fn rest_api_url(&self) -> String
pub fn rest_api_url(&self) -> String
Get the base REST API URL for this org.
Sourcepub fn tooling_api_url(&self) -> String
pub fn tooling_api_url(&self) -> String
Get the Tooling API URL for this org.
Sourcepub fn metadata_api_url(&self) -> String
pub fn metadata_api_url(&self) -> String
Get the Metadata API URL for this org.
Sourcepub fn bulk_api_url(&self) -> String
pub fn bulk_api_url(&self) -> String
Get the Bulk API 2.0 URL for this org.
Trait Implementations§
Source§impl Clone for SalesforceCredentials
impl Clone for SalesforceCredentials
Source§fn clone(&self) -> SalesforceCredentials
fn clone(&self) -> SalesforceCredentials
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more