Use owner enums for error and T-Bank protocol values with ESLint regression checks. Preserve open wire fallbacks and keep protocol imports independent of DI and Prisma. Verified npm ci, verify, 226 unit tests, 262 isolated e2e tests and unchanged OpenAPI.
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import { BadRequestException, Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
import { Environment } from '../../config/env.validation';
|
|
import { isTbankErrorCode, TbankErrorCode } from './tbank-error-code';
|
|
import { TbankCrypto } from './tbank.crypto';
|
|
|
|
@Injectable()
|
|
export class TbankTokenService {
|
|
private readonly crypto: TbankCrypto;
|
|
|
|
constructor(config: ConfigService<Environment, true>) {
|
|
this.crypto = TbankCrypto.fromBase64(
|
|
config.getOrThrow('TOKEN_ENCRYPTION_KEY'),
|
|
);
|
|
}
|
|
|
|
encrypt(value: string): string {
|
|
return this.crypto.encrypt(value);
|
|
}
|
|
|
|
decrypt(payload: string): string {
|
|
return this.crypto.decrypt(payload);
|
|
}
|
|
|
|
async translateInvalidToken<T>(operation: () => Promise<T>): Promise<T> {
|
|
try {
|
|
return await operation();
|
|
} catch (error) {
|
|
if (error instanceof Error && isTbankErrorCode(error.message, TbankErrorCode.InvalidToken)) {
|
|
throw new BadRequestException({ code: TbankErrorCode.InvalidToken });
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
}
|