Skip to content
Source
Sign inSign up
TanStack/source

Factory: use Anthropic Workload Identity Federation in place of a static API key #654

Closedtombeckenham opened this issue 2 comments
tombeckenhamcommented

Problem

A factory can use Anthropic models only with a static API key (sk-ant-...), stored in the vault and sent as x-api-key (apps/web/src/server/factory/models.ts, the anthropic entry near line 565, and setRepoProviderKey). A static key lives for a long time, and a leak is usable until someone rotates it.

Anthropic now has Workload Identity Federation (WIF, GA). An organisation registers an OIDC issuer and a federation rule in the Claude Console. A workload then swaps a short-lived signed JWT for a short-lived Anthropic token at /v1/oauth/token. No long-lived secret is stored.

What exists already

  • Source is an OIDC issuer with discovery and JWKS: /oidc/.well-known/openid-configuration, /oidc/jwks.
  • The keys Worker signs JWTs (RunnerKeys.signJwt, OIDC_SIGNING_KEYS, apps/web/src/keys.ts). The app identity tokens already use it.

Reuse this. Do not add a second issuer or a second signing path.

Proposal

Let the Anthropic key of a factory (repository and organisation) be one of two things:

  1. A static key, as today.
  2. A federation: rule id (fdrl_...), organisation id, and a workspace id when the rule covers more than one workspace. None of these is a secret.

With a federation, FactoryJobObject asks the keys Worker for a JWT, exchanges it for an Anthropic token, and sends that as a Bearer token. The token and the JWT never enter the sandbox, the run log, an answer or an error text.

The person sets up the Anthropic side once, in the Claude Console: add Source as a federation issuer (JWKS by discovery), make a service account, and make a rule that matches the subject of their repository, scoped to one workspace with workspace:developer and a short token lifetime. To revoke, they archive the rule.

Check first, before any build

These are not verified. Read Anthropic's WIF docs (https://platform.claude.com/docs/en/manage-claude/authentication.md, https://platform.claude.com/docs/en/manage-claude/wif-admin-api.md) and settle each one:

  • The audience and subject claims that Anthropic expects, against the claims of our OIDC tokens. The audience must never have the source-app: form (APP_AUDIENCE_PREFIX).
  • The exact request and answer of the token exchange, and how long the token lives. The SDK does the exchange from ANTHROPIC_FEDERATION_RULE_ID, ANTHROPIC_ORGANIZATION_ID and ANTHROPIC_IDENTITY_TOKEN; the factory calls through TanStack AI's Anthropic adapter, so it may need the exchange by hand.
  • Whether the AI Gateway path (cloudflareBindingFetch in models.ts) can carry a Bearer token in place of a key.
  • What the subject of a factory run should be, so that a rule can match one repository and no other repository can pass as it.
  • Whether a CI job with permissions: { idToken: true } already works with WIF with no forge change (the env vars above, and api.anthropic.com in the job's egress). If it does, this part is docs only.

Post the findings on this issue before the code change. If one of them blocks the design, stop and say so.

Done when

  • A factory runs an Anthropic model with a federation and no stored key, on the direct path, and on the gateway path if that can carry it.
  • The key check (checkProviderKey) has a federation twin that tries one exchange and reports a clear reason on failure.
  • REST, MCP and src set, show and delete a federation, the same as a factory key. The settings stay for people only, as the factory keys are.
  • Tests cover the exchange, an expired token, a refused rule, and that no token reaches a log, an answer or the sandbox.
  • Docs (docs/factory/), the source skill (references/factory.md) and the landing page describe it, with the Console steps and screenshots.
  1. Findings of "Check first"

    Read: Anthropic's authentication.md, wif-admin-api.md, workload-identity-federation.md, wif-reference.md and wif-providers/github-actions.md (21 Nov 2026), against jobs/oidc.ts, keys.ts, factory/models.ts, factory/providers.ts and @tanstack/ai-anthropic 1.1.1. Nothing was run against a real Anthropic organisation: these come from the docs and the code.

    Nothing blocks the design. One point needs a decision first (the web Worker, at the end).

    1. Audience and subject claims

    • Anthropic fixes no audience. A rule may match aud exactly (match.audience), and it is optional. Its GitHub example uses https://api.anthropic.com. We use the same. It does not have the source-app: form.
    • Required claims: sub, iat, exp, and iss equal to the registered issuer URL. nbf is checked with 30 s of leeway. Our tokens have all of them.
    • Signing: RS256 with a kid in the header is accepted. signJwt does this.
    • Issuer: https, port 443, public host, keys by discovery at <issuer>/.well-known/openid-configuration. https://source.tanstack.com/oidc fits, with no change.
    • Token age: exp - iat at most 1 hour by default. Ours is 300 s.
    • jti: a token with a jti is good for one exchange for each issuer (check_jti, on by default). So each exchange needs a new JWT. We sign a new one each time.

    2. The token exchange

    • Request: POST https://api.anthropic.com/v1/oauth/token, content-type: application/json, no version header. Body: grant_type urn:ietf:params:oauth:grant-type:jwt-bearer, assertion (the JWT), federation_rule_id (fdrl_...), organization_id (a UUID), and workspace_id (wrkspc_...) when the rule covers more than one workspace. service_account_id is optional, and we do not send it.
    • Answer: { access_token: "sk-ant-oat01-...", token_type: "Bearer", expires_in, scope }. It goes on a model call as Authorization: Bearer.
    • Lifetime: the lesser of the rule's token_lifetime_seconds (60 to 86400) and twice what is left of the JWT, never under 60 s. With our 300 s JWT, a token lives at most 600 s. There is no refresh token: to renew, exchange a new JWT.
    • A factory run can last longer than 600 s, so the token must be renewed inside the run. The exchange is by hand: the adapter takes one fixed credential, so a fetch wrapper sets the Authorization header of each request, and renews the token 60 s before its end. @tanstack/ai-anthropic already sends an sk-ant-oat credential as a Bearer token with no x-api-key.
    • Errors: 400 for a malformed rule id or workspace id, or a missing field. Every refusal of the JWT or the rule is the same 401 with the text "Authentication failed". The real reason is only in the Console's authentication history. So our "clear reason" can say only: Anthropic refused the exchange, the likely causes, and where to read the reason.

    3. The AI Gateway path

    Not applicable. The gateway path (gatewayClaudeAdapter, cloudflareBindingFetch) is the forge-paid path: the Workers AI binding authenticates, and no key of the customer is used. There is no key for a federation to replace. Anthropic's docs do not mention a gateway. So a federation works on the direct path only (provider: 'anthropic').

    4. The subject of a factory run

    A CI job with idToken: true picks its own audience, so repository code can get a token with the audience https://api.anthropic.com and the subject repo:<owner>/<repo>:ref:<ref>. A factory token must have a shape that no CI job can get. Proposal:

    • sub: repo:<owner>/<repo>:factory. CI subjects end in :ref:<ref> or :pull_request, so they never match.
    • kind: factory. CI tokens have no kind, and app tokens have kind: app.
    • repository, repository_id, repository_owner, repository_owner_id, as in CI tokens.
    • aud: https://api.anthropic.com. exp: iat plus 300. A new jti each time.

    A rule for one repository: subject_prefix repo:acme/api:factory (exact, no *), audience https://api.anthropic.com, and claims { "repository_id": "<id>" }. The id does not change when a repository is renamed, so another repository that later takes the name does not pass. A rule for an organisation: claims { "kind": "factory", "repository_owner_id": "<id>" }.

    The docs must warn: subject_prefix repo:acme/* with no kind also matches the CI jobs of every repository of acme.

    5. A CI job with idToken: true

    It needs no forge change, by the docs. The job asks for a token with the audience https://api.anthropic.com, writes it to a file, and sets ANTHROPIC_FEDERATION_RULE_ID, ANTHROPIC_ORGANIZATION_ID and ANTHROPIC_IDENTITY_TOKEN_FILE. The job's egress needs api.anthropic.com. @anthropic-ai/sdk 0.135.0 reads these variables. So this part is docs only (a new page next to docs/ci/oidc-aws.md).

    One limit, the same as on GitHub Actions: the Anthropic token lives at most 600 s, and the SDK renews it from the file, where the old JWT is expired and used. A job that calls Claude for longer must write a new token to the file before each renewal.

    The decision: the web Worker cannot sign

    Only the runner Worker signs JWTs (RunnerKeys.signJwt). FactoryJobObject is in the runner, so factory runs are fine. Two callers are in the web Worker:

    • The check when a person saves a federation (the twin of checkProviderKey).
    • The PR chat and the architecture chat (PrChatObject), which call adapterFor with the same keys.

    Options:

    • A. Give WebKeys one narrow method that signs only a factory token: the keys Worker sets aud, kind: factory, exp and jti itself, and the caller gives only the repository. The web Worker still cannot sign a CI token or an app token. Same key, same signer. The check and the chats then work.
    • B. Keep the web Worker with no signing. The check runs in the runner (a new entrypoint that the web Worker calls). The chats refuse a federation, with a message that says to add a key.

    Not possible from here

    The screenshots of the Claude Console need an Anthropic organisation with admin access. The Console steps can be written from Anthropic's docs, and the screenshots added by a person.

  2. tombeckenham assignedtombeckenham

  3. Two changes to the findings above, made during the build

    A security review of the first commit found two weak points in point 4. Both are fixed in the pull request.

    • Audience. The factory token no longer has the audience https://api.anthropic.com. A CI job can ask for that audience, so a loose rule (repo:acme/*) would also take the token of a CI job, which runs repository code. The factory audience is now source-factory:anthropic, and the CI host refuses every audience that starts with source-factory:, as it refuses source-app:. A rule that names this audience never takes a CI token. A CI job that calls Claude itself still uses https://api.anthropic.com.
    • Ids. The subject repo:<owner>/<repo>:factory has names, and another repository can take a name after a rename. A rule must also pin repository_id (or repository_owner_id for an organization). REST and src do not show these ids, so the Provider keys form now shows what the rule must match, with the id.

    The decision on the web Worker: option A. WebKeys and RunnerKeys get signFactoryJwt, and the keys Worker sets every claim but the names and ids.

Sign in to join this conversation.