{
  "openapi": "3.1.0",
  "info": {
    "title": "KMS API",
    "version": "1.0.0",
    "description": "REST API Key Management Service. Field biner (`plaintext`, `ciphertext`, `aad`) dikirim sebagai base64 standar. Body maksimal 64 KiB, field yang tidak dikenal ditolak. Setiap respons `/v1` memakai `Cache-Control: no-store`.\n\nKlasifikasi: INTERNAL."
  },
  "servers": [
    {
      "url": "/",
      "description": "Server KMS ini"
    }
  ],
  "tags": [
    {
      "name": "Kriptografi",
      "description": "Operasi yang dipakai API klien. Klien butuh izin per key per operasi."
    },
    {
      "name": "Keys",
      "description": "Siklus hidup key (admin)."
    },
    {
      "name": "Klien",
      "description": "API klien yang masuk dengan signed request (admin)."
    },
    {
      "name": "Izin",
      "description": "Izin per key per operasi untuk klien (admin). Klien baru tanpa akses."
    },
    {
      "name": "Audit",
      "description": "Audit log (admin)."
    },
    {
      "name": "Passkey",
      "description": "Login Web UI dengan passkey (WebAuthn). Aktif bila `KMS_PUBLIC_ORIGIN` di-set. Passkey pertama didaftarkan setelah masuk dengan API token; sejak itu API token tidak berlaku lagi."
    },
    {
      "name": "Sistem",
      "description": "Health check."
    }
  ],
  "paths": {
    "/healthz": {
      "get": {
        "tags": [
          "Sistem"
        ],
        "summary": "Health check",
        "description": "Selalu `200` selama proses berjalan. Tanpa autentikasi.",
        "operationId": "health",
        "security": [],
        "x-kms-access": "public",
        "responses": {
          "200": {
            "description": "Server berjalan.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string",
                      "const": "ok"
                    }
                  },
                  "additionalProperties": false,
                  "required": [
                    "status"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/v1/keys": {
      "post": {
        "tags": [
          "Keys"
        ],
        "summary": "Buat key",
        "description": "Membuat key baru (enabled, versi 1) dengan material acak yang dibungkus master key.",
        "operationId": "createKey",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "201": {
            "description": "Key dibuat.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Key"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid (JSON rusak, field tidak dikenal, nilai tidak valid).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Key disabled (`key is disabled`) atau konflik (nama/public key sudah dipakai).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateKeyRequest"
              }
            }
          }
        }
      },
      "get": {
        "tags": [
          "Keys"
        ],
        "summary": "Daftar key",
        "description": "Metadata semua key. Material key tidak pernah dikirim.",
        "operationId": "listKeys",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Daftar key.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "keys": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Key"
                      }
                    }
                  },
                  "additionalProperties": false,
                  "required": [
                    "keys"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/keys/{id}": {
      "get": {
        "tags": [
          "Keys"
        ],
        "summary": "Detail key",
        "description": "Metadata satu key. Klien butuh izin apa pun pada key ini.",
        "operationId": "getKey",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          },
          {
            "clientSignature": []
          }
        ],
        "x-kms-access": "admin, client",
        "responses": {
          "200": {
            "description": "Metadata key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Key"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key tidak ditemukan (`key not found`). Klien menerima 403, bukan 404.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "x-kms-permission": "any",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/keys/{id}/rotate": {
      "post": {
        "tags": [
          "Keys"
        ],
        "summary": "Rotate key",
        "description": "Membuat versi baru untuk enkripsi berikutnya. Ciphertext lama tetap bisa didekripsi (versi ada di header ciphertext).",
        "operationId": "rotateKey",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Key dengan versi baru.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Key"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Key disabled (`key is disabled`) atau konflik (nama/public key sudah dipakai).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key tidak ditemukan (`key not found`). Klien menerima 403, bukan 404.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/keys/{id}/enable": {
      "post": {
        "tags": [
          "Keys"
        ],
        "summary": "Enable key",
        "description": "Mengaktifkan kembali key yang disabled.",
        "operationId": "enableKey",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Key setelah diubah.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Key"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key tidak ditemukan (`key not found`). Klien menerima 403, bukan 404.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/keys/{id}/disable": {
      "post": {
        "tags": [
          "Keys"
        ],
        "summary": "Disable key",
        "description": "Key disabled menolak semua operasi kriptografi (409) sampai di-enable lagi.",
        "operationId": "disableKey",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Key setelah diubah.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Key"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key tidak ditemukan (`key not found`). Klien menerima 403, bukan 404.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/keys/{id}/encrypt": {
      "post": {
        "tags": [
          "Kriptografi"
        ],
        "summary": "Encrypt",
        "description": "Mengenkripsi plaintext (maks. ~48 KiB) dengan versi terbaru key. Untuk data besar pakai data key (envelope encryption).",
        "operationId": "encrypt",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          },
          {
            "clientSignature": []
          }
        ],
        "x-kms-access": "admin, client",
        "responses": {
          "200": {
            "description": "Ciphertext.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EncryptResponse"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid (JSON rusak, field tidak dikenal, nilai tidak valid).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Key disabled (`key is disabled`) atau konflik (nama/public key sudah dipakai).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Body lebih dari 64 KiB.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key tidak ditemukan (`key not found`). Klien menerima 403, bukan 404.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "x-kms-permission": "encrypt",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/EncryptRequest"
              }
            }
          }
        }
      }
    },
    "/v1/keys/{id}/decrypt": {
      "post": {
        "tags": [
          "Kriptografi"
        ],
        "summary": "Decrypt",
        "description": "Mendekripsi ciphertext dari `encrypt` atau `datakey` milik key yang sama. `aad` harus sama persis. Ciphertext rusak, key lain, atau AAD salah → `400 invalid ciphertext`.",
        "operationId": "decrypt",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          },
          {
            "clientSignature": []
          }
        ],
        "x-kms-access": "admin, client",
        "responses": {
          "200": {
            "description": "Plaintext.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DecryptResponse"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid (JSON rusak, field tidak dikenal, nilai tidak valid).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Key disabled (`key is disabled`) atau konflik (nama/public key sudah dipakai).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Body lebih dari 64 KiB.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key tidak ditemukan (`key not found`). Klien menerima 403, bukan 404.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "x-kms-permission": "decrypt",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/DecryptRequest"
              }
            }
          }
        }
      }
    },
    "/v1/keys/{id}/datakey": {
      "post": {
        "tags": [
          "Kriptografi"
        ],
        "summary": "Buat data key",
        "description": "Membuat data key AES-256 untuk envelope encryption: pakai `plaintext` untuk mengenkripsi data di aplikasi lalu buang dari memori; simpan hanya `ciphertext`. Body boleh kosong.",
        "operationId": "generateDataKey",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          },
          {
            "clientSignature": []
          }
        ],
        "x-kms-access": "admin, client",
        "responses": {
          "200": {
            "description": "Data key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DataKeyResponse"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid (JSON rusak, field tidak dikenal, nilai tidak valid).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Key disabled (`key is disabled`) atau konflik (nama/public key sudah dipakai).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key tidak ditemukan (`key not found`). Klien menerima 403, bukan 404.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "x-kms-permission": "datakey",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ],
        "requestBody": {
          "required": false,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/DataKeyRequest"
              }
            }
          }
        }
      }
    },
    "/v1/keys/{id}/reencrypt": {
      "post": {
        "tags": [
          "Kriptografi"
        ],
        "summary": "Re-encrypt",
        "description": "Dekripsi lalu enkripsi ulang **di dalam KMS** (plaintext tidak keluar) ke versi terbaru `destination_key_id` (default: key yang sama). `new_aad` (boleh `\"\"`) mengganti AAD; tanpa `new_aad`, AAD lama dipakai. Klien butuh `decrypt` pada key sumber dan `encrypt` pada key tujuan.",
        "operationId": "reEncrypt",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          },
          {
            "clientSignature": []
          }
        ],
        "x-kms-access": "admin, client",
        "responses": {
          "200": {
            "description": "Ciphertext baru.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ReEncryptResponse"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid (JSON rusak, field tidak dikenal, nilai tidak valid).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Key disabled (`key is disabled`) atau konflik (nama/public key sudah dipakai).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Body lebih dari 64 KiB.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key tidak ditemukan (`key not found`). Klien menerima 403, bukan 404.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "x-kms-permission": "decrypt (sumber) + encrypt (tujuan)",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ReEncryptRequest"
              }
            }
          }
        }
      }
    },
    "/v1/keys/{id}/grants": {
      "get": {
        "tags": [
          "Izin"
        ],
        "summary": "Klien yang punya akses ke key",
        "description": "Untuk review akses: klien mana saja yang boleh memakai key ini, dan operasinya.",
        "operationId": "listKeyGrants",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Daftar izin.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GrantList"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key tidak ditemukan (`key not found`). Klien menerima 403, bukan 404.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/audit": {
      "get": {
        "tags": [
          "Audit"
        ],
        "summary": "Audit log",
        "description": "Event terbaru dulu. Jika ada `next_before`, kirim sebagai `before` untuk halaman berikutnya. Tidak pernah berisi plaintext, material key, atau AAD.",
        "operationId": "listAudit",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Satu halaman event.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AuditPage"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid (JSON rusak, field tidak dikenal, nilai tidak valid).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "key_id",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Hanya event untuk key ini."
          },
          {
            "name": "action",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Hanya aksi ini (lihat `actions` di respons)."
          },
          {
            "name": "success",
            "in": "query",
            "schema": {
              "type": "boolean"
            },
            "description": "`true` hanya berhasil, `false` hanya gagal."
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 200,
              "default": 50
            },
            "description": "Jumlah event per halaman."
          },
          {
            "name": "before",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1
            },
            "description": "Cursor: hanya event dengan `id` lebih kecil."
          }
        ]
      }
    },
    "/v1/clients": {
      "post": {
        "tags": [
          "Klien"
        ],
        "summary": "Daftarkan klien",
        "description": "Mendaftarkan API klien dari public key PEM (Ed25519 atau ECDSA P-256). Klien baru **tanpa akses** sampai diberi izin. Private key ditolak dan tidak pernah disimpan.",
        "operationId": "createClient",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "201": {
            "description": "Klien terdaftar.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Client"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid (JSON rusak, field tidak dikenal, nilai tidak valid).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Key disabled (`key is disabled`) atau konflik (nama/public key sudah dipakai).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateClientRequest"
              }
            }
          }
        }
      },
      "get": {
        "tags": [
          "Klien"
        ],
        "summary": "Daftar klien",
        "description": "Semua klien terdaftar.",
        "operationId": "listClients",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Daftar klien.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "clients": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Client"
                      }
                    }
                  },
                  "additionalProperties": false,
                  "required": [
                    "clients"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/clients/{id}": {
      "get": {
        "tags": [
          "Klien"
        ],
        "summary": "Detail klien",
        "description": "Termasuk public key PEM.",
        "operationId": "getClient",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Klien.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Client"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Klien tidak ditemukan.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID klien (`cli_…`).",
            "schema": {
              "type": "string",
              "pattern": "^cli_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/clients/{id}/enable": {
      "post": {
        "tags": [
          "Klien"
        ],
        "summary": "Enable klien",
        "description": "Signature klien diterima lagi.",
        "operationId": "enableClient",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Klien setelah diubah.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Client"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Klien tidak ditemukan.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID klien (`cli_…`).",
            "schema": {
              "type": "string",
              "pattern": "^cli_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/clients/{id}/disable": {
      "post": {
        "tags": [
          "Klien"
        ],
        "summary": "Disable klien",
        "description": "Semua request bertanda tangan dari klien ini ditolak (401) sampai di-enable lagi. Izinnya tetap tersimpan.",
        "operationId": "disableClient",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Klien setelah diubah.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Client"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Klien tidak ditemukan.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID klien (`cli_…`).",
            "schema": {
              "type": "string",
              "pattern": "^cli_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/clients/{id}/grants": {
      "get": {
        "tags": [
          "Izin"
        ],
        "summary": "Izin klien",
        "description": "Key dan operasi yang boleh dipakai klien ini.",
        "operationId": "listClientGrants",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Daftar izin.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GrantList"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Klien tidak ditemukan.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID klien (`cli_…`).",
            "schema": {
              "type": "string",
              "pattern": "^cli_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/clients/{id}/grants/{key_id}": {
      "put": {
        "tags": [
          "Izin"
        ],
        "summary": "Set izin klien pada key",
        "description": "**Mengganti** semua operasi klien pada key ini (idempotent). Daftar tidak boleh kosong; untuk menghapus akses pakai `DELETE`.",
        "operationId": "setClientGrant",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Izin setelah disimpan.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Grant"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid (JSON rusak, field tidak dikenal, nilai tidak valid).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Klien tidak ditemukan (`client not found`) atau key tidak ditemukan.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID klien (`cli_…`).",
            "schema": {
              "type": "string",
              "pattern": "^cli_[0-9a-f]{32}$"
            }
          },
          {
            "name": "key_id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SetGrantRequest"
              }
            }
          }
        }
      },
      "delete": {
        "tags": [
          "Izin"
        ],
        "summary": "Cabut izin klien pada key",
        "description": "Menghapus semua operasi klien pada key ini. Request klien ke key ini langsung ditolak.",
        "operationId": "revokeClientGrant",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "204": {
            "description": "Izin dicabut."
          },
          "401": {
            "description": "Tidak terautentikasi: token admin salah, atau signature klien tidak valid (signature, timestamp, nonce, atau klien disabled).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Terautentikasi, tetapi tidak diizinkan: klien memanggil endpoint admin, atau tidak punya izin untuk operasi ini pada key ini (termasuk key yang tidak ada).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Klien tidak ditemukan atau tidak punya izin pada key ini (`grant not found`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID klien (`cli_…`).",
            "schema": {
              "type": "string",
              "pattern": "^cli_[0-9a-f]{32}$"
            }
          },
          {
            "name": "key_id",
            "in": "path",
            "required": true,
            "description": "ID key (`key_…`).",
            "schema": {
              "type": "string",
              "pattern": "^key_[0-9a-f]{32}$"
            }
          }
        ]
      }
    },
    "/v1/auth/status": {
      "get": {
        "tags": [
          "Passkey"
        ],
        "summary": "Cara masuk Web UI",
        "description": "Tanpa autentikasi. Memberi tahu apakah passkey aktif dan terdaftar, dan apakah API token masih diterima.",
        "operationId": "authStatus",
        "security": [],
        "x-kms-access": "public",
        "responses": {
          "200": {
            "description": "Status.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AuthStatus"
                }
              }
            }
          }
        }
      }
    },
    "/v1/auth/passkey/begin": {
      "post": {
        "tags": [
          "Passkey"
        ],
        "summary": "Mulai login passkey",
        "description": "Tanpa autentikasi; header `Origin` wajib sama dengan `KMS_PUBLIC_ORIGIN`. Mengirim cookie `kms_ceremony` (5 menit, sekali pakai) yang harus ikut di request berikutnya.",
        "operationId": "passkeyLoginBegin",
        "security": [],
        "x-kms-access": "public",
        "responses": {
          "200": {
            "description": "Opsi untuk `navigator.credentials.get()`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebAuthnRequestOptions"
                }
              }
            }
          },
          "403": {
            "description": "`Origin` tidak cocok.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Passkey tidak aktif (`KMS_PUBLIC_ORIGIN` kosong).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Belum ada passkey: masuk dengan API token.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Terlalu banyak login yang belum selesai.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/auth/passkey/finish": {
      "post": {
        "tags": [
          "Passkey"
        ],
        "summary": "Selesaikan login passkey",
        "description": "Tanpa autentikasi; butuh cookie `kms_ceremony` dari langkah begin dan header `Origin`. Memverifikasi tanda tangan, user verification (biometrik/PIN), origin dan counter. Berhasil: cookie session admin. Setiap kegagalan dicatat di audit (`PasskeyLogin`, gagal).",
        "operationId": "passkeyLoginFinish",
        "security": [],
        "x-kms-access": "public",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/WebAuthnCredential"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Masuk. Set-Cookie session.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "passkey": {
                      "$ref": "#/components/schemas/Passkey"
                    }
                  },
                  "required": [
                    "passkey"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Challenge kedaluwarsa atau sudah dipakai.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Verifikasi gagal.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "`Origin` tidak cocok.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Passkey tidak aktif.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/auth/logout": {
      "post": {
        "tags": [
          "Passkey"
        ],
        "summary": "Keluar",
        "description": "Mengakhiri session pemanggil dan menghapus cookie-nya. Tanpa autentikasi.",
        "operationId": "logout",
        "security": [],
        "x-kms-access": "public",
        "responses": {
          "204": {
            "description": "Keluar."
          },
          "403": {
            "description": "`Origin` tidak cocok.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/passkeys": {
      "get": {
        "tags": [
          "Passkey"
        ],
        "summary": "Daftar passkey",
        "operationId": "listPasskeys",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "responses": {
          "200": {
            "description": "Semua passkey, terlama dulu.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PasskeyList"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi, atau API token dipakai padahal sudah ada passkey.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Bukan admin, atau `Origin` tidak cocok pada request dengan cookie session.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": [
          "Passkey"
        ],
        "summary": "Selesaikan pendaftaran passkey",
        "description": "Butuh cookie `kms_ceremony` dari `POST /v1/passkeys/register`. Menyimpan passkey (hanya bila dibuat dengan user verification) dan memberi cookie session untuknya. **Sejak passkey pertama tersimpan, API token admin ditolak.** Dicatat di audit (`RegisterPasskey`).",
        "operationId": "registerPasskeyFinish",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/WebAuthnCredential"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Passkey tersimpan.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "passkey": {
                      "$ref": "#/components/schemas/Passkey"
                    },
                    "admin_token_accepted": {
                      "type": "boolean",
                      "const": false
                    }
                  },
                  "required": [
                    "passkey",
                    "admin_token_accepted"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Challenge kedaluwarsa, respons tidak valid, atau dibuat tanpa user verification.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi, atau API token dipakai padahal sudah ada passkey.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Bukan admin, atau `Origin` tidak cocok pada request dengan cookie session.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Passkey tidak aktif.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Passkey ini sudah terdaftar.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/passkeys/register": {
      "post": {
        "tags": [
          "Passkey"
        ],
        "summary": "Mulai pendaftaran passkey",
        "description": "Passkey pertama didaftarkan setelah masuk dengan API token; berikutnya dari session passkey. Mengirim cookie `kms_ceremony` (5 menit, sekali pakai) yang harus ikut di request berikutnya.",
        "operationId": "registerPasskeyBegin",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RegisterPasskeyRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Opsi untuk `navigator.credentials.create()`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebAuthnCreationOptions"
                }
              }
            }
          },
          "400": {
            "description": "Nama tidak valid.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Tidak terautentikasi, atau API token dipakai padahal sudah ada passkey.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Bukan admin, atau `Origin` tidak cocok pada request dengan cookie session.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Passkey tidak aktif.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Terlalu banyak pendaftaran yang belum selesai.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/passkeys/{id}": {
      "delete": {
        "tags": [
          "Passkey"
        ],
        "summary": "Hapus passkey",
        "description": "Session dari passkey itu langsung berakhir. Passkey terakhir tidak bisa dihapus lewat API (supaya API token tidak diam-diam aktif lagi): daftarkan yang lain dulu, atau jalankan `kms passkey reset` di host. Dicatat di audit (`DeletePasskey`).",
        "operationId": "deletePasskey",
        "security": [
          {
            "adminToken": []
          },
          {
            "adminSession": []
          }
        ],
        "x-kms-access": "admin",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID passkey (base64url).",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Dihapus."
          },
          "401": {
            "description": "Tidak terautentikasi, atau API token dipakai padahal sudah ada passkey.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Bukan admin, atau `Origin` tidak cocok pada request dengan cookie session.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Passkey tidak ditemukan.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Passkey terakhir.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "adminToken": {
        "type": "http",
        "scheme": "bearer",
        "description": "`Authorization: Bearer $KMS_API_TOKEN`. Untuk Web UI dan administrasi; tidak dibatasi izin. **Hanya berlaku selama belum ada passkey terdaftar**: setelah passkey pertama didaftarkan, token ini ditolak (`401 admin token disabled`) sampai `kms passkey reset` dijalankan di host."
      },
      "clientSignature": {
        "type": "apiKey",
        "in": "header",
        "name": "X-KMS-Signature",
        "description": "Signed request untuk API klien. Kirim 4 header: `X-KMS-Client-ID`, `X-KMS-Timestamp` (unix detik), `X-KMS-Nonce` (16–64 byte acak, base64url tanpa padding, baru setiap request), dan `X-KMS-Signature` (base64 standar).\n\nString yang ditandatangani (dipisah `\\n`, tanpa newline di akhir): `KMS-SIG-V1`, client id, timestamp, nonce, METHOD, path ter-escape[`?`query mentah], hex SHA-256 body.\n\nEd25519 menandatangani string itu langsung; ECDSA P-256 menandatangani SHA-256-nya (signature ASN.1 DER). Timestamp maksimal 5 menit dari jam server; nonce tidak boleh dipakai ulang."
      },
      "adminSession": {
        "type": "apiKey",
        "in": "cookie",
        "name": "__Host-kms_session",
        "description": "Session admin dari login passkey (WebAuthn, mis. Touch ID / Face ID) di Web UI. Cookie `HttpOnly`, `SameSite=Strict`, berlaku 8 jam, hilang saat server restart. Nama cookie `kms_session` (tanpa `__Host-`) bila `KMS_PUBLIC_ORIGIN` memakai http (localhost). Request selain GET/HEAD wajib membawa header `Origin` yang sama dengan `KMS_PUBLIC_ORIGIN`, kalau tidak `403`."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string",
            "description": "Pesan singkat, mis. `invalid ciphertext`, `forbidden`, `key is disabled`."
          }
        },
        "additionalProperties": false,
        "required": [
          "error"
        ]
      },
      "Key": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "examples": [
              "key_3f1c…"
            ]
          },
          "name": {
            "type": "string"
          },
          "state": {
            "type": "string",
            "enum": [
              "enabled",
              "disabled"
            ]
          },
          "current_version": {
            "type": "integer",
            "minimum": 1,
            "description": "Versi yang dipakai untuk enkripsi berikutnya."
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": false,
        "required": [
          "id",
          "name",
          "state",
          "current_version",
          "created_at",
          "updated_at"
        ],
        "description": "Metadata key. Material key tidak pernah keluar dari KMS."
      },
      "CreateKeyRequest": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_.-]{1,128}$",
            "examples": [
              "payments"
            ]
          }
        },
        "additionalProperties": false,
        "required": [
          "name"
        ]
      },
      "EncryptRequest": {
        "type": "object",
        "properties": {
          "plaintext": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Data yang dienkripsi (base64)."
          },
          "aad": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Opsional. Konteks (mis. `tenant:42`) yang wajib dikirim lagi saat decrypt."
          }
        },
        "additionalProperties": false,
        "required": [
          "plaintext"
        ]
      },
      "EncryptResponse": {
        "type": "object",
        "properties": {
          "ciphertext": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Ciphertext (format KMS v1: versi key ada di header)."
          },
          "key_version": {
            "type": "integer"
          }
        },
        "additionalProperties": false,
        "required": [
          "ciphertext",
          "key_version"
        ]
      },
      "DecryptRequest": {
        "type": "object",
        "properties": {
          "ciphertext": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Ciphertext dari encrypt/datakey."
          },
          "aad": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "AAD yang sama dengan saat encrypt."
          }
        },
        "additionalProperties": false,
        "required": [
          "ciphertext"
        ]
      },
      "DecryptResponse": {
        "type": "object",
        "properties": {
          "plaintext": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Plaintext."
          }
        },
        "additionalProperties": false,
        "required": [
          "plaintext"
        ]
      },
      "DataKeyRequest": {
        "type": "object",
        "properties": {
          "aad": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Opsional, diikat ke ciphertext data key."
          }
        },
        "additionalProperties": false
      },
      "DataKeyResponse": {
        "type": "object",
        "properties": {
          "plaintext": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Data key 32 byte. Rahasia: pakai lalu buang, jangan disimpan atau di-log."
          },
          "ciphertext": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Data key terenkripsi; simpan di samping data. Dekripsi dengan `/decrypt`."
          },
          "key_version": {
            "type": "integer"
          }
        },
        "additionalProperties": false,
        "required": [
          "plaintext",
          "ciphertext",
          "key_version"
        ]
      },
      "ReEncryptRequest": {
        "type": "object",
        "properties": {
          "ciphertext": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Ciphertext milik key `{id}`."
          },
          "aad": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "AAD saat ini."
          },
          "destination_key_id": {
            "type": "string",
            "description": "Opsional. Key tujuan; default key yang sama."
          },
          "new_aad": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Opsional. Kalau ada (boleh `\"\"`), mengganti AAD pada ciphertext baru."
          }
        },
        "additionalProperties": false,
        "required": [
          "ciphertext"
        ]
      },
      "ReEncryptResponse": {
        "type": "object",
        "properties": {
          "ciphertext": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "Ciphertext baru."
          },
          "key_id": {
            "type": "string",
            "description": "Key tujuan."
          },
          "key_version": {
            "type": "integer",
            "description": "Versi key tujuan."
          },
          "source_key_version": {
            "type": "integer",
            "description": "Versi ciphertext asal; bandingkan untuk tahu apakah versinya naik."
          }
        },
        "additionalProperties": false,
        "required": [
          "ciphertext",
          "key_id",
          "key_version",
          "source_key_version"
        ]
      },
      "AuditEvent": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "at": {
            "type": "string",
            "format": "date-time"
          },
          "action": {
            "type": "string"
          },
          "key_id": {
            "type": "string"
          },
          "key_version": {
            "type": "integer"
          },
          "success": {
            "type": "boolean"
          },
          "detail": {
            "type": "string",
            "description": "Pesan error, atau info seperti `from v1`. Tidak pernah berisi data rahasia."
          },
          "actor": {
            "type": "string",
            "description": "`admin` (API token), `passkey:<12 karakter awal ID passkey>` (session Web UI), `client:<id>`, `cli` (perintah di host), atau kosong (event lama)."
          }
        },
        "additionalProperties": false,
        "required": [
          "id",
          "at",
          "action",
          "success"
        ]
      },
      "AuditPage": {
        "type": "object",
        "properties": {
          "events": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/AuditEvent"
            }
          },
          "actions": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Semua nilai `action` yang valid untuk filter."
          },
          "next_before": {
            "type": "integer",
            "description": "Ada jika masih ada halaman berikutnya."
          }
        },
        "additionalProperties": false,
        "required": [
          "events",
          "actions"
        ]
      },
      "Client": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "examples": [
              "cli_9ed8…"
            ]
          },
          "name": {
            "type": "string"
          },
          "algorithm": {
            "type": "string",
            "enum": [
              "ed25519",
              "ecdsa-p256"
            ]
          },
          "fingerprint": {
            "type": "string",
            "description": "16 karakter pertama hex SHA-256 dari public key (DER)."
          },
          "public_key": {
            "type": "string",
            "description": "PEM `PUBLIC KEY`."
          },
          "state": {
            "type": "string",
            "enum": [
              "enabled",
              "disabled"
            ]
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": false,
        "required": [
          "id",
          "name",
          "algorithm",
          "fingerprint",
          "public_key",
          "state",
          "created_at",
          "updated_at"
        ]
      },
      "CreateClientRequest": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_.-]{1,128}$",
            "examples": [
              "billing-api"
            ]
          },
          "public_key": {
            "type": "string",
            "description": "PEM `PUBLIC KEY` (SPKI) Ed25519 atau ECDSA P-256, mis. hasil `openssl pkey -in client.key -pubout`."
          }
        },
        "additionalProperties": false,
        "required": [
          "name",
          "public_key"
        ]
      },
      "Operation": {
        "type": "string",
        "enum": [
          "encrypt",
          "decrypt",
          "datakey"
        ]
      },
      "Grant": {
        "type": "object",
        "properties": {
          "client_id": {
            "type": "string"
          },
          "key_id": {
            "type": "string"
          },
          "operations": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Operation"
            }
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": false,
        "required": [
          "client_id",
          "key_id",
          "operations",
          "updated_at"
        ]
      },
      "GrantList": {
        "type": "object",
        "properties": {
          "grants": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Grant"
            }
          },
          "operations": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Operation"
            },
            "description": "Semua operasi yang bisa diberikan."
          }
        },
        "additionalProperties": false,
        "required": [
          "grants",
          "operations"
        ]
      },
      "SetGrantRequest": {
        "type": "object",
        "properties": {
          "operations": {
            "type": "array",
            "minItems": 1,
            "items": {
              "$ref": "#/components/schemas/Operation"
            },
            "examples": [
              [
                "encrypt",
                "datakey"
              ]
            ]
          }
        },
        "additionalProperties": false,
        "required": [
          "operations"
        ]
      },
      "AuthStatus": {
        "type": "object",
        "properties": {
          "passkeys_configured": {
            "type": "boolean",
            "description": "`KMS_PUBLIC_ORIGIN` di-set: passkey bisa dipakai."
          },
          "passkeys_registered": {
            "type": "boolean",
            "description": "Ada minimal satu passkey."
          },
          "admin_token_accepted": {
            "type": "boolean",
            "description": "API token admin masih diterima (= belum ada passkey)."
          },
          "signed_in": {
            "type": "boolean",
            "description": "Request ini membawa session passkey yang masih berlaku."
          }
        },
        "required": [
          "passkeys_configured",
          "passkeys_registered",
          "admin_token_accepted",
          "signed_in"
        ],
        "additionalProperties": false
      },
      "Passkey": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "description": "Credential ID WebAuthn (base64url)."
          },
          "name": {
            "type": "string"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "last_used_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time",
            "description": "`null` bila belum pernah dipakai login."
          },
          "current": {
            "type": "boolean",
            "description": "Passkey dari session pemanggil."
          }
        },
        "required": [
          "id",
          "name",
          "created_at",
          "last_used_at",
          "current"
        ],
        "additionalProperties": false
      },
      "PasskeyList": {
        "type": "object",
        "properties": {
          "passkeys": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Passkey"
            }
          }
        },
        "required": [
          "passkeys"
        ],
        "additionalProperties": false
      },
      "RegisterPasskeyRequest": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64,
            "examples": [
              "MacBook Touch ID"
            ]
          }
        },
        "required": [
          "name"
        ],
        "additionalProperties": false
      },
      "WebAuthnCreationOptions": {
        "type": "object",
        "description": "`PublicKeyCredentialCreationOptions` dibungkus `{\"publicKey\": …}`; field biner base64url. Berikan ke `navigator.credentials.create()` setelah mengubah base64url ke ArrayBuffer. `residentKey` dan `userVerification` selalu `required`.",
        "properties": {
          "publicKey": {
            "type": "object"
          }
        },
        "required": [
          "publicKey"
        ]
      },
      "WebAuthnRequestOptions": {
        "type": "object",
        "description": "`PublicKeyCredentialRequestOptions` dibungkus `{\"publicKey\": …}`; field biner base64url. Berikan ke `navigator.credentials.get()`. `userVerification` selalu `required`.",
        "properties": {
          "publicKey": {
            "type": "object"
          }
        },
        "required": [
          "publicKey"
        ]
      },
      "WebAuthnCredential": {
        "type": "object",
        "description": "`PublicKeyCredential` hasil browser, diserialisasi seperti `PublicKeyCredential.toJSON()`: `id`, `rawId`, `type`, dan `response` dengan field biner base64url (`clientDataJSON`, plus `attestationObject` saat daftar atau `authenticatorData`, `signature`, `userHandle` saat login).",
        "properties": {
          "id": {
            "type": "string"
          },
          "rawId": {
            "type": "string"
          },
          "type": {
            "type": "string",
            "const": "public-key"
          },
          "response": {
            "type": "object"
          }
        },
        "required": [
          "id",
          "rawId",
          "type",
          "response"
        ]
      }
    }
  }
}
