@nauth-toolkit/core 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/LICENSE +90 -0
  2. package/README.md +30 -0
  3. package/package.json +7 -2
  4. package/jest.config.js +0 -15
  5. package/jest.setup.ts +0 -6
  6. package/src/adapters/database-columns.ts +0 -165
  7. package/src/adapters/express.adapter.ts +0 -385
  8. package/src/adapters/fastify.adapter.ts +0 -416
  9. package/src/adapters/index.ts +0 -16
  10. package/src/adapters/storage.factory.ts +0 -143
  11. package/src/bootstrap.ts +0 -374
  12. package/src/dto/auth-challenge.dto.ts +0 -231
  13. package/src/dto/auth-response.dto.ts +0 -253
  14. package/src/dto/challenge-response.dto.ts +0 -234
  15. package/src/dto/change-password-request.dto.ts +0 -50
  16. package/src/dto/change-password-response.dto.ts +0 -29
  17. package/src/dto/change-password.dto.ts +0 -57
  18. package/src/dto/error-response.dto.ts +0 -136
  19. package/src/dto/get-available-methods.dto.ts +0 -55
  20. package/src/dto/get-challenge-data-response.dto.ts +0 -28
  21. package/src/dto/get-challenge-data.dto.ts +0 -69
  22. package/src/dto/get-client-info.dto.ts +0 -104
  23. package/src/dto/get-device-token-response.dto.ts +0 -25
  24. package/src/dto/get-events-by-type.dto.ts +0 -76
  25. package/src/dto/get-ip-address-response.dto.ts +0 -24
  26. package/src/dto/get-mfa-status.dto.ts +0 -94
  27. package/src/dto/get-risk-assessment-history.dto.ts +0 -39
  28. package/src/dto/get-session-id-response.dto.ts +0 -25
  29. package/src/dto/get-setup-data-response.dto.ts +0 -31
  30. package/src/dto/get-setup-data.dto.ts +0 -75
  31. package/src/dto/get-suspicious-activity.dto.ts +0 -42
  32. package/src/dto/get-user-agent-response.dto.ts +0 -23
  33. package/src/dto/get-user-auth-history.dto.ts +0 -95
  34. package/src/dto/get-user-by-email.dto.ts +0 -61
  35. package/src/dto/get-user-by-id.dto.ts +0 -46
  36. package/src/dto/get-user-devices.dto.ts +0 -53
  37. package/src/dto/get-user-response.dto.ts +0 -17
  38. package/src/dto/has-provider.dto.ts +0 -56
  39. package/src/dto/index.ts +0 -57
  40. package/src/dto/is-trusted-device-response.dto.ts +0 -34
  41. package/src/dto/list-providers-response.dto.ts +0 -23
  42. package/src/dto/login.dto.ts +0 -95
  43. package/src/dto/logout-all-response.dto.ts +0 -24
  44. package/src/dto/logout-all.dto.ts +0 -65
  45. package/src/dto/logout-response.dto.ts +0 -25
  46. package/src/dto/logout.dto.ts +0 -64
  47. package/src/dto/refresh-token.dto.ts +0 -36
  48. package/src/dto/remove-devices.dto.ts +0 -85
  49. package/src/dto/resend-code-response.dto.ts +0 -32
  50. package/src/dto/resend-code.dto.ts +0 -51
  51. package/src/dto/reset-password.dto.ts +0 -115
  52. package/src/dto/respond-challenge.dto.ts +0 -272
  53. package/src/dto/set-mfa-exemption.dto.ts +0 -112
  54. package/src/dto/set-must-change-password-response.dto.ts +0 -27
  55. package/src/dto/set-must-change-password.dto.ts +0 -46
  56. package/src/dto/set-preferred-method.dto.ts +0 -80
  57. package/src/dto/setup-mfa.dto.ts +0 -98
  58. package/src/dto/signup.dto.ts +0 -174
  59. package/src/dto/social-auth.dto.ts +0 -422
  60. package/src/dto/trust-device-response.dto.ts +0 -30
  61. package/src/dto/trust-device.dto.ts +0 -9
  62. package/src/dto/update-user-attributes-request.dto.ts +0 -51
  63. package/src/dto/user-response.dto.ts +0 -138
  64. package/src/dto/user-update.dto.ts +0 -222
  65. package/src/dto/verify-email.dto.ts +0 -313
  66. package/src/dto/verify-mfa-code.dto.ts +0 -103
  67. package/src/dto/verify-phone-by-sub.dto.ts +0 -78
  68. package/src/dto/verify-phone.dto.ts +0 -245
  69. package/src/entities/auth-audit.entity.ts +0 -232
  70. package/src/entities/challenge-session.entity.ts +0 -116
  71. package/src/entities/index.ts +0 -29
  72. package/src/entities/login-attempt.entity.ts +0 -64
  73. package/src/entities/mfa-device.entity.ts +0 -151
  74. package/src/entities/rate-limit.entity.ts +0 -44
  75. package/src/entities/session.entity.ts +0 -180
  76. package/src/entities/social-account.entity.ts +0 -96
  77. package/src/entities/storage-lock.entity.ts +0 -39
  78. package/src/entities/trusted-device.entity.ts +0 -112
  79. package/src/entities/user.entity.ts +0 -243
  80. package/src/entities/verification-token.entity.ts +0 -141
  81. package/src/enums/auth-audit-event-type.enum.ts +0 -360
  82. package/src/enums/error-codes.enum.ts +0 -420
  83. package/src/enums/mfa-method.enum.ts +0 -97
  84. package/src/enums/risk-factor.enum.ts +0 -111
  85. package/src/exceptions/nauth.exception.ts +0 -231
  86. package/src/handlers/auth.handler.ts +0 -260
  87. package/src/handlers/client-info.handler.ts +0 -101
  88. package/src/handlers/csrf.handler.ts +0 -156
  89. package/src/handlers/token-delivery.handler.ts +0 -118
  90. package/src/index.ts +0 -118
  91. package/src/interfaces/client-info.interface.ts +0 -85
  92. package/src/interfaces/config.interface.ts +0 -2135
  93. package/src/interfaces/entities.interface.ts +0 -226
  94. package/src/interfaces/index.ts +0 -15
  95. package/src/interfaces/logger.interface.ts +0 -283
  96. package/src/interfaces/mfa-provider.interface.ts +0 -154
  97. package/src/interfaces/oauth.interface.ts +0 -148
  98. package/src/interfaces/provider.interface.ts +0 -47
  99. package/src/interfaces/social-auth-provider.interface.ts +0 -131
  100. package/src/interfaces/storage-adapter.interface.ts +0 -82
  101. package/src/interfaces/template.interface.ts +0 -510
  102. package/src/interfaces/token-verifier.interface.ts +0 -110
  103. package/src/internal.ts +0 -178
  104. package/src/platform/interfaces.ts +0 -299
  105. package/src/schemas/auth-config.schema.ts +0 -646
  106. package/src/services/adaptive-mfa-decision.service.spec.ts +0 -1058
  107. package/src/services/adaptive-mfa-decision.service.ts +0 -457
  108. package/src/services/auth-audit.service.spec.ts +0 -675
  109. package/src/services/auth-audit.service.ts +0 -558
  110. package/src/services/auth-challenge-helper.service.spec.ts +0 -3227
  111. package/src/services/auth-challenge-helper.service.ts +0 -825
  112. package/src/services/auth-flow-context-builder.service.ts +0 -520
  113. package/src/services/auth-flow-rules.ts +0 -202
  114. package/src/services/auth-flow-state-definitions.ts +0 -190
  115. package/src/services/auth-flow-state-machine.service.ts +0 -207
  116. package/src/services/auth-flow-state-machine.types.ts +0 -316
  117. package/src/services/auth.service.spec.ts +0 -4195
  118. package/src/services/auth.service.ts +0 -3727
  119. package/src/services/challenge.service.spec.ts +0 -1363
  120. package/src/services/challenge.service.ts +0 -696
  121. package/src/services/client-info.service.spec.ts +0 -572
  122. package/src/services/client-info.service.ts +0 -374
  123. package/src/services/csrf.service.ts +0 -54
  124. package/src/services/email-verification.service.spec.ts +0 -1229
  125. package/src/services/email-verification.service.ts +0 -578
  126. package/src/services/geo-location.service.spec.ts +0 -603
  127. package/src/services/geo-location.service.ts +0 -599
  128. package/src/services/index.ts +0 -13
  129. package/src/services/jwt.service.spec.ts +0 -882
  130. package/src/services/jwt.service.ts +0 -621
  131. package/src/services/mfa-base.service.spec.ts +0 -246
  132. package/src/services/mfa-base.service.ts +0 -611
  133. package/src/services/mfa.service.spec.ts +0 -693
  134. package/src/services/mfa.service.ts +0 -960
  135. package/src/services/password.service.spec.ts +0 -166
  136. package/src/services/password.service.ts +0 -309
  137. package/src/services/phone-verification.service.spec.ts +0 -1120
  138. package/src/services/phone-verification.service.ts +0 -751
  139. package/src/services/risk-detection.service.spec.ts +0 -1292
  140. package/src/services/risk-detection.service.ts +0 -1012
  141. package/src/services/risk-scoring.service.spec.ts +0 -204
  142. package/src/services/risk-scoring.service.ts +0 -131
  143. package/src/services/session.service.spec.ts +0 -1293
  144. package/src/services/session.service.ts +0 -803
  145. package/src/services/social-account.service.spec.ts +0 -725
  146. package/src/services/social-auth-base.service.spec.ts +0 -418
  147. package/src/services/social-auth-base.service.ts +0 -581
  148. package/src/services/social-auth.service.spec.ts +0 -238
  149. package/src/services/social-auth.service.ts +0 -436
  150. package/src/services/social-provider-registry.service.spec.ts +0 -238
  151. package/src/services/social-provider-registry.service.ts +0 -122
  152. package/src/services/trusted-device.service.spec.ts +0 -505
  153. package/src/services/trusted-device.service.ts +0 -339
  154. package/src/storage/account-lockout-storage.service.spec.ts +0 -310
  155. package/src/storage/account-lockout-storage.service.ts +0 -89
  156. package/src/storage/index.ts +0 -3
  157. package/src/storage/memory-storage.adapter.ts +0 -443
  158. package/src/storage/rate-limit-storage.service.spec.ts +0 -247
  159. package/src/storage/rate-limit-storage.service.ts +0 -38
  160. package/src/templates/html-template.engine.spec.ts +0 -161
  161. package/src/templates/html-template.engine.ts +0 -688
  162. package/src/templates/index.ts +0 -7
  163. package/src/utils/common-passwords.spec.ts +0 -230
  164. package/src/utils/common-passwords.ts +0 -170
  165. package/src/utils/context-storage.ts +0 -188
  166. package/src/utils/cookie-names.util.ts +0 -67
  167. package/src/utils/cookies.util.ts +0 -94
  168. package/src/utils/index.ts +0 -12
  169. package/src/utils/ip-extractor.spec.ts +0 -330
  170. package/src/utils/ip-extractor.ts +0 -220
  171. package/src/utils/nauth-logger.spec.ts +0 -388
  172. package/src/utils/nauth-logger.ts +0 -215
  173. package/src/utils/pii-redactor.spec.ts +0 -130
  174. package/src/utils/pii-redactor.ts +0 -288
  175. package/src/utils/setup/get-repositories.ts +0 -140
  176. package/src/utils/setup/init-services.ts +0 -422
  177. package/src/utils/setup/init-social.ts +0 -189
  178. package/src/utils/setup/init-storage.ts +0 -94
  179. package/src/utils/setup/register-mfa.ts +0 -165
  180. package/src/utils/setup/run-nauth-migrations.ts +0 -61
  181. package/src/utils/token-delivery-policy.ts +0 -38
  182. package/src/validators/template.validator.ts +0 -219
  183. package/tsconfig.json +0 -37
  184. package/tsconfig.lint.json +0 -6
@@ -1,116 +0,0 @@
1
- /**
2
- * Base Challenge Session Entity
3
- *
4
- * Stores temporary authentication challenge sessions.
5
- * These are short-lived sessions used to track pending challenges
6
- * that must be completed before full authentication is granted.
7
- * Database adapters extend this class and add ORM-specific decorators.
8
- *
9
- * @remarks
10
- * Similar to AWS Cognito's challenge sessions, these are NOT full JWT tokens.
11
- * They expire quickly (typically 15 minutes) and are deleted after completion.
12
- * This class is database-agnostic. TypeORM, Prisma, or other ORMs
13
- * extend this class in their respective packages.
14
- *
15
- * @example
16
- * ```typescript
17
- * // Creating a challenge session after signup
18
- * const challengeSession = new ChallengeSession();
19
- * challengeSession.userId = user.id;
20
- * challengeSession.challengeName = 'VERIFY_EMAIL';
21
- * challengeSession.sessionToken = randomUUID();
22
- * challengeSession.expiresAt = new Date(Date.now() + 15 * 60 * 1000);
23
- * challengeSession.metadata = { email: user.email };
24
- * ```
25
- */
26
- export class BaseChallengeSession {
27
- /**
28
- * Primary key
29
- */
30
- id!: number;
31
-
32
- /**
33
- * User ID foreign key
34
- * References the user this challenge session belongs to
35
- */
36
- userId!: number;
37
-
38
- /**
39
- * Challenge type that must be completed
40
- *
41
- * @example 'VERIFY_EMAIL', 'VERIFY_PHONE', 'MFA_REQUIRED', 'FORCE_CHANGE_PASSWORD'
42
- */
43
- challengeName!: string;
44
-
45
- /**
46
- * Temporary session token (UUID)
47
- * This is returned to the client and must be submitted when responding to challenge
48
- */
49
- sessionToken!: string;
50
-
51
- /**
52
- * Session expiration time
53
- * Challenge sessions are short-lived (typically 15 minutes)
54
- */
55
- expiresAt!: Date;
56
-
57
- /**
58
- * Whether this challenge has been completed successfully
59
- * Completed challenges cannot be attempted again
60
- */
61
- isCompleted?: boolean;
62
-
63
- /**
64
- * When the challenge was completed successfully
65
- * NULL if not yet completed
66
- */
67
- completedAt?: Date | null;
68
-
69
- /**
70
- * Number of failed attempts to complete this challenge
71
- * Used to prevent brute-force attacks on verification codes
72
- */
73
- attempts!: number;
74
-
75
- /**
76
- * Maximum allowed attempts before session is invalidated
77
- */
78
- maxAttempts!: number;
79
-
80
- /**
81
- * Challenge-specific metadata
82
- * Stores information needed for challenge completion
83
- *
84
- * @example
85
- * ```typescript
86
- * {
87
- * email: 'user@example.com',
88
- * phone: '+1234567890',
89
- * verificationTokenId: 123
90
- * }
91
- * ```
92
- */
93
- challengeParameters?: Record<string, unknown> | null;
94
-
95
- /**
96
- * Additional metadata (alias for challengeParameters for backwards compatibility)
97
- */
98
- metadata?: Record<string, unknown> | null;
99
-
100
- /**
101
- * IP address where the challenge session was created
102
- * For security auditing
103
- */
104
- ipAddress?: string | null;
105
-
106
- /**
107
- * User agent where the challenge session was created
108
- * For security auditing
109
- */
110
- userAgent?: string | null;
111
-
112
- /**
113
- * Creation timestamp
114
- */
115
- createdAt!: Date;
116
- }
@@ -1,29 +0,0 @@
1
- /**
2
- * Base Entity Classes
3
- *
4
- * Database-agnostic entity classes containing all fields and business logic.
5
- * Database adapters (TypeORM, Prisma, etc.) extend these classes and add ORM-specific decorators.
6
- *
7
- * @remarks
8
- * These base classes provide:
9
- * - Field definitions
10
- * - Business logic methods
11
- * - JSDoc documentation
12
- * - Type safety
13
- *
14
- * Database packages add:
15
- * - ORM decorators (@Entity, @Column, etc.)
16
- * - Database-specific configuration
17
- * - Indexes and constraints
18
- */
19
- export { BaseUser } from './user.entity';
20
- export { BaseSession } from './session.entity';
21
- export { BaseTrustedDevice } from './trusted-device.entity';
22
- export { BaseLoginAttempt } from './login-attempt.entity';
23
- export { BaseVerificationToken } from './verification-token.entity';
24
- export { BaseSocialAccount } from './social-account.entity';
25
- export { BaseChallengeSession } from './challenge-session.entity';
26
- export { BaseMFADevice } from './mfa-device.entity';
27
- export { BaseAuthAudit, type AuthAuditEventStatus } from './auth-audit.entity';
28
- export { BaseRateLimit } from './rate-limit.entity';
29
- export { BaseStorageLock } from './storage-lock.entity';
@@ -1,64 +0,0 @@
1
- /**
2
- * Base Login Attempt Entity
3
- *
4
- * Failed login tracking for security auditing and rate limiting.
5
- * Database adapters extend this class and add ORM-specific decorators.
6
- *
7
- * @remarks
8
- * This class is database-agnostic. TypeORM, Prisma, or other ORMs
9
- * extend this class in their respective packages.
10
- */
11
- export class BaseLoginAttempt {
12
- /**
13
- * Internal login attempt ID
14
- */
15
- id!: number;
16
-
17
- /**
18
- * Email/username used in login attempt
19
- * May be NULL for malformed requests
20
- */
21
- email?: string | null;
22
-
23
- /**
24
- * Internal user ID (foreign key to users table)
25
- * Nullable since login attempt might be for non-existent user
26
- */
27
- userId?: number | null;
28
-
29
- /**
30
- * IP address of login attempt
31
- */
32
- ipAddress?: string | null;
33
-
34
- /**
35
- * User agent string
36
- */
37
- userAgent?: string | null;
38
-
39
- /**
40
- * Whether login was successful
41
- */
42
- success!: boolean;
43
-
44
- /**
45
- * Reason for login failure
46
- * Examples: "invalid_credentials", "account_locked", "mfa_required"
47
- */
48
- failureReason?: string | null;
49
-
50
- /**
51
- * Whether MFA was required for this attempt
52
- */
53
- mfaRequired!: boolean;
54
-
55
- /**
56
- * Additional metadata (JSON)
57
- */
58
- metadata?: Record<string, unknown> | null;
59
-
60
- /**
61
- * Login attempt timestamp
62
- */
63
- createdAt!: Date;
64
- }
@@ -1,151 +0,0 @@
1
- import { MFADeviceMethod } from '../enums/mfa-method.enum';
2
-
3
- /**
4
- * Base MFA Device Entity
5
- *
6
- * Stores multi-factor authentication device registrations.
7
- * Supports multiple MFA methods: TOTP (authenticator apps), SMS, Email, and Passkeys (WebAuthn).
8
- * Database adapters extend this class and add ORM-specific decorators.
9
- *
10
- * @remarks
11
- * Each user can register multiple MFA devices of different types for redundancy.
12
- * Devices can be enabled/disabled without deletion for security audit purposes.
13
- * This class is database-agnostic. TypeORM, Prisma, or other ORMs
14
- * extend this class in their respective packages.
15
- *
16
- * @example
17
- * ```typescript
18
- * // Create TOTP device
19
- * const totpDevice = new MFADevice();
20
- * totpDevice.userId = user.id;
21
- * totpDevice.type = 'totp';
22
- * totpDevice.name = 'Google Authenticator';
23
- * totpDevice.secret = encryptedSecret;
24
- * totpDevice.isActive = true;
25
- *
26
- * // Create Passkey device
27
- * const passkeyDevice = new MFADevice();
28
- * passkeyDevice.userId = user.id;
29
- * passkeyDevice.type = 'passkey';
30
- * passkeyDevice.name = 'iPhone 15 Pro';
31
- * passkeyDevice.credentialId = 'credential-id-here';
32
- * passkeyDevice.publicKey = 'public-key-here';
33
- * passkeyDevice.counter = 0;
34
- * passkeyDevice.isActive = true;
35
- * ```
36
- */
37
- export class BaseMFADevice {
38
- /**
39
- * Internal device ID (auto-increment integer)
40
- */
41
- id!: number;
42
-
43
- /**
44
- * Internal user ID (foreign key to users table)
45
- * References the user who owns this MFA device
46
- */
47
- userId!: number;
48
-
49
- /**
50
- * MFA method type
51
- *
52
- * - 'totp': Time-based One-Time Password (Google Authenticator, Authy, etc.)
53
- * - 'sms': SMS-based verification codes
54
- * - 'email': Email-based verification codes
55
- * - 'passkey': WebAuthn/FIDO2 passkeys (biometric, security keys)
56
- */
57
- type!: MFADeviceMethod;
58
-
59
- /**
60
- * User-friendly device name
61
- * Helps users identify their devices (e.g., "iPhone 15 Pro", "Google Authenticator")
62
- */
63
- name!: string;
64
-
65
- /**
66
- * TOTP secret (encrypted)
67
- * Used only for TOTP devices
68
- * ⚠️ SECURITY: Must be encrypted at rest
69
- */
70
- secret?: string | null;
71
-
72
- /**
73
- * Phone number for SMS MFA
74
- * Used only for SMS devices
75
- * Must be in E.164 format (e.g., +1234567890)
76
- */
77
- phoneNumber?: string | null;
78
-
79
- /**
80
- * Email address for Email MFA
81
- * Used only for Email devices
82
- * Must be a valid email address format
83
- */
84
- email?: string | null;
85
-
86
- /**
87
- * WebAuthn credential ID (base64url encoded)
88
- * Unique identifier for this passkey
89
- * Used only for passkey devices
90
- */
91
- credentialId?: string | null;
92
-
93
- /**
94
- * WebAuthn public key (base64url encoded)
95
- * Used to verify passkey signatures
96
- * Used only for passkey devices
97
- */
98
- publicKey?: string | null;
99
-
100
- /**
101
- * WebAuthn signature counter
102
- * Incremented with each authentication to prevent replay attacks
103
- * Used only for passkey devices
104
- */
105
- counter?: number | null;
106
-
107
- /**
108
- * WebAuthn transports (USB, NFC, BLE, internal)
109
- * Helps browser suggest the right authentication method
110
- * Used only for passkey devices
111
- */
112
- transports?: string[] | null;
113
-
114
- /**
115
- * Whether this device is currently active
116
- * Inactive devices cannot be used for authentication but remain in database for audit
117
- */
118
- isActive!: boolean;
119
-
120
- /**
121
- * Whether this is the user's preferred/primary MFA method
122
- * Used to pre-select MFA method during authentication
123
- */
124
- isPrimary!: boolean;
125
-
126
- /**
127
- * Last time this device was used for authentication
128
- */
129
- lastUsedAt?: Date | null;
130
-
131
- /**
132
- * Number of times this device has been used
133
- * Useful for analytics and detecting suspicious patterns
134
- */
135
- usageCount!: number;
136
-
137
- /**
138
- * Additional device metadata (browser, OS, IP on registration, etc.)
139
- */
140
- metadata?: Record<string, unknown> | null;
141
-
142
- /**
143
- * Device registration timestamp
144
- */
145
- createdAt!: Date;
146
-
147
- /**
148
- * Last update timestamp
149
- */
150
- updatedAt!: Date;
151
- }
@@ -1,44 +0,0 @@
1
- /**
2
- * Base Rate Limit Entity
3
- *
4
- * Stores rate limiting counters for transient state management.
5
- * Used by DatabaseStorageAdapter to track rate limits across multiple servers.
6
- *
7
- * @remarks
8
- * This class is database-agnostic. TypeORM, Prisma, or other ORMs
9
- * extend this class in their respective packages.
10
- */
11
- export class BaseRateLimit {
12
- /**
13
- * Internal rate limit record ID (auto-increment integer)
14
- */
15
- id!: number;
16
-
17
- /**
18
- * Unique key identifier for the rate limit counter
19
- * Format: <endpoint>:<identifier> (e.g., "email-verification:user:123")
20
- */
21
- key!: string;
22
-
23
- /**
24
- * Counter value (stored as string, parsed as integer)
25
- */
26
- value!: string;
27
-
28
- /**
29
- * Expiration timestamp
30
- * Used for TTL-based cleanup via scheduled jobs
31
- * Can be null for records that don't expire immediately
32
- */
33
- expiresAt!: Date | null;
34
-
35
- /**
36
- * Record creation timestamp
37
- */
38
- createdAt!: Date;
39
-
40
- /**
41
- * Record last update timestamp
42
- */
43
- updatedAt!: Date;
44
- }
@@ -1,180 +0,0 @@
1
- /**
2
- * Base Session Entity
3
- *
4
- * JWT session tracking with device information and security features.
5
- * Database adapters extend this class and add ORM-specific decorators.
6
- *
7
- * @remarks
8
- * This class is database-agnostic. TypeORM, Prisma, or other ORMs
9
- * extend this class in their respective packages.
10
- */
11
- export class BaseSession {
12
- /**
13
- * Internal session ID (auto-increment integer)
14
- */
15
- id!: number;
16
-
17
- /**
18
- * Version for optimistic locking
19
- * Automatically incremented on each update by the ORM
20
- * Used to detect race conditions and concurrent modifications
21
- *
22
- * ⚠️ SECURITY CRITICAL: Prevents TOCTOU vulnerabilities
23
- */
24
- version!: number;
25
-
26
- /**
27
- * Internal user ID (foreign key to users table)
28
- * Uses integer for optimal performance in joins and lookups
29
- */
30
- userId!: number;
31
-
32
- /**
33
- * Access token hash (SHA-256)
34
- * Used for token revocation and session tracking
35
- */
36
- accessTokenHash!: string;
37
-
38
- /**
39
- * Refresh token hash (SHA-256)
40
- * Used for token rotation and reuse detection
41
- */
42
- refreshTokenHash!: string;
43
-
44
- /**
45
- * Token family identifier
46
- * Used for refresh token rotation and reuse detection
47
- */
48
- tokenFamily?: string | null;
49
-
50
- /**
51
- * Device identifier (UUID)
52
- * Unique identifier for the device/browser
53
- */
54
- deviceId?: string | null;
55
-
56
- /**
57
- * User-friendly device name
58
- * Examples: "iPhone 15 Pro", "Chrome on MacBook"
59
- */
60
- deviceName?: string | null;
61
-
62
- /**
63
- * Device type
64
- * Examples: "mobile", "desktop", "tablet"
65
- */
66
- deviceType?: string | null;
67
-
68
- /**
69
- * Device fingerprint hash
70
- * Combination of device characteristics for additional security
71
- */
72
- deviceFingerprint?: string | null;
73
-
74
- /**
75
- * IP address when session was created
76
- */
77
- ipAddress?: string | null;
78
-
79
- /**
80
- * Country from IP geolocation (optional)
81
- */
82
- ipCountry?: string | null;
83
-
84
- /**
85
- * City from IP geolocation (optional)
86
- */
87
- ipCity?: string | null;
88
-
89
- /**
90
- * Latitude from IP geolocation (optional)
91
- * Used for impossible travel detection
92
- */
93
- ipLatitude?: number | null;
94
-
95
- /**
96
- * Longitude from IP geolocation (optional)
97
- * Used for impossible travel detection
98
- */
99
- ipLongitude?: number | null;
100
-
101
- /**
102
- * ISP from IP geolocation (optional)
103
- */
104
- ipIsp?: string | null;
105
-
106
- /**
107
- * User agent string
108
- */
109
- userAgent?: string | null;
110
-
111
- /**
112
- * Platform extracted from user agent
113
- * Examples: "iOS", "Android", "Windows", "macOS"
114
- */
115
- platform?: string | null;
116
-
117
- /**
118
- * Browser extracted from user agent
119
- * Examples: "Chrome", "Safari", "Firefox"
120
- */
121
- browser?: string | null;
122
-
123
- /**
124
- * Authentication method used to create this session
125
- * Examples: "password", "google", "facebook", "github", "apple"
126
- * null for legacy sessions
127
- */
128
- authMethod?: string | null;
129
-
130
- /**
131
- * "Remember me" flag
132
- * Longer expiration for remembered sessions
133
- */
134
- isRemembered!: boolean;
135
-
136
- /**
137
- * Trusted device flag
138
- * Trusted devices may skip MFA
139
- */
140
- isTrustedDevice!: boolean;
141
-
142
- /**
143
- * Session expiration timestamp
144
- * After this time, session is invalid
145
- */
146
- expiresAt!: Date;
147
-
148
- /**
149
- * Last activity timestamp
150
- * Updated on each API request
151
- */
152
- lastActivityAt?: Date | null;
153
-
154
- /**
155
- * Session revocation status
156
- * Revoked sessions cannot be used
157
- */
158
- isRevoked!: boolean;
159
-
160
- /**
161
- * When session was revoked
162
- */
163
- revokedAt?: Date | null;
164
-
165
- /**
166
- * Reason for session revocation
167
- * Examples: "user_logout", "token_reuse_detected", "admin_revoked"
168
- */
169
- revokeReason?: string | null;
170
-
171
- /**
172
- * Additional session metadata (JSON)
173
- */
174
- metadata?: Record<string, unknown> | null;
175
-
176
- /**
177
- * Session creation timestamp
178
- */
179
- createdAt!: Date;
180
- }
@@ -1,96 +0,0 @@
1
- /**
2
- * Base Social Account Entity
3
- *
4
- * Stores OAuth provider linkage (no token storage, one-time attribute import).
5
- * Each record represents a user's account linked to a specific social provider.
6
- * Database adapters extend this class and add ORM-specific decorators.
7
- *
8
- * @remarks
9
- * This class is database-agnostic. TypeORM, Prisma, or other ORMs
10
- * extend this class in their respective packages.
11
- *
12
- * @example
13
- * ```typescript
14
- * // User has Google and Apple accounts linked
15
- * const socialAccounts = [
16
- * { provider: 'google', providerId: 'google_123', providerEmail: 'user@gmail.com' },
17
- * { provider: 'apple', providerId: 'apple_456', providerEmail: 'user@icloud.com' }
18
- * ];
19
- * ```
20
- */
21
- export class BaseSocialAccount {
22
- /**
23
- * Internal database ID (auto-increment integer)
24
- * Used for foreign key relationships and internal queries
25
- * NOT exposed externally
26
- */
27
- id!: number;
28
-
29
- /**
30
- * Foreign key to users table
31
- * References the user who owns this social account
32
- */
33
- userId!: number;
34
-
35
- /**
36
- * Social provider name
37
- * Examples: 'google', 'apple', 'facebook'
38
- */
39
- provider!: string;
40
-
41
- /**
42
- * Provider's unique identifier for this user
43
- * This is the ID that the OAuth provider uses to identify the user
44
- * Examples: Google sub, Apple user ID, Facebook ID
45
- */
46
- providerId!: string;
47
-
48
- /**
49
- * Email address from the provider (for audit/debugging)
50
- * May be different from user's primary email if they have multiple emails
51
- * Used for account linking by email verification
52
- */
53
- providerEmail?: string | null;
54
-
55
- /**
56
- * When this social account was linked to the user
57
- * Used for audit trails and account management
58
- */
59
- linkedAt!: Date;
60
-
61
- /**
62
- * When this social account was last used for authentication
63
- * Updated on each successful social login
64
- * Used for analytics and account cleanup
65
- */
66
- lastUsedAt?: Date | null;
67
-
68
- /**
69
- * Raw OAuth profile data from provider (for debugging)
70
- * Contains the full response from the OAuth provider
71
- * Useful for troubleshooting and attribute mapping
72
- *
73
- * @example
74
- * ```json
75
- * {
76
- * "sub": "google_123",
77
- * "email": "user@gmail.com",
78
- * "given_name": "John",
79
- * "family_name": "Doe",
80
- * "picture": "https://...",
81
- * "locale": "en"
82
- * }
83
- * ```
84
- */
85
- metadata?: Record<string, unknown> | null;
86
-
87
- /**
88
- * Account creation timestamp
89
- */
90
- createdAt!: Date;
91
-
92
- /**
93
- * Last account update timestamp
94
- */
95
- updatedAt!: Date;
96
- }