games_app/backend/dist-test-faq/services/securityNotificationService.js

240 lines
8.2 KiB
JavaScript

"use strict";
/**
* User-facing security email and in-app notifications.
* Failures are logged but never block the primary action.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.notifyNewLogin = notifyNewLogin;
exports.notifyNewCountryLogin = notifyNewCountryLogin;
exports.notifyPasswordChanged = notifyPasswordChanged;
exports.notifyTotpEnabled = notifyTotpEnabled;
exports.notifyTotpDisabled = notifyTotpDisabled;
exports.notifyBreakGlassAdmin2faReset = notifyBreakGlassAdmin2faReset;
exports.notifyRolesChanged = notifyRolesChanged;
exports.notifyPermissionsChanged = notifyPermissionsChanged;
const crypto_1 = require("../utils/crypto");
const userRepository = __importStar(require("../repositories/userRepository"));
const userSecurityNotificationRepository = __importStar(require("../repositories/userSecurityNotificationRepository"));
const emailService = __importStar(require("./emailService"));
async function resolveUserEmail(userId) {
const profile = await userRepository.getProfileForUser(userId);
if (!profile?.email_encrypted)
return null;
try {
return (0, crypto_1.decrypt)(profile.email_encrypted);
}
catch {
return null;
}
}
function logNotifyError(context, e) {
const detail = e instanceof Error ? e.message : String(e);
console.error(`[security-notify] ${context}:`, detail);
}
async function recordInAppNotification(data) {
try {
await userSecurityNotificationRepository.createUserSecurityNotification(data);
}
catch (e) {
logNotifyError('in-app notification', e);
}
}
async function notifyNewLogin(userId, ctx) {
void recordInAppNotification({
userId,
eventType: 'new_login',
title: 'New sign-in detected',
body: 'A sign-in from a new device or IP address was detected on your account.',
metadata: { ...ctx },
});
const email = await resolveUserEmail(userId);
if (!email)
return;
try {
await emailService.sendNewLoginEmail(email, ctx);
}
catch (e) {
logNotifyError('new login email', e);
}
}
async function notifyNewCountryLogin(userId, ctx) {
const country = ctx.countryCode ?? 'unknown';
void recordInAppNotification({
userId,
eventType: 'new_country_login',
title: 'Sign-in from a new country',
body: `A sign-in from country ${country} was detected on your account.`,
metadata: { ...ctx },
});
const email = await resolveUserEmail(userId);
if (!email)
return;
try {
await emailService.sendNewCountryLoginEmail(email, ctx);
}
catch (e) {
logNotifyError('new country login email', e);
}
}
async function notifyPasswordChanged(userId, ctx) {
void recordInAppNotification({
userId,
eventType: 'password_changed',
title: 'Password changed',
body: 'Your account password was changed.',
metadata: { ...ctx },
});
const email = await resolveUserEmail(userId);
if (!email)
return;
try {
await emailService.sendPasswordChangedEmail(email, ctx);
}
catch (e) {
logNotifyError('password changed email', e);
}
}
async function notifyTotpEnabled(userId, ctx) {
void recordInAppNotification({
userId,
eventType: 'totp_enabled',
title: 'Two-factor authentication enabled',
body: '2FA was enabled on your account.',
metadata: { ...ctx },
});
const email = await resolveUserEmail(userId);
if (!email)
return;
try {
await emailService.sendTotpEnabledEmail(email, ctx);
}
catch (e) {
logNotifyError('2FA enabled email', e);
}
}
async function notifyTotpDisabled(userId, ctx) {
void recordInAppNotification({
userId,
eventType: 'totp_disabled',
title: 'Two-factor authentication disabled',
body: '2FA was disabled on your account.',
metadata: { ...ctx },
});
const email = await resolveUserEmail(userId);
if (!email)
return;
try {
await emailService.sendTotpDisabledEmail(email, ctx);
}
catch (e) {
logNotifyError('2FA disabled email', e);
}
}
/**
* Notifies the account owner after an emergency CLI break-glass 2FA reset.
*/
async function notifyBreakGlassAdmin2faReset(userId, ctx) {
const operator = ctx.operator?.trim() || 'unknown';
const hostname = ctx.hostname?.trim() || 'unknown-host';
void recordInAppNotification({
userId,
eventType: 'break_glass_admin_2fa_reset',
title: 'Emergency Super Admin 2FA reset',
body: 'Two-factor authentication was cleared via the break-glass CLI. ' +
'If you did not request this, secure the VPS immediately and change your password.',
metadata: { operator, hostname, channel: 'cli_break_glass' },
});
const email = await resolveUserEmail(userId);
if (!email)
return;
try {
await emailService.sendSecurityAlertEmail(email, {
alertId: `break-glass-${userId.slice(0, 8)}`,
severity: 'critical',
type: 'break_glass_admin_2fa_reset',
title: 'Emergency Super Admin 2FA reset',
details: [
'Your Super Admin 2FA (authenticator + recovery codes) was cleared using the server break-glass CLI.',
`Operator: ${operator}`,
`Host: ${hostname}`,
'Next: sign in with password only, re-enable 2FA in Profile, and store new recovery codes offline.',
'If you did not authorize this, change your password and audit VPS SSH access immediately.',
].join('\n'),
userId,
createdAt: new Date(),
});
}
catch (e) {
logNotifyError('break-glass 2FA reset email', e);
}
}
async function notifyRolesChanged(userId, roles, ctx) {
void recordInAppNotification({
userId,
eventType: 'roles_changed',
title: 'Account roles updated',
body: `Your roles were updated to: ${roles.join(', ') || '(none)'}.`,
metadata: { roles, ...ctx },
});
const email = await resolveUserEmail(userId);
if (!email)
return;
try {
await emailService.sendRolesChangedEmail(email, roles, ctx);
}
catch (e) {
logNotifyError('roles changed email', e);
}
}
async function notifyPermissionsChanged(userId, permissions, ctx) {
void recordInAppNotification({
userId,
eventType: 'permissions_changed',
title: 'Account permissions updated',
body: `Your permissions were updated (${permissions.length} total).`,
metadata: { permissions, ...ctx },
});
const email = await resolveUserEmail(userId);
if (!email)
return;
try {
await emailService.sendPermissionsChangedEmail(email, permissions, ctx);
}
catch (e) {
logNotifyError('permissions changed email', e);
}
}
//# sourceMappingURL=securityNotificationService.js.map