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 | import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; // ============================================================ // USER SESSION — Active user sessions across devices // ============================================================ @Schema({ timestamps: true, collection: 'user_sessions' }) export class UserSession extends Document { @Prop({ required: true, index: true }) userId: string; @Prop({ required: true, index: true }) tenantId: string; @Prop({ required: true, unique: true }) sessionId: string; @Prop({ default: null }) refreshTokenHash: string; // SHA-256 of the active refresh token for this session @Prop({ default: null }) deviceId: string; // Links to TrustedDevice @Prop({ default: null }) deviceName: string; @Prop({ default: null }) deviceType: string; @Prop({ default: null }) browser: string; @Prop({ default: null }) os: string; @Prop({ default: null }) ipAddress: string; @Prop({ default: null }) location: string; @Prop({ required: true, enum: ['active', 'expired', 'revoked', 'logged_out'], }) status: string; @Prop({ required: true }) createdAt: Date; @Prop({ required: true }) expiresAt: Date; @Prop({ default: null }) lastActivityAt: Date; @Prop({ default: null }) revokedAt: Date; @Prop({ default: null }) revokedBy: string; @Prop({ default: null }) revokedReason: string; @Prop({ default: false }) isCurrent: boolean; // Marks the session requesting the list @Prop({ default: null }) loginMethod: string; // 'email' | 'google' | 'apple' updatedAt: Date; } export const UserSessionSchema = SchemaFactory.createForClass(UserSession); UserSessionSchema.index({ sessionId: 1 }, { unique: true }); UserSessionSchema.index({ userId: 1, status: 1 }); UserSessionSchema.index({ refreshTokenHash: 1 }); UserSessionSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 86400 }); // Clean up 24h after expiry // ============================================================ // SESSION TOKEN FAMILY — Tracks refresh token chains for reuse detection // ============================================================ @Schema({ timestamps: true, collection: 'session_token_families' }) export class SessionTokenFamily extends Document { @Prop({ required: true, index: true }) sessionId: string; @Prop({ required: true, index: true }) userId: string; @Prop({ required: true }) tenantId: string; @Prop({ required: true, unique: true }) familyId: string; // Root family identifier @Prop({ default: 1 }) generation: number; // Increments with each rotation @Prop({ required: true }) currentTokenHash: string; @Prop({ default: null }) previousTokenHash: string; @Prop({ default: false }) isCompromised: boolean; // True if reuse detected @Prop({ default: null }) compromisedAt: Date; @Prop({ required: true }) expiresAt: Date; createdAt: Date; updatedAt: Date; } export const SessionTokenFamilySchema = SchemaFactory.createForClass(SessionTokenFamily); SessionTokenFamilySchema.index({ familyId: 1 }, { unique: true }); SessionTokenFamilySchema.index({ currentTokenHash: 1 }); // ============================================================ // SESSION ACTIVITY — Per-session activity trail // ============================================================ @Schema({ timestamps: true, collection: 'session_activities' }) export class SessionActivity extends Document { @Prop({ required: true, index: true }) sessionId: string; @Prop({ required: true }) userId: string; @Prop({ required: true }) tenantId: string; @Prop({ required: true, enum: [ 'session_created', 'token_refreshed', 'session_expired', 'session_revoked', 'session_logged_out', 'password_changed', 'mfa_completed', 'permission_escalation', ], }) activity: 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 SessionActivitySchema = SchemaFactory.createForClass(SessionActivity); SessionActivitySchema.index({ sessionId: 1, createdAt: -1 }); SessionActivitySchema.index({ userId: 1, createdAt: -1 }); |