{
  "info": {
    "_postman_id": "29ddcd48-b3ca-4d25-bef2-eebd3d18e02a",
    "name": "EDIN ID API",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
    "_exporter_id": "3345740"
  },
  "item": [
    {
      "name": "1. Get and save public key",
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "exec": [
              "pm.collectionVariables.set(\"public-key\", \"\");"
            ],
            "type": "text/javascript",
            "packages": {},
            "requests": {}
          }
        },
        {
          "listen": "test",
          "script": {
            "exec": [
              "pm.collectionVariables.set(\"public-key\", pm.response.text());"
            ],
            "type": "text/javascript",
            "packages": {},
            "requests": {}
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{domain}}/api/external/key",
          "host": [
            "{{domain}}"
          ],
          "path": [
            "api",
            "external",
            "key"
          ]
        }
      },
      "response": []
    },
    {
      "name": "2. Sign with company employee key",
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "exec": [
              "pm.collectionVariables.set(\"user-token\", \"\");",
              "const data = JSON.parse(pm.collectionVariables.get(\"public-key\"));",
              "",
              "const encrypted = await utils.rsaEncrypt(Uint8Array.from(data.key), pm.collectionVariables.get(\"employee_key_password\"));",
              "pm.collectionVariables.set(\"user-token\", encrypted);"
            ],
            "type": "text/javascript",
            "packages": {},
            "requests": {}
          }
        }
      ],
      "request": {
        "method": "POST",
        "header": [],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"key\": \"{{employee_key_uuid}}\",\n    \"password\": \"{{user-token}}\",\n    \"algorithm\": \"DSTU4145_GOST34311\",\n    \"hashes\": [\n        {\"hash\": \"{{sign_hash}}\", \"description\": \"sign description\"}\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{domain}}/api/external/company/sign",
          "host": [
            "{{domain}}"
          ],
          "path": [
            "api",
            "external",
            "company",
            "sign"
          ]
        }
      },
      "response": []
    },
    {
      "name": "3. Sign file with company employee key",
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "exec": [
              "pm.collectionVariables.set(\"user-token\", \"\");",
              "const data = JSON.parse(pm.collectionVariables.get(\"public-key\"));",
              "",
              "const encrypted = await utils.rsaEncrypt(Uint8Array.from(data.key), pm.collectionVariables.get(\"employee_key_password\"));",
              "pm.collectionVariables.set(\"user-token\", encrypted);"
            ],
            "type": "text/javascript",
            "packages": {},
            "requests": {}
          }
        }
      ],
      "request": {
        "method": "POST",
        "header": [],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "key",
              "value": "{{employee_key_uuid}}",
              "type": "text",
              "uuid": "fa73ed0b-0d4d-4864-84ed-5af176b3c78d"
            },
            {
              "key": "password",
              "value": "{{user-token}}",
              "type": "text",
              "uuid": "cc9f1076-a46b-4712-8d2c-aabc3cd6e266"
            },
            {
              "key": "file.pdf",
              "description": "file",
              "type": "file",
              "uuid": "2f1d1b13-ed41-4bf9-8f57-fd12f4729529",
              "src": "/Users/trombolayevgen/Downloads/ДС ЕВО_10042026.docx"
            },
            {
              "key": "type",
              "value": "sign",
              "type": "text",
              "uuid": "9d6792f7-bd87-4a1b-a42e-471b3cbefc19"
            }
          ]
        },
        "url": {
          "raw": "{{domain}}/api/external/company/sign/file",
          "host": [
            "{{domain}}"
          ],
          "path": [
            "api",
            "external",
            "company",
            "sign",
            "file"
          ]
        }
      },
      "response": []
    }
  ],
  "auth": {
    "type": "apikey",
    "apikey": [
      {
        "key": "value",
        "value": "{{system-id}}",
        "type": "string"
      },
      {
        "key": "key",
        "value": "x-system-id",
        "type": "string"
      }
    ]
  },
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "packages": {},
        "requests": {},
        "exec": [
          "/**",
          " * Encrypt data with RSA public key",
          " * @param publicKey - get from server",
          " * @param password - user key password",
          " */",
          "utils = {",
          "    rsaEncrypt: async function(publicKey, password) {",
          "        const cryptoKey = await crypto.subtle.importKey(\"spki\", publicKey, {",
          "            name: \"RSA-OAEP\",",
          "            hash: \"SHA-256\"",
          "        }, true, [\"encrypt\"]);",
          "        const encodedText = new TextEncoder().encode(password);",
          "        const encrypted = await crypto.subtle.encrypt({name: \"RSA-OAEP\"}, cryptoKey, encodedText);",
          "        //",
          "        return toBase64(encrypted);",
          "    }",
          "}",
          "",
          "function toBase64(buffer) {",
          "    const bytes = new Uint8Array(buffer);",
          "    let binary = '';",
          "    for (let i = 0; i < bytes.byteLength; i++) {",
          "        binary += String.fromCharCode(bytes[i]);",
          "    }",
          "    return btoa(binary);",
          "}"
        ]
      }
    },
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "packages": {},
        "requests": {},
        "exec": [
          ""
        ]
      }
    }
  ],
  "variable": [
    {
      "key": "domain",
      "value": ""
    },
    {
      "key": "system-id",
      "value": ""
    },
    {
      "key": "employee_key_uuid",
      "value": ""
    },
    {
      "key": "employee_key_password",
      "value": ""
    },
    {
      "key": "sign_hash",
      "value": ""
    },
    {
      "key": "user-token",
      "value": ""
    },
    {
      "key": "public-key",
      "value": ""
    }
  ]
}