Salesforce session acquisition. Usually used indirectly through ::salesforce::api, which calls session() and retries once with a fresh session on 401 INVALID_SESSION_ID.

Auth matrix: the first flow whose credentials are present wins, in this order (or force one with salesforce.auth.flow):

FlowRequired contextToken endpoint hostJWT aud
access-tokensalesforce.access.token, salesforce.instance.urlnone (no exchange)n/a
refresh-tokensalesforce.client-id, salesforce.refresh-token (+ salesforce.client-secret if the app requires it)My Domain if set, else login/testn/a
jwt-bearersalesforce.client-id, salesforce.username, salesforce.private.keyMy Domain if set, else login/testsalesforce.jwt.audience, else login/test
client-credentialssalesforce.client-id, salesforce.client-secret, salesforce.my-domain.urlMy Domain (required)n/a

"login/test" is https://login.salesforce.com, or https://test.salesforce.com when salesforce.sandbox is "true". The JWT audience is never the My Domain URL: Salesforce accepts only login, test, or an Experience Cloud site URL there. The client-credentials flow, by contrast, only works against the org's My Domain token endpoint.

Caching: a minted session is cached in the run's context for expires_in seconds when the token response carries it, otherwise salesforce.session.ttl seconds (default 900). The cache is keyed on a SHA-256 of the credentials, so each set of credentials has its own session, and changing them mid-run mints a new one.

Per-call credentials: every session function takes an opts Map (see resolve-config). Credentials in opts are a complete identity: nothing in it falls back to context, so a per-call token is never sent to the project's org and a context secret is never sent to a per-call URL. They are masked in call records, and only the minted session is stored, keyed as above.

  • access-token (or token) + instance-url
  • refresh-token + client-id, and client-secret if the app needs it
  • private-key + client-id + username, optionally audience
  • client-secret + client-id + my-domain-url

my-domain-url also picks the token host of the refresh-token and JWT flows. flow, sandbox and ttl work as their context variables.

Values

CACHE_KEY

CACHE_KEY: Str "salesforce.session.cache"

DEFAULT_SESSION_TTL

DEFAULT_SESSION_TTL: Int 900

FLOWS

FLOWS: Vec [
  "access-token",
  "refresh-token",
  "jwt-bearer",
  "client-credentials"
]

IDENTITY_OPTS

IDENTITY_OPTS: Vec [
  "token",
  "access-token",
  "refresh-token",
  "client-id",
  "client-secret",
  "private-key",
  "username"
]

TOKEN_FIELDS

TOKEN_FIELDS: Vec [
  "access_token",
  "refresh_token",
  "id_token"
]

URL_OPTS

URL_OPTS: Vec [
  "instance-url",
  "my-domain-url",
  "audience"
]

Functions

blank-option

fn (opts: Map, names: Vec): Str?

The first of names that opts has with a blank or null value, else null. Exposed for testing.

Example

::salesforce::auth/blank-option({username: " "}, ["username", "client-id"])  // "username"
::salesforce::auth/blank-option({}, ["username"])  // null

build-jwt-claims

fn (client-id: Str, username: Str, audience: Str): Map

Build the connected-app JWT-bearer claims: iss = consumer key, sub = username, aud = the audience from jwt-audience, 3-minute expiry (Salesforce's maximum). Exposed for testing.

Example

::salesforce::auth/build-jwt-claims("3MVG...", "ada@example.com", "https://login.salesforce.com").sub  // "ada@example.com"

cache-key

fn (fingerprint: Str): Str

The context key holding the cached session for a credentials fingerprint. Exposed for testing.

Example

::salesforce::auth/cache-key("abc")  // "salesforce.session.cache.abc"

cached-session

fn (cache: Any, key: Str, now: Int): Any

The cached session for a credentials fingerprint, or null when there is none, it belongs to other credentials, or it has expired. cache and now are explicit for testing.

Example

cache {fingerprint: "abc", expires-at: 2000, session: {token: "TOKEN"}}
::salesforce::auth/cached-session(cache, "abc", 1000).token  // "TOKEN"
::salesforce::auth/cached-session(cache, "abc", 3000)  // null

config

fn (): Map

Read the auth settings from context into a Map: access-token, instance-url, client-id, client-secret, username, private-key, refresh-token, my-domain, sandbox, audience, flow, ttl.

Example

cfg ::salesforce::auth/config()
cfg.sandbox
Context Vars: salesforce.access.token, salesforce.instance.url, salesforce.client-id, salesforce.client-secret, salesforce.username, salesforce.private.key, salesforce.refresh-token, salesforce.my-domain.url, salesforce.sandbox, salesforce.jwt.audience, salesforce.auth.flow, salesforce.session.ttl

exchange

fn (flow: Str, cfg: Map): Map

Mint a session for flow from a config Map, bypassing the cache. Returns {token, instance-url, flow, expires-at} or an Err (missing settings, or the token endpoint's {status, body}).

Example

minted ::salesforce::auth/exchange("refresh-token", ::salesforce::auth/config())
minted.instance-url

fingerprint

fn (flow: Str, config: Map): Str

A hash of the credentials in a config Map, so a cached session is only reused for the same credentials. Exposed for testing.

Example

length(::salesforce::auth/fingerprint("refresh-token", {client-id: "3MVG..."}))  // 64

has-identity

fn (opts: Map): Bool

True when opts carries credentials of its own. Exposed for testing.

Example

::salesforce::auth/has-identity({refresh-token: "r"})  // true
::salesforce::auth/has-identity({instance-url: "https://acme.my.salesforce.com"})  // false

invalidate

fn (): Any
fn (opts: Map): Any

Drop the run's cached session for these credentials so the next request mints a new one. ::salesforce::api calls this on 401 INVALID_SESSION_ID.

Example

// The next request mints a new session
::salesforce::auth/invalidate()
::salesforce::org/limits()

is-configured

fn (): Bool

True when the context holds a complete set of credentials for some flow. Used by hot.dev/crm to detect Salesforce, and by the integration tests to skip without credentials.

Example

if(::salesforce::auth/is-configured(), ::salesforce::org/limits(), null)

is-truthy

fn (value: Any): Bool

True for true and the strings "true", "1", "yes" (any case); context values often arrive as strings. Exposed for testing.

Example

::salesforce::auth/is-truthy("YES")  // true
::salesforce::auth/is-truthy("0")  // false

jwt-audience

fn (sandbox: Bool, override: Any): Str

The JWT-bearer aud claim: the explicit override (e.g. an Experience Cloud site URL) when set, else login or test by the sandbox flag. Never the My Domain URL. Exposed for testing.

Example

::salesforce::auth/jwt-audience(false, null)  // "https://login.salesforce.com"
::salesforce::auth/jwt-audience(false, "https://acme.my.site.com/portal/")  // "https://acme.my.site.com/portal"

login-host

fn (sandbox: Bool): Str

https://test.salesforce.com for sandboxes, https://login.salesforce.com otherwise. Exposed for testing.

Example

::salesforce::auth/login-host(true)  // "https://test.salesforce.com"

mark-credentials

fn (opts: Map): Any

Mask the credentials in opts in this run's call records. Called first wherever opts arrives, so the records of the calls after it are masked too. Returns null.

Example

::salesforce::auth/mark-credentials({access-token: ::hot::ctx/get("tenant.salesforce.token")})

missing-for-flow

fn (flow: Str, config: Map): Vec

The context variable names a flow still needs, given a config Map. Empty when the flow is fully configured. Exposed for testing.

Example

::salesforce::auth/missing-for-flow("refresh-token", {client-id: "3MVG..."})  // ["salesforce.refresh-token"]

missing-opts-for-flow

fn (flow: Str, config: Map): Vec

The opts keys a flow still needs, given a config Map from resolve-config. Empty when the flow is fully configured. Exposed for testing.

Example

::salesforce::auth/missing-opts-for-flow("refresh-token", {client-id: "3MVG..."})  // ["refresh-token"]

normalize-url

fn (url: Str): Str

Trim a host URL, drop trailing slashes, and default the scheme to https:// (acme.my.salesforce.com/ → https://acme.my.salesforce.com). Exposed for testing.

Example

::salesforce::auth/normalize-url("acme.my.salesforce.com/")  // "https://acme.my.salesforce.com"

resolve-config

fn (opts: Map, ctx-config: Map): Map

The one resolver, pure: the auth config Map for a call from opts first, then ctx-config (see config). Credentials in opts replace the context's identity entirely, so nothing from it goes to a per-call URL or mixes with a per-call credential; sandbox and ttl still fall back to context. A token (a session from session, say) is an access token whatever its flow says. An Err when a credential or URL key is in opts but blank or null, or when opts has instance-url, my-domain-url or audience without a credential. Exposed for testing.

Example

::salesforce::auth/resolve-config({refresh-token: "r", client-id: "c"}, {client-id: "ctx"}).client-id  // "c"
::salesforce::auth/resolve-config({}, {client-id: "ctx"}).client-id  // "ctx"
is-err(::salesforce::auth/resolve-config({instance-url: "https://acme.my.salesforce.com"}, {}))  // true

select-flow

fn (config: Map): Any

Pick the auth flow for a config Map (see config). An explicit flow wins; otherwise the first of access-token, refresh-token, jwt-bearer, client-credentials whose credentials are present. Returns null when nothing usable is configured, or an Err for an unknown explicit flow. Exposed for testing.

Example

::salesforce::auth/select-flow({refresh-token: "r"})  // "refresh-token"
::salesforce::auth/select-flow({})  // null

session

fn (): Map
fn (opts: Map): Map

Resolve a Salesforce session: {token, instance-url, flow, expires-at}. Reuses the run's cached session when it is still valid for the same credentials, otherwise mints one (see the auth matrix in the namespace docs). opts carries per-call credentials (see resolve-config). Returns an Err when nothing is configured, opts is refused, or the token exchange fails.

Example

::salesforce::auth/session({access-token: "00D...", instance-url: "https://acme.my.salesforce.com"}).instance-url

session-expiry

fn (tokens: Map, now: Int, ttl: Int): Int

When a minted session should be treated as expired (epoch seconds): expires_in from the token response when present, else ttl, minus 30 seconds of slack. Exposed for testing.

Example

::salesforce::auth/session-expiry({expires_in: 3600}, 1000, 900)  // 4570
::salesforce::auth/session-expiry({}, 1000, 900)  // 1870

token-url

fn (flow: Str, my-domain: Any, sandbox: Bool): Str

The OAuth token endpoint for a flow. Client credentials require the My Domain URL (Salesforce rejects that grant at login/test); the other flows use My Domain when it is set and login/test otherwise. Returns an Err for client credentials without a My Domain URL. Exposed for testing.

Example

::salesforce::auth/token-url("refresh-token", null, true)
// "https://test.salesforce.com/services/oauth2/token"

::salesforce::auth/token-url("client-credentials", "acme.my.salesforce.com", false)
// "https://acme.my.salesforce.com/services/oauth2/token"