feat(scenario): shared browser per run, sign step type, key loading helper

- all steps in a run share one browser/page instance so wizard state persists
- add 'sign' step type: execCode is {keyId} and signs via EDS widget on current page
- extract AuthService.loadKeyDescriptor() to deduplicate key file parsing
- extend StepType, DTOs, and MCP enums to include 'sign'
This commit is contained in:
ars9 committed 2026-04-08 15:42:42 +03:00
1 parent eeece70866
commit e85acc8fb2
7 files changed
+376 -61

No files matched your search

+52 -13
View File
@@ -7,6 +7,7 @@ import {
import { TraceLogger } from '../common/trace-logger';
import { ConfigService } from '@nestjs/config';
import { chromium } from 'playwright';
import type { Page } from 'playwright';
import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';
@@ -41,6 +42,18 @@ export class AuthService {
.map(f => path.basename(f, '.json'));
}
private loadKeyDescriptor(keyId: string): KeyDescriptor {
const keyJsonPath = path.join(this.keysDir, `${keyId}.json`);
if (!fs.existsSync(keyJsonPath)) {
throw new BadRequestException(`Key not found: ${keyId}`);
}
try {
return JSON.parse(fs.readFileSync(keyJsonPath, 'utf-8')) as KeyDescriptor;
} catch (err) {
throw new BadRequestException(`Cannot read key descriptor: ${keyId}`, { cause: err });
}
}
async login(keyId: string, environmentName: string, sessionName?: string): Promise<{ token: string; sessionName: string }> {
const resolvedSession = sessionName ?? crypto.randomUUID();
@@ -52,19 +65,7 @@ export class AuthService {
if (!loginUrl) throw new BadRequestException(`Environment "${environmentName}" is missing id_url`);
if (!cabinetUrl) throw new BadRequestException(`Environment "${environmentName}" is missing cabinet_url`);
const keyJsonPath = path.join(this.keysDir, `${keyId}.json`);
if (!fs.existsSync(keyJsonPath)) {
throw new BadRequestException(`Key not found: ${keyId}`);
}
let descriptor: KeyDescriptor;
try {
descriptor = JSON.parse(fs.readFileSync(keyJsonPath, 'utf-8'));
} catch (err) {
throw new BadRequestException(`Cannot read key descriptor: ${keyId}`, { cause: err });
}
const descriptor = this.loadKeyDescriptor(keyId);
const useLoginPassword = !!descriptor.login;
if (!useLoginPassword) {
@@ -158,4 +159,42 @@ export class AuthService {
await browser.close();
}
}
async signWithKey(keyId: string, page: Page): Promise<void> {
const descriptor = this.loadKeyDescriptor(keyId);
if (!descriptor.keyFile) {
throw new BadRequestException(`Key descriptor for "${keyId}" must have "keyFile" to sign`);
}
const keyFilePath = path.resolve(this.keysDir, descriptor.keyFile);
if (!fs.existsSync(keyFilePath)) {
throw new BadRequestException(`Key file not found: ${descriptor.keyFile}`);
}
this.logger.log(`Signing with key ${keyId} on page: ${page.url()}`);
// Open the EDS sign widget
await page.locator('button').filter({ hasText: /підпис|sign/i }).first().click();
await page.waitForTimeout(500);
// Select the file key tab inside the widget
await page.locator('button, [role="tab"], li').filter({ hasText: /файлов|file key/i }).first().click();
await page.waitForTimeout(300);
// Upload the key file
const fileInput = page.locator('input[accept=".dat,.pfx,.pk8,.zs2,.jks"][type="file"]');
await fileInput.setInputFiles(keyFilePath);
await page.waitForTimeout(300);
// Enter password
await page.locator('input[type="password"]').fill(descriptor.password);
await page.waitForTimeout(200);
// Submit
await page.locator('button').filter({ hasText: /підпис|sign|підтвер/i }).last().click();
await page.waitForLoadState('networkidle');
this.logger.log(`Sign completed for key ${keyId}`);
}
}