force 0.4.0

Production-ready Salesforce Platform API client with REST and Bulk API 2.0 support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#![allow(dead_code)]
//! Shared harness for the tiered, env-gated live-contract test suite.
//!
//! This module mirrors the auth contract of `tests/live_salesforce.rs` so that
//! the sibling integration-test binaries (`live_core`, `live_special`,
//! `live_account_engagement`) share one credential loader and skip idiom. It is
//! deliberately included via `mod common;` from each test binary; because Rust
//! compiles it once per binary, unused helpers trip `dead_code` — hence the
//! blanket `allow` above.
//!
//! # Auth contract (identical env var names to `live_salesforce.rs`)
//!
//! The core loader tries sources in priority order:
//!
//! 1. **JWT Bearer** (feature `jwt`):
//!    `SF_JWT_CLIENT_ID`, `SF_JWT_USERNAME`, `SF_JWT_PRIVATE_KEY_PATH`,
//!    optional `SF_JWT_LOGIN_URL` (default `https://login.salesforce.com`).
//! 2. **Client Credentials**: `SF_CLIENT_ID`, `SF_CLIENT_SECRET`, `SF_TOKEN_URL`.
//! 3. **Username-Password** (feature `username_password`): `SF_UP_CLIENT_ID`,
//!    `SF_UP_CLIENT_SECRET`, `SF_UP_USERNAME`, `SF_UP_PASSWORD`,
//!    `SF_UP_SECURITY_TOKEN`, optional `SF_UP_TOKEN_URL`.
//! 4. **Bare access token**: `SF_ACCESS_TOKEN`, `SF_INSTANCE_URL`.
//! 5. **Salesforce CLI**: `SF_TARGET_ORG` (or the default org).
//!
//! Tier loaders (each returns `Option`, gated on its own env vars) enable the
//! special-surface tests: Data Cloud, Apex REST, Consent, Models, Agent API,
//! CPQ, and Account Engagement.

use async_trait::async_trait;
use force::auth::{AccessToken, Authenticator, TokenResponse};
use force::client::{ForceClient, builder};
use force::config::ClientConfig;
use force::error::Result;

#[cfg(feature = "jwt")]
use force::auth::JwtBearerFlow;

/// Prefix for every ephemeral record these tests create. Cleanup targets only
/// records whose name/field carries this prefix so live runs never touch real
/// org data.
pub const LIVE_TEST_PREFIX: &str = "force-rs-live-test";

// ─── Unified live authenticator ────────────────────────────────────────────

/// Authenticator that dispatches to whichever flow supplied credentials.
#[derive(Debug, Clone)]
pub enum LiveAuth {
    #[cfg(feature = "jwt")]
    Jwt(JwtBearerFlow),
    ClientCredentials(force::auth::ClientCredentials),
    #[cfg(feature = "username_password")]
    UsernamePassword(force::auth::UsernamePassword),
    Token(EnvAuthenticator),
}

#[async_trait]
impl Authenticator for LiveAuth {
    async fn authenticate(&self) -> Result<AccessToken> {
        match self {
            #[cfg(feature = "jwt")]
            Self::Jwt(flow) => flow.authenticate().await,
            Self::ClientCredentials(flow) => flow.authenticate().await,
            #[cfg(feature = "username_password")]
            Self::UsernamePassword(flow) => flow.authenticate().await,
            Self::Token(env) => env.authenticate().await,
        }
    }

    async fn refresh(&self) -> Result<AccessToken> {
        match self {
            #[cfg(feature = "jwt")]
            Self::Jwt(flow) => flow.refresh().await,
            Self::ClientCredentials(flow) => flow.refresh().await,
            #[cfg(feature = "username_password")]
            Self::UsernamePassword(flow) => flow.refresh().await,
            Self::Token(env) => env.refresh().await,
        }
    }
}

impl std::fmt::Display for LiveAuth {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            #[cfg(feature = "jwt")]
            Self::Jwt(_) => write!(f, "JWT Bearer"),
            Self::ClientCredentials(_) => write!(f, "Client Credentials"),
            #[cfg(feature = "username_password")]
            Self::UsernamePassword(_) => write!(f, "Username-Password"),
            Self::Token(_) => write!(f, "Access Token"),
        }
    }
}

/// Bare-token authenticator used by the `SF_ACCESS_TOKEN` and Salesforce CLI
/// fallbacks.
#[derive(Debug, Clone)]
pub struct EnvAuthenticator {
    access_token: String,
    instance_url: String,
}

#[async_trait]
impl Authenticator for EnvAuthenticator {
    async fn authenticate(&self) -> Result<AccessToken> {
        Ok(AccessToken::from_response(TokenResponse {
            access_token: secrecy::SecretString::new(self.access_token.clone().into()),
            instance_url: self.instance_url.clone(),
            token_type: "Bearer".to_string(),
            issued_at: chrono::Utc::now().timestamp_millis().to_string(),
            expires_in: Some(7_200),
            refresh_token: None,
            signature: "live-test".to_string(),
        }))
    }

    async fn refresh(&self) -> Result<AccessToken> {
        self.authenticate().await
    }
}

// ─── Env helpers ───────────────────────────────────────────────────────────

/// Reads a trimmed, non-empty env var, or `None`.
pub fn env_string(key: &str) -> Option<String> {
    std::env::var(key).ok().and_then(|v| {
        let v = v.trim().to_string();
        if v.is_empty() { None } else { Some(v) }
    })
}

/// Prints a standardized skip line to stderr for the given test.
pub fn skip(test_name: &str, reason: &str) {
    eprintln!("SKIP {test_name}: {reason}");
}

/// Normalizes a Salesforce OAuth token URL: accepts a bare host, base URL, or a
/// full `/services/oauth2/token` endpoint and always returns the token endpoint.
fn normalize_token_url(raw: &str) -> String {
    let trimmed = raw.trim().trim_end_matches('/');
    let base = if trimmed.contains("://") {
        trimmed.to_string()
    } else {
        format!("https://{trimmed}")
    };
    if base.ends_with("/services/oauth2/token") {
        base
    } else {
        format!("{base}/services/oauth2/token")
    }
}

/// Resolves the JWT `(audience, token_url)` pair from a login URL.
#[cfg(feature = "jwt")]
fn jwt_endpoints(login_url: &str) -> (String, String) {
    let trimmed = login_url.trim().trim_end_matches('/');
    let base = if trimmed.contains("://") {
        trimmed.to_string()
    } else {
        format!("https://{trimmed}")
    };
    let audience = base
        .trim_end_matches("/services/oauth2/token")
        .trim_end_matches('/')
        .to_string();
    let token_url = format!("{audience}/services/oauth2/token");
    (audience, token_url)
}

/// Resolves a private key path, trying `CARGO_MANIFEST_DIR` and the workspace
/// root as fallbacks for a relative path.
#[cfg(feature = "jwt")]
fn resolve_key_path(raw: &str) -> std::path::PathBuf {
    let path = std::path::PathBuf::from(raw);
    if path.exists() || !path.is_relative() {
        return path;
    }
    let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") else {
        return path;
    };
    let from_manifest = std::path::PathBuf::from(&manifest_dir).join(&path);
    if from_manifest.exists() {
        return from_manifest;
    }
    if let Some(root) = std::path::PathBuf::from(&manifest_dir)
        .parent()
        .and_then(|p| p.parent())
    {
        let from_root = root.join(&path);
        if from_root.exists() {
            return from_root;
        }
    }
    path
}

// ─── Core-tier config ──────────────────────────────────────────────────────

/// Resolved core-tier configuration: an authenticator plus the API version.
pub struct CoreConfig {
    pub auth: LiveAuth,
    pub api_version: String,
}

#[cfg(feature = "jwt")]
fn try_jwt_auth() -> Option<LiveAuth> {
    let client_id = env_string("SF_JWT_CLIENT_ID")?;
    let username = env_string("SF_JWT_USERNAME")?;
    let private_key_path = env_string("SF_JWT_PRIVATE_KEY_PATH")?;

    let resolved = resolve_key_path(&private_key_path);
    let private_key_pem = std::fs::read_to_string(&resolved).ok()?;
    let login_url = env_string("SF_JWT_LOGIN_URL")
        .unwrap_or_else(|| "https://login.salesforce.com".to_string());
    let (audience, token_url) = jwt_endpoints(&login_url);

    let flow = JwtBearerFlow::new(
        &client_id,
        &username,
        &private_key_pem,
        audience.as_str(),
        token_url.as_str(),
    )
    .ok()?;
    Some(LiveAuth::Jwt(flow))
}

fn try_client_credentials_auth() -> Option<LiveAuth> {
    let client_id = env_string("SF_CLIENT_ID")?;
    let client_secret = env_string("SF_CLIENT_SECRET")?;
    let token_url = normalize_token_url(&env_string("SF_TOKEN_URL")?);
    Some(LiveAuth::ClientCredentials(
        force::auth::ClientCredentials::new(client_id, client_secret, token_url),
    ))
}

#[cfg(feature = "username_password")]
fn try_username_password_auth() -> Option<LiveAuth> {
    let client_id = env_string("SF_UP_CLIENT_ID")?;
    let client_secret = env_string("SF_UP_CLIENT_SECRET")?;
    let username = env_string("SF_UP_USERNAME")?;
    let password = env_string("SF_UP_PASSWORD")?;
    let security_token = env_string("SF_UP_SECURITY_TOKEN").unwrap_or_default();
    let token_url = normalize_token_url(
        &env_string("SF_UP_TOKEN_URL")
            .unwrap_or_else(|| "https://login.salesforce.com".to_string()),
    );
    Some(LiveAuth::UsernamePassword(
        force::auth::UsernamePassword::new(
            client_id,
            client_secret,
            username,
            password,
            security_token,
            token_url,
        ),
    ))
}

fn try_token_auth() -> Option<LiveAuth> {
    let access_token = env_string("SF_ACCESS_TOKEN")?;
    let instance_url = env_string("SF_INSTANCE_URL")?;
    Some(LiveAuth::Token(EnvAuthenticator {
        access_token,
        instance_url,
    }))
}

#[derive(Debug, serde::Deserialize)]
struct SfCliOrgDisplayEnvelope {
    result: SfCliOrgDisplayResult,
}

#[derive(Debug, serde::Deserialize)]
struct SfCliOrgDisplayResult {
    #[serde(rename = "accessToken")]
    access_token: Option<String>,
    #[serde(rename = "instanceUrl")]
    instance_url: Option<String>,
}

const fn sf_cli_command_candidates() -> &'static [&'static str] {
    #[cfg(windows)]
    {
        &["sf.cmd", "sf"]
    }
    #[cfg(not(windows))]
    {
        &["sf"]
    }
}

fn try_sf_cli_auth() -> Option<LiveAuth> {
    let target_org = env_string("SF_TARGET_ORG");

    for command_name in sf_cli_command_candidates() {
        let mut command = std::process::Command::new(command_name);
        command.args(["org", "display", "--verbose", "--json"]);
        if let Some(ref org) = target_org {
            command.args(["--target-org", org]);
        }

        let Ok(output) = command.output() else {
            continue;
        };
        if !output.status.success() {
            continue;
        }
        let Ok(stdout) = String::from_utf8(output.stdout) else {
            continue;
        };
        let Ok(envelope) = serde_json::from_str::<SfCliOrgDisplayEnvelope>(&stdout) else {
            continue;
        };
        if let (Some(access_token), Some(instance_url)) =
            (envelope.result.access_token, envelope.result.instance_url)
        {
            return Some(LiveAuth::Token(EnvAuthenticator {
                access_token,
                instance_url,
            }));
        }
    }
    None
}

fn load_live_auth() -> Option<LiveAuth> {
    #[cfg(feature = "jwt")]
    if let Some(auth) = try_jwt_auth() {
        return Some(auth);
    }
    if let Some(auth) = try_client_credentials_auth() {
        return Some(auth);
    }
    #[cfg(feature = "username_password")]
    if let Some(auth) = try_username_password_auth() {
        return Some(auth);
    }
    if let Some(auth) = try_token_auth() {
        return Some(auth);
    }
    try_sf_cli_auth()
}

/// Loads core-tier credentials, trying every auth source in priority order.
///
/// Returns `None` when no credentials are configured, which callers turn into a
/// clean skip.
pub fn load_core_config() -> Option<CoreConfig> {
    let auth = load_live_auth()?;
    let api_version = env_string("SF_API_VERSION").unwrap_or_else(|| "v62.0".to_string());
    Some(CoreConfig { auth, api_version })
}

/// Builds a `ForceClient` from a resolved [`CoreConfig`].
///
/// # Errors
///
/// Returns an error if the client builder fails (e.g. initial authentication).
pub async fn create_client(config: &CoreConfig) -> Result<ForceClient<LiveAuth>> {
    let client_config = ClientConfig {
        api_version: config.api_version.clone(),
        ..Default::default()
    };
    builder()
        .config(client_config)
        .authenticate(config.auth.clone())
        .build()
        .await
}

// ─── Special-surface tier loaders ──────────────────────────────────────────

/// Data Cloud tier: gated on the `SF_DATA_CLOUD` boolean flag.
///
/// Returns a [`DataCloudConfig`], honoring an optional
/// `SF_DATA_CLOUD_TOKEN_URL` override for the token-exchange endpoint.
#[cfg(feature = "data_cloud")]
pub fn load_data_cloud() -> Option<force::auth::DataCloudConfig> {
    let enabled = std::env::var("SF_DATA_CLOUD").is_ok_and(|v| {
        matches!(
            v.trim().to_ascii_lowercase().as_str(),
            "1" | "true" | "yes" | "on"
        )
    });
    if !enabled {
        return None;
    }
    Some(force::auth::DataCloudConfig {
        token_exchange_url: env_string("SF_DATA_CLOUD_TOKEN_URL"),
        api_version: env_string("SF_DATA_CLOUD_API_VERSION"),
    })
}

/// Apex REST tier: the custom `/services/apexrest/{path}` suffix to GET.
pub fn apex_rest_path() -> Option<String> {
    env_string("SF_APEX_REST_PATH")
}

/// Consent tier: the `(action, ids)` pair from `SF_CONSENT_ACTION` +
/// comma-separated `SF_CONSENT_IDS`.
pub fn consent_params() -> Option<(String, Vec<String>)> {
    let action = env_string("SF_CONSENT_ACTION")?;
    let ids: Vec<String> = env_string("SF_CONSENT_IDS")?
        .split(',')
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();
    if ids.is_empty() {
        return None;
    }
    Some((action, ids))
}

/// Models (Agentforce) tier: the model name from `SF_MODELS_MODEL`.
pub fn models_model() -> Option<String> {
    env_string("SF_MODELS_MODEL")
}

/// Agent API tier: the agent id from `SF_AGENT_ID`.
pub fn agent_id() -> Option<String> {
    env_string("SF_AGENT_ID")
}

/// Account Engagement (Pardot) tier: the business unit id from
/// `SF_AE_BUSINESS_UNIT_ID`.
pub fn account_engagement_bu() -> Option<String> {
    env_string("SF_AE_BUSINESS_UNIT_ID")
}

/// CPQ tier: an existing quote id from `SF_CPQ_QUOTE_ID`.
pub fn cpq_quote_id() -> Option<String> {
    env_string("SF_CPQ_QUOTE_ID")
}