# Transaction tokens

> Short-lived tokens that carry one request's identity, purpose and context through your internal services, never the caller's own token.

A transaction token carries one request through your internal services without carrying the caller's token. The
first service a request reaches trades the caller's access token for a short JWT that names the transaction, the user
or agent it is for, its purpose and the service that asked. Every service further in reads who the call is for and
why, and none of them can act as the caller.

Use it in systems with many internal hops, where passing the original token along is a risk, or wherever internal
services need to know who a call is for and why without being able to act as the caller.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) names the trust domain and marks each
  workload; `RealmViewer` can read both. The workloads then ask for transaction tokens themselves.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** each workload that asks can sign a `private_key_jwt` assertion, or holds a JWT-SVID
  ([Workload identity](../../../agents/workload-identity/)).

## How it works

The first service a request reaches is the **workload**. It exchanges the access token at the realm for a
transaction token, valid only inside your **trust domain**: the name you give the set of services that share these
tokens. Your internal services pass it along, in the `Txn-Token` header as the standard describes, and each one
checks it against the realm's published keys. The checking is your services' own: the realm issues and replaces
transaction tokens, and the [gateway](../../../../gateway/) doesn't read them.

**1. The workload asks**, authenticating with its key, and naming the trust domain as the `audience`, the purpose as
the `scope`, and the access token it received as the `subject_token`:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \
  -d requested_token_type=urn:ietf:params:oauth:token-type:txn_token \
  -d audience=orders.example.internal \
  -d scope=orders.read \
  -d subject_token=eyJ… \
  -d subject_token_type=urn:ietf:params:oauth:token-type:access_token \
  --data-urlencode 'request_details={"orderId":"A-1042"}' \
  --data-urlencode 'request_context={"ip":"203.0.113.7"}' \
  -d client_id=checkout-api \
  -d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
  -d client_assertion=eyJ…
```

```json
{
  "access_token": "eyJ…",
  "issued_token_type": "urn:ietf:params:oauth:token-type:txn_token",
  "token_type": "N_A",
  "expires_in": 60
}
```

The token is in `access_token`, as token exchange returns every token, but it isn't an access token: `token_type`
is `N_A`.

**2. The token** is a JWT signed `RS256`, with the header `typ` `txntoken+jwt`:

```json
{
  "iss": "https://foobar.cvyuh.com/oauth2",
  "aud": "orders.example.internal",
  "txn": "9d3f6c2a-71b4-4e0a-8a55-3c2e1f9b7d60",
  "sub": "2b7e4f10-8c3a-4d1e-9f6b-5a0c7d2e9b13",
  "scope": "orders.read",
  "req_wl": "checkout-api",
  "tctx": { "orderId": "A-1042" },
  "rctx": { "ip": "203.0.113.7" },
  "iat": <unix time>,
  "exp": <iat + 60>,
  "jti": "5c1e8a4b-2f7d-4b19-a0c3-6e9d4f2a8b15"
}
```

| Claim | What it says |
|---|---|
| `txn` | The transaction |
| `sub`, `act` | The subject, and the chain of who acts for whom, from the access token |
| `scope` | The purpose |
| `req_wl` | The workload that asked: its SPIFFE ID when it signed in by its SVID, else its client id |
| `tctx` | The details the workload sent in `request_details` |
| `rctx` | The context the workload sent in `request_context` |

**3. To swap**, a service further in sends the transaction token as the `subject_token`, with
`subject_token_type=urn:ietf:params:oauth:token-type:txn_token`, the same `audience`, and a `scope` no wider. It gets
a narrower token for its own calls.

## Set it up

These steps use the admin API; [Call the API](../../../../develop/api/) shows how to get a session.

1. Name your trust domain in `txnTokenTrustDomain` and add the token-exchange grant to the realm's `grantTypes`, with
   `PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`. Read the realm's settings first with `GET` on the same address, and send back the whole list with the new value
   added: **a list you send replaces the realm's list.**
2. On each workload's client (`https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`), set `issuesTxnTokens`, the token-exchange grant,
   and `tokenEndpointAuthMethod` `private_key_jwt`.

| Where | Setting | What it does |
|---|---|---|
| Realm | `txnTokenTrustDomain` | Your trust domain's name. Absent, no transaction token is issued |
| Realm | `txnTokenLifetimeSecs` | 60 by default, 300 at most |
| Realm and workload | `grantTypes` include `urn:ietf:params:oauth:grant-type:token-exchange` | Off by default |
| Workload app | `issuesTxnTokens`, with `tokenEndpointAuthMethod` `private_key_jwt` | Lets it ask (default off) |
| Workload app | `assertionTrust` `spiffe`, `spiffeId` | It proves itself by its SVID, registers no key, and is named by its SPIFFE ID |

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `txnTokenTrustDomain` | none: no transaction tokens | your trust domain's name |
| `txnTokenLifetimeSecs` | 60 | up to 300, and never past the token it came from |
| `request_details`, `request_context` | none | a JSON object each, at most 4,096 bytes |
| `issuesTxnTokens` on a client | off | per client, `private_key_jwt` only |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_target` | The realm names no trust domain, or `audience` isn't exactly it | Set `txnTokenTrustDomain`, and send it as `audience` |
| `unauthorized_client` | The client isn't marked `issuesTxnTokens` | Mark it, with `private_key_jwt` |
| `invalid_request` with a valid access token | It wasn't issued to this workload or for an API it serves, or no `scope` was sent | Exchange the token your workload received, and send `scope` |
| A downstream service refuses it as an access token | It isn't one | Verify it as a transaction token, against the realm's keys |

## Audit record

Every request, issued or refused, is a `TOKEN_EXCHANGED` record keyed by the user or agent the transaction is for,
with the access token's id as its parent and, for a refusal, its `reason`. It's on the subject's trail
([Read a user's or agent's trail](/audit-reports/audit-trail/read-a-trail/)) and in **Audit → Authentication**.

## Good to know

- **It is off until you name your trust domain.** The token's `aud` is exactly that name, and a token for any other
  domain is refused.
- **Only a workload you mark asks:** an app with `issuesTxnTokens`, which must prove itself with a key
  (`private_key_jwt`): one registered on it, or its JWT-SVID under `assertionTrust` `spiffe`
  ([workload identity](../../../agents/workload-identity/)).
- **The access token must be meant for the workload** (issued to it, or for a resource it serves), and still active.
- **The purpose is never wider than the access token's scope.** A `scope` must be sent, and an access token with no
  scope can't buy one. The access token must still be active as [introspection](../../revoke-access/introspection/) judges it, which
  includes its [governed scopes](../../../authorization/entitlement-scopes/) still being
  granted. No new entitlement or issuance-policy decision is made for the transaction token itself.
- **No actor token.** The chain of who acts for whom comes from the access token, and an `actor_token` is refused.
- **The caller's token is never inside it,** nor its app or key binding. `request_details` and `request_context` are
  each a JSON object of at most 4 KiB (4096 bytes).
- **It is short-lived:** a minute by default, at most five, and never longer than the token it came from. Revoking
  that token doesn't revoke a transaction token already issued from it, which ends at its own `exp`. Workloads
  verify it offline against the realm's keys, so there is nothing they could ask.
- **A swap keeps the transaction.** The transaction, subject, trust domain and details stay the same. The purpose may
  only narrow, and the context may gain entries but never change one. Each workload that asked is kept in order
  (`req_wl_chain`). A swapped token never outlives the one it replaced, and an expired one isn't swapped.
- **It is never an access token.** Presented as one, it is refused, and introspection answers it inactive.
- **Every exchange is recorded.** Issued or refused, it is on the subject's
  [audit trail](../../../../audit-reports/audit-trail/).

## Reference

### Errors

| Error | When |
|---|---|
| `unauthorized_client` | The client isn't a workload marked `issuesTxnTokens` |
| `invalid_target` | The realm names no trust domain, or `audience` isn't it |
| `invalid_scope` | A scope beyond the subject token's |
| `invalid_request` | No `scope`; an `actor_token` is sent; `request_details` or `request_context` isn't a JSON object or is over 4096 bytes; the subject token is missing, invalid, not meant for this workload, inactive or expired; on a swap, the transaction token is for another trust domain, or a `request_context` entry would change one it holds |

## Related

- [Token exchange](../token-exchange/): the exchange this builds on.
- [Workload identity](../../../agents/workload-identity/): a workload signs in by its SVID.
- [Token swap](/gateway/protect-apis/token-swap/): a gateway calls each upstream with a token
  meant for it.
