{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "schema_version": {
      "const": 1
    },
    "service": {
      "const": "csc-v1"
    },
    "listener": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "mode": {
          "enum": [
            "direct_https",
            "private_proxy"
          ]
        },
        "bind_address": {
          "type": "string",
          "minLength": 1,
          "maxLength": 15
        },
        "port": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        },
        "external_base_url": {
          "type": "string",
          "minLength": 1,
          "maxLength": 1024,
          "format": "uri",
          "pattern": "^https://"
        },
        "tls": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "file": {
              "type": "string",
              "minLength": 1,
              "maxLength": 4096
            },
            "password_file": {
              "type": "string",
              "minLength": 1,
              "maxLength": 4096
            },
            "alias": {
              "type": "string",
              "minLength": 1,
              "maxLength": 4096
            }
          },
          "required": [
            "file",
            "password_file",
            "alias"
          ]
        }
      },
      "required": [
        "mode",
        "bind_address",
        "port",
        "external_base_url"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "mode": {
                "const": "direct_https"
              }
            }
          },
          "then": {
            "required": [
              "tls"
            ]
          },
          "else": {
            "not": {
              "required": [
                "tls"
              ]
            }
          }
        }
      ]
    },
    "database": {
      "allOf": [
        {
          "$ref": "database.configuration.schema.json"
        },
        {
          "required": [
            "migration"
          ]
        }
      ],
      "description": "Distinct migration/runtime usernames required in all CSC engines; startup validates least privilege and exact owned tables."
    },
    "saml_transport": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "file": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096
        },
        "password_file": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096
        },
        "alias": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096
        },
        "encryption_alias": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096
        }
      },
      "required": [
        "file",
        "password_file",
        "alias",
        "encryption_alias"
      ]
    },
    "access_configuration": {
      "type": "string",
      "minLength": 1,
      "maxLength": 4096
    },
    "logo_url": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1024,
      "format": "uri",
      "pattern": "^https://"
    },
    "clients": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "client_id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "pattern": "^[!-~]+$"
          },
          "kid": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "pattern": "^[!-~]+$"
          },
          "public_key_file": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4096
          },
          "display_name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128
          },
          "redirect_uris": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 1024,
              "format": "uri",
              "pattern": "^https://"
            },
            "minItems": 1,
            "maxItems": 8,
            "uniqueItems": true
          },
          "connections": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
            },
            "minItems": 1,
            "maxItems": 16,
            "uniqueItems": true
          }
        },
        "required": [
          "client_id",
          "kid",
          "public_key_file",
          "display_name",
          "redirect_uris",
          "connections"
        ]
      },
      "minItems": 1,
      "maxItems": 64
    },
    "identities": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "connection": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
          },
          "subject_attribute": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4096
          },
          "authentication_context": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4096
          }
        },
        "required": [
          "connection",
          "subject_attribute",
          "authentication_context"
        ]
      },
      "minItems": 1,
      "maxItems": 16
    },
    "credentials": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "credential_id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 255,
            "pattern": "^[!-~]+$"
          },
          "subject_reference": {
            "type": "string",
            "minLength": 1,
            "maxLength": 69,
            "pattern": "^saml-[a-f0-9]{64}$"
          },
          "connection": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
          },
          "clients": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128
            },
            "minItems": 1,
            "maxItems": 64,
            "uniqueItems": true
          },
          "certificate_sha256": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64,
            "pattern": "^[a-f0-9]{64}$"
          },
          "enabled": {
            "type": "boolean"
          },
          "signing": {
            "oneOf": [
              {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "backend": {
                    "const": "pkcs12"
                  },
                  "credential": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 128,
                    "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
                  },
                  "keystore": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {
                      "file": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 4096
                      },
                      "password_file": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 4096
                      },
                      "alias": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 4096
                      }
                    },
                    "required": [
                      "file",
                      "password_file",
                      "alias"
                    ]
                  }
                },
                "required": [
                  "backend",
                  "credential",
                  "keystore"
                ]
              },
              {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "backend": {
                    "const": "pkcs11"
                  },
                  "credential": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 128,
                    "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
                  },
                  "module_file": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 4096
                  },
                  "module_sha256": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 64,
                    "pattern": "^[a-f0-9]{64}$"
                  },
                  "configuration_file": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 4096
                  },
                  "token_label": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 4096
                  },
                  "key_label": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 4096
                  },
                  "pin_file": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 4096
                  }
                },
                "required": [
                  "backend",
                  "credential",
                  "module_file",
                  "module_sha256",
                  "configuration_file",
                  "token_label",
                  "key_label",
                  "pin_file"
                ]
              }
            ]
          }
        },
        "required": [
          "credential_id",
          "subject_reference",
          "connection",
          "clients",
          "certificate_sha256",
          "enabled",
          "signing"
        ]
      },
      "minItems": 1,
      "maxItems": 256
    },
    "event_log": {
      "$ref": "event-log.configuration.schema.json"
    }
  },
  "required": [
    "schema_version",
    "service",
    "listener",
    "database",
    "saml_transport",
    "access_configuration",
    "logo_url",
    "clients",
    "identities",
    "credentials"
  ],
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:idombud:signing:csc-customer-configuration:v1",
  "title": "Signing Server CSC configuration",
  "description": "CSC service configuration. Global parser limits: 65536 UTF-8 bytes, 512 nodes and depth 12. References must resolve; keys must be distinct; native/certificate pins and protected paths must validate before startup."
}
