Skip to content

feat(workload-identity): Add workload identity federation - #1772

Draft
gjtorikian wants to merge 15 commits into
mainfrom
feat/workload-identity
Draft

gjtorikian wants to merge 15 commits into
mainfrom
feat/workload-identity

Conversation

@gjtorikian

Copy link
Copy Markdown
Contributor

Summary

  • Adds workload identity federation as a second way to authenticate, next to an API key. Pass credentials: new WorkloadIdentityCredentials({...}) to new WorkOS(...) in place of an API key. The SDK exchanges the identity token that the workload's platform issues for a short-lived WorkOS access token, keeps it in memory and renews it before it expires. A CI job or a cloud workload then needs no stored API key.
  • Adds three provider packages that fetch the identity token on their platform: @workos-inc/workload-identity-github-actions, @workos-inc/workload-identity-gcp and @workos-inc/workload-identity-aws. A callback, a token file or an environment variable covers any other platform. The SDK gains no dependency: @aws-sdk/client-sts is a dependency of the AWS package only.
  • The exchange follows one design shared by all nine backend SDKs: the token URL must be https, a failed exchange is retried like an API request, a 401 on an exchanged token is recovered once per request, and no token appears in an error or a log.
  • Releases: the repository becomes a release-please manifest with one entry per package, so the SDK and each provider get their own release pull request and publish job.
  • Tested with the 31 conformance cases that all nine SDKs share, and end to end with real identity tokens from GitHub Actions, Google Cloud and AWS.

Authorize API requests with a short-lived access token, exchanged from the
workload's own identity token, in place of an API key:

    new WorkOS({ credentials: new WorkloadIdentityCredentials({ ... }) })

- Credential seam: the HTTP client resolves credentials before every attempt
  and recovers once from a 401. An API key is the static implementation, and
  its requests are unchanged.
- WIF core in the SDK: the jwt-bearer exchange, a per-instance token cache
  refreshed at min(60s, 20% of lifetime), one shared in-flight exchange,
  retries on network failures, 429 and 5xx, WorkloadIdentityError, tokenFile
  and tokenEnv.
- Three provider packages under packages/: GitHub Actions, Google Cloud and
  AWS. None depends on @workos-inc/node.
- Release: the repository becomes a release-please manifest monorepo with one
  entry, release PR and publish job per provider package. CI and scripts/ci
  cover each package.

Built against a mock of the token endpoint. Nothing is published.

Spec: docs/ideation/wif-backend-sdks/spec-phase-3.md
Review: passed in cycle 2 of 3.
A manifest entry of 0.0.0 does not make the first release 0.1.0 for the
node release type. release-please treats 0.0.0 as "never released", and
the node strategy then uses the default first version, 1.0.0. Set
initial-version on the three provider entries so the first release PR of
each is 0.1.0.

The key is read only when a package has no earlier release. It does not
change which commits count for a package.

Spec: docs/ideation/wif-backend-sdks/spec-phase-3.md
release-please counts a commit for the SDK unless every file it touches
is under packages/, and pull requests are squash-merged. Write down the
rule that keeps a provider change out of the SDK release:

- a provider feat or fix touches only packages/<name>/;
- a provider dependency change goes in its own chore(deps) pull request,
  with the package.json and the root package-lock.json together, merged
  before the feat or fix that needs it.

Spec: docs/ideation/wif-backend-sdks/spec-phase-3.md
The Google Cloud provider used fetch. Started with NODE_USE_ENV_PROXY=1,
Node sends fetch requests through the proxy named by HTTP_PROXY, and
fetch has no per-request way out of that. The request for an identity
token then went to the proxy. The metadata server exists only inside
the workload's own VM or container, so a proxy cannot reach it: the
request failed, or the provider returned whatever the proxy answered as
the token.

The provider now makes the request with node:http and an agent of its
own, which the setting does not reach. The URL, the header, the
10-second timeout and the status and empty-token errors are unchanged,
and a redirect is still not followed. A connection failure now rejects
with the socket error itself instead of "fetch failed".

A test gives the default agent a proxy and checks that the proxy
receives nothing.

Spec: docs/ideation/wif-backend-sdks/spec-phase-3.md
The rule that the GCP metadata request never goes through a proxy is now
conformance case WIF-GCP-03. The conformance check reads case IDs from
test names, so the existing test for the rule now carries the ID. No
behavior changes.

Spec: docs/ideation/wif-backend-sdks/spec-phase-3.md
…hints with the server

A reading of the server code on 2026-11-13 showed three things the SDK
had slightly wrong.

The server binds a workload identity client to exactly one environment
and takes the environment from that binding, so the
x-url-environment-id header selects nothing. environmentId is now
optional: a missing value is no longer a configuration error, and the
header is sent only when a value is set. An empty string counts as not
set. WORKOS_ENVIRONMENT_ID still fills the field when it is omitted,
and an explicit value still wins. Setting it is a safety check: a value
that names a different environment gets 403 environment_mismatch.

Four hints said the wrong thing for their code. An audience that no
trust rule names is invalid_grant, not invalid_target; invalid_target
is only about the resource. An application with no enabled trust rule
is invalid_client, not unauthorized_client. invalid_grant also covers
an identity token that was already exchanged. The four hints now carry
the shared wording.

The scope in the tests is orgs:write, the real scope name on the
server.

The README says that environmentId is optional and why, and that the
server accepts an identity token for one exchange, which matters for
tokenFile and tokenEnv.

WIF-02 now has two parts: (a) with an environment ID, (b) without one.

Spec: docs/ideation/wif-backend-sdks/shared-design.md (decisions 6 to 8)
…t the README examples

environmentId now comes only from the caller's code. The variable was
new in this work and exists nowhere else: not on main, not in the
server, the CLI or the docs. The header it filled is an optional safety
check, and a variable left in the environment could turn that check
into a 403 environment_mismatch with no line of code to point at. Left
out, undefined, null or an empty string all mean "not set": no
x-url-environment-id header and no error. WORKOS_CLIENT_ID,
WORKOS_WIF_TOKEN_URL and WORKOS_WIF_SCOPE are unchanged.

WIF-02 (b) now sets the variable and checks that no header is sent.
WIF-22 (c) and (d) no longer involve the environment ID.

The README and doc comments are corrected in three places:

- The tokenFile example was the path of a Kubernetes service account
  token, which the server refuses. It is now a file that another
  process writes, such as a sidecar or an earlier CI step.
- tokenFile and tokenEnv suit a job shorter than four minutes, not
  five: the SDK exchanges again when the 300-second token has 60
  seconds left.
- environmentId is described as optional and set in code, and no
  variable is named for it.

Spec: docs/ideation/wif-backend-sdks/shared-design.md (decisions 9 and 10)
…WIF_SCOPE

tokenUrl and scope now come only from the caller's code.
WORKOS_CLIENT_ID is the only variable the credentials read.

Both names came from the CLI spike, where variables are the only way to
configure a command; an SDK takes code. With no scope the token carries
every scope the application holds, so a variable left in the
environment would change permissions with no line of code to point at.
Adding a variable later breaks no one, and removing one after a release
does.

- tokenUrl is required. Missing or empty is an invalid_configuration
  error at construction: "tokenUrl is required". Its type is now
  `tokenUrl: string`, so TypeScript reports a missing one too.
- scope is optional. Not set or empty means no scope field in the form.
- clientId is unchanged: it still defaults from WORKOS_CLIENT_ID, and
  its error still names that variable.

WIF-22 now sets both variables and checks that they are ignored: the
POST goes to the token URL passed in code, the form has no scope, and
credentials built with no token URL fail at construction.

The README says WORKOS_CLIENT_ID is the only variable read. Every
example, in the README and in the doc comments of the SDK and the three
provider packages, passes the client ID and the token URL in code.

Spec: docs/ideation/wif-backend-sdks/shared-design.md (decision 11)
WorkloadIdentityCredentials now rejects any tokenUrl that is not an
absolute https URL. The POST to the token URL carries the workload's
identity token and the response carries the access token, so neither
may travel over plain HTTP. workos-php already has this rule, and every
SDK now follows it.

- The check runs at construction. A rejected URL is an
  invalid_configuration error: "tokenUrl must be an absolute https URL".
  The same text replaces "tokenUrl is not a valid URL" and "tokenUrl
  must be an http or https URL".
- Rejected: http:// on any host, localhost and loopback addresses
  included, a URL with no scheme, a URL with no host and any other
  scheme. The scheme is matched without regard to case, so HTTPS:// is
  accepted.
- The scheme and the two slashes are checked in the text before the URL
  is parsed. The parser repairs https:/path and https:///path into a
  URL whose host is the first path segment, which would let a URL with
  no host through.
- No option, environment variable or test seam relaxes the rule.

WIF-18 is the one test that talks to a real plain-HTTP server. It now
passes an https token URL and moves the endpoint under the credentials:
its stand-in for fetch sends the request the SDK built, unchanged apart
from the address, to the local server. The GCP provider's test moves
the metadata host the same way. The SDK gains no setting for it.

The README and the TSDoc on tokenUrl say the URL must be https.
Conflicts:

- .release-please-manifest.json: the root package takes main's version (12.4.0), and the three provider entries stay.
- package-lock.json: main's lockfile, with the provider packages added back by npm install --package-lock-only.
- src/workos.ts: main's fetchFn argument and the branch's credentials both go to the HTTP client. The static Authorization header stays removed: the credentials set it on every attempt.
- src/index.worker.ts: main removed the worker's own createHttpClient, so the worker uses the base one, which passes the credentials.
- src/common/net/fetch-client.ts: a request takes both main's abort signal and the branch's attempt state, and the resend after a 401 passes the signal too. main made the client never follow a redirect, so the branch's redirect setting, which only the token exchange used, is removed.
The client resolves its credentials before every attempt, and for an API key that put a promise between making a request and sending it. A caller that aborted right after making the request then found nothing in flight: two of main's new tests for the signal option wait on a fetch that was never called.

An API key has nothing to wait for, so its snapshot is now read without a promise, and the request is sent in the same turn, as on main. Other credentials are still awaited.
… client

Some methods send the API key in the request body as `client_secret`: the user management `authenticateWith...` calls that reach `/user_management/authenticate`, and `sso.getProfileAndToken`. A client built with `credentials` has no API key, but it passed the API-key check, so these requests went out with no client secret and with the workload's access token as the bearer. That token is not a client secret. The methods that also serve public clients (`authenticateWithCode`, `authenticateWithRefreshToken` and `sso.getProfileAndToken`) switched to their public-client form without saying so.

These methods now throw `ApiKeyRequiredException` on such a client before any request or token exchange is made. The message names the method and says to use a client built with an API key. Refreshing a sealed session goes through `authenticateWithRefreshToken`, so it throws too.

The check is on the credentials, not on the missing key. A public client, which has neither, keeps its PKCE and refresh token flows, and a client with an API key is unchanged. `authenticateWithCodeAndVerifier` never sends a client secret and is not checked.
@gjtorikian

Copy link
Copy Markdown
Contributor Author

@greptileai review this draft

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge, with no new actionable issues found.

Summary

Adds workload identity as an alternative to an API key.

  • WorkOS clients can use workload identity tokens instead of API keys.
  • GitHub Actions jobs can provide their own WorkOS identity token.
  • Google Cloud workloads can fetch an identity token for WorkOS.
  • AWS workloads can fetch a signed identity token for WorkOS.
  • The SDK and each provider package are checked and released separately.

The latest changes add AWS request limits and remove echoed identity tokens from OAuth errors. No new actionable issues were found. The earlier factory finding is fixed: clients with credentials now receive the full WorkOS type.

Diagram

sequenceDiagram
  participant App
  participant SDK
  participant Provider as Platform provider
  participant Token as Token endpoint
  participant API as WorkOS API
  App->>SDK: Call API
  opt Cached token needs replacement
    SDK->>Provider: Request identity token
    Provider-->>SDK: Identity token
    SDK->>Token: Exchange identity token
    Token-->>SDK: Short-lived access token
  end
  SDK->>API: Send access token
  API-->>App: Result
Loading

Reviews (3) · Last reviewed commit: "fix(workload-identity): Make one bounded..." · Reviewed by Greptile

…dentials

`createWorkOS` picked its return type from `apiKey` alone. Every client accepts `credentials`, so `createWorkOS({ clientId, credentials })` was typed as the public client, which hides `organizations` and the rest of the API a workload identity client is for, and `createWorkOS({ credentials })` matched no overload.

A third overload now takes options that require `credentials` and returns the full `WorkOS` type. The public and API-key option types no longer accept `credentials`, so each call matches exactly one overload. An API key together with credentials, which the constructor rejects, is now a compile error too.

Types only: the factory still passes its options to the constructor unchanged.
@gjtorikian

Copy link
Copy Markdown
Contributor Author

@greptileai review this draft

…en endpoint repeats it

A token endpoint may repeat the assertion it was sent in its `error` or `error_description`. The SDK copied both strings into `WorkloadIdentityError`, so the identity token then appeared in the error's code, description, message and stack.

Every occurrence of the assertion is now replaced with `[redacted]` before either string is kept. Each retry calls the provider again, so the assertion replaced is the one that attempt sent. The replacement happens before the description is cut to 500 characters, so a cut cannot leave the start of a token behind. It covers a terminal error and the last failure when the retries run out.
… call

The AWS SDK retries inside its client, so one provider call sent three STS requests when STS throttled or answered 5xx. The exchange already calls the provider again when it retries, and providers are meant to make one request per call. The client also had no limit on connecting or on waiting for an answer, so a call to a stalled STS did not end.

The provider's own STS client now makes one attempt, whatever `AWS_MAX_ATTEMPTS` or the shared config file says, and has the same 10-second limit the other two providers use: on connecting, on the wait for the response, and on a response that goes silent. `throwOnRequestTimeout` is set because the AWS SDK only logs a warning for its request limit without it.

No process-wide AWS configuration, environment variable, credential source or region rule changes.
@gjtorikian

Copy link
Copy Markdown
Contributor Author

@greptileai review this draft

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant