@nauth-toolkit/core 0.1.0 → 0.1.5
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.
- package/LICENSE +90 -0
- package/README.md +9 -0
- package/package.json +8 -3
- package/jest.config.js +0 -15
- package/jest.setup.ts +0 -6
- package/src/adapters/database-columns.ts +0 -165
- package/src/adapters/express.adapter.ts +0 -385
- package/src/adapters/fastify.adapter.ts +0 -416
- package/src/adapters/index.ts +0 -16
- package/src/adapters/storage.factory.ts +0 -143
- package/src/bootstrap.ts +0 -374
- package/src/dto/auth-challenge.dto.ts +0 -231
- package/src/dto/auth-response.dto.ts +0 -253
- package/src/dto/challenge-response.dto.ts +0 -234
- package/src/dto/change-password-request.dto.ts +0 -50
- package/src/dto/change-password-response.dto.ts +0 -29
- package/src/dto/change-password.dto.ts +0 -57
- package/src/dto/error-response.dto.ts +0 -136
- package/src/dto/get-available-methods.dto.ts +0 -55
- package/src/dto/get-challenge-data-response.dto.ts +0 -28
- package/src/dto/get-challenge-data.dto.ts +0 -69
- package/src/dto/get-client-info.dto.ts +0 -104
- package/src/dto/get-device-token-response.dto.ts +0 -25
- package/src/dto/get-events-by-type.dto.ts +0 -76
- package/src/dto/get-ip-address-response.dto.ts +0 -24
- package/src/dto/get-mfa-status.dto.ts +0 -94
- package/src/dto/get-risk-assessment-history.dto.ts +0 -39
- package/src/dto/get-session-id-response.dto.ts +0 -25
- package/src/dto/get-setup-data-response.dto.ts +0 -31
- package/src/dto/get-setup-data.dto.ts +0 -75
- package/src/dto/get-suspicious-activity.dto.ts +0 -42
- package/src/dto/get-user-agent-response.dto.ts +0 -23
- package/src/dto/get-user-auth-history.dto.ts +0 -95
- package/src/dto/get-user-by-email.dto.ts +0 -61
- package/src/dto/get-user-by-id.dto.ts +0 -46
- package/src/dto/get-user-devices.dto.ts +0 -53
- package/src/dto/get-user-response.dto.ts +0 -17
- package/src/dto/has-provider.dto.ts +0 -56
- package/src/dto/index.ts +0 -57
- package/src/dto/is-trusted-device-response.dto.ts +0 -34
- package/src/dto/list-providers-response.dto.ts +0 -23
- package/src/dto/login.dto.ts +0 -95
- package/src/dto/logout-all-response.dto.ts +0 -24
- package/src/dto/logout-all.dto.ts +0 -65
- package/src/dto/logout-response.dto.ts +0 -25
- package/src/dto/logout.dto.ts +0 -64
- package/src/dto/refresh-token.dto.ts +0 -36
- package/src/dto/remove-devices.dto.ts +0 -85
- package/src/dto/resend-code-response.dto.ts +0 -32
- package/src/dto/resend-code.dto.ts +0 -51
- package/src/dto/reset-password.dto.ts +0 -115
- package/src/dto/respond-challenge.dto.ts +0 -272
- package/src/dto/set-mfa-exemption.dto.ts +0 -112
- package/src/dto/set-must-change-password-response.dto.ts +0 -27
- package/src/dto/set-must-change-password.dto.ts +0 -46
- package/src/dto/set-preferred-method.dto.ts +0 -80
- package/src/dto/setup-mfa.dto.ts +0 -98
- package/src/dto/signup.dto.ts +0 -174
- package/src/dto/social-auth.dto.ts +0 -422
- package/src/dto/trust-device-response.dto.ts +0 -30
- package/src/dto/trust-device.dto.ts +0 -9
- package/src/dto/update-user-attributes-request.dto.ts +0 -51
- package/src/dto/user-response.dto.ts +0 -138
- package/src/dto/user-update.dto.ts +0 -222
- package/src/dto/verify-email.dto.ts +0 -313
- package/src/dto/verify-mfa-code.dto.ts +0 -103
- package/src/dto/verify-phone-by-sub.dto.ts +0 -78
- package/src/dto/verify-phone.dto.ts +0 -245
- package/src/entities/auth-audit.entity.ts +0 -232
- package/src/entities/challenge-session.entity.ts +0 -116
- package/src/entities/index.ts +0 -29
- package/src/entities/login-attempt.entity.ts +0 -64
- package/src/entities/mfa-device.entity.ts +0 -151
- package/src/entities/rate-limit.entity.ts +0 -44
- package/src/entities/session.entity.ts +0 -180
- package/src/entities/social-account.entity.ts +0 -96
- package/src/entities/storage-lock.entity.ts +0 -39
- package/src/entities/trusted-device.entity.ts +0 -112
- package/src/entities/user.entity.ts +0 -243
- package/src/entities/verification-token.entity.ts +0 -141
- package/src/enums/auth-audit-event-type.enum.ts +0 -360
- package/src/enums/error-codes.enum.ts +0 -420
- package/src/enums/mfa-method.enum.ts +0 -97
- package/src/enums/risk-factor.enum.ts +0 -111
- package/src/exceptions/nauth.exception.ts +0 -231
- package/src/handlers/auth.handler.ts +0 -260
- package/src/handlers/client-info.handler.ts +0 -101
- package/src/handlers/csrf.handler.ts +0 -156
- package/src/handlers/token-delivery.handler.ts +0 -118
- package/src/index.ts +0 -118
- package/src/interfaces/client-info.interface.ts +0 -85
- package/src/interfaces/config.interface.ts +0 -2135
- package/src/interfaces/entities.interface.ts +0 -226
- package/src/interfaces/index.ts +0 -15
- package/src/interfaces/logger.interface.ts +0 -283
- package/src/interfaces/mfa-provider.interface.ts +0 -154
- package/src/interfaces/oauth.interface.ts +0 -148
- package/src/interfaces/provider.interface.ts +0 -47
- package/src/interfaces/social-auth-provider.interface.ts +0 -131
- package/src/interfaces/storage-adapter.interface.ts +0 -82
- package/src/interfaces/template.interface.ts +0 -510
- package/src/interfaces/token-verifier.interface.ts +0 -110
- package/src/internal.ts +0 -178
- package/src/platform/interfaces.ts +0 -299
- package/src/schemas/auth-config.schema.ts +0 -646
- package/src/services/adaptive-mfa-decision.service.spec.ts +0 -1058
- package/src/services/adaptive-mfa-decision.service.ts +0 -457
- package/src/services/auth-audit.service.spec.ts +0 -675
- package/src/services/auth-audit.service.ts +0 -558
- package/src/services/auth-challenge-helper.service.spec.ts +0 -3227
- package/src/services/auth-challenge-helper.service.ts +0 -825
- package/src/services/auth-flow-context-builder.service.ts +0 -520
- package/src/services/auth-flow-rules.ts +0 -202
- package/src/services/auth-flow-state-definitions.ts +0 -190
- package/src/services/auth-flow-state-machine.service.ts +0 -207
- package/src/services/auth-flow-state-machine.types.ts +0 -316
- package/src/services/auth.service.spec.ts +0 -4195
- package/src/services/auth.service.ts +0 -3727
- package/src/services/challenge.service.spec.ts +0 -1363
- package/src/services/challenge.service.ts +0 -696
- package/src/services/client-info.service.spec.ts +0 -572
- package/src/services/client-info.service.ts +0 -374
- package/src/services/csrf.service.ts +0 -54
- package/src/services/email-verification.service.spec.ts +0 -1229
- package/src/services/email-verification.service.ts +0 -578
- package/src/services/geo-location.service.spec.ts +0 -603
- package/src/services/geo-location.service.ts +0 -599
- package/src/services/index.ts +0 -13
- package/src/services/jwt.service.spec.ts +0 -882
- package/src/services/jwt.service.ts +0 -621
- package/src/services/mfa-base.service.spec.ts +0 -246
- package/src/services/mfa-base.service.ts +0 -611
- package/src/services/mfa.service.spec.ts +0 -693
- package/src/services/mfa.service.ts +0 -960
- package/src/services/password.service.spec.ts +0 -166
- package/src/services/password.service.ts +0 -309
- package/src/services/phone-verification.service.spec.ts +0 -1120
- package/src/services/phone-verification.service.ts +0 -751
- package/src/services/risk-detection.service.spec.ts +0 -1292
- package/src/services/risk-detection.service.ts +0 -1012
- package/src/services/risk-scoring.service.spec.ts +0 -204
- package/src/services/risk-scoring.service.ts +0 -131
- package/src/services/session.service.spec.ts +0 -1293
- package/src/services/session.service.ts +0 -803
- package/src/services/social-account.service.spec.ts +0 -725
- package/src/services/social-auth-base.service.spec.ts +0 -418
- package/src/services/social-auth-base.service.ts +0 -581
- package/src/services/social-auth.service.spec.ts +0 -238
- package/src/services/social-auth.service.ts +0 -436
- package/src/services/social-provider-registry.service.spec.ts +0 -238
- package/src/services/social-provider-registry.service.ts +0 -122
- package/src/services/trusted-device.service.spec.ts +0 -505
- package/src/services/trusted-device.service.ts +0 -339
- package/src/storage/account-lockout-storage.service.spec.ts +0 -310
- package/src/storage/account-lockout-storage.service.ts +0 -89
- package/src/storage/index.ts +0 -3
- package/src/storage/memory-storage.adapter.ts +0 -443
- package/src/storage/rate-limit-storage.service.spec.ts +0 -247
- package/src/storage/rate-limit-storage.service.ts +0 -38
- package/src/templates/html-template.engine.spec.ts +0 -161
- package/src/templates/html-template.engine.ts +0 -688
- package/src/templates/index.ts +0 -7
- package/src/utils/common-passwords.spec.ts +0 -230
- package/src/utils/common-passwords.ts +0 -170
- package/src/utils/context-storage.ts +0 -188
- package/src/utils/cookie-names.util.ts +0 -67
- package/src/utils/cookies.util.ts +0 -94
- package/src/utils/index.ts +0 -12
- package/src/utils/ip-extractor.spec.ts +0 -330
- package/src/utils/ip-extractor.ts +0 -220
- package/src/utils/nauth-logger.spec.ts +0 -388
- package/src/utils/nauth-logger.ts +0 -215
- package/src/utils/pii-redactor.spec.ts +0 -130
- package/src/utils/pii-redactor.ts +0 -288
- package/src/utils/setup/get-repositories.ts +0 -140
- package/src/utils/setup/init-services.ts +0 -422
- package/src/utils/setup/init-social.ts +0 -189
- package/src/utils/setup/init-storage.ts +0 -94
- package/src/utils/setup/register-mfa.ts +0 -165
- package/src/utils/setup/run-nauth-migrations.ts +0 -61
- package/src/utils/token-delivery-policy.ts +0 -38
- package/src/validators/template.validator.ts +0 -219
- package/tsconfig.json +0 -37
- package/tsconfig.lint.json +0 -6
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Entity Interface Contracts
|
|
3
|
-
*
|
|
4
|
-
* These interfaces define the shape of entities without importing concrete implementations.
|
|
5
|
-
* Database packages must implement these interfaces to ensure type safety across the modular architecture.
|
|
6
|
-
*
|
|
7
|
-
* This allows core to maintain strict typing while entities live in separate packages.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* User Entity Interface
|
|
12
|
-
*
|
|
13
|
-
* Core user authentication record
|
|
14
|
-
*/
|
|
15
|
-
export interface IUser {
|
|
16
|
-
id: number;
|
|
17
|
-
sub: string;
|
|
18
|
-
email: string;
|
|
19
|
-
username: string | null;
|
|
20
|
-
phone: string | null;
|
|
21
|
-
firstName: string | null;
|
|
22
|
-
lastName: string | null;
|
|
23
|
-
passwordHash: string | null;
|
|
24
|
-
passwordChangedAt: Date | null;
|
|
25
|
-
passwordHistory: string[] | null;
|
|
26
|
-
isEmailVerified: boolean;
|
|
27
|
-
isPhoneVerified: boolean;
|
|
28
|
-
isActive: boolean;
|
|
29
|
-
mustChangePassword: boolean;
|
|
30
|
-
isLocked: boolean;
|
|
31
|
-
lockReason: string | null;
|
|
32
|
-
lockedAt: Date | null;
|
|
33
|
-
lockedUntil: Date | null;
|
|
34
|
-
failedLoginAttempts: number;
|
|
35
|
-
lastFailedLoginAt: Date | null;
|
|
36
|
-
lastLoginAt: Date | null;
|
|
37
|
-
lastLoginIp: string | null;
|
|
38
|
-
hasSocialAuth: boolean;
|
|
39
|
-
socialProviders: string[] | null;
|
|
40
|
-
mfaEnabled: boolean;
|
|
41
|
-
mfaMethods: string[] | null;
|
|
42
|
-
preferredMfaMethod: string | null;
|
|
43
|
-
mfaExempt?: boolean;
|
|
44
|
-
mfaExemptReason?: string | null;
|
|
45
|
-
mfaExemptGrantedAt?: Date | null;
|
|
46
|
-
mfaExemptGrantedBy?: string | null;
|
|
47
|
-
backupCodes: string[] | null;
|
|
48
|
-
metadata: Record<string, unknown> | null;
|
|
49
|
-
createdAt: Date;
|
|
50
|
-
updatedAt: Date;
|
|
51
|
-
deletedAt: Date | null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Session Entity Interface
|
|
56
|
-
*
|
|
57
|
-
* JWT session tracking
|
|
58
|
-
*/
|
|
59
|
-
export interface ISession {
|
|
60
|
-
id: number;
|
|
61
|
-
userId: number;
|
|
62
|
-
accessTokenHash: string;
|
|
63
|
-
refreshTokenHash: string;
|
|
64
|
-
tokenFamily: string;
|
|
65
|
-
deviceId: string | null;
|
|
66
|
-
deviceName: string | null;
|
|
67
|
-
deviceType: string | null;
|
|
68
|
-
deviceFingerprint: string | null;
|
|
69
|
-
ipAddress: string | null;
|
|
70
|
-
ipCountry: string | null;
|
|
71
|
-
ipCity: string | null;
|
|
72
|
-
ipIsp: string | null;
|
|
73
|
-
userAgent: string | null;
|
|
74
|
-
platform: string | null;
|
|
75
|
-
browser: string | null;
|
|
76
|
-
authMethod: string | null;
|
|
77
|
-
isRemembered: boolean;
|
|
78
|
-
isTrustedDevice: boolean;
|
|
79
|
-
expiresAt: Date;
|
|
80
|
-
lastActivityAt: Date | null;
|
|
81
|
-
isRevoked: boolean;
|
|
82
|
-
revokedAt: Date | null;
|
|
83
|
-
revokeReason: string | null;
|
|
84
|
-
version: number;
|
|
85
|
-
metadata: Record<string, unknown> | null;
|
|
86
|
-
createdAt: Date;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Login Attempt Entity Interface
|
|
91
|
-
*
|
|
92
|
-
* Failed login tracking
|
|
93
|
-
*/
|
|
94
|
-
export interface ILoginAttempt {
|
|
95
|
-
id: number;
|
|
96
|
-
email: string | null;
|
|
97
|
-
userId: number | null;
|
|
98
|
-
ipAddress: string | null;
|
|
99
|
-
userAgent: string | null;
|
|
100
|
-
success: boolean;
|
|
101
|
-
failureReason: string | null;
|
|
102
|
-
mfaRequired: boolean;
|
|
103
|
-
metadata: Record<string, unknown> | null;
|
|
104
|
-
createdAt: Date;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Verification Token Entity Interface
|
|
109
|
-
*
|
|
110
|
-
* Email/phone/password reset tokens
|
|
111
|
-
*/
|
|
112
|
-
export interface IVerificationToken {
|
|
113
|
-
id: number;
|
|
114
|
-
userId: number;
|
|
115
|
-
challengeSessionId?: number | null;
|
|
116
|
-
type: 'email' | 'phone' | 'password_reset';
|
|
117
|
-
token: string;
|
|
118
|
-
code: string | null;
|
|
119
|
-
expiresAt: Date;
|
|
120
|
-
attempts: number;
|
|
121
|
-
usedAt: Date | null;
|
|
122
|
-
ipAddress: string | null;
|
|
123
|
-
userAgent: string | null;
|
|
124
|
-
createdAt: Date;
|
|
125
|
-
// Helper methods (if entity implements them)
|
|
126
|
-
isExpired?: () => boolean;
|
|
127
|
-
maxAttemptsExceeded?: (max: number) => boolean;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Social Account Entity Interface
|
|
132
|
-
*
|
|
133
|
-
* OAuth provider linkage
|
|
134
|
-
*/
|
|
135
|
-
export interface ISocialAccount {
|
|
136
|
-
id: number;
|
|
137
|
-
userId: number;
|
|
138
|
-
provider: string;
|
|
139
|
-
providerId: string;
|
|
140
|
-
providerEmail: string | null;
|
|
141
|
-
linkedAt: Date;
|
|
142
|
-
lastUsedAt: Date | null;
|
|
143
|
-
metadata: Record<string, unknown> | null;
|
|
144
|
-
createdAt: Date;
|
|
145
|
-
updatedAt: Date;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Challenge Session Entity Interface
|
|
150
|
-
*
|
|
151
|
-
* Temporary sessions for challenge-response flows
|
|
152
|
-
*/
|
|
153
|
-
export interface IChallengeSession {
|
|
154
|
-
id: number;
|
|
155
|
-
userId: number;
|
|
156
|
-
user?: IUser; // Optional relation
|
|
157
|
-
sessionToken: string;
|
|
158
|
-
challengeName: string;
|
|
159
|
-
challengeParameters: Record<string, unknown> | null;
|
|
160
|
-
metadata?: Record<string, unknown> | null;
|
|
161
|
-
attempts: number;
|
|
162
|
-
maxAttempts: number;
|
|
163
|
-
expiresAt: Date;
|
|
164
|
-
isCompleted?: boolean;
|
|
165
|
-
completedAt?: Date | null;
|
|
166
|
-
ipAddress: string | null;
|
|
167
|
-
userAgent: string | null;
|
|
168
|
-
createdAt: Date;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* MFA Device Entity Interface
|
|
173
|
-
*
|
|
174
|
-
* Multi-factor authentication device registrations
|
|
175
|
-
*/
|
|
176
|
-
import { MFADeviceMethod } from '../enums/mfa-method.enum';
|
|
177
|
-
|
|
178
|
-
export interface IMFADevice {
|
|
179
|
-
id: number;
|
|
180
|
-
userId: number;
|
|
181
|
-
type: MFADeviceMethod;
|
|
182
|
-
name: string;
|
|
183
|
-
secret: string | null;
|
|
184
|
-
credentialId: string | null;
|
|
185
|
-
publicKey: string | null;
|
|
186
|
-
counter: number | null;
|
|
187
|
-
transports: string[] | null;
|
|
188
|
-
phoneNumber?: string | null; // For SMS MFA
|
|
189
|
-
email?: string | null; // For Email MFA
|
|
190
|
-
isPrimary?: boolean; // Primary device flag
|
|
191
|
-
isActive: boolean;
|
|
192
|
-
lastUsedAt: Date | null;
|
|
193
|
-
createdAt: Date;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Authentication Audit Entity Interface
|
|
198
|
-
*
|
|
199
|
-
* Audit trail record for authentication and security events
|
|
200
|
-
*/
|
|
201
|
-
export interface IAuthAudit {
|
|
202
|
-
id: number;
|
|
203
|
-
userId: number;
|
|
204
|
-
eventType: string;
|
|
205
|
-
eventStatus: 'SUCCESS' | 'FAILURE' | 'INFO' | 'SUSPICIOUS';
|
|
206
|
-
riskFactor?: number | null;
|
|
207
|
-
riskFactors?: string[] | null; // Stored as string[] in DB, but should use RiskFactor enum values
|
|
208
|
-
adaptiveMfaTriggered?: boolean | null;
|
|
209
|
-
ipAddress?: string | null;
|
|
210
|
-
ipCountry?: string | null;
|
|
211
|
-
ipCity?: string | null;
|
|
212
|
-
userAgent?: string | null;
|
|
213
|
-
platform?: string | null;
|
|
214
|
-
browser?: string | null;
|
|
215
|
-
deviceId?: string | null;
|
|
216
|
-
deviceName?: string | null;
|
|
217
|
-
deviceType?: string | null;
|
|
218
|
-
sessionId?: number | null;
|
|
219
|
-
challengeSessionId?: number | null;
|
|
220
|
-
authMethod?: string | null;
|
|
221
|
-
performedBy?: string | null;
|
|
222
|
-
reason?: string | null;
|
|
223
|
-
description?: string | null;
|
|
224
|
-
metadata?: Record<string, unknown> | null;
|
|
225
|
-
createdAt: Date;
|
|
226
|
-
}
|
package/src/interfaces/index.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export * from './config.interface';
|
|
2
|
-
export * from './entities.interface';
|
|
3
|
-
export * from './logger.interface';
|
|
4
|
-
export * from './mfa-provider.interface';
|
|
5
|
-
export * from './oauth.interface';
|
|
6
|
-
export * from './provider.interface';
|
|
7
|
-
// NOTE: SMS provider configurations moved to their respective packages
|
|
8
|
-
// Import from @nauth-toolkit/sms-aws-sns, @nauth-toolkit/sms-twilio, etc.
|
|
9
|
-
// export * from './sms-config.interface'; // Deprecated - kept for backward compatibility notes only
|
|
10
|
-
export * from './social-auth-provider.interface';
|
|
11
|
-
export * from './storage-adapter.interface';
|
|
12
|
-
export * from './template.interface';
|
|
13
|
-
export * from './token-verifier.interface';
|
|
14
|
-
// Note: ClientInfo interface is exported directly from core/index.ts as IClientInfo
|
|
15
|
-
// to avoid naming conflict with ClientInfo decorator
|
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Logging Provider Interface
|
|
3
|
-
*
|
|
4
|
-
* Allows users to plug in their own logging solution (Winston, Pino, etc.)
|
|
5
|
-
* while nauth-toolkit automatically redacts PII (Personally Identifiable Information).
|
|
6
|
-
*
|
|
7
|
-
* Key features:
|
|
8
|
-
* - Standard log levels (debug, log, warn, error)
|
|
9
|
-
* - Automatic PII redaction (emails, passwords, tokens, IPs)
|
|
10
|
-
* - Structured logging support
|
|
11
|
-
* - Contextual logging with metadata
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* // Use default NestJS logger
|
|
16
|
-
* AuthModule.forRoot({
|
|
17
|
-
* logger: new NestJsLoggerAdapter(),
|
|
18
|
-
* })
|
|
19
|
-
*
|
|
20
|
-
* // Use Winston
|
|
21
|
-
* AuthModule.forRoot({
|
|
22
|
-
* logger: new WinstonLoggerAdapter(winstonInstance),
|
|
23
|
-
* })
|
|
24
|
-
*
|
|
25
|
-
* // Use Pino
|
|
26
|
-
* AuthModule.forRoot({
|
|
27
|
-
* logger: new PinoLoggerAdapter(pinoInstance),
|
|
28
|
-
* })
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Log Level
|
|
34
|
-
*
|
|
35
|
-
* Standard logging levels in order of severity
|
|
36
|
-
*/
|
|
37
|
-
export enum LogLevel {
|
|
38
|
-
DEBUG = 'debug',
|
|
39
|
-
LOG = 'log',
|
|
40
|
-
INFO = 'info',
|
|
41
|
-
WARN = 'warn',
|
|
42
|
-
ERROR = 'error',
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Log Metadata
|
|
47
|
-
*
|
|
48
|
-
* Additional context for log entries.
|
|
49
|
-
* All values are automatically redacted if they contain PII.
|
|
50
|
-
*/
|
|
51
|
-
export interface LogMetadata {
|
|
52
|
-
/**
|
|
53
|
-
* User ID (non-PII, safe to log)
|
|
54
|
-
*/
|
|
55
|
-
userId?: string;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Session ID (non-PII, safe to log)
|
|
59
|
-
*/
|
|
60
|
-
sessionId?: string;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Request ID for tracing
|
|
64
|
-
*/
|
|
65
|
-
requestId?: string;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Event type (e.g., 'login', 'signup', 'password_change')
|
|
69
|
-
*/
|
|
70
|
-
event?: string;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* IP address (will be redacted to first 3 octets)
|
|
74
|
-
*/
|
|
75
|
-
ipAddress?: string;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Device information (non-PII)
|
|
79
|
-
*/
|
|
80
|
-
deviceType?: string;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Error details
|
|
84
|
-
*/
|
|
85
|
-
error?: Error | string;
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Duration in milliseconds (for performance tracking)
|
|
89
|
-
*/
|
|
90
|
-
duration?: number;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Additional custom metadata
|
|
94
|
-
*/
|
|
95
|
-
[key: string]: unknown;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Logger Provider Interface
|
|
100
|
-
*
|
|
101
|
-
* Contract that all logging providers must implement.
|
|
102
|
-
* nauth-toolkit will call these methods and automatically redact PII.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* class CustomLogger implements LoggerProvider {
|
|
107
|
-
* debug(message: string, metadata?: LogMetadata): void {
|
|
108
|
-
* myLogger.debug(message, metadata);
|
|
109
|
-
* }
|
|
110
|
-
*
|
|
111
|
-
* log(message: string, metadata?: LogMetadata): void {
|
|
112
|
-
* myLogger.info(message, metadata);
|
|
113
|
-
* }
|
|
114
|
-
*
|
|
115
|
-
* warn(message: string, metadata?: LogMetadata): void {
|
|
116
|
-
* myLogger.warn(message, metadata);
|
|
117
|
-
* }
|
|
118
|
-
*
|
|
119
|
-
* error(message: string, metadata?: LogMetadata): void {
|
|
120
|
-
* myLogger.error(message, metadata);
|
|
121
|
-
* }
|
|
122
|
-
* }
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
|
-
export interface LoggerProvider {
|
|
126
|
-
/**
|
|
127
|
-
* Log debug message (lowest priority)
|
|
128
|
-
*
|
|
129
|
-
* Used for detailed debugging information.
|
|
130
|
-
*
|
|
131
|
-
* @param message - Log message
|
|
132
|
-
* @param metadata - Additional context (PII will be redacted)
|
|
133
|
-
*/
|
|
134
|
-
debug(message: string, metadata?: LogMetadata): void;
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Log informational message
|
|
138
|
-
*
|
|
139
|
-
* Used for general informational messages about system operation.
|
|
140
|
-
*
|
|
141
|
-
* @param message - Log message
|
|
142
|
-
* @param metadata - Additional context (PII will be redacted)
|
|
143
|
-
*/
|
|
144
|
-
log(message: string, metadata?: LogMetadata): void;
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Log warning message
|
|
148
|
-
*
|
|
149
|
-
* Used for potentially harmful situations.
|
|
150
|
-
*
|
|
151
|
-
* @param message - Log message
|
|
152
|
-
* @param metadata - Additional context (PII will be redacted)
|
|
153
|
-
*/
|
|
154
|
-
warn(message: string, metadata?: LogMetadata): void;
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Log error message (highest priority)
|
|
158
|
-
*
|
|
159
|
-
* Used for error events that might still allow the application to continue.
|
|
160
|
-
*
|
|
161
|
-
* @param message - Log message
|
|
162
|
-
* @param metadata - Additional context (PII will be redacted)
|
|
163
|
-
*/
|
|
164
|
-
error(message: string, metadata?: LogMetadata): void;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* PII Redaction Options
|
|
169
|
-
*
|
|
170
|
-
* Configure what PII should be redacted from logs
|
|
171
|
-
*/
|
|
172
|
-
export interface PiiRedactionOptions {
|
|
173
|
-
/**
|
|
174
|
-
* Redact email addresses
|
|
175
|
-
* @default true
|
|
176
|
-
*
|
|
177
|
-
* Example: `user@example.com` → `u***@***.com`
|
|
178
|
-
*/
|
|
179
|
-
redactEmails?: boolean;
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Redact full IP addresses (keep first 3 octets)
|
|
183
|
-
* @default true
|
|
184
|
-
*
|
|
185
|
-
* Example: `192.168.1.100` → `192.168.1.***`
|
|
186
|
-
*/
|
|
187
|
-
redactIpAddresses?: boolean;
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Redact tokens and secrets
|
|
191
|
-
* @default true
|
|
192
|
-
*
|
|
193
|
-
* Example: `eyJhbGciOiJIUzI1...` → `eyJ***...***`
|
|
194
|
-
*/
|
|
195
|
-
redactTokens?: boolean;
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Redact passwords and password hashes
|
|
199
|
-
* @default true
|
|
200
|
-
*
|
|
201
|
-
* Never log passwords! Always redacted.
|
|
202
|
-
*/
|
|
203
|
-
redactPasswords?: boolean;
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Redact phone numbers
|
|
207
|
-
* @default true
|
|
208
|
-
*
|
|
209
|
-
* Example: `+1234567890` → `+123***7890`
|
|
210
|
-
*/
|
|
211
|
-
redactPhoneNumbers?: boolean;
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Redact names (firstName, lastName)
|
|
215
|
-
* @default true
|
|
216
|
-
*
|
|
217
|
-
* Example: `John Doe` → `J*** D***`
|
|
218
|
-
*/
|
|
219
|
-
redactNames?: boolean;
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Custom fields to redact
|
|
223
|
-
*
|
|
224
|
-
* Field names that should be fully redacted if present in metadata.
|
|
225
|
-
*
|
|
226
|
-
* @default ['ssn', 'creditCard', 'bankAccount']
|
|
227
|
-
*/
|
|
228
|
-
customRedactionFields?: string[];
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Logger Configuration
|
|
233
|
-
*
|
|
234
|
-
* Configuration for the logging system
|
|
235
|
-
*/
|
|
236
|
-
export interface LoggerConfig {
|
|
237
|
-
/**
|
|
238
|
-
* Logger provider instance
|
|
239
|
-
*
|
|
240
|
-
* @default NestJsLoggerAdapter (built-in)
|
|
241
|
-
*/
|
|
242
|
-
provider?: LoggerProvider;
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Minimum log level to output
|
|
246
|
-
*
|
|
247
|
-
* @default LogLevel.LOG
|
|
248
|
-
*/
|
|
249
|
-
level?: LogLevel;
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Enable PII redaction
|
|
253
|
-
*
|
|
254
|
-
* @default true (always enabled in production)
|
|
255
|
-
*/
|
|
256
|
-
enablePiiRedaction?: boolean;
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* PII redaction options
|
|
260
|
-
*/
|
|
261
|
-
piiRedactionOptions?: PiiRedactionOptions;
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Log authentication events
|
|
265
|
-
*
|
|
266
|
-
* @default true
|
|
267
|
-
*/
|
|
268
|
-
logAuthEvents?: boolean;
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Log security events (lockouts, suspicious activity)
|
|
272
|
-
*
|
|
273
|
-
* @default true
|
|
274
|
-
*/
|
|
275
|
-
logSecurityEvents?: boolean;
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Log performance metrics
|
|
279
|
-
*
|
|
280
|
-
* @default false
|
|
281
|
-
*/
|
|
282
|
-
logPerformance?: boolean;
|
|
283
|
-
}
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import { IUser } from './entities.interface';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* MFA Provider Service Interface
|
|
5
|
-
*
|
|
6
|
-
* Defines the contract that all MFA provider services must implement.
|
|
7
|
-
* Each MFA method (TOTP, SMS, Passkey) is a separate provider that extends
|
|
8
|
-
* the base class and implements this interface.
|
|
9
|
-
*
|
|
10
|
-
* Provider-specific types (e.g., SetupTOTPResponseDTO) are defined in each
|
|
11
|
-
* provider package, not in core, to maintain proper separation of concerns.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* @Injectable()
|
|
16
|
-
* export class TOTPMFAProviderService extends BaseMFAProviderService implements IMFAProviderService {
|
|
17
|
-
* readonly methodName = 'totp';
|
|
18
|
-
*
|
|
19
|
-
* async setup(user: IUser): Promise<unknown> {
|
|
20
|
-
* // TOTP-specific setup logic
|
|
21
|
-
* }
|
|
22
|
-
*
|
|
23
|
-
* async verify(user: IUser, code: string, deviceId?: number): Promise<boolean> {
|
|
24
|
-
* // TOTP verification logic
|
|
25
|
-
* }
|
|
26
|
-
* }
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export interface IMFAProviderService {
|
|
30
|
-
/**
|
|
31
|
-
* Unique method name for this MFA provider
|
|
32
|
-
* Examples: 'totp', 'sms', 'passkey'
|
|
33
|
-
*/
|
|
34
|
-
readonly methodName: string;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Check if this MFA method is allowed by configuration
|
|
38
|
-
*
|
|
39
|
-
* @returns True if method is allowed
|
|
40
|
-
*/
|
|
41
|
-
isMethodAllowed(): boolean;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Setup MFA device for user
|
|
45
|
-
*
|
|
46
|
-
* Initiates the setup process for this MFA method.
|
|
47
|
-
* Provider-specific setup data is returned.
|
|
48
|
-
*
|
|
49
|
-
* @param user - User setting up MFA
|
|
50
|
-
* @returns Provider-specific setup data (e.g., QR code for TOTP, options for Passkey)
|
|
51
|
-
* @throws {NAuthException} If method is not allowed or setup fails
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```typescript
|
|
55
|
-
* // TOTP provider returns { secret, qrCode, manualEntryKey }
|
|
56
|
-
* const setupData = await totpProvider.setup(user);
|
|
57
|
-
*
|
|
58
|
-
* // Passkey provider returns WebAuthn registration options
|
|
59
|
-
* const options = await passkeyProvider.setup(user);
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
setup(user: IUser, setupData?: unknown): Promise<unknown>;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Verify and complete MFA setup
|
|
66
|
-
*
|
|
67
|
-
* Validates the verification code/credential and creates the MFA device.
|
|
68
|
-
*
|
|
69
|
-
* @param user - User completing setup
|
|
70
|
-
* @param verificationData - Provider-specific verification data (code, credential, etc.)
|
|
71
|
-
* @param deviceName - Optional device name
|
|
72
|
-
* @returns Created MFA device ID
|
|
73
|
-
* @throws {NAuthException} If verification fails
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* ```typescript
|
|
77
|
-
* // TOTP: verificationData = { secret, code }
|
|
78
|
-
* const deviceId = await totpProvider.verifySetup(user, { secret: '...', code: '123456' });
|
|
79
|
-
*
|
|
80
|
-
* // SMS: verificationData = { phoneNumber, code }
|
|
81
|
-
* const deviceId = await smsProvider.verifySetup(user, { phoneNumber: '+1234567890', code: '123456' });
|
|
82
|
-
*
|
|
83
|
-
* // Passkey: verificationData = { credential, challenge }
|
|
84
|
-
* const deviceId = await passkeyProvider.verifySetup(user, { credential: {...}, challenge: '...' });
|
|
85
|
-
* ```
|
|
86
|
-
*/
|
|
87
|
-
verifySetup(user: IUser, verificationData: unknown, deviceName?: string): Promise<number>;
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Verify MFA code/credential during authentication
|
|
91
|
-
*
|
|
92
|
-
* Validates the MFA code or credential for an existing device.
|
|
93
|
-
*
|
|
94
|
-
* @param user - User being authenticated
|
|
95
|
-
* @param code - MFA code or credential (provider-specific)
|
|
96
|
-
* @param deviceId - Optional device ID to verify against (if not provided, finds active device)
|
|
97
|
-
* @returns True if verification succeeds
|
|
98
|
-
* @throws {NAuthException} If device not found or verification fails
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```typescript
|
|
102
|
-
* // TOTP: code = '123456'
|
|
103
|
-
* const isValid = await totpProvider.verify(user, '123456');
|
|
104
|
-
*
|
|
105
|
-
* // SMS: code = '123456'
|
|
106
|
-
* const isValid = await smsProvider.verify(user, '123456');
|
|
107
|
-
*
|
|
108
|
-
* // Passkey: code = { credential: {...}, challenge: '...' }
|
|
109
|
-
* const isValid = await passkeyProvider.verify(user, { credential: {...}, challenge: '...' });
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
verify(user: IUser, code: unknown, deviceId?: number): Promise<boolean>;
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Send verification code/challenge for authentication
|
|
116
|
-
*
|
|
117
|
-
* Used during login to send SMS code or generate passkey challenge.
|
|
118
|
-
* Not applicable for TOTP (user generates code locally).
|
|
119
|
-
*
|
|
120
|
-
* @param user - User requesting verification
|
|
121
|
-
* @returns Provider-specific challenge data (e.g., masked phone for SMS, WebAuthn options for Passkey)
|
|
122
|
-
* @throws {NAuthException} If no device registered or send fails
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* ```typescript
|
|
126
|
-
* // SMS: returns masked phone number
|
|
127
|
-
* const maskedPhone = await smsProvider.sendChallenge(user); // '***-***-1234'
|
|
128
|
-
*
|
|
129
|
-
* // Passkey: returns WebAuthn authentication options
|
|
130
|
-
* const options = await passkeyProvider.sendChallenge(user); // { challenge: '...', ... }
|
|
131
|
-
* ```
|
|
132
|
-
*/
|
|
133
|
-
sendChallenge?(user: IUser): Promise<unknown>; // Optional - only providers like SMS need it
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Generate backup codes for user
|
|
137
|
-
*
|
|
138
|
-
* Creates single-use recovery codes that can be used when MFA devices are unavailable.
|
|
139
|
-
* Provided by BaseMFAProviderService for all providers.
|
|
140
|
-
*
|
|
141
|
-
* @param user - User to generate codes for
|
|
142
|
-
* @returns Generated backup codes (plain text - shown only once)
|
|
143
|
-
*
|
|
144
|
-
* @example
|
|
145
|
-
* ```typescript
|
|
146
|
-
* const codes = await provider.generateBackupCodes?.(user);
|
|
147
|
-
* // Returns: ['ABC12345', 'DEF67890', ...]
|
|
148
|
-
* ```
|
|
149
|
-
*/
|
|
150
|
-
generateBackupCodes?(user: IUser): Promise<string[]>; // Optional - provided by BaseMFAProviderService
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Helper type to check if a provider implements sendChallenge
|
|
154
|
-
export type MFAProviderWithChallenge = IMFAProviderService & Required<Pick<IMFAProviderService, 'sendChallenge'>>;
|