91 lines
3.3 KiB
TypeScript
91 lines
3.3 KiB
TypeScript
import 'dotenv/config';
|
|
import path from 'node:path';
|
|
|
|
const DEV_JWT_SECRETS = new Set(['dev-access-secret', 'dev-refresh-secret']);
|
|
|
|
export interface SecretsGuardInput {
|
|
accessSecret: string;
|
|
refreshSecret: string;
|
|
}
|
|
|
|
/**
|
|
* Straż startowa: w produkcji nie odpalamy z deweloperskimi sekretami JWT.
|
|
* Rzucana wyjątkiem przy imporcie (patrz dół pliku) — proces nie wstanie
|
|
* zamiast działać na z góry złamanych sekretach. Hasło pierwszego admina
|
|
* sprawdza seed (jedyny moment, w którym powstaje konto).
|
|
*/
|
|
export function assertProductionSecrets(cfg: SecretsGuardInput): void {
|
|
const problems: string[] = [];
|
|
if (!cfg.accessSecret || DEV_JWT_SECRETS.has(cfg.accessSecret)) {
|
|
problems.push('JWT_ACCESS_SECRET missing or using a development default');
|
|
}
|
|
if (!cfg.refreshSecret || DEV_JWT_SECRETS.has(cfg.refreshSecret)) {
|
|
problems.push('JWT_REFRESH_SECRET missing or using a development default');
|
|
}
|
|
if (cfg.accessSecret && cfg.accessSecret === cfg.refreshSecret) {
|
|
problems.push('JWT_ACCESS_SECRET and JWT_REFRESH_SECRET must differ');
|
|
}
|
|
if (problems.length > 0) {
|
|
throw new Error(`Unsafe production configuration:\n- ${problems.join('\n- ')}`);
|
|
}
|
|
}
|
|
|
|
function escapeRegexLiteral(value: string): string {
|
|
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
}
|
|
|
|
/**
|
|
* CORS_EXTRA_ORIGINS: lista dokładnych originów oddzielonych przecinkiem,
|
|
* np. "http://192.168.50.127:13002,http://192.168.50.127:13003" — używana gdy
|
|
* apka jest hostowana poza główną domeną (host na alternatywnym urządzeniu).
|
|
*/
|
|
export function parseExtraCorsOrigins(raw: string | undefined): string[] {
|
|
return (raw ?? '')
|
|
.split(',')
|
|
.map((s) => s.trim())
|
|
.filter((s) => s.length > 0);
|
|
}
|
|
|
|
export function buildCorsOriginPatterns(nodeEnv: string, extraOrigins: string[]): RegExp[] {
|
|
return [
|
|
/^https?:\/\/(www\.)?domena\.pl(:\d+)?$/,
|
|
/^https?:\/\/admin\.domena\.pl(:\d+)?$/,
|
|
/^https?:\/\/(www\.)?tisachi\.ddns\.net(:\d+)?$/,
|
|
// dodatkowe origins traktujemy dosłownie (escapowane) — pełne dopasowanie originu
|
|
...extraOrigins.map((o) => new RegExp(`^${escapeRegexLiteral(o)}$`)),
|
|
// localhost tylko poza produkcją (dev/testy e2e); w produkcji originy wyłącznie znad domeny
|
|
...(nodeEnv === 'production'
|
|
? []
|
|
: [/^https?:\/\/localhost(:\d+)?$/, /^https?:\/\/127\.0\.0\.1(:\d+)?$/]),
|
|
];
|
|
}
|
|
|
|
const nodeEnv = process.env.NODE_ENV || 'development';
|
|
|
|
export const config = {
|
|
port: parseInt(process.env.PORT || '9002', 10),
|
|
nodeEnv,
|
|
databaseUrl: process.env.DATABASE_URL || 'postgresql://fitos:fitos@localhost:5432/fitos',
|
|
jwt: {
|
|
accessSecret: process.env.JWT_ACCESS_SECRET || 'dev-access-secret',
|
|
refreshSecret: process.env.JWT_REFRESH_SECRET || 'dev-refresh-secret',
|
|
accessTtlSec: 15 * 60,
|
|
refreshTtlSec: 7 * 24 * 3600,
|
|
},
|
|
logLevel: process.env.LOG_LEVEL || 'info',
|
|
uploadDir: process.env.UPLOAD_DIR || path.join(process.cwd(), 'uploads'),
|
|
// true only when served over HTTPS — over plain HTTP browsers drop Secure cookies (breaks refresh)
|
|
cookieSecure: process.env.COOKIE_SECURE === 'true',
|
|
corsOriginPatterns: buildCorsOriginPatterns(
|
|
nodeEnv,
|
|
parseExtraCorsOrigins(process.env.CORS_EXTRA_ORIGINS),
|
|
),
|
|
};
|
|
|
|
if (config.nodeEnv === 'production') {
|
|
assertProductionSecrets({
|
|
accessSecret: config.jwt.accessSecret,
|
|
refreshSecret: config.jwt.refreshSecret,
|
|
});
|
|
}
|