{
  "id": "authz-3",
  "title": "KBAC: How do I return step-up authentication advice in an authorization response?",
  "description": "Demonstrates authZEN 'advice' - when authorization is denied due to insufficient authentication level, the response includes guidance on what authentication step-up is needed to gain access.",
  "category": "KBAC",
  "tags": [
    "KBAC Policy",
    "authZEN",
    "Step-Up Authentication",
    "Advice",
    "MFA",
    "Authentication Level",
    "Conditional Access"
  ],
  "api": "KBAC",
  "language": "Python",
  "lastUpdated": "2026-07-14",
  "openApiEndpoints": [
    "/capture/v1/nodes",
    "/capture/v1/relationships",
    "/configs/v1/authorization-policies",
    "/access/v1/evaluation"
  ],
  "relatedGuides": [
    "/guides/guide-authzen",
    "/guides/guide-dynamic-authz",
    "/guides/guide-sandbox"
  ],
  "commonErrors": [
    {
      "code": "401",
      "message": "UNAUTHENTICATED",
      "solution": "Check X-IK-ClientKey header contains valid AppAgent credentials"
    },
    {
      "code": "404",
      "message": "NOT_FOUND",
      "solution": "Verify the policy exists and the subject/resource nodes exist in the IKG"
    }
  ],
  "summary": "This example demonstrates authZEN \"advice\" responses for step-up authentication:\n\nWhat is advice?\nWhen authorization is denied, the response can include guidance on HOW to get access (not just \"denied\").\n\nExample flow:\n1. The application asks whether alice may service her laptop, without a user token\n2. The graph condition holds (alice OWNS the laptop), but the policy's filter on the token's exp claim fails, so KBAC denies and returns the advice written in the policy: insufficient_user_authentication, \"Authentication is missing or expired\"\n3. The application sends the user to authenticate\n4. The same request is repeated with alice's fresh user token in the Authorization header\n5. The filter now holds and the decision is true\n\nThis enables progressive authentication - users only need a valid, recent token for sensitive operations.\n\nPolicy version note: the policy in this example uses \"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.",
  "usecase": "Scenario: A user wants to service their laptop (e.g., wipe data, change settings).\n\nPolicy conditions:\n1. Person must OWN the Laptop (graph condition: MATCH (subject:Person)-[:OWNS]->(resource:Laptop))\n2. The user token's exp claim must be greater than a timestamp fixed in the policy (condition.filter: $token.exp > <timestamp>), i.e. the token must not be expired\n3. Because the policy is 2.0-kbac, a user token sent with the request must belong to the requested subject (alice); otherwise the decision is false\n\nWithout a user token:\n- Request: \"Can alice service airbook-xyz?\" with X-IK-ClientKey only\n- The graph condition holds, $token.exp is null, the filter fails\n- Response: {\"decision\": false, \"context\": {\"advice\": [{\"error\": \"insufficient_user_authentication\", \"error_description\": \"Authentication is missing or expired\", \"sub\": \"$token.exp\"}]}}\n\nWith alice's valid user token (Authorization: Bearer):\n- Request: the same body\n- Response: {\"decision\": true}\n\nThe advice is the map written by the policy author on the failing filter leaf; the application decides what to do with it (here: send the user to authenticate).",
  "requirements": "Prerequisites:\n- ServiceAccount credentials: For creating policies (Bearer token)\n- AppAgent credentials: For data ingestion and authorization queries (X-IK-ClientKey)\n- Auth0 (or similar) user token: For the user requiring access\n\nRequired API access:\n- POST /capture/v1/nodes/ and /capture/v1/relationships/ (graph data)\n- POST /configs/v1/authorization-policies (create policy)\n- POST /access/v1/evaluation (authorization check with advice)",
  "steps": "Step 1: Ingest Graph Data\n- Authentication: AppAgent credential (X-IK-ClientKey header)\n- Action: POST the shared AuthZEN dataset; the relevant part is Person(alice) -[OWNS]-> Laptop(airbook-xyz)\n- Result: Graph ready for authorization queries\n\nStep 2: Create Step-Up Policy\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST a 2.0-kbac policy with subject type Person, action CAN_SERVICE, resource type Laptop, cypher MATCH (subject:Person)-[:OWNS]->(resource:Laptop), and a condition.filter $token.exp > <timestamp> carrying an advice map (error, error_description, sub)\n- Result: Policy ID returned\n\nStep 3: Evaluation Without a User Token (Returns Advice)\n- Authentication: AppAgent credential (X-IK-ClientKey header) only\n- Request: POST /access/v1/evaluation, subject Person alice, resource Laptop airbook-xyz, action CAN_SERVICE\n- Response: {\"decision\": false} with context.advice containing the policy's advice map\n- The graph condition matched but the filter failed, so the advice is returned\n\nStep 4: Evaluation With alice's User Token (Grants Access)\n- Authentication: AppAgent credential + alice's user token (Authorization: Bearer header)\n- Request: the same evaluation request\n- Response: {\"decision\": true}\n- The token's exp claim satisfies the filter, and the token's subject is the requested subject\n\nStep 5: Cleanup\n- Action: DELETE policy configuration",
  "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 the nodes needed for this use case.",
      "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 the relationships needed for this use case.",
      "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": "KBAC Policy: a Person can service a Laptop they OWN, provided the user token's exp claim is greater than the timestamp in the filter. The advice map on the filter leaf is returned when the filter fails.",
      "filename": "policy.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "2.0-kbac"
        },
        "subject": {
          "type": "Person"
        },
        "actions": [
          "CAN_SERVICE"
        ],
        "resource": {
          "type": "Laptop"
        },
        "condition": {
          "cypher": "MATCH (subject:Person)-[:OWNS]->(resource:Laptop)",
          "filter": {
            "attribute": "$token.exp",
            "operator": ">",
            "value": 1795613722,
            "advice": {
              "error": "insufficient_user_authentication",
              "error_description": "Authentication is missing or expired",
              "sub": "$token.exp"
            }
          }
        }
      }
    },
    {
      "description": "Request to create the KBAC Policy configuration using REST.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "description of policy",
        "display_name": "policy name",
        "name": "policy-name",
        "policy": "{\"meta\":{\"policy_version\":\"2.0-kbac\"},\"subject\":{\"type\":\"Person\"},\"actions\":[\"CAN_SERVICE\"],\"resource\":{\"type\":\"Laptop\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[:OWNS]->(resource:Laptop)\",\"filter\":{\"attribute\":\"$token.exp\",\"operator\":\">\",\"value\":1795613722,\"advice\":{\"error\":\"insufficient_user_authentication\",\"error_description\":\"Authentication is missing or expired\",\"sub\":\"$token.exp\"}}}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "description": "Request to create the KBAC Policy configuration using REST.",
      "filename": "policy_request.py",
      "language": "Python",
      "code": "\nimport http.client\n\nconn = http.client.HTTPSConnection(\"eu.api.indykite.com\")\n\npayload = \"{\"description\": \"\",\n  \"display_name\": \"\",\n  \"name\": \"\",\n  \"policy\": \"\",\n  \"project_id\": \"\",\n  \"status\": \"ACTIVE\",\n  \"tags\": [\n    \"\"\n  ]}\"\nheaders = {\n    'Content-Type': \"application/json\",\n    'Authorization': \"YOUR_SECRET_TOKEN\"\n}\n\nconn.request(\"POST\", \"/configs/v1/authorization-policies\", payload, headers)\nres = conn.getresponse()\ndata = res.read()\n"
    },
    {
      "description": "Request to read the KBAC Policy configuration using REST.",
      "filename": "GET https://eu.api.indykite.com/configs/v1/authorization-policies/{id}",
      "language": "Json",
      "ingest": {
        "id": "your_policy_configuration_gid"
      }
    },
    {
      "description": "Request to read the KBAC Policy configuration using REST.",
      "filename": "policy_request.py",
      "language": "Python",
      "code": "\nimport http.client\n\nconn = http.client.HTTPSConnection(\"eu.api.indykite.com\")\nheaders = { 'Authorization': \"YOUR_SECRET_TOKEN\" }\nconn.request(\"GET\", \"/configs/v1/authorization-policies/{{id}}\", headers=headers)\nres = conn.getresponse()\ndata = res.read()\n"
    },
    {
      "step": "Step 3",
      "description": "Run a KBAC authZEN Evaluation without a user token: the graph condition holds, the filter on $token.exp fails, and the advice is returned.",
      "filename": "POST https://eu.api.indykite.com/access/v1/evaluation",
      "language": "Json",
      "ingest": {
        "subject": {
          "type": "Person",
          "id": "alice"
        },
        "resource": {
          "type": "Laptop",
          "id": "airbook-xyz"
        },
        "action": {
          "name": "CAN_SERVICE"
        }
      }
    },
    {
      "description": "Response to the KBAC authZEN Evaluation request without a user token: denied, with the policy's advice.",
      "filename": "Response 200",
      "language": "Json",
      "ingest": {
        "context": {
          "advice": [
            {
              "error": "insufficient_user_authentication",
              "error_description": "Authentication is missing or expired",
              "sub": "$token.exp"
            }
          ]
        },
        "decision": false
      }
    },
    {
      "step": "Step 4",
      "description": "Add the Person node access token in the headers:\n        key:Authorization value: Bearer access_token_value\n        Then run the same KBAC authZEN Evaluation.",
      "filename": "POST https://eu.api.indykite.com/access/v1/evaluation",
      "language": "Json",
      "ingest": {
        "subject": {
          "type": "Person",
          "id": "alice"
        },
        "resource": {
          "type": "Laptop",
          "id": "airbook-xyz"
        },
        "action": {
          "name": "CAN_SERVICE"
        }
      }
    },
    {
      "description": "The same evaluation using Python, with alice's user token in the Authorization header.",
      "filename": "evaluation.py",
      "language": "Python",
      "code": "\nimport http.client\n\nconn = http.client.HTTPSConnection(\"eu.api.indykite.com\")\n\npayload = \"{\n  \t\"subject\": {\"type\": \"Person\",\n        \"id\": \"alice\"}, \n    \"resource\": {\"type\": \"Laptop\",\n        \"id\": \"airbook-xyz\"},\n    \"action\": {\"name\": \"CAN_SERVICE\"}\n}\"\n\nheaders = {\n    'Content-Type': \"application/json\",\n    'Authorization': \"Bearer eyJh....\",\n    'X-IK-ClientKey': \"eyJh....\"\n}\n\nconn.request(\"POST\", \"/access/v1/evaluation\", payload, headers)\n\nres = conn.getresponse()\ndata = res.read()\n"
    },
    {
      "description": "Response to the KBAC authZEN Evaluation request with a valid user token: authorized.",
      "filename": "Response 200",
      "language": "Json",
      "ingest": {
        "decision": true
      }
    },
    {
      "step": "Step 5",
      "description": "Delete the KBAC Policies.",
      "filename": "DELETE https://eu.api.indykite.com/configs/v1/authorization-policies/{id}",
      "language": "Json",
      "ingest": {
        "id": "your_policy_configuration_gid"
      }
    },
    {
      "description": "Request to delete the KBAC Policies.",
      "filename": "del.py",
      "language": "Python",
      "code": "\nimport http.client\n\nconn = http.client.HTTPSConnection(\"eu.api.indykite.com\")\n\nheaders = { 'Authorization': \"Bearer ...\", 'Content-Type': \"application/json\" }\n\nconn.request(\"DELETE\", \"/configs/v1/authorization-policies/{id}\", headers=headers)\n\nres = conn.getresponse()\ndata = res.read()\n"
    }
  ],
  "links": {
    "html": "/resources/authz-3",
    "markdown": "/resources/authz-3.md",
    "openApi": "https://openapi.indykite.com/"
  }
}