feat(environment): add environment CRUD module and wire into auth login
This commit is contained in:
+16
-15
@@ -3,6 +3,7 @@ import {
|
||||
BadRequestException,
|
||||
InternalServerErrorException,
|
||||
Logger,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { chromium } from 'playwright';
|
||||
@@ -10,6 +11,7 @@ import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as crypto from 'crypto';
|
||||
import { SessionService } from '../session/session.service';
|
||||
import { EnvironmentService } from '../environment/environment.service';
|
||||
|
||||
interface KeyDescriptor {
|
||||
keyFile: string;
|
||||
@@ -20,24 +22,15 @@ interface KeyDescriptor {
|
||||
export class AuthService {
|
||||
private readonly logger = new Logger(AuthService.name);
|
||||
private readonly keysDir: string;
|
||||
private readonly loginUrl: string;
|
||||
private readonly cabinetUrl: string;
|
||||
|
||||
constructor(
|
||||
private readonly config: ConfigService,
|
||||
private readonly sessionService: SessionService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
) {
|
||||
this.keysDir = path.resolve(
|
||||
this.config.get<string>('KEYS_DIR', 'keys'),
|
||||
);
|
||||
this.loginUrl = this.config.get<string>(
|
||||
'ID_LOGIN_URL',
|
||||
'https://id-liquio-diia-stg.kitsoft.ua/',
|
||||
);
|
||||
this.cabinetUrl = this.config.get<string>(
|
||||
'CABINET_URL',
|
||||
'https://cabinet-liquio-diia-stg.kitsoft.ua/messages',
|
||||
);
|
||||
}
|
||||
|
||||
listKeys(): string[] {
|
||||
@@ -47,9 +40,17 @@ export class AuthService {
|
||||
.map(f => path.basename(f, '.json'));
|
||||
}
|
||||
|
||||
async login(keyId: string, sessionName?: string): Promise<{ token: string; sessionName: string }> {
|
||||
async login(keyId: string, environmentName: string, sessionName?: string): Promise<{ token: string; sessionName: string }> {
|
||||
const resolvedSession = sessionName ?? crypto.randomUUID();
|
||||
|
||||
const env = await this.environmentService.findAll().then(all => all.find(e => e.name === environmentName));
|
||||
if (!env) throw new NotFoundException(`Environment "${environmentName}" not found`);
|
||||
|
||||
const loginUrl = env.urls.id_url;
|
||||
const cabinetUrl = env.urls.cabinet_url;
|
||||
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)) {
|
||||
@@ -73,8 +74,8 @@ export class AuthService {
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
|
||||
this.logger.log(`Navigating to ${this.loginUrl}`);
|
||||
await page.goto(this.loginUrl, { waitUntil: 'networkidle' });
|
||||
this.logger.log(`Navigating to ${loginUrl}`);
|
||||
await page.goto(loginUrl, { waitUntil: 'networkidle' });
|
||||
|
||||
// Click "Файловий ключ" button
|
||||
await page.getByText('Файловий ключ').click();
|
||||
@@ -92,8 +93,8 @@ export class AuthService {
|
||||
await page.locator('#id-app-login-file-key-sign-button').click();
|
||||
|
||||
// Wait until redirected to cabinet
|
||||
this.logger.log(`Waiting for redirect to ${this.cabinetUrl}`);
|
||||
await page.waitForURL(this.cabinetUrl, { timeout: 30000 });
|
||||
this.logger.log(`Waiting for redirect to ${cabinetUrl}`);
|
||||
await page.waitForURL(cabinetUrl, { timeout: 30000 });
|
||||
|
||||
// Extract token from localStorage
|
||||
const token = await page.evaluate(() => localStorage.getItem('token'));
|
||||
|
||||
Reference in New Issue
Block a user