Skip to content

feat(workload-identity): authenticate with a workload token source instead of an API key - #1758

Closed
nicknisi wants to merge 2 commits into
mainfrom
wif/p14-workload-identity
Closed

nicknisi wants to merge 2 commits into
mainfrom
wif/p14-workload-identity

Conversation

@nicknisi

@nicknisi nicknisi commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Description

Adds a workloadIdentity client option, so a workload that already has an identity from its platform (a GitHub Actions job, a GCP or AWS service account) can call the WorkOS API without an API key.

const workos = new WorkOS({
  workloadIdentity: {
    clientId: 'client_1234',
    tokenSource: () => getPlatformIdToken(),
    tokenEndpoint: 'https://<authkit domain>/oauth2/token',
  },
});
  • On the first request the SDK exchanges the ID token for a short-lived access token (RFC 7523 jwt-bearer, no client secret), keeps it in memory, and exchanges again shortly before expiry and once after a 401. Concurrent requests share one exchange.
  • environmentId and scope are optional. x-url-environment-id is sent when an environment ID is set.
  • It cannot be combined with an API key, and WORKOS_API_KEY is not read when it is set.
  • A workload identity is not a client secret. Methods that send the API key as client_secret (authenticateWithPassword and the other non-PKCE authenticate methods) still throw ApiKeyRequiredException without an API key. The PKCE flows work as in public client mode and do not carry the workload token.
  • One timeout covers the whole exchange, including tokenSource().
  • Exchange failures are an OauthException with the endpoint's error and error_description. Neither token appears in errors or logs.
  • The exchange uses fetch only, so the worker entry supports it.

The server side is in review as a stack starting at workos/workos#79789, so nothing can use this yet.

Open for discussion, not settled:

  • The option shape (nested, with its own clientId), and whether this belongs in the core client or a separate package.
  • A POST is re-sent once after a 401.
  • A failed exchange is not retried by the SDK. The CLI retries three times (feat: authenticate in GitHub Actions with workload identity federation cli#268 follow-up).
  • createWorkOS types a workload-identity-only client as a public client. Use new WorkOS(...) for the full type.

Checked: 35 new unit tests against a mocked token endpoint, the full suite (1,342 tests), typecheck, lint and prettier. Not checked: an exchange against a real token endpoint, and npm run build.

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[x] Yes

No docs PR yet. Docs wait on the server side merging.

…stead of an API key

Add a workloadIdentity client option. The SDK exchanges the platform's ID
token for a short-lived WorkOS API access token (RFC 7523 jwt-bearer, no
client secret), keeps it in memory, and exchanges again shortly before
expiry and once on a 401. Concurrent requests share one exchange, and
x-url-environment-id is sent when an environment ID is configured.

Exchange failures surface as OauthException with the endpoint's error
code and description. Neither token appears in errors or logs. The
exchange uses fetch only, so the worker entry supports it too.
@nicknisi
nicknisi requested review from a team as code owners November 12, 2026 15:55
@nicknisi
nicknisi requested a review from mattolson November 12, 2026 15:55
@greptile-apps

greptile-apps Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge; both previously reported failures are fixed.

Summary

Adds workloadIdentity as an alternative to an API key.

  • Workload clients exchange platform identity for a cached WorkOS access token.
  • Regular WorkOS API calls use the workload token and refresh it after a 401.
  • Password and challenge sign-ins still require a real API key.

Both previous findings are fixed. No new actionable issues were found in the changes since the previous review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[API request] --> B{Uses workload identity?}
  B -->|No| C[Send with existing credentials]
  B -->|Yes| D[Reuse token or share one timed exchange]
  D --> E[Send request]
  E --> F{First response is 401?}
  F -->|No| G[Return response or error]
  F -->|Yes| H[Drop rejected token and get a replacement]
  H --> I[Send once more]
  I --> G
Loading

Reviews (2) · Last reviewed commit: "fix(workload-identity): keep the API key..." · Reviewed by Greptile

Comment thread src/workos.ts Resolved
…t methods and bound the token source

Methods that send the API key as client_secret (authenticateWithPassword and the other non-PKCE authenticate methods) passed the API key check when only a workload identity was configured, so they exchanged a token and sent it to the authenticate endpoint. They now throw ApiKeyRequiredException as they do for a public client.

The exchange timeout now covers tokenSource() too. A token source that never settles used to hold every later request on the shared in-flight exchange.

@gjtorikian gjtorikian left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is a little different than the one I launched: https://github.com/workos/workos-node/commits/feat/workload-identity/

Ultimately I'm not precious about which change gets merged in. I am going to mark this review as blocking, though, because I think we should test it in prod against GCP/AWS/GHA before merging it into a release branch, and I don't want someone to accidentally do so.

@nicknisi

Copy link
Copy Markdown
Member Author

Closing this in favour of feat/workload-identity. That branch has the provider packages and the credentials abstraction, and it is the one to take forward.

I ran it against the server stack (workos/workos#79789 to workos/workos#79802) to check that they fit:

  • Run: https://github.com/nicknisi/wif-test/actions/runs/43312647246, at 762c7a0. A GitHub Actions job built the SDK and the GitHub Actions provider from the branch, exchanged its OIDC token with no API key, then listed, created and read back an organization. It made one exchange for four API calls.
  • The server was a local build of the stack reached through tunnels, not a deployed environment. So this covers GitHub Actions only. It is not prod, and not GCP or AWS.

Two notes from the server side:

  • environmentId is required in WorkloadIdentityCredentials. As built, the server binds a client to one environment and treats x-url-environment-id as optional (it must match when sent). Whether that binding stays is still an open question, so nothing needs to change yet.
  • requireApiKey passes when credentials is set, so the methods that send the API key as client_secret (authenticateWithPassword and the other non-PKCE authenticate methods) would exchange and send a machine token. Greptile flagged the same thing on this PR. I read that in the diff and did not test it.

@nicknisi nicknisi closed this Nov 12, 2026
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.

2 participants