Files
back_end_oudelaa/scripts/check-superadmin-config.js

62 lines
3.3 KiB
JavaScript

// Offline diagnostics only. Never sends credentials or changes configuration/database.
const fs = require('node:fs');
const path = require('node:path');
const dotenv = require('dotenv');
const bcrypt = require('bcrypt');
const readEnv = (file) => fs.existsSync(file) ? dotenv.parse(fs.readFileSync(file)) : {};
function safeBase(value) {
try {
const url = new URL(value);
return url.origin + url.pathname.replace(/\/$/, '');
} catch { return null; }
}
async function inspect(backend, dashboard) {
const hash = backend.SUPERADMIN_PASSWORD_HASH || '';
const plain = backend.SUPERADMIN_PASSWORD || '';
const hashValid = /^\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}$/.test(hash);
const target = safeBase(dashboard.API_BASE_URL || dashboard.NEXT_PUBLIC_API_BASE_URL);
const localOrigin = safeBase(backend.PUBLIC_BASE_URL || `http://localhost:${backend.PORT || 4000}`);
const issues = [];
if (!backend.SUPERADMIN_EMAIL) issues.push('SUPERADMIN_EMAIL is missing');
if (hash && !hashValid) issues.push('SUPERADMIN_PASSWORD_HASH is malformed; plaintext does not override it');
if (!hash && !plain) issues.push('No SuperAdmin password or hash configured');
if (backend.NODE_ENV === 'production' && !hash) issues.push('Production requires SUPERADMIN_PASSWORD_HASH');
for (const key of ['SUPERADMIN_ACCESS_SECRET', 'SUPERADMIN_REFRESH_SECRET']) {
if (!backend[key] || backend[key].length < 16) issues.push(`${key} is missing or too short`);
}
let plainMatchesHash = null;
if (hashValid && plain) plainMatchesHash = await bcrypt.compare(plain, hash);
if (plainMatchesHash === false) issues.push('Hash takes precedence and does not match the configured plaintext password');
if (target && localOrigin && new URL(target).origin !== new URL(localOrigin).origin) {
issues.push('Dashboard target differs from the backend origin in this environment; local credentials do not configure that target');
}
return {
identitySource: 'Backend environment; no users collection lookup',
credentialMode: hash ? 'bcrypt-hash' : plain ? 'plaintext-development' : 'missing',
emailConfigured: !!backend.SUPERADMIN_EMAIL,
hashValid: hash ? hashValid : null, plainMatchesHash,
totpRequired: !!backend.SUPERADMIN_TOTP_SECRET,
dashboardPrimary: target, backendDeclaredOrigin: localOrigin,
endpoint: target ? `${target}/auth/superadmin/login` : null,
issues,
limitation: 'Offline configuration inspection; remote runtime credentials and connectivity are not verified.',
};
}
async function main() {
const root = path.resolve(__dirname, '..');
const backend = { ...readEnv(path.join(root, '.env')), ...process.env };
// This approximates Next development precedence; deployed dashboard variables must be checked on its host.
const dashboardRoot = path.join(root, 'oudelaa_dashboard');
const dashboard = {
...readEnv(path.join(dashboardRoot, '.env')),
...readEnv(path.join(dashboardRoot, '.env.development')),
...readEnv(path.join(dashboardRoot, '.env.local')),
...readEnv(path.join(dashboardRoot, '.env.development.local')),
...process.env,
};
console.log(JSON.stringify(await inspect(backend, dashboard), null, 2));
}
if (require.main === module) main().catch(() => { console.error('Configuration inspection failed; no secrets printed.'); process.exitCode = 1; });
module.exports = { inspect };