All files / src/platform/identity/schemas mfa.schema.ts

0% Statements 0/73
0% Branches 0/28
100% Functions 0/0
0% Lines 0/63

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                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
 
// ============================================================
// MFA METHOD — Stores user's enrolled MFA methods
// ============================================================
 
@Schema({ timestamps: true, collection: 'mfa_methods' })
export class MfaMethod extends Document {
  @Prop({ required: true, index: true })
  userId: string;
 
  @Prop({ required: true })
  tenantId: string;
 
  @Prop({ required: true, enum: ['totp', 'email_otp', 'backup_codes'] })
  type: string;
 
  @Prop({ default: null })
  secret: string; // Encrypted TOTP secret
 
  @Prop({ default: null })
  label: string; // e.g., "Authenticator App"
 
  @Prop({ default: false })
  isVerified: boolean;
 
  @Prop({ default: false })
  isPrimary: boolean;
 
  @Prop({ default: true })
  isActive: boolean;
 
  @Prop({ default: null })
  lastUsedAt: Date;
 
  @Prop({ default: null })
  enrolledAt: Date;
 
  createdAt: Date;
  updatedAt: Date;
}
 
export const MfaMethodSchema = SchemaFactory.createForClass(MfaMethod);
MfaMethodSchema.index({ userId: 1, type: 1 });
 
// ============================================================
// MFA CHALLENGE — Active MFA verification challenges
// ============================================================
 
@Schema({ timestamps: true, collection: 'mfa_challenges' })
export class MfaChallenge extends Document {
  @Prop({ required: true, index: true })
  userId: string;
 
  @Prop({ required: true })
  tenantId: string;
 
  @Prop({ required: true, unique: true })
  challengeId: string;
 
  @Prop({ required: true, enum: ['totp', 'email_otp', 'backup_code'] })
  method: string;
 
  @Prop({ default: null })
  otpCode: string; // Hashed OTP for email_otp
 
  @Prop({ required: true })
  expiresAt: Date;
 
  @Prop({ default: false })
  isCompleted: boolean;
 
  @Prop({ default: 0 })
  attempts: number;
 
  @Prop({ default: 5 })
  maxAttempts: number;
 
  @Prop({ default: null })
  completedAt: Date;
 
  @Prop({ default: null })
  ipAddress: string;
 
  @Prop({ default: null })
  userAgent: string;
 
  createdAt: Date;
  updatedAt: Date;
}
 
export const MfaChallengeSchema = SchemaFactory.createForClass(MfaChallenge);
MfaChallengeSchema.index({ challengeId: 1 }, { unique: true });
MfaChallengeSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });
 
// ============================================================
// MFA RECOVERY CODE — One-time backup codes
// ============================================================
 
@Schema({ timestamps: true, collection: 'mfa_recovery_codes' })
export class MfaRecoveryCode extends Document {
  @Prop({ required: true, index: true })
  userId: string;
 
  @Prop({ required: true })
  tenantId: string;
 
  @Prop({ required: true })
  codeHash: string; // bcrypt hash of the recovery code
 
  @Prop({ default: false })
  isUsed: boolean;
 
  @Prop({ default: null })
  usedAt: Date;
 
  @Prop({ default: null })
  usedIpAddress: string;
 
  @Prop({ required: true })
  batchId: string; // Group codes by generation batch
 
  createdAt: Date;
  updatedAt: Date;
}
 
export const MfaRecoveryCodeSchema =
  SchemaFactory.createForClass(MfaRecoveryCode);
MfaRecoveryCodeSchema.index({ userId: 1, batchId: 1 });
 
// ============================================================
// MFA ENFORCEMENT POLICY — Per-tenant MFA policies
// ============================================================
 
@Schema({ timestamps: true, collection: 'mfa_enforcement_policies' })
export class MfaEnforcementPolicy extends Document {
  @Prop({ required: true, unique: true })
  tenantId: string;
 
  @Prop({ default: false })
  isEnabled: boolean;
 
  @Prop({ default: false })
  isEnforcedForAll: boolean;
 
  @Prop({ type: [String], default: [] })
  enforcedRoles: string[]; // e.g., ['super_admin', 'hr_admin']
 
  @Prop({ type: [String], default: ['totp', 'email_otp'] })
  allowedMethods: string[];
 
  @Prop({ default: 30 })
  gracePeriodDays: number; // Days before enforcement kicks in
 
  @Prop({ default: 10 })
  backupCodeCount: number;
 
  @Prop({ default: null })
  enforcedAt: Date;
 
  @Prop({ default: null })
  updatedBy: string;
 
  createdAt: Date;
  updatedAt: Date;
}
 
export const MfaEnforcementPolicySchema =
  SchemaFactory.createForClass(MfaEnforcementPolicy);
 
// ============================================================
// MFA AUDIT LOG — Tracks all MFA events
// ============================================================
 
@Schema({ timestamps: true, collection: 'mfa_audit_logs' })
export class MfaAuditLog extends Document {
  @Prop({ required: true, index: true })
  userId: string;
 
  @Prop({ required: true })
  tenantId: string;
 
  @Prop({
    required: true,
    enum: [
      'mfa_enrolled',
      'mfa_verified',
      'mfa_challenge_created',
      'mfa_challenge_passed',
      'mfa_challenge_failed',
      'mfa_disabled',
      'backup_codes_generated',
      'backup_code_used',
      'mfa_method_removed',
      'mfa_policy_updated',
    ],
  })
  event: string;
 
  @Prop({ default: null })
  method: string;
 
  @Prop({ default: null })
  ipAddress: string;
 
  @Prop({ default: null })
  userAgent: string;
 
  @Prop({ type: Object, default: {} })
  metadata: Record<string, any>;
 
  createdAt: Date;
  updatedAt: Date;
}
 
export const MfaAuditLogSchema = SchemaFactory.createForClass(MfaAuditLog);
MfaAuditLogSchema.index({ userId: 1, createdAt: -1 });
MfaAuditLogSchema.index({ tenantId: 1, event: 1, createdAt: -1 });