feat: add tbank tokens

This commit is contained in:
2026-08-13 19:31:43 +03:00
parent 4cbfc2621c
commit 31cbba8654
25 changed files with 3592 additions and 24 deletions
+5
View File
@@ -7,12 +7,16 @@ describe('validateEnv', () => {
DATABASE_URL: 'file:./data.db',
FRONTEND_ORIGIN: 'http://localhost:5173/',
JWT_SECRET: 'a-secret-that-is-at-least-thirty-two-characters',
TOKEN_ENCRYPTION_KEY: 'MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY=',
}),
).toEqual({
DATABASE_URL: 'file:./data.db',
FRONTEND_ORIGIN: 'http://localhost:5173',
PORT: 3000,
JWT_SECRET: 'a-secret-that-is-at-least-thirty-two-characters',
TOKEN_ENCRYPTION_KEY: 'MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY=',
TBANK_GRPC_ENDPOINT: 'invest-public-api.tbank.ru:443',
TBANK_RATE_LIMIT_PER_SECOND: 5,
});
});
@@ -35,6 +39,7 @@ describe('validateEnv', () => {
DATABASE_URL: 'file:./data.db',
FRONTEND_ORIGIN: origin,
JWT_SECRET: 'a-secret-that-is-at-least-thirty-two-characters',
TOKEN_ENCRYPTION_KEY: 'MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY=',
}),
).toThrow('FRONTEND_ORIGIN must be a valid HTTP origin');
},
+38
View File
@@ -3,6 +3,10 @@ export interface Environment {
FRONTEND_ORIGIN: string;
JWT_SECRET: string;
PORT: number;
TOKEN_ENCRYPTION_KEY: string;
TBANK_CA_CERT_PATH?: string;
TBANK_GRPC_ENDPOINT: string;
TBANK_RATE_LIMIT_PER_SECOND: number;
}
export function validateEnv(config: Record<string, unknown>): Environment {
@@ -10,6 +14,11 @@ export function validateEnv(config: Record<string, unknown>): Environment {
const frontendOrigin = config['FRONTEND_ORIGIN'];
const port = config['PORT'] ?? 3000;
const jwtSecret = config['JWT_SECRET'];
const tokenEncryptionKey = config['TOKEN_ENCRYPTION_KEY'];
const tbankEndpoint =
config['TBANK_GRPC_ENDPOINT'] ?? 'invest-public-api.tbank.ru:443';
const tbankRateLimit = config['TBANK_RATE_LIMIT_PER_SECOND'] ?? 5;
const tbankCaCertPath = config['TBANK_CA_CERT_PATH'];
if (typeof databaseUrl !== 'string' || !databaseUrl.startsWith('file:')) {
throw new Error('DATABASE_URL must be a SQLite file URL');
@@ -44,10 +53,39 @@ export function validateEnv(config: Record<string, unknown>): Environment {
throw new Error('JWT_SECRET must be at least 32 characters long');
}
if (
typeof tokenEncryptionKey !== 'string' ||
!/^[A-Za-z0-9+/]+={0,2}$/.test(tokenEncryptionKey) ||
Buffer.from(tokenEncryptionKey, 'base64').length !== 32
) {
throw new Error(
'TOKEN_ENCRYPTION_KEY must be a base64-encoded 32-byte key',
);
}
if (typeof tbankEndpoint !== 'string' || !tbankEndpoint) {
throw new Error('TBANK_GRPC_ENDPOINT must be a non-empty endpoint');
}
const parsedTbankRateLimit = Number(tbankRateLimit);
if (!Number.isInteger(parsedTbankRateLimit) || parsedTbankRateLimit < 1) {
throw new Error('TBANK_RATE_LIMIT_PER_SECOND must be a positive integer');
}
if (tbankCaCertPath !== undefined && typeof tbankCaCertPath !== 'string') {
throw new Error('TBANK_CA_CERT_PATH must be a file path');
}
return {
DATABASE_URL: databaseUrl,
FRONTEND_ORIGIN: parsedOrigin.origin,
JWT_SECRET: jwtSecret,
PORT: parsedPort,
TOKEN_ENCRYPTION_KEY: tokenEncryptionKey,
...(typeof tbankCaCertPath === 'string' && tbankCaCertPath
? { TBANK_CA_CERT_PATH: tbankCaCertPath }
: {}),
TBANK_GRPC_ENDPOINT: tbankEndpoint,
TBANK_RATE_LIMIT_PER_SECOND: parsedTbankRateLimit,
};
}