{
  "id": "authz-6",
  "title": "KBAC: Token-Scope-Gated Read/Write Permissions",
  "description": "Gate KBAC actions on the bearer token's scope claim. CAN_READ requires scope 'cars.read', CAN_WRITE requires scope 'cars.write'. The graph relationship grants the base capability; the token scope decides which verbs are exercisable on this request.",
  "category": "KBAC",
  "tags": [
    "KBAC Policy",
    "authZEN",
    "Token Scope",
    "$token.scope",
    "Token Filter",
    "Scoped Access"
  ],
  "api": "KBAC",
  "language": "Json",
  "lastUpdated": "2026-07-14",
  "openApiEndpoints": [
    "/configs/v1/authorization-policies",
    "/configs/v1/token-introspects",
    "/access/v1/evaluation"
  ],
  "relatedGuides": [
    "/guides/guide-dynamic-authz",
    "/guides/guide-authzen",
    "/guides/guide-token-introspect"
  ],
  "commonErrors": [
    {
      "code": "401",
      "message": "invalid bearer token",
      "solution": "Confirm the Token Introspect configuration matches the issuer of the bearer token. The scope claim cannot be evaluated against an unverified token."
    },
    {
      "code": "200",
      "message": "decision: false unexpectedly",
      "solution": "Print the introspected token to confirm the scope claim is present and includes the expected value. The token filter does an IN check against the list of space-separated scopes."
    }
  ],
  "summary": "This example demonstrates KBAC policies that take both the graph and the bearer token's scope claim into account.\n\nPattern:\n- The condition.cypher establishes the relationship-based base permission (e.g., subject DRIVES resource).\n- The condition.filter narrows the action by inspecting $token.scope with the CONTAINS operator:\n  - CAN_READ requires the scope claim to contain 'cars.read'.\n  - CAN_WRITE requires the scope claim to contain 'cars.write'.\n- Two separate policies coexist; the runtime selects them by action.\n\nEffect: a single user identity (Knight Rider, who DRIVES KITT) can be authorized for CAN_READ today but denied CAN_WRITE because the issued bearer token does not include the cars.write scope. Re-authenticate with a wider-scoped token to gain CAN_WRITE without changing any policy.\n\nPolicy version note: the policies in this example use \"policy_version\": \"2.0-kbac\". The same policy JSON is also accepted with \"policy_version\": \"3.0-kbac\" (identical schema, raw-Cypher semantics; do not reference $subject_id or external properties). Only 3.0-kbac additionally accepts USE graph.byName() routing and CALL { } subqueries for composite / data-residency IKGs - see resources authz-7 and authz-8. Note for 3.0-kbac with bearer tokens: the token's subject must match the requested subject; mismatches are denied with 403 Forbidden.",
  "usecase": "Scenario: A vehicle service portal issues bearer tokens with scopes that match what the user can actually do this session.\n\n- Mechanic token: scope = \"cars.read cars.write\" -> can read AND modify car records.\n- Customer token: scope = \"cars.read\" -> can read their own car record, cannot modify it.\n\nPolicies:\n1. CAN_READ on Car when subject DRIVES car AND $token.scope contains 'cars.read'.\n2. CAN_WRITE on Car when subject DRIVES car AND $token.scope contains 'cars.write'.\n\nCalls:\n- Knight Rider with cars.read token: CAN_READ KITT -> true; CAN_WRITE KITT -> false.\n- Mechanic with full scopes: both -> true.\n\nThis is the cleanest place to put scope checks: in the policy condition, not in the client.",
  "requirements": "Prerequisites:\n- ServiceAccount credentials: For creating the policies.\n- AppAgent credentials: For calling /access/v1/evaluation.\n- A Token Introspect configuration so the API can verify the bearer token and extract its scope claim.\n- Graph data with DRIVES relationships.\n\nRequired API access:\n- POST /configs/v1/authorization-policies (one per action)\n- POST /access/v1/evaluation (caller passes the user's bearer token in the Authorization header)",
  "steps": "Step 1: Capture Graph Data\n- Action: POST Person and Car nodes plus DRIVES relationships (re-use the authZEN dataset).\n\nStep 2: Create the CAN_READ Policy\n- Action: POST a policy whose condition.filter requires $token.scope to CONTAIN 'cars.read'.\n\nStep 3: Create the CAN_WRITE Policy\n- Action: POST a similar policy requiring $token.scope to CONTAIN 'cars.write'.\n\nStep 4: Evaluate with a Narrow-Scope Token\n- Action: POST /access/v1/evaluation with action CAN_READ, sending Authorization: Bearer <token> where the token's scope claim contains only cars.read.\n- Result: authorized.\n\nStep 5: Evaluate the Same Subject for CAN_WRITE\n- Action: POST /access/v1/evaluation with action CAN_WRITE using the same narrow-scope token.\n- Result: denied - the scope check fails even though DRIVES exists.",
  "graph": {
    "caption": "The graph the example runs on: 9 nodes (4 Person, 2 Car, 1 Bus, 1 Ticket, 1 Laptop) and 8 relationships (DRIVES, HAS, FOR, OWNS). Hover a node for its properties, click it to highlight its neighbourhood.",
    "nodes": [
      {
        "external_id": "alice",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "alice@email.com"
          },
          {
            "type": "given_name",
            "value": "Alice"
          },
          {
            "type": "last_name",
            "value": "Smith"
          }
        ]
      },
      {
        "external_id": "knightrider",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "knightrider@demo.com"
          },
          {
            "type": "name",
            "value": "Michael Knight"
          }
        ]
      },
      {
        "external_id": "satchmo",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "satchmo@demo.com"
          },
          {
            "type": "name",
            "value": "Louis Armstrong"
          }
        ]
      },
      {
        "external_id": "karel",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "karel@demo.com"
          },
          {
            "type": "name",
            "value": "Karel Plihal"
          }
        ]
      },
      {
        "external_id": "kitt",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "manufacturer",
            "value": "pontiac"
          },
          {
            "type": "model",
            "value": "Firebird"
          }
        ]
      },
      {
        "external_id": "cadillacv16",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "manufacturer",
            "value": "Cadillac"
          },
          {
            "type": "model",
            "value": "V-16"
          }
        ]
      },
      {
        "external_id": "harmonika",
        "type": "Bus",
        "is_identity": false,
        "properties": [
          {
            "type": "manufacturer",
            "value": "Ikarus"
          },
          {
            "type": "model",
            "value": "280"
          }
        ]
      },
      {
        "external_id": "listek",
        "type": "Ticket",
        "is_identity": false,
        "properties": []
      },
      {
        "external_id": "airbook-xyz",
        "type": "Laptop",
        "is_identity": false,
        "properties": []
      }
    ],
    "relationships": [
      {
        "source": "knightrider",
        "target": "kitt",
        "type": "DRIVES",
        "properties": []
      },
      {
        "source": "satchmo",
        "target": "cadillacv16",
        "type": "DRIVES",
        "properties": []
      },
      {
        "source": "karel",
        "target": "listek",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "listek",
        "target": "harmonika",
        "type": "FOR",
        "properties": []
      },
      {
        "source": "karel",
        "target": "airbook-xyz",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "airbook-xyz",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "knightrider",
        "target": "kitt",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "cadillacv16",
        "type": "DRIVES",
        "properties": []
      }
    ]
  },
  "code": [
    {
      "step": "Step 1",
      "description": "Capture Person and Car nodes.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/nodes/",
      "language": "Json",
      "ingest": {
        "nodes": [
          {
            "external_id": "alice",
            "is_identity": true,
            "type": "Person",
            "properties": [
              {
                "type": "email",
                "value": "alice@email.com"
              },
              {
                "type": "given_name",
                "value": "Alice"
              },
              {
                "type": "last_name",
                "value": "Smith"
              }
            ]
          },
          {
            "external_id": "knightrider",
            "type": "Person",
            "is_identity": true,
            "properties": [
              {
                "type": "email",
                "value": "knightrider@demo.com"
              },
              {
                "type": "name",
                "value": "Michael Knight"
              }
            ]
          },
          {
            "external_id": "satchmo",
            "type": "Person",
            "is_identity": true,
            "properties": [
              {
                "type": "email",
                "value": "satchmo@demo.com"
              },
              {
                "type": "name",
                "value": "Louis Armstrong"
              }
            ]
          },
          {
            "external_id": "karel",
            "type": "Person",
            "is_identity": true,
            "properties": [
              {
                "type": "email",
                "value": "karel@demo.com"
              },
              {
                "type": "name",
                "value": "Karel Plihal"
              }
            ]
          },
          {
            "external_id": "kitt",
            "type": "Car",
            "is_identity": false,
            "properties": [
              {
                "type": "manufacturer",
                "value": "pontiac"
              },
              {
                "type": "model",
                "value": "Firebird"
              }
            ]
          },
          {
            "external_id": "cadillacv16",
            "type": "Car",
            "is_identity": false,
            "properties": [
              {
                "type": "manufacturer",
                "value": "Cadillac"
              },
              {
                "type": "model",
                "value": "V-16"
              }
            ]
          },
          {
            "external_id": "harmonika",
            "type": "Bus",
            "is_identity": false,
            "properties": [
              {
                "type": "manufacturer",
                "value": "Ikarus"
              },
              {
                "type": "model",
                "value": "280"
              }
            ]
          },
          {
            "external_id": "listek",
            "type": "Ticket",
            "is_identity": false
          },
          {
            "external_id": "airbook-xyz",
            "type": "Laptop",
            "is_identity": false
          }
        ]
      }
    },
    {
      "description": "Capture DRIVES relationships.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/relationships/",
      "language": "Json",
      "ingest": {
        "relationships": [
          {
            "source": {
              "external_id": "knightrider",
              "type": "Person"
            },
            "target": {
              "external_id": "kitt",
              "type": "Car"
            },
            "type": "DRIVES"
          },
          {
            "source": {
              "external_id": "satchmo",
              "type": "Person"
            },
            "target": {
              "external_id": "cadillacv16",
              "type": "Car"
            },
            "type": "DRIVES"
          },
          {
            "source": {
              "external_id": "karel",
              "type": "Person"
            },
            "target": {
              "external_id": "listek",
              "type": "Ticket"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "listek",
              "type": "Ticket"
            },
            "target": {
              "external_id": "harmonika",
              "type": "Bus"
            },
            "type": "FOR"
          },
          {
            "source": {
              "external_id": "karel",
              "type": "Person"
            },
            "target": {
              "external_id": "airbook-xyz",
              "type": "Laptop"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "airbook-xyz",
              "type": "Laptop"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "knightrider",
              "type": "Person"
            },
            "target": {
              "external_id": "kitt",
              "type": "Car"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "cadillacv16",
              "type": "Car"
            },
            "type": "DRIVES"
          }
        ]
      }
    },
    {
      "step": "Step 2",
      "description": "Policy: CAN_READ on Car requires the bearer token's scope claim to contain 'cars.read'.",
      "filename": "policy_read.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "2.0-kbac"
        },
        "subject": {
          "type": "Person"
        },
        "actions": [
          "CAN_READ"
        ],
        "resource": {
          "type": "Car"
        },
        "condition": {
          "cypher": "MATCH (subject:Person)-[:DRIVES]->(resource:Car)",
          "filter": {
            "operator": "CONTAINS",
            "attribute": "$token.scope",
            "value": "cars.read"
          }
        }
      }
    },
    {
      "description": "Request to create the CAN_READ policy.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "Authorize CAN_READ on Car only when the access token's scope claim contains 'cars.read'.",
        "display_name": "policy - scope cars.read",
        "name": "policy-scope-cars-read",
        "policy": "{\"meta\":{\"policy_version\":\"2.0-kbac\"},\"subject\":{\"type\":\"Person\"},\"actions\":[\"CAN_READ\"],\"resource\":{\"type\":\"Car\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[:DRIVES]->(resource:Car)\",\"filter\":{\"operator\":\"CONTAINS\",\"attribute\":\"$token.scope\",\"value\":\"cars.read\"}}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "step": "Step 3",
      "description": "Policy: CAN_WRITE on Car requires the bearer token's scope claim to contain 'cars.write'.",
      "filename": "policy_write.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "2.0-kbac"
        },
        "subject": {
          "type": "Person"
        },
        "actions": [
          "CAN_WRITE"
        ],
        "resource": {
          "type": "Car"
        },
        "condition": {
          "cypher": "MATCH (subject:Person)-[:DRIVES]->(resource:Car)",
          "filter": {
            "operator": "CONTAINS",
            "attribute": "$token.scope",
            "value": "cars.write"
          }
        }
      }
    },
    {
      "description": "Request to create the CAN_WRITE policy.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "Authorize CAN_WRITE on Car only when the access token's scope claim contains 'cars.write'.",
        "display_name": "policy - scope cars.write",
        "name": "policy-scope-cars-write",
        "policy": "{\"meta\":{\"policy_version\":\"2.0-kbac\"},\"subject\":{\"type\":\"Person\"},\"actions\":[\"CAN_WRITE\"],\"resource\":{\"type\":\"Car\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[:DRIVES]->(resource:Car)\",\"filter\":{\"operator\":\"CONTAINS\",\"attribute\":\"$token.scope\",\"value\":\"cars.write\"}}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "step": "Step 4",
      "description": "Evaluation: Knight Rider asks if he can CAN_READ KITT. Send Authorization: Bearer <token> where the token's scope claim contains cars.read (plus X-IK-ClientKey for the AppAgent).",
      "filename": "POST https://eu.api.indykite.com/access/v1/evaluation",
      "language": "Json",
      "ingest": {
        "subject": {
          "type": "Person",
          "id": "knightrider"
        },
        "resource": {
          "type": "Car",
          "id": "kitt"
        },
        "action": {
          "name": "CAN_READ"
        }
      }
    },
    {
      "description": "Response - authorized (DRIVES exists and the token scope contains cars.read).",
      "filename": "Response 200",
      "language": "Json",
      "ingest": {
        "decision": true
      }
    },
    {
      "step": "Step 5",
      "description": "Same subject and resource, but action is CAN_WRITE and the same token only carries cars.read. The condition.filter scope check denies.",
      "filename": "POST https://eu.api.indykite.com/access/v1/evaluation",
      "language": "Json",
      "ingest": {
        "subject": {
          "type": "Person",
          "id": "knightrider"
        },
        "resource": {
          "type": "Car",
          "id": "kitt"
        },
        "action": {
          "name": "CAN_WRITE"
        }
      }
    },
    {
      "description": "Response - denied. The DRIVES relationship is present but the scope check fails.",
      "filename": "Response 200",
      "language": "Json",
      "ingest": {
        "decision": false
      }
    },
    {
      "description": "Same CAN_READ evaluation in Python (token passed in the Authorization header).",
      "filename": "evaluate_scope_read.py",
      "language": "Python",
      "code": "\nimport http.client\nimport json\n\nconn = http.client.HTTPSConnection(\"eu.api.indykite.com\")\n\npayload = json.dumps({\n  \"subject\": {\"type\": \"Person\", \"id\": \"knightrider\"},\n  \"resource\": {\"type\": \"Car\", \"id\": \"kitt\"},\n  \"action\": {\"name\": \"CAN_READ\"}\n})\n\nheaders = {\n  'Content-Type': \"application/json\",\n  'X-IK-ClientKey': \"\",\n  # User access token whose scope claim must contain 'cars.read'\n  'Authorization': \"Bearer <token>\"\n}\n\nconn.request(\"POST\", \"/access/v1/evaluation\", payload, headers)\n\nres = conn.getresponse()\ndata = res.read()\n"
    }
  ],
  "links": {
    "html": "/resources/authz-6",
    "markdown": "/resources/authz-6.md",
    "openApi": "https://openapi.indykite.com/"
  }
}