Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 5x 5x 24x 24x 29x 29x 29x 29x 29x 29x 29x 29x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 28x 28x 28x 28x 28x 28x 28x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x | import { prisma } from "./prisma";
import { logger } from '@/lib/logging';
import type { Prisma } from "@prisma/client";
/**
* Security audit actions
*/
export enum AuditAction {
// Authentication
LOGIN_SUCCESS = "LOGIN_SUCCESS",
LOGIN_FAILURE = "LOGIN_FAILURE",
LOGOUT = "LOGOUT",
SIGNUP = "SIGNUP",
// Password
PASSWORD_CHANGE = "PASSWORD_CHANGE",
PASSWORD_RESET_REQUEST = "PASSWORD_RESET_REQUEST",
PASSWORD_RESET_COMPLETE = "PASSWORD_RESET_COMPLETE",
// Email
EMAIL_VERIFICATION_SENT = "EMAIL_VERIFICATION_SENT",
EMAIL_VERIFIED = "EMAIL_VERIFIED",
// Account Security
ACCOUNT_LOCKED = "ACCOUNT_LOCKED",
ACCOUNT_UNLOCKED = "ACCOUNT_UNLOCKED",
// Orders
ORDER_CREATE = "ORDER_CREATE",
ORDER_UPDATE = "ORDER_UPDATE",
ORDER_CANCEL = "ORDER_CANCEL",
// Payments
PAYMENT_INITIATED = "PAYMENT_INITIATED",
PAYMENT_SUCCESS = "PAYMENT_SUCCESS",
PAYMENT_FAILURE = "PAYMENT_FAILURE",
REFUND_PROCESSED = "REFUND_PROCESSED",
// Admin
ADMIN_ACTION = "ADMIN_ACTION",
ADMIN_USER_UPDATE = "ADMIN_USER_UPDATE",
ADMIN_PRODUCT_CREATE = "ADMIN_PRODUCT_CREATE",
ADMIN_PRODUCT_UPDATE = "ADMIN_PRODUCT_UPDATE",
ADMIN_PRODUCT_DELETE = "ADMIN_PRODUCT_DELETE",
// Profile
PROFILE_UPDATE = "PROFILE_UPDATE",
ADDRESS_CREATE = "ADDRESS_CREATE",
ADDRESS_UPDATE = "ADDRESS_UPDATE",
ADDRESS_DELETE = "ADDRESS_DELETE"}
interface AuditLogOptions {
userId?: number;
resource?: string;
resourceId?: number;
metadata?: Record<string, unknown>;
}
/**
* Extract request information for audit logging
*/
function getRequestInfo(request?: Request) {
if (!request) {
return { ipAddress: null, userAgent: null };
}
const ipAddress =
request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ||
request.headers.get("x-real-ip") ||
null;
const userAgent = request.headers.get("user-agent") || null;
return { ipAddress, userAgent };
}
/**
* Log an audit event
* Use this for security-relevant actions that should be tracked
*/
export async function logAuditEvent(
action: AuditAction,
request?: Request,
options: AuditLogOptions = {}
): Promise<void> {
const { ipAddress, userAgent } = getRequestInfo(request);
try {
await prisma.auditLog.create({
data: {
action,
userId: options.userId || null,
resource: options.resource || null,
resourceId: options.resourceId || null,
ipAddress,
userAgent,
metadata: (options.metadata as Prisma.InputJsonValue) || undefined}});
logger.info(`${action}`, {
category: "AUDIT",
userId: options.userId,
resource: options.resource,
resourceId: options.resourceId,
ip: ipAddress});
} catch (error) {
// Don't fail the main operation if audit logging fails
logger.error(
"Failed to log audit event",
error instanceof Error ? error : new Error(String(error)),
{
category: "AUDIT",
action
}
);
}
}
/**
* Log login attempt (success or failure)
*/
export async function logLoginAttempt(
request: Request,
userId: number | null,
success: boolean,
email?: string
): Promise<void> {
await logAuditEvent(
success ? AuditAction.LOGIN_SUCCESS : AuditAction.LOGIN_FAILURE,
request,
{
userId: userId || undefined,
metadata: {
email: email ? email.substring(0, 3) + "***" : undefined, // Partially mask email
success}}
);
}
/**
* Log password reset request
*/
export async function logPasswordResetRequest(
request: Request,
userId: number
): Promise<void> {
await logAuditEvent(AuditAction.PASSWORD_RESET_REQUEST, request, {
userId});
}
/**
* Log password reset completion
*/
export async function logPasswordResetComplete(
request: Request,
userId: number
): Promise<void> {
await logAuditEvent(AuditAction.PASSWORD_RESET_COMPLETE, request, {
userId});
}
/**
* Log account lockout
*/
export async function logAccountLocked(
request: Request,
userId: number,
failedAttempts: number
): Promise<void> {
await logAuditEvent(AuditAction.ACCOUNT_LOCKED, request, {
userId,
metadata: {
failedAttempts,
reason: "Too many failed login attempts"}});
}
/**
* Log order creation
*/
export async function logOrderCreate(
request: Request,
userId: number,
orderId: number,
total: number
): Promise<void> {
await logAuditEvent(AuditAction.ORDER_CREATE, request, {
userId,
resource: "order",
resourceId: orderId,
metadata: { total }});
}
/**
* Log payment event
*/
export async function logPaymentEvent(
request: Request | undefined,
userId: number,
orderId: number,
success: boolean,
amount: number,
paymentIntentId?: string
): Promise<void> {
await logAuditEvent(
success ? AuditAction.PAYMENT_SUCCESS : AuditAction.PAYMENT_FAILURE,
request,
{
userId,
resource: "order",
resourceId: orderId,
metadata: {
amount,
paymentIntentId}}
);
}
/**
* Log admin action
*/
export async function logAdminAction(
request: Request,
adminUserId: number,
action: string,
resource: string,
resourceId?: number,
details?: Record<string, unknown>
): Promise<void> {
await logAuditEvent(AuditAction.ADMIN_ACTION, request, {
userId: adminUserId,
resource,
resourceId,
metadata: {
action,
...details}});
}
/**
* Get recent audit logs for a user
*/
export async function getUserAuditLogs(
userId: number,
limit: number = 50
) {
return prisma.auditLog.findMany({
where: { userId },
orderBy: { createdAt: "desc" },
take: limit});
}
/**
* Get recent security events (for admin dashboard)
*/
export async function getSecurityEvents(
limit: number = 100,
actions?: AuditAction[]
) {
return prisma.auditLog.findMany({
where: actions
? { action: { in: actions } }
: {
action: {
in: [
AuditAction.LOGIN_FAILURE,
AuditAction.ACCOUNT_LOCKED,
AuditAction.PASSWORD_RESET_REQUEST,
AuditAction.PAYMENT_FAILURE,
]}},
orderBy: { createdAt: "desc" },
take: limit,
include: {
user: {
select: { id: true, email: true, name: true }}}});
}
|