# Workload identity

> Agents, and services that ask for transaction tokens, prove who they are with their platform's own SPIFFE identity, so no key or secret is registered for them or handed to them.

Workload identity lets an agent sign in to your realm with the identity its platform already gives it. Where each
workload has a SPIFFE identity, for example under SPIRE, the workload sends that identity's signed token, its
JWT-SVID, in place of a key. You trust the platform once, and register no key and hand out no secret per workload.

**Who may use it today:** agents' clients, and the clients of services that ask for
[transaction tokens](../../applications/tokens/transaction-tokens/). Any other app or service's client can't
authenticate with an SVID yet: give it a key instead
([Client authentication](../../applications/client-security/client-authentication/)).

A **SPIFFE ID** names one workload, such as `spiffe://prod.example.com/ns/agents/sa/invoice-agent`. Its first part,
`prod.example.com`, is the **trust domain**: the platform that vouches for it.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`); `RealmViewer` can read the settings but not
  change them.
- **Where:** today this is set through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
- **First:** the agent is registered ([AI agents](../ai-agents/)), or the service's client has `issuesTxnTokens`; and
  your platform issues JWT-SVIDs with a `jti` (under SPIRE, `-jwtSVIDIncludeJTI` on the workload entry).

## How it works

The agent fetches an SVID from its platform, addressed to the realm's issuer, and sends it as its client assertion
with its `client_id`:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -H "DPoP: <proof>" \
  -d grant_type=client_credentials \
  -d scope=invoices:read \
  -d client_id=invoice-agent-client \
  -d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-spiffe \
  -d client_assertion=eyJhbGciOiJFUzI1NiIsImtpZCI6InByb2QtMSJ9.eyJzdWIiOi….…
```

The SVID's claims:

```json
{
  "sub": "spiffe://prod.example.com/ns/agents/sa/invoice-agent",
  "aud": ["https://foobar.cvyuh.com/oauth2"],
  "iat": <unix time>,
  "exp": <unix time, at most 300 seconds later>,
  "jti": "1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
}
```

An agent's client is held to `fapi2` by default, so the request also carries a
[DPoP](../../applications/client-security/dpop/) proof.

## Set it up

Name the trust domains the realm trusts in its `spiffeTrustDomains` (on `oauth2Settings`), then give the agent's
client `assertionTrust: spiffe` and the one `spiffeId` it answers for. Each call sends your session as
`$CVYUH_SESSION` ([Call the API](/develop/api/)):

```sh
curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "spiffeTrustDomains": {
          "prod.example.com": { "bundleEndpoint": "https://spiffe.prod.example.com/bundle" } } }'

curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=invoice-agent-client" \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "status": "active",
        "tokenEndpointAuthMethod": "private_key_jwt", "assertionTrust": "spiffe",
        "spiffeId": "spiffe://prod.example.com/ns/agents/sa/invoice-agent",
        "grantTypes": ["client_credentials"], "scopes": ["invoices:read"],
        "allowedResources": ["https://app.example.com/api"],
        "defaultAudience": "https://app.example.com/api" }'
```

Then link the client to its agent, as in [AI agents](../ai-agents/#set-it-up).

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| SPIFFE IDs per client | One | One |
| An SVID's life (`clientAssertionMaxLifetimeSecs`, plus `clockSkewSecs`) | 300 seconds, plus 60 | As the realm's client assertions |
| Signing algorithms (`tokenEndpointAuthSigningAlgs`) | `ES256`, `RS256`; `ES256` alone for an agent's `fapi2` client | The realm's list |
| Uses of one SVID | Once, by its `jti` | Once |
| Trust domains (`spiffeTrustDomains`) | None | As many as you name |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Every SVID is refused `invalid_client` | The client is neither an agent's nor one with `issuesTxnTokens` | Link the client to its agent, or give a plain service a key |
| The first SVID works and later ones are refused | Your platform reissues the same SVID, or SVIDs carry no `jti` | Have each fetch return a fresh SVID with a unique `jti` |
| An SVID is refused after a key change at your platform | The realm still holds the trust domain's old keys inline | Use a `bundleEndpoint`, or update `jwks` |
| An SVID signed `RS256` is refused for an agent | The agent's client is `fapi2`, which takes `ES256` or `PS256` | Sign with `ES256` |

## Audit record

Each token request is a `TOKEN_ISSUED` record, issued or refused, keyed by the agent the token is about or, for a
refusal, by the client that asked. Changes to `spiffeTrustDomains` and to the client are in the change history:
**Audit → Config** in the dashboard.

## Good to know

- **One workload per client.** The agent's client names one SPIFFE ID, and only an SVID for exactly that ID
  authenticates it.
- **Only trust domains the realm names.** Each one's keys come inline, or from its bundle endpoint, which must be
  `https` and is fetched over the public internet. An SVID from any other trust domain, or signed by a key outside the
  bundle, is refused `invalid_client`.
- **Only the algorithms the realm accepts.** The SVID is signed with an algorithm in the realm's
  `tokenEndpointAuthSigningAlgs`. An agent's client is `fapi2` by default, which narrows that list to ES256 and PS256,
  so by default an SVID must be signed ES256.
- **Each SVID is used once.** An SVID must carry a `jti`, and the same one is refused the second time, as every client
  assertion is. One without a `jti` is refused. Under SPIRE, register the agent's workload entry with
  `-jwtSVIDIncludeJTI`: each SVID then carries a unique `jti`, and every fetch returns a fresh one.
- **It must be addressed to the realm alone:** its `aud` is the issuer, or a list holding only the issuer. Its `exp` must lie no
  further ahead than the realm allows a client assertion: `clientAssertionMaxLifetimeSecs`, 300 seconds by default,
  plus the realm's clock skew (`clockSkewSecs`, 60). An `iat` or `nbf`, if present, must not be in the future.
- **Only as `jwt-spiffe`.** The same SVID sent as an ordinary `jwt-bearer` assertion is refused `invalid_client`.
- **From there it's any agent's client:** the agent must be active, and its tokens follow its security profile, DPoP
  included.
- **A workload that asks for transaction tokens may use it too,** with no agent: an app with `issuesTxnTokens`. Its
  transaction tokens name it by its SPIFFE ID (`req_wl`). See [Transaction tokens](../../applications/tokens/transaction-tokens/).
- **It registers no key.** A client under `spiffe` trust has no `jwks` or `jwksUri`: its SVID is its one credential,
  and a client written with a key beside it is refused.
- **Dynamic registration doesn't offer it:** a registered client is never an agent's.

## Coming next

A workload signs in with its Kubernetes service-account token, checked by its cluster, in place of an SVID.

## Reference

### Settings

| Where | Setting | What it does |
|---|---|---|
| Realm | `spiffeTrustDomains` | The trust domains whose SVIDs are trusted, each by its keys (`jwks`) or its `https` `bundleEndpoint`, one or the other |
| Realm | `tokenEndpointAuthSigningAlgs` | The algorithms an SVID may be signed with (`ES256`, `RS256` by default). A `fapi2` client uses only the ES256 and PS256 among them |
| Client | `tokenEndpointAuthMethod` `private_key_jwt`, and `agent` naming its agent | The client is an agent's, and authenticates by a signed assertion |
| Client | `assertionTrust` `spiffe`, `spiffeId` | The agent's, or the transaction-token workload's, client is authenticated by the SVID of this one SPIFFE ID, and registers no key |

### Errors

**`invalid_client`** (401): the SVID doesn't verify under its trust domain's keys, names another SPIFFE ID, isn't
addressed to the realm, has expired or lives too long, has no `jti` or was used before; or it was sent as
`jwt-bearer`.

## Related

- [AI agents](../ai-agents/): the agents that sign in this way.
- [Client authentication](../../applications/client-security/client-authentication/): every way a client proves itself.
- [Transaction tokens](../../applications/tokens/transaction-tokens/): what a workload trades its token for.
- [DPoP](../../applications/client-security/dpop/): binding an agent's tokens to its key.
