{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://hummand.com.br/.well-known/humanproof/schema-1.json",
  "title": "humanproof/1",
  "description": "Recibo assinado de um ato da Hummand. Só hashes: nenhum campo carrega biometria, imagem ou contexto em claro. Campo desconhecido é erro.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "formato", "recibo_id", "ato_id", "tenant", "tipo", "nivel", "resultado", "motivo_codigo",
    "sujeito", "contexto", "consentimentos", "etapas", "fontes", "garantias", "mandato_ref",
    "encadeamento", "revisado_por", "politica_versao", "ts_criado", "ts_concluido", "prev_hash",
    "kid", "alg", "assinatura"
  ],
  "properties": {
    "formato": { "const": "humanproof/1" },
    "recibo_id": { "$ref": "#/$defs/id" },
    "ato_id": { "$ref": "#/$defs/id" },
    "tenant": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "nome_publico"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "nome_publico": { "type": "string", "minLength": 1, "maxLength": 200 }
      }
    },
    "tipo": { "type": "string", "pattern": "^[a-z_]+(\\.[a-z_]+)*$" },
    "nivel": { "type": "integer", "minimum": 0, "maximum": 3 },
    "resultado": { "enum": ["positivo", "negativo", "expirado", "cancelado", "falhou", "revogado"] },
    "motivo_codigo": { "type": ["string", "null"], "maxLength": 64 },
    "sujeito": {
      "type": "object",
      "additionalProperties": false,
      "required": ["ref_cliente_hash"],
      "properties": {
        "ref_cliente_hash": { "anyOf": [{ "$ref": "#/$defs/hash" }, { "type": "null" }] },
        "referencia": {
          "type": "object",
          "additionalProperties": false,
          "required": ["tipo", "origem", "hash"],
          "properties": {
            "tipo": { "type": "string", "maxLength": 32 },
            "origem": { "type": "string", "maxLength": 32 },
            "hash": { "$ref": "#/$defs/hash" }
          }
        }
      }
    },
    "contexto": {
      "type": "object",
      "additionalProperties": false,
      "required": ["hash", "chaves_declaradas"],
      "properties": {
        "hash": { "$ref": "#/$defs/hash" },
        "chaves_declaradas": { "type": "array", "items": { "type": "string", "maxLength": 64 } }
      }
    },
    "consentimentos": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["kind", "ts", "texto_hash", "versao"],
        "properties": {
          "kind": { "type": "string", "maxLength": 32 },
          "ts": { "$ref": "#/$defs/ts" },
          "texto_hash": { "$ref": "#/$defs/hash" },
          "versao": { "type": "string", "maxLength": 32 }
        }
      }
    },
    "etapas": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["nome", "resultado", "ts"],
        "properties": {
          "nome": { "type": "string", "maxLength": 32 },
          "resultado": { "type": "string", "maxLength": 32 },
          "ts": { "$ref": "#/$defs/ts" }
        }
      }
    },
    "fontes": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["fonte"],
        "properties": {
          "fonte": { "type": "string", "maxLength": 64 },
          "componente": { "type": "string", "maxLength": 64 },
          "versao": { "type": "string", "maxLength": 32 }
        }
      }
    },
    "garantias": {
      "type": "object",
      "additionalProperties": false,
      "required": ["humano_vivo", "mesma_pessoa_da_referencia", "identidade_civil", "consentiu", "biometria_retida"],
      "properties": {
        "humano_vivo": { "type": ["boolean", "null"] },
        "mesma_pessoa_da_referencia": { "type": ["boolean", "null"] },
        "identidade_civil": { "type": ["boolean", "null"] },
        "consentiu": { "type": ["boolean", "null"] },
        "biometria_retida": { "const": false }
      }
    },
    "mandato_ref": { "type": ["string", "null"], "maxLength": 128 },
    "encadeamento": {
      "type": ["object", "null"],
      "additionalProperties": false,
      "required": ["grupo", "posicao", "exige_humano_distinto"],
      "properties": {
        "grupo": { "$ref": "#/$defs/id" },
        "posicao": { "enum": [1, 2] },
        "exige_humano_distinto": { "type": "boolean" }
      }
    },
    "revisado_por": { "type": ["string", "null"], "maxLength": 128 },
    "politica_versao": { "type": "string", "minLength": 1, "maxLength": 64 },
    "ts_criado": { "$ref": "#/$defs/ts" },
    "ts_concluido": { "$ref": "#/$defs/ts" },
    "prev_hash": { "$ref": "#/$defs/hash" },
    "kid": { "type": "string", "minLength": 1, "maxLength": 64 },
    "alg": { "enum": ["ES256", "EdDSA"] },
    "assinatura": { "type": "string", "pattern": "^[A-Za-z0-9_-]{86}$" },
    "backfill": { "type": "boolean" },

    "escopo": {
      "type": "object",
      "additionalProperties": false,
      "required": ["acoes", "validade_ate", "revogavel"],
      "properties": {
        "acoes": { "type": "array", "items": { "type": "string", "maxLength": 64 } },
        "limites": {
          "type": "object",
          "additionalProperties": { "type": ["string", "integer", "boolean"] }
        },
        "validade_ate": { "$ref": "#/$defs/ts" },
        "revogavel": { "type": "boolean" }
      }
    },
    "mandatario": {
      "type": "object",
      "additionalProperties": false,
      "required": ["tipo", "ref_hash"],
      "properties": {
        "tipo": { "enum": ["pessoa", "agente"] },
        "ref_hash": { "$ref": "#/$defs/hash" }
      }
    },
    "documento_anexo": {
      "type": "object",
      "additionalProperties": false,
      "required": ["hash", "tipo_declarado"],
      "properties": {
        "hash": { "$ref": "#/$defs/hash" },
        "tipo_declarado": { "type": "string", "maxLength": 64 }
      }
    },
    "verificacao_mandato": {
      "type": "object",
      "additionalProperties": false,
      "required": ["valido", "dentro_escopo", "dentro_validade", "revogado"],
      "properties": {
        "valido": { "type": "boolean" },
        "dentro_escopo": { "type": "boolean" },
        "dentro_validade": { "type": "boolean" },
        "revogado": { "type": "boolean" }
      }
    },
    "recibos_individuais": {
      "type": "array",
      "minItems": 2,
      "maxItems": 2,
      "items": { "$ref": "#/$defs/id" }
    },
    "humanos_distintos": { "type": "boolean" },
    "gatilho": {
      "type": "object",
      "additionalProperties": false,
      "required": ["origem", "ref_hash"],
      "properties": {
        "origem": { "enum": ["cliente", "fonte"] },
        "ref_hash": { "$ref": "#/$defs/hash" }
      }
    },
    "assinatura_juridica": {
      "type": "object",
      "additionalProperties": false,
      "required": ["tipo", "cadeia_valida", "cert_serial_hash"],
      "properties": {
        "tipo": { "enum": ["icp-brasil", "gov.br", "avancada-humanproof"] },
        "cadeia_valida": { "type": "boolean" },
        "cert_serial_hash": { "$ref": "#/$defs/hash" }
      }
    }
  },
  "$defs": {
    "id": { "type": "string", "minLength": 1, "maxLength": 128 },
    "hash": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
    "ts": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]+)?Z$" }
  }
}
