Factory: use Anthropic Workload Identity Federation in place of a static API key #654
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:
- A static key, as today.
- 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_IDandANTHROPIC_IDENTITY_TOKEN; the factory calls through TanStack AI's Anthropic adapter, so it may need the exchange by hand. - Whether the AI Gateway path (
cloudflareBindingFetchinmodels.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, andapi.anthropic.comin 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
srcset, 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.
tombeckenham assignedtombeckenham
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 nowsource-factory:anthropic, and the CI host refuses every audience that starts withsource-factory:, as it refusessource-app:. A rule that names this audience never takes a CI token. A CI job that calls Claude itself still useshttps://api.anthropic.com. - Ids. The subject
repo:<owner>/<repo>:factoryhas names, and another repository can take a name after a rename. A rule must also pinrepository_id(orrepository_owner_idfor an organization). REST andsrcdo 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.
WebKeysandRunnerKeysgetsignFactoryJwt, and the keys Worker sets every claim but the names and ids.- Audience. The factory token no longer has the audience
Sign in to join this conversation.
Findings of "Check first"#
Read: Anthropic's
authentication.md,wif-admin-api.md,workload-identity-federation.md,wif-reference.mdandwif-providers/github-actions.md(21 Nov 2026), againstjobs/oidc.ts,keys.ts,factory/models.ts,factory/providers.tsand@tanstack/ai-anthropic1.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#
audexactly (match.audience), and it is optional. Its GitHub example useshttps://api.anthropic.com. We use the same. It does not have thesource-app:form.sub,iat,exp, andissequal to the registered issuer URL.nbfis checked with 30 s of leeway. Our tokens have all of them.kidin the header is accepted.signJwtdoes this.<issuer>/.well-known/openid-configuration.https://source.tanstack.com/oidcfits, with no change.exp - iatat most 1 hour by default. Ours is 300 s.jti: a token with ajtiis 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#
POST https://api.anthropic.com/v1/oauth/token,content-type: application/json, no version header. Body:grant_typeurn:ietf:params:oauth:grant-type:jwt-bearer,assertion(the JWT),federation_rule_id(fdrl_...),organization_id(a UUID), andworkspace_id(wrkspc_...) when the rule covers more than one workspace.service_account_idis optional, and we do not send it.{ access_token: "sk-ant-oat01-...", token_type: "Bearer", expires_in, scope }. It goes on a model call asAuthorization: Bearer.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.fetchwrapper sets theAuthorizationheader of each request, and renews the token 60 s before its end.@tanstack/ai-anthropicalready sends ansk-ant-oatcredential as a Bearer token with nox-api-key.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: truepicks its own audience, so repository code can get a token with the audiencehttps://api.anthropic.comand the subjectrepo:<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 nokind, and app tokens havekind: app.repository,repository_id,repository_owner,repository_owner_id, as in CI tokens.aud:https://api.anthropic.com.exp:iatplus 300. A newjtieach time.A rule for one repository:
subject_prefixrepo:acme/api:factory(exact, no*),audiencehttps://api.anthropic.com, andclaims{ "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_prefixrepo:acme/*with nokindalso matches the CI jobs of every repository ofacme.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 setsANTHROPIC_FEDERATION_RULE_ID,ANTHROPIC_ORGANIZATION_IDandANTHROPIC_IDENTITY_TOKEN_FILE. The job'segressneedsapi.anthropic.com.@anthropic-ai/sdk0.135.0 reads these variables. So this part is docs only (a new page next todocs/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).FactoryJobObjectis in the runner, so factory runs are fine. Two callers are in the web Worker:checkProviderKey).PrChatObject), which calladapterForwith the same keys.Options:
WebKeysone narrow method that signs only a factory token: the keys Worker setsaud,kind: factory,expandjtiitself, 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.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.