# KBAC: Decide on a Delegation Chain with the X-IK-Token Header and $ik_token Claims

> Let a policy decide not only who the user is but through which agent the user is acting: a delegation token in X-IK-Token, minted by the IndyKite Token Service with an RFC 8693 exchange, is bound as $ik_token in the policy, next to the user token's claims as $token. The example grants CAN_SHARE on a managed document only when the call comes through the knightrider agent, shows the decision flip to false without the delegation token, proves that input_params cannot impersonate either token, and lists the 401 and 422 the platform returns for a mismatched subject and a mistyped prefix.

**Category:** KBAC  
**API:** KBAC  
**Tags:** KBAC Policy, authZEN, Delegation, X-IK-Token, Token Service, AI Agents, RFC 8693
**Last Updated:** 2026-10-23
**OpenAPI Endpoints:** /access/v1/evaluation, /access/v1/search/resource, /configs/v1/authorization-policies, /configs/v1/token-introspects, /capture/v1/nodes, /capture/v1/relationships
**Related Guides:** /guides/guide-authzen, /guides/guide-token-service, /guides/guide-token-introspect, /guides/guide-agent-gateway, /guides/guide-mcp

## Summary

An AuthZEN call can carry two user-side tokens besides the application agent credential:
- Authorization: Bearer <user token>, the end user, issued by your identity provider. Its claims are bound as $token in policies.
- X-IK-Token: <delegation token>, optional, minted by the IndyKite Token Service through an RFC 8693 token exchange. Its sub is the same user; its act claim names the agent acting for the user, and nested act claims the agents before it. Its claims are bound as $ik_token.

Both are validated through the project's Token Introspect configurations, so the Token Service needs one of its own. The platform then enforces that the two tokens describe the same subject: a delegation token whose sub differs from the user token's is refused with 401 before any policy runs.

In a policy, $token.<claim> and $ik_token.<path> are usable in the condition Cypher and in a filter. The dot path walks the chain: $ik_token.act.sub is the agent that made this call, $ik_token.act.act.sub the one before it. The names token and ik_token are reserved: the platform binds them from the request headers and replaces anything a caller puts under those names in context.input_params. A token that was not sent binds an empty claim set, so a comparison against $ik_token.act.sub is false and the decision is false: a policy that mentions the delegation token fails closed without it.

For a rule over the whole chain the platform also derives two lists from the nested act claims, oldest hop first: $ik_token.act_chain, the sub of every hop, and $ik_token.act_chain_types, the type of every hop (null when a hop has none). A Cypher condition can compare them with a variable-length path in the graph, so a chain is allowed exactly when the graph has that path, in that order.

## Use Case

Scenario: karel manages docA. An AI agent, knightrider, shares documents on behalf of users, and the rule is that sharing a managed document is allowed only when it is done through that agent, never by the user's own client or by another agent.

1. Capture karel, docA and docB, with karel MANAGE docA.
2. Register two Token Introspect configurations: one for the identity provider that issues user tokens, one for the Token Service that issues delegation tokens.
3. Create the CAN_SHARE policy: the subject must manage the document, must be the user of the token, and $ik_token.act.sub must be "knightrider".
4. Exchange karel's token and the agent's token at the Token Service for a delegation token.
5. Evaluate with both tokens: true. Evaluate without X-IK-Token: false. Try to fake the chain through input_params: still false. Search the documents karel can share through the agent: docA only.
6. Two errors worth seeing once: a policy that mistypes the prefix ($iktoken) makes every evaluation of that action fail with 422, and a delegation token for another subject is refused with 401.
7. One step further: add the orchestrator and retriever agents with INVOKES edges (karel -> orchestrator -> retriever) and a CAN_INVOKE policy whose condition equals the whole chain to the path. A two-hop token invokes retriever but not orchestrator, a reversed chain is refused, and an input param cannot replace the chain.

## Requirements

Prerequisites:
- ServiceAccount credentials: For the Token Introspect configurations and the policies.
- AppAgent credentials: For the Capture calls and the AuthZEN calls (X-IK-ClientKey). The agent must hold Capture and Authorization.
- An identity provider that issues JWT access tokens for the user (karel) and for the agent (knightrider), with a Token Introspect configuration for its issuer and audience.
- An IndyKite Token Service you run (public container image indykite/token-service), configured to accept those tokens as subject_token and actor_token; see the Token Service guide for its configuration and for the Token Introspect configuration that makes the platform trust it.

Required API access:
- POST /configs/v1/token-introspects
- POST /configs/v1/authorization-policies
- POST /capture/v1/nodes, POST /capture/v1/relationships
- POST <ITS_BASE_URL>/oauth2/token
- POST /access/v1/evaluation, POST /access/v1/search/resource

## Steps

Step 1: Capture the graph
- Authentication: AppAgent credential (X-IK-ClientKey).
- Action: POST the nodes (karel as an identity node, docA, docB) and the MANAGE relationship from karel to docA.

Step 2: Trust both issuers
- Authentication: ServiceAccount credential.
- Action: POST two Token Introspect configurations: the identity provider (issuer and audience of the user and agent tokens) and the Token Service (its base URL as issuer, the same audience, its public JWK pinned in offline_validation.public_jwks).
- Result: the platform validates Authorization: Bearer through the first and X-IK-Token through the second. Allow a few minutes for a new configuration to apply.

Step 3: Create the policy
- Authentication: ServiceAccount credential.
- Action: POST the CAN_SHARE policy. Its condition joins the graph pattern (karel MANAGE the document) with two token comparisons: subject.external_id = $token.sub and $ik_token.act.sub = 'knightrider'.
- Result: an ACTIVE policy that can only ever be satisfied through the knightrider agent.

Step 4: Mint the delegation token
- Action: POST the token exchange to the Token Service with karel's token as subject_token and the agent's token as actor_token.
- Result: a short-lived JWT with sub karel and act.sub knightrider.

Step 5: Decide
- Authentication: AppAgent credential plus Authorization: Bearer (karel) plus X-IK-Token (the delegation token).
- Action: POST /access/v1/evaluation for karel, docA, CAN_SHARE.
- Result: {"decision": true}.
- Then the same call without X-IK-Token: {"decision": false}. The policy references $ik_token, nothing was sent, the comparison is false.
- Then the call without X-IK-Token but with ik_token and token objects in context.input_params: still {"decision": false}. The reserved names are bound from the headers, never from the body.
- Then POST /access/v1/search/resource with both tokens: results lists docA only.

Step 6: Recognise the two errors
- A policy whose Cypher says $iktoken (no underscore) is stored, but every evaluation of its action fails with 422 "missing or wrong input params, 'iktoken'": the unknown name is taken for an input parameter the caller did not send.
- A delegation token minted for another user, sent with karel's Authorization token, is refused with 401 "Token in X-IK-Token header carries a different sub than the subject token", before any policy is evaluated. The same check applies on the ContX IQ execute endpoint.

Step 7: Compare the whole chain with the graph
- Authentication: AppAgent credential for the Capture calls, ServiceAccount credential for the policy, both tokens for the decisions.
- Action: capture orchestrator and retriever as Agent nodes and the INVOKES edges karel -> orchestrator -> retriever. POST the CAN_INVOKE policy: MATCH (subject:Person)-[rels:INVOKES*]->(resource:Agent) WHERE [r IN rels | endNode(r).external_id] = $ik_token.act_chain AND [r IN rels | endNode(r).type] = $ik_token.act_chain_types. Mint a two-hop delegation token (orchestrator acts for karel, retriever acts for orchestrator; the Token Service nests the second exchange under the first).
- Result: karel CAN_INVOKE retriever is {"decision": true}: the chain [orchestrator, retriever] of types [Agent, Agent] equals the path.
- The same token for orchestrator is {"decision": false}: the chain is longer than the path karel -> orchestrator.
- A token with the hops in the other order, [retriever, orchestrator], is {"decision": false}: right agents, wrong order.
- A token whose hops carry no type claim derives act_chain_types as [null, null] and is {"decision": false} against a condition that compares types.
- Sending act_chain in context.input_params changes nothing: both lists come from the token only.
- POST /access/v1/search/resource for CAN_INVOKE on Agent with the two-hop token lists retriever only.

## Graph

The graph the decisions are made on: karel MANAGE docA, nothing links karel to docB. Whether karel may CAN_SHARE docA then depends on the delegation token, not on the graph.

- Person `karel` (identity): email=karel@example.com, name=Karel Plihal
- Document `docA`: title=Q4 forecast
- Document `docB`: title=Board minutes
- `karel` -[MANAGE]-> `docA`

## Code Examples

### Step 1

```bash
export API_URL=https://eu.api.indykite.com   # or https://us.api.indykite.com, the region of your project
export CLIENT_KEY="<application agent credential with the Authorization permission>"
export USER_TOKEN="<access token of the user karel, issued by your identity provider>"
export AGENT_TOKEN="<access token of the agent knightrider, issued by your identity provider>"
export ITS_BASE_URL="https://its.example.com"    # your IndyKite Token Service
export ITS_CLIENT_ID="<client id registered in the Token Service>"
export ITS_CLIENT_SECRET="<its secret>"
```

### Step 1

karel as an identity node, two documents.

**POST https://eu.api.indykite.com/capture/v1/nodes**

```json
{
  "nodes": [
    {
      "external_id": "karel",
      "type": "Person",
      "is_identity": true,
      "properties": [
        {
          "type": "email",
          "value": "karel@example.com"
        },
        {
          "type": "name",
          "value": "Karel Plihal"
        }
      ]
    },
    {
      "external_id": "docA",
      "type": "Document",
      "properties": [
        {
          "type": "title",
          "value": "Q4 forecast"
        }
      ]
    },
    {
      "external_id": "docB",
      "type": "Document",
      "properties": [
        {
          "type": "title",
          "value": "Board minutes"
        }
      ]
    }
  ]
}
```

### Step 1

karel manages docA. Nothing links karel to docB.

**POST https://eu.api.indykite.com/capture/v1/relationships**

```json
{
  "relationships": [
    {
      "source": {
        "external_id": "karel",
        "type": "Person"
      },
      "target": {
        "external_id": "docA",
        "type": "Document"
      },
      "type": "MANAGE"
    }
  ]
}
```

### Step 2

Token Introspect configuration for the identity provider that issues the user token (and the agent token the Token Service will take as actor). Keys are discovered from the issuer.

**POST https://eu.api.indykite.com/configs/v1/token-introspects**

```json
{
  "name": "idp-users",
  "project_id": "gid:YOUR_PROJECT_GID",
  "jwt_matcher": {
    "issuer": "https://your-idp.example.com/",
    "audience": "api.authz"
  },
  "offline_validation": {},
  "ikg_node_type": "Person",
  "perform_upsert": false
}
```

### Step 2

Token Introspect configuration for the Token Service: issuer is its base URL (https), audience the one the delegation token will carry, and its public JWK pinned. perform_upsert is false: the delegation token names an existing user, it must not create one.

**POST https://eu.api.indykite.com/configs/v1/token-introspects**

```json
{
  "name": "its-delegation",
  "project_id": "gid:YOUR_PROJECT_GID",
  "jwt_matcher": {
    "issuer": "https://its.example.com",
    "audience": "api.authz"
  },
  "offline_validation": {
    "public_jwks": [
      "{\"kty\":\"RSA\",\"alg\":\"RS256\",\"kid\":\"its-2026-q4\",\"use\":\"sig\",\"n\":\"...\",\"e\":\"AQAB\"}"
    ]
  },
  "ikg_node_type": "Person",
  "perform_upsert": false
}
```

### Step 3

The policy. $token.sub ties the subject to the bearer of the user token; $ik_token.act.sub requires the call to come through the knightrider agent. Both are bound by the platform from the request headers.

**policy.json**

```json
{
  "meta": {
    "policy_version": "2.0-kbac"
  },
  "subject": {
    "type": "Person"
  },
  "actions": [
    "CAN_SHARE"
  ],
  "resource": {
    "type": "Document"
  },
  "condition": {
    "cypher": "MATCH (subject:Person)-[:MANAGE]->(resource:Document) WHERE subject.external_id = $token.sub AND $ik_token.act.sub = 'knightrider'"
  }
}
```

### Step 3

The create request: the policy document is sent as a string in the policy field.

**POST https://eu.api.indykite.com/configs/v1/authorization-policies**

```json
{
  "project_id": "gid:YOUR_PROJECT_GID",
  "name": "share-when-delegated-to-knightrider",
  "display_name": "Share a managed document, only through the knightrider agent",
  "policy": "{\"meta\":{\"policy_version\":\"2.0-kbac\"},\"subject\":{\"type\":\"Person\"},\"actions\":[\"CAN_SHARE\"],\"resource\":{\"type\":\"Document\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[:MANAGE]->(resource:Document) WHERE subject.external_id = $token.sub AND $ik_token.act.sub = 'knightrider'\"}}",
  "status": "ACTIVE",
  "tags": []
}
```

### Step 4

Exchange karel's token (subject) and the agent's token (actor) for a delegation token at your Token Service.

**POST <ITS_BASE_URL>/oauth2/token**

```bash
# RFC 8693 token exchange: the user's token is the subject, the agent's token the actor.
# The result keeps the user's sub and nests the agent under act.
DELEGATION_TOKEN=$(curl -sS -u "$ITS_CLIENT_ID:$ITS_CLIENT_SECRET" -X POST "$ITS_BASE_URL/oauth2/token" \
  -d "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
  -d "subject_token=$USER_TOKEN" \
  -d "subject_token_type=urn:ietf:params:oauth:token-type:access_token" \
  -d "actor_token=$AGENT_TOKEN" \
  -d "actor_token_type=urn:ietf:params:oauth:token-type:access_token" \
  -d "audience=api.authz" | jq -r .access_token)
```


The claims of the delegation token: the user's sub, the agent under act. A second exchange with this token as subject_token would nest this act inside the next agent's.

```json

{
  "iss": "https://its.example.com",
  "sub": "karel",
  "aud": ["api.authz"],
  "iat": 1792000000,
  "exp": 1792000300,
  "jti": "…",
  "act": {
    "sub": "knightrider",
    "type": "Agent"
  }
}
```

### Step 5

Decision with both tokens.

**POST https://eu.api.indykite.com/access/v1/evaluation**

```bash
curl -sS -X POST "$API_URL/access/v1/evaluation" \
  -H "X-IK-ClientKey: $CLIENT_KEY" \
  -H "Authorization: Bearer $USER_TOKEN" \
  -H "X-IK-Token: $DELEGATION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "subject":  { "type": "Person", "id": "karel" },
    "resource": { "type": "Document", "id": "docA" },
    "action":   { "name": "CAN_SHARE" }
  }'
```


karel manages docA and acts through knightrider.

**Response 200**

```json
{
  "decision": true
}
```

### Step 5

The same decision without X-IK-Token.

**POST https://eu.api.indykite.com/access/v1/evaluation**

```bash
curl -sS -X POST "$API_URL/access/v1/evaluation" \
  -H "X-IK-ClientKey: $CLIENT_KEY" \
  -H "Authorization: Bearer $USER_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "subject":  { "type": "Person", "id": "karel" },
    "resource": { "type": "Document", "id": "docA" },
    "action":   { "name": "CAN_SHARE" }
  }'
```


No delegation token: $ik_token is an empty claim set, the comparison is false, the policy fails closed.

**Response 200**

```json
{
  "decision": false
}
```

### Step 5

Trying to supply the chain in the body instead of the header, without X-IK-Token. token and ik_token are reserved: the platform replaces them with what the headers carry, here nothing for ik_token.

**POST https://eu.api.indykite.com/access/v1/evaluation**

```json
{
  "subject": {
    "type": "Person",
    "id": "karel"
  },
  "resource": {
    "type": "Document",
    "id": "docA"
  },
  "action": {
    "name": "CAN_SHARE"
  },
  "context": {
    "input_params": {
      "ik_token": {
        "act": {
          "sub": "knightrider"
        }
      },
      "token": {
        "sub": "karel"
      }
    }
  }
}
```


Still false. The body cannot impersonate a token.

**Response 200**

```json
{
  "decision": false
}
```

### Step 5

Which documents may karel share through the agent?

**POST https://eu.api.indykite.com/access/v1/search/resource**

```bash
curl -sS -X POST "$API_URL/access/v1/search/resource" \
  -H "X-IK-ClientKey: $CLIENT_KEY" \
  -H "Authorization: Bearer $USER_TOKEN" \
  -H "X-IK-Token: $DELEGATION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "subject":  { "type": "Person", "id": "karel" },
    "resource": { "type": "Document" },
    "action":   { "name": "CAN_SHARE" }
  }'
```


docA only: docB is not managed by karel.

**Response 200**

```json
{
  "results": [
    {
      "type": "Document",
      "id": "docA"
    }
  ]
}
```

### Step 6

A policy with a mistyped prefix ($iktoken). It is accepted at creation.

**POST https://eu.api.indykite.com/configs/v1/authorization-policies**

```json
{
  "project_id": "gid:YOUR_PROJECT_GID",
  "name": "archive-mistyped-prefix",
  "display_name": "Archive (mistyped $iktoken prefix, for the error demo)",
  "policy": "{\"meta\":{\"policy_version\":\"2.0-kbac\"},\"subject\":{\"type\":\"Person\"},\"actions\":[\"CAN_ARCHIVE\"],\"resource\":{\"type\":\"Document\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[:MANAGE]->(resource:Document) WHERE $iktoken.act.sub = 'knightrider'\"}}",
  "status": "ACTIVE",
  "tags": []
}
```

### Step 6

Any evaluation of CAN_ARCHIVE then fails, with or without the delegation token.

**POST https://eu.api.indykite.com/access/v1/evaluation**

```json
{
  "subject": {
    "type": "Person",
    "id": "karel"
  },
  "resource": {
    "type": "Document",
    "id": "docA"
  },
  "action": {
    "name": "CAN_ARCHIVE"
  }
}
```


422: the unknown name is treated as an input parameter the caller did not send.

```json

HTTP/2 422

{
  "message": "Unprocessable Entity",
  "errors": [
    "missing or wrong input params, 'iktoken'"
  ]
}
```

### Step 6

A delegation token for another user, sent with karel's Authorization token, on any AuthZEN or ContX IQ endpoint.

```json

HTTP/2 401

{
  "message": "Token in X-IK-Token header carries a different sub than the subject token"
}
```

### Step 7

Two agents for the chain example.

**POST https://eu.api.indykite.com/capture/v1/nodes**

```json
{
  "nodes": [
    {
      "external_id": "orchestrator",
      "type": "Agent",
      "properties": [
        {
          "type": "name",
          "value": "Orchestrator"
        }
      ]
    },
    {
      "external_id": "retriever",
      "type": "Agent",
      "properties": [
        {
          "type": "name",
          "value": "Retriever"
        }
      ]
    }
  ]
}
```

### Step 7

The path the graph permits: karel INVOKES orchestrator, orchestrator INVOKES retriever.

**POST https://eu.api.indykite.com/capture/v1/relationships**

```json
{
  "relationships": [
    {
      "source": {
        "external_id": "karel",
        "type": "Person"
      },
      "target": {
        "external_id": "orchestrator",
        "type": "Agent"
      },
      "type": "INVOKES"
    },
    {
      "source": {
        "external_id": "orchestrator",
        "type": "Agent"
      },
      "target": {
        "external_id": "retriever",
        "type": "Agent"
      },
      "type": "INVOKES"
    }
  ]
}
```

### Step 7

The CAN_INVOKE policy: the agents along the INVOKES path, and their types, must equal $ik_token.act_chain and $ik_token.act_chain_types, hop for hop.

**POST https://eu.api.indykite.com/configs/v1/authorization-policies**

```json
{
  "project_id": "gid:YOUR_PROJECT_GID",
  "name": "invoke-along-the-delegation-chain",
  "display_name": "Invoke an agent only along the delegation chain the graph permits",
  "policy": "{\"meta\":{\"policy_version\":\"2.0-kbac\"},\"subject\":{\"type\":\"Person\"},\"actions\":[\"CAN_INVOKE\"],\"resource\":{\"type\":\"Agent\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[rels:INVOKES*]->(resource:Agent) WHERE [r IN rels | endNode(r).external_id] = $ik_token.act_chain AND [r IN rels | endNode(r).type] = $ik_token.act_chain_types\"}}",
  "status": "ACTIVE",
  "tags": []
}
```

### Step 7

The claims of a two-hop delegation token and the two lists the platform derives from them, oldest hop first.

```json

{
  "sub": "karel",
  "act": {
    "sub": "orchestrator",
    "type": "Agent",
    "act": {
      "sub": "retriever",
      "type": "Agent"
    }
  }
}
# derived by the platform, oldest hop first:
#   $ik_token.act_chain       = ["orchestrator", "retriever"]
#   $ik_token.act_chain_types = ["Agent", "Agent"]
```

### Step 7

Decision with the two-hop token: karel invokes retriever through orchestrator.

**POST https://eu.api.indykite.com/access/v1/evaluation**

```bash
curl -sS -X POST "$API_URL/access/v1/evaluation" \
  -H "X-IK-ClientKey: $CLIENT_KEY" \
  -H "Authorization: Bearer $USER_TOKEN" \
  -H "X-IK-Token: $DELEGATION_TOKEN_TWO_HOPS" \
  -H "Content-Type: application/json" \
  -d '{
    "subject":  { "type": "Person", "id": "karel" },
    "resource": { "type": "Agent", "id": "retriever" },
    "action":   { "name": "CAN_INVOKE" }
  }'
```


{"decision": true}, and what the other chains answer.

**Response 200**

```json

# token chain                 resource       decision   why
# [orchestrator, retriever]   retriever      true       equals the path karel -> orchestrator -> retriever
# [orchestrator, retriever]   orchestrator   false      chain longer than the path karel -> orchestrator
# [retriever, orchestrator]   retriever      false      right agents, wrong order
# hops without a type claim   retriever      false      types derive as [null, null]
# input_params.act_chain      any            unchanged  the lists come from the token only
```

## Common Errors

### 401: Token in X-IK-Token header carries a different sub than the subject token

**Solution:** The delegation token was minted for another user than the one in Authorization: Bearer. Exchange the right user token; the Token Service preserves the subject's sub.

### 401: Token in X-IK-Token header is missing the sub claim

**Solution:** The delegation token has no sub claim. Tokens from the IndyKite Token Service always carry one; a token from another issuer must too.

### 401: Invalid AppAgent JWT in X-IK-ClientKey header

**Solution:** The application agent credential is missing, malformed, expired or revoked. The user and delegation tokens are checked only after it.

### 422: missing or wrong input params, 'iktoken'

**Solution:** The policy references a name that is neither $token nor $ik_token, so the platform treats it as an ordinary input parameter the caller did not send. Fix the prefix in the policy rather than working around it: a caller could silence the 422 by sending iktoken in input_params with a value of their choosing, which is exactly what the reserved names prevent.

### 200: {"decision": false} although the chain is right

**Solution:** Check that X-IK-Token is sent as the raw JWT without a Bearer prefix, that a Token Introspect configuration matches its issuer and audience (allow a few minutes after creating one), and that the user token's sub equals the subject's external_id as the policy requires.

### 200: {"decision": false} although every agent in the chain is right

**Solution:** The derived lists are ordered oldest hop first and compared hop for hop with the path: check the order of the nested act claims, that the path in the graph has exactly as many hops as the chain, and, when the policy compares $ik_token.act_chain_types, that every hop carries a type claim equal to the node's type (a hop without one derives null).

---

Source: https://developer.indykite.com/resources/authz-10