{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aiwg.io/schemas/authorization-grant.aiwg.io/v1",
  "title": "Authorization grant v1",
  "description": "Bounded human grant carried inside a signed x.aiwg.authorization-grant effect. Authentication is the capturing host's responsibility. See docs/contracts/authorization-grant.v1.md.",
  "type": "object",
  "additionalProperties": false,
  "required": ["apiVersion", "grantId", "scope", "constraints", "grantedBy", "provenance", "questionDigest", "policyVersion", "createdAt", "expiresAt", "revocation"],
  "properties": {
    "apiVersion": { "const": "authorization-grant.aiwg.io/v1" },
    "grantId": { "$ref": "#/$defs/identifier" },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": ["categories", "targets", "environments", "resources", "verbs"],
      "properties": {
        "categories": { "$ref": "#/$defs/descriptors" },
        "targets": { "$ref": "#/$defs/descriptors" },
        "environments": {
          "type": "array", "minItems": 1, "uniqueItems": true,
          "items": { "enum": ["local", "development", "test", "testnet", "staging"] }
        },
        "resources": { "$ref": "#/$defs/descriptors" },
        "verbs": { "$ref": "#/$defs/descriptors" }
      }
    },
    "constraints": {
      "type": "object",
      "additionalProperties": false,
      "required": ["reversible", "maxImpact", "exclusions", "conditions"],
      "properties": {
        "reversible": { "const": true },
        "maxImpact": { "enum": ["low", "medium"] },
        "exclusions": { "const": ["secrets", "destructive", "production", "policy-override"] },
        "maxUses": { "type": "integer", "minimum": 1 },
        "conditions": { "$ref": "#/$defs/descriptorsOrEmpty" }
      }
    },
    "grantedBy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "id", "issuer", "authenticationRef"],
      "properties": {
        "kind": { "const": "human" },
        "id": { "$ref": "#/$defs/descriptor" },
        "issuer": { "$ref": "#/$defs/identifier" },
        "authenticationRef": { "$ref": "#/$defs/descriptor" }
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["provider", "sessionId", "userTurnDigest"],
      "properties": {
        "provider": { "$ref": "#/$defs/identifier" },
        "sessionId": { "$ref": "#/$defs/identifier" },
        "userTurnDigest": { "$ref": "#/$defs/digest" },
        "nativeEventRef": { "$ref": "#/$defs/descriptor" }
      }
    },
    "questionDigest": { "$ref": "#/$defs/digest" },
    "policyVersion": { "$ref": "#/$defs/identifier" },
    "createdAt": { "$ref": "#/$defs/timestamp" },
    "expiresAt": {
      "oneOf": [{ "$ref": "#/$defs/timestamp" }, { "const": "until-revoked" }]
    },
    "revocation": {
      "oneOf": [
        {
          "type": "object", "additionalProperties": false, "required": ["state"],
          "properties": { "state": { "const": "active" } }
        },
        {
          "type": "object", "additionalProperties": false, "required": ["state", "revokedAt", "decisionEventId"],
          "properties": {
            "state": { "const": "revoked" },
            "revokedAt": { "$ref": "#/$defs/timestamp" },
            "decisionEventId": { "$ref": "#/$defs/digest" }
          }
        }
      ]
    },
    "supersedes": { "$ref": "#/$defs/identifier" },
    "revokes": { "$ref": "#/$defs/identifier" }
  },
  "allOf": [
    {
      "if": { "properties": { "expiresAt": { "const": "until-revoked" } } },
      "then": { "properties": { "constraints": { "type": "object", "properties": { "maxImpact": { "const": "low" } } } } }
    }
  ],
  "$defs": {
    "identifier": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._:/@-]{0,255}$" },
    "descriptor": { "type": "string", "minLength": 1, "maxLength": 256, "pattern": "\\S" },
    "descriptors": { "type": "array", "minItems": 1, "maxItems": 64, "uniqueItems": true, "items": { "$ref": "#/$defs/descriptor" } },
    "descriptorsOrEmpty": { "type": "array", "maxItems": 64, "uniqueItems": true, "items": { "$ref": "#/$defs/descriptor" } },
    "digest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
    "timestamp": { "type": "string", "format": "date-time", "pattern": "Z$" }
  }
}
