{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://procurauth.github.io/procurauth/schemas/evaluation-context.schema.json",
  "title": "Evaluation context",
  "description": "The members that the protocol adds to the member context of an AuthZEN 1.0 evaluation. Merge them into the context of a request and into the context of a response. A receiver ignores each member that it does not know. The root schema accepts the context of a request and of a response. The members $defs.request_context and $defs.answer_context are stricter. A schema cannot check that a ceiling is declared.",
  "type": "object",
  "properties": {
    "usage": {
      "description": "In a request: the units of one use (protocol 6.4). In an answer: the units that the PDP took (protocol 6.12).",
      "type": "object",
      "required": ["units"],
      "properties": {
        "units": { "type": "integer", "minimum": 0, "maximum": 1 }
      }
    },
    "limits": {
      "description": "In a true answer: the ceilings that apply (protocol 7.3). It holds ceilings only. A budget value never appears in an answer.",
      "type": "object",
      "propertyNames": { "pattern": "^[a-z][a-z0-9_]{0,31}$" },
      "additionalProperties": { "type": "integer", "minimum": 0 }
    },
    "match": {
      "description": "In a request: node, the default, or subtree (protocol 4.4).",
      "enum": ["node", "subtree"]
    },
    "approval": {
      "description": "In a request: the effect of the operation, for the use of an action that needed an approval (protocol 17.1). The question never has op or view.",
      "type": "object",
      "additionalProperties": false,
      "required": ["effect"],
      "properties": {
        "effect": { "$ref": "#/$defs/effect" }
      }
    },
    "permission_only": {
      "description": "In a request: the value true marks the question of an other permission of an operation, not of its use action (protocol 5.5a). The PDP then ignores confirm (protocol 17.8). The question has no usage and no approval.",
      "const": true
    },
    "evaluation_id": {
      "description": "In a false answer with the reason approval_required or approval_pending, for a question with an effect: a text that only the PDP reads (protocol 17.1). The PEP sends it back in the submission of a request (protocol 17.4).",
      "type": "string",
      "minLength": 1,
      "maxLength": 1024
    },
    "reason": {
      "description": "In a false answer: the reason (protocol 11.1). The values that the protocol gives are unknown_subject, invalid_resource, invalid_request, approval_required, approval_pending, approval_denied, approval_expired and budget_exceeded. A PEP maps another text as a false answer with no reason (protocol 10.3).",
      "type": "string"
    },
    "retry_after": {
      "description": "In a false answer with the reason budget_exceeded: the seconds to wait, a whole number from 1 (protocol 11.1).",
      "type": "integer",
      "minimum": 1
    }
  },
  "$defs": {
    "effect": {
      "description": "The effect of an operation: the text sha256: and 64 lower-case hexadecimal digits (protocol 17.3).",
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "request_context": {
      "description": "The context of a question. The units are 1 (protocol 6.4). A question with usage has no match equal to subtree (protocol 6.11). A question with permission_only has no usage and no approval (protocol 5.5a).",
      "type": "object",
      "properties": {
        "usage": {
          "type": "object",
          "required": ["units"],
          "properties": { "units": { "const": 1 } }
        },
        "match": { "enum": ["node", "subtree"] },
        "approval": { "$ref": "#/properties/approval" },
        "permission_only": { "$ref": "#/properties/permission_only" }
      },
      "allOf": [
        {
          "if": { "required": ["usage"] },
          "then": {
            "not": {
              "required": ["match"],
              "properties": { "match": { "const": "subtree" } }
            }
          }
        },
        {
          "if": { "required": ["permission_only"] },
          "then": { "not": { "anyOf": [ { "required": ["usage"] }, { "required": ["approval"] } ] } }
        }
      ]
    },
    "answer_context": {
      "description": "The context of an answer. The reason budget_exceeded comes with retry_after (protocol 11.1).",
      "type": "object",
      "properties": {
        "usage": {
          "type": "object",
          "required": ["units"],
          "properties": { "units": { "enum": [0, 1] } }
        },
        "limits": { "$ref": "#/properties/limits" },
        "reason": { "type": "string" },
        "retry_after": { "type": "integer", "minimum": 1 },
        "evaluation_id": { "$ref": "#/properties/evaluation_id" }
      },
      "if": {
        "required": ["reason"],
        "properties": { "reason": { "const": "budget_exceeded" } }
      },
      "then": { "required": ["retry_after"] }
    }
  },
  "examples": [
    { "usage": { "units": 1 } },
    { "match": "subtree" },
    { "permission_only": true },
    { "limits": { "max_rows": 100, "max_duration": 30 }, "usage": { "units": 1 } },
    { "reason": "budget_exceeded", "retry_after": 12 },
    { "reason": "unknown_subject" },
    { "approval": { "effect": "sha256:0000000000000000000000000000000000000000000000000000000000000000" } },
    { "reason": "approval_required", "evaluation_id": "ev_3f9a" }
  ]
}
