{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agenthandelstandard.se/schemas/mandate.schema.json",
  "title": "SACP mandate",
  "description": "Authority to commit a buyer to a financial obligation, separate from the technical ability to call the shop API. Carried as the payload of a signed JWS. Added in SACP 1.1.0-draft.",
  "type": "object",
  "required": [
    "sacp_mandate_version",
    "mandate_id",
    "level",
    "issuer",
    "principal",
    "agent",
    "scope",
    "limits",
    "revocation",
    "validity"
  ],
  "additionalProperties": false,
  "properties": {
    "sacp_mandate_version": {
      "const": "1.1"
    },
    "mandate_id": {
      "type": "string",
      "minLength": 1
    },
    "level": {
      "enum": [
        "consumer",
        "enterprise"
      ]
    },
    "issuer": {
      "type": "object",
      "required": [
        "id"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "format": "uri"
        },
        "kid": {
          "type": "string"
        },
        "jwks_uri": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    "principal": {
      "type": "object",
      "required": [
        "type",
        "id"
      ],
      "additionalProperties": false,
      "properties": {
        "type": {
          "enum": [
            "person",
            "organization"
          ]
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        }
      }
    },
    "delegated_by": {
      "type": "object",
      "required": [
        "id"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "role": {
          "type": "string"
        }
      }
    },
    "delegation_chain": {
      "type": "array",
      "maxItems": 5,
      "items": {
        "type": "object",
        "required": [
          "delegator",
          "delegate"
        ],
        "additionalProperties": false,
        "properties": {
          "delegator": {
            "type": "string"
          },
          "delegate": {
            "type": "string"
          },
          "max_per_transaction": {
            "type": "object",
            "required": [
              "amount",
              "currency"
            ],
            "additionalProperties": false,
            "properties": {
              "amount": {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
              },
              "currency": {
                "type": "string",
                "pattern": "^[A-Z]{3}$"
              }
            }
          },
          "not_after": {
            "type": "string",
            "format": "date-time"
          }
        }
      }
    },
    "agent": {
      "type": "object",
      "required": [
        "id",
        "key_thumbprint"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string"
        },
        "key_thumbprint": {
          "type": "string",
          "description": "RFC 7638 JWK thumbprint of the key the agent signs requests with."
        }
      }
    },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "categories": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "merchants": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "countries": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[A-Z]{2}$"
          }
        },
        "currencies": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[A-Z]{3}$"
          }
        }
      }
    },
    "limits": {
      "type": "object",
      "required": [
        "per_transaction"
      ],
      "additionalProperties": false,
      "properties": {
        "per_transaction": {
          "type": "object",
          "required": [
            "amount",
            "currency"
          ],
          "additionalProperties": false,
          "properties": {
            "amount": {
              "type": "string",
              "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
            },
            "currency": {
              "type": "string",
              "pattern": "^[A-Z]{3}$"
            }
          }
        },
        "per_period": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "period",
              "amount",
              "currency"
            ],
            "additionalProperties": false,
            "properties": {
              "period": {
                "enum": [
                  "day",
                  "week",
                  "month",
                  "quarter",
                  "year"
                ]
              },
              "amount": {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
              },
              "currency": {
                "type": "string",
                "pattern": "^[A-Z]{3}$"
              }
            }
          }
        },
        "max_transactions_per_period": {
          "type": "integer",
          "minimum": 1
        },
        "counter_uri": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    "approval_policy": {
      "type": "object",
      "required": [
        "ref"
      ],
      "additionalProperties": false,
      "properties": {
        "ref": {
          "type": "string",
          "format": "uri"
        },
        "sha256": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "thresholds": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "above",
              "currency",
              "require"
            ],
            "additionalProperties": false,
            "properties": {
              "above": {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
              },
              "currency": {
                "type": "string",
                "pattern": "^[A-Z]{3}$"
              },
              "require": {
                "type": "object",
                "required": [
                  "approvals"
                ],
                "additionalProperties": false,
                "properties": {
                  "approvals": {
                    "type": "integer",
                    "minimum": 1
                  },
                  "approver_roles": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "approver_ids": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "revocation": {
      "type": "object",
      "required": [
        "status_uri"
      ],
      "additionalProperties": false,
      "properties": {
        "status_uri": {
          "type": "string",
          "format": "uri"
        },
        "max_check_age_seconds": {
          "type": "integer",
          "minimum": 0,
          "default": 300
        }
      }
    },
    "validity": {
      "type": "object",
      "required": [
        "not_before",
        "not_after"
      ],
      "additionalProperties": false,
      "properties": {
        "not_before": {
          "type": "string",
          "format": "date-time"
        },
        "not_after": {
          "type": "string",
          "format": "date-time"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "level": {
            "const": "enterprise"
          }
        }
      },
      "then": {
        "required": [
          "delegated_by",
          "approval_policy"
        ],
        "properties": {
          "limits": {
            "required": [
              "per_transaction",
              "per_period"
            ]
          }
        }
      }
    }
  ],
  "$defs": {
    "MandateStatus": {
      "description": "Body returned by revocation.status_uri.",
      "type": "object",
      "required": [
        "mandate_id",
        "status",
        "as_of"
      ],
      "properties": {
        "mandate_id": {
          "type": "string"
        },
        "status": {
          "enum": [
            "active",
            "suspended",
            "revoked"
          ]
        },
        "as_of": {
          "type": "string",
          "format": "date-time"
        },
        "revoked_at": {
          "type": "string",
          "format": "date-time"
        },
        "reason": {
          "type": "string"
        }
      }
    }
  }
}
