All files / src/platform/identity/schemas device-trust.schema.ts

0% Statements 0/64
0% Branches 0/40
100% Functions 0/0
0% Lines 0/58

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                                                                                                                                                                                                                                                                                                                                                                                           
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
 
// ============================================================
// TRUSTED DEVICE — Devices user has marked as trusted
// ============================================================
 
@Schema({ timestamps: true, collection: 'trusted_devices' })
export class TrustedDevice extends Document {
  @Prop({ required: true, index: true })
  userId: string;
 
  @Prop({ required: true })
  tenantId: string;
 
  @Prop({ required: true })
  deviceId: string;
 
  @Prop({ default: null })
  deviceName: string;
 
  @Prop({ default: null })
  deviceType: string; // 'mobile_android' | 'mobile_ios' | 'web_browser' | 'desktop'
 
  @Prop({ default: null })
  browser: string;
 
  @Prop({ default: null })
  os: string;
 
  @Prop({ default: null })
  osVersion: string;
 
  @Prop({ default: null })
  appVersion: string;
 
  @Prop({ required: true })
  fingerprintHash: string; // SHA-256 of device fingerprint components
 
  @Prop({ type: Object, default: {} })
  fingerprintComponents: Record<string, any>; // Screen, timezone, language, etc.
 
  @Prop({ default: true })
  isTrusted: boolean;
 
  @Prop({ default: null })
  trustedAt: Date;
 
  @Prop({ default: null })
  trustedUntil: Date; // Optional trust expiration
 
  @Prop({ default: null })
  lastSeenAt: Date;
 
  @Prop({ default: null })
  lastSeenIp: string;
 
  @Prop({ default: null })
  lastSeenLocation: string;
 
  @Prop({ default: false })
  isRevoked: boolean;
 
  @Prop({ default: null })
  revokedAt: Date;
 
  @Prop({ default: null })
  revokedBy: string;
 
  @Prop({ default: null })
  revokedReason: string;
 
  // Push token for this specific device (if registered)
  @Prop({ default: null })
  pushToken: string;
 
  @Prop({ default: null })
  pushTokenUpdatedAt: Date;
 
  createdAt: Date;
  updatedAt: Date;
}
 
export const TrustedDeviceSchema = SchemaFactory.createForClass(TrustedDevice);
TrustedDeviceSchema.index({ userId: 1, deviceId: 1 }, { unique: true });
TrustedDeviceSchema.index({ userId: 1, isTrusted: 1 });
TrustedDeviceSchema.index({ fingerprintHash: 1 });
 
// ============================================================
// DEVICE REGISTRATION — Pending device trust verification
// ============================================================
 
@Schema({ timestamps: true, collection: 'device_registrations' })
export class DeviceRegistration extends Document {
  @Prop({ required: true, index: true })
  userId: string;
 
  @Prop({ required: true })
  tenantId: string;
 
  @Prop({ required: true })
  deviceId: string;
 
  @Prop({ required: true, unique: true })
  verificationCode: string;
 
  @Prop({ required: true })
  expiresAt: Date;
 
  @Prop({ default: false })
  isVerified: boolean;
 
  @Prop({ default: null })
  verifiedAt: Date;
 
  @Prop({ default: null })
  ipAddress: string;
 
  @Prop({ default: null })
  userAgent: string;
 
  createdAt: Date;
  updatedAt: Date;
}
 
export const DeviceRegistrationSchema =
  SchemaFactory.createForClass(DeviceRegistration);
DeviceRegistrationSchema.index({ verificationCode: 1 }, { unique: true });
DeviceRegistrationSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });
 
// ============================================================
// DEVICE RISK EVENT — Risk signals from device behavior
// ============================================================
 
@Schema({ timestamps: true, collection: 'device_risk_events' })
export class DeviceRiskEvent extends Document {
  @Prop({ required: true, index: true })
  userId: string;
 
  @Prop({ required: true })
  tenantId: string;
 
  @Prop({ default: null })
  deviceId: string;
 
  @Prop({
    required: true,
    enum: [
      'new_device',
      'new_location',
      'impossible_travel',
      'fingerprint_mismatch',
      'suspicious_user_agent',
      'multiple_failed_attempts',
      'token_reuse_detected',
      'concurrent_sessions_exceeded',
    ],
  })
  riskType: string;
 
  @Prop({ default: 0 })
  riskScore: number; // 0-100
 
  @Prop({ type: Object, default: {} })
  details: Record<string, any>;
 
  @Prop({ default: null })
  ipAddress: string;
 
  @Prop({ default: null })
  location: string;
 
  @Prop({ required: true, enum: ['logged', 'alerted', 'blocked', 'dismissed'] })
  action: string;
 
  @Prop({ default: null })
  resolvedBy: string;
 
  @Prop({ default: null })
  resolvedAt: Date;
 
  createdAt: Date;
  updatedAt: Date;
}
 
export const DeviceRiskEventSchema =
  SchemaFactory.createForClass(DeviceRiskEvent);
DeviceRiskEventSchema.index({ userId: 1, createdAt: -1 });
DeviceRiskEventSchema.index({ tenantId: 1, riskType: 1, createdAt: -1 });