@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,422 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Service Initialization Helper
|
|
3
|
-
*
|
|
4
|
-
* Initializes all NAuth services in correct dependency order.
|
|
5
|
-
* Matches NestJS AuthModule service initialization.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { Repository } from 'typeorm';
|
|
9
|
-
// Public API imports
|
|
10
|
-
import {
|
|
11
|
-
NAuthConfig,
|
|
12
|
-
NAuthLogger,
|
|
13
|
-
StorageAdapter,
|
|
14
|
-
ClientInfoService,
|
|
15
|
-
RateLimitStorageService,
|
|
16
|
-
AccountLockoutStorageService,
|
|
17
|
-
EmailVerificationService,
|
|
18
|
-
PhoneVerificationService,
|
|
19
|
-
MFAService,
|
|
20
|
-
AuthService,
|
|
21
|
-
SocialAuthService,
|
|
22
|
-
NAuthException,
|
|
23
|
-
AuthErrorCode,
|
|
24
|
-
} from '../../index';
|
|
25
|
-
// Internal API imports (for framework adapter use only)
|
|
26
|
-
import {
|
|
27
|
-
PasswordService,
|
|
28
|
-
JwtService,
|
|
29
|
-
SessionService,
|
|
30
|
-
AuthAuditService, // Internal version with recordEvent()
|
|
31
|
-
ChallengeService,
|
|
32
|
-
TrustedDeviceService,
|
|
33
|
-
AuthFlowContextBuilder,
|
|
34
|
-
AuthFlowStateMachineService,
|
|
35
|
-
AuthChallengeHelperService,
|
|
36
|
-
SocialProviderRegistry,
|
|
37
|
-
GeoLocationService,
|
|
38
|
-
RiskDetectionService,
|
|
39
|
-
RiskScoringService,
|
|
40
|
-
AdaptiveMFADecisionService,
|
|
41
|
-
} from '../../internal';
|
|
42
|
-
import {
|
|
43
|
-
BaseUser,
|
|
44
|
-
BaseSession,
|
|
45
|
-
BaseLoginAttempt,
|
|
46
|
-
BaseVerificationToken,
|
|
47
|
-
BaseSocialAccount,
|
|
48
|
-
BaseChallengeSession,
|
|
49
|
-
BaseMFADevice,
|
|
50
|
-
BaseAuthAudit,
|
|
51
|
-
BaseTrustedDevice,
|
|
52
|
-
} from '../../entities';
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Service container returned by initServices()
|
|
56
|
-
*/
|
|
57
|
-
export interface NAuthServices {
|
|
58
|
-
// Core services (always available)
|
|
59
|
-
passwordService: PasswordService;
|
|
60
|
-
jwtService: JwtService;
|
|
61
|
-
clientInfoService: ClientInfoService;
|
|
62
|
-
rateLimitStorageService: RateLimitStorageService;
|
|
63
|
-
accountLockoutStorageService: AccountLockoutStorageService;
|
|
64
|
-
sessionService: SessionService;
|
|
65
|
-
challengeService: ChallengeService;
|
|
66
|
-
emailVerificationService: EmailVerificationService;
|
|
67
|
-
authFlowContextBuilder: AuthFlowContextBuilder;
|
|
68
|
-
authFlowStateMachine: AuthFlowStateMachineService;
|
|
69
|
-
authChallengeHelperService: AuthChallengeHelperService;
|
|
70
|
-
authService: AuthService;
|
|
71
|
-
socialProviderRegistry: SocialProviderRegistry;
|
|
72
|
-
socialAuthService: SocialAuthService;
|
|
73
|
-
|
|
74
|
-
// Conditional services
|
|
75
|
-
auditService?: AuthAuditService;
|
|
76
|
-
phoneVerificationService?: PhoneVerificationService;
|
|
77
|
-
trustedDeviceService?: TrustedDeviceService;
|
|
78
|
-
mfaService?: MFAService;
|
|
79
|
-
geoLocationService?: GeoLocationService;
|
|
80
|
-
riskDetectionService?: RiskDetectionService;
|
|
81
|
-
riskScoringService?: RiskScoringService;
|
|
82
|
-
adaptiveMFADecisionService?: AdaptiveMFADecisionService;
|
|
83
|
-
csrfService?: unknown; // CsrfService (created in createNAuth)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Initialize all services in correct dependency order
|
|
88
|
-
*
|
|
89
|
-
* Service initialization order matches NestJS AuthModule:
|
|
90
|
-
* 1. PasswordService, JwtService (no dependencies)
|
|
91
|
-
* 2. ClientInfoService (no dependencies)
|
|
92
|
-
* 3. AuthAuditService (if enabled)
|
|
93
|
-
* 4. RateLimitStorageService, AccountLockoutStorageService
|
|
94
|
-
* 5. SessionService
|
|
95
|
-
* 6. ChallengeService
|
|
96
|
-
* 7. EmailVerificationService
|
|
97
|
-
* 8. PhoneVerificationService (if SMS configured)
|
|
98
|
-
* 9. TrustedDeviceService (if rememberDevices enabled)
|
|
99
|
-
* 10. AuthFlowContextBuilder, AuthFlowStateMachine
|
|
100
|
-
* 11. AuthChallengeHelperService
|
|
101
|
-
* 12. MFAService (if enabled)
|
|
102
|
-
* 13. AuthService
|
|
103
|
-
* 14. SocialAuthService
|
|
104
|
-
* 15. GeoLocationService (if MaxMind configured)
|
|
105
|
-
* 16. Risk services (if adaptive MFA configured)
|
|
106
|
-
*
|
|
107
|
-
* @param config - NAuth configuration
|
|
108
|
-
* @param repositories - Repository container
|
|
109
|
-
* @param storageAdapter - Initialized storage adapter
|
|
110
|
-
* @param logger - Logger instance
|
|
111
|
-
* @param emailProvider - Email provider instance
|
|
112
|
-
* @param smsProvider - SMS provider instance (optional)
|
|
113
|
-
* @returns Service container with all initialized services
|
|
114
|
-
*/
|
|
115
|
-
export function initServices(
|
|
116
|
-
config: NAuthConfig,
|
|
117
|
-
repositories: {
|
|
118
|
-
userRepository: Repository<BaseUser>;
|
|
119
|
-
sessionRepository: Repository<BaseSession>;
|
|
120
|
-
loginAttemptRepository: Repository<BaseLoginAttempt>;
|
|
121
|
-
verificationTokenRepository: Repository<BaseVerificationToken>;
|
|
122
|
-
socialAccountRepository: Repository<BaseSocialAccount>;
|
|
123
|
-
challengeSessionRepository: Repository<BaseChallengeSession>;
|
|
124
|
-
mfaDeviceRepository: Repository<BaseMFADevice>;
|
|
125
|
-
authAuditRepository: Repository<BaseAuthAudit>;
|
|
126
|
-
trustedDeviceRepository: Repository<BaseTrustedDevice> | null;
|
|
127
|
-
},
|
|
128
|
-
storageAdapter: StorageAdapter,
|
|
129
|
-
logger: NAuthLogger,
|
|
130
|
-
emailProvider: unknown,
|
|
131
|
-
smsProvider?: unknown,
|
|
132
|
-
): NAuthServices {
|
|
133
|
-
// ============================================================================
|
|
134
|
-
// 1. Core Services (No Dependencies)
|
|
135
|
-
// ============================================================================
|
|
136
|
-
|
|
137
|
-
const passwordService = new PasswordService(config.password);
|
|
138
|
-
const jwtService = new JwtService(config.jwt);
|
|
139
|
-
const clientInfoService = new ClientInfoService();
|
|
140
|
-
|
|
141
|
-
// ============================================================================
|
|
142
|
-
// 2. Audit Service (Conditional)
|
|
143
|
-
// ============================================================================
|
|
144
|
-
|
|
145
|
-
const auditService =
|
|
146
|
-
config.auditLogs?.enabled !== false
|
|
147
|
-
? new AuthAuditService(repositories.authAuditRepository, repositories.userRepository, logger, clientInfoService)
|
|
148
|
-
: undefined;
|
|
149
|
-
|
|
150
|
-
// ============================================================================
|
|
151
|
-
// 3. Storage Services
|
|
152
|
-
// ============================================================================
|
|
153
|
-
|
|
154
|
-
const rateLimitStorageService = new RateLimitStorageService(storageAdapter);
|
|
155
|
-
const accountLockoutStorageService = new AccountLockoutStorageService(storageAdapter);
|
|
156
|
-
|
|
157
|
-
// ============================================================================
|
|
158
|
-
// 4. Session Service
|
|
159
|
-
// ============================================================================
|
|
160
|
-
|
|
161
|
-
const sessionService = new SessionService(
|
|
162
|
-
repositories.sessionRepository,
|
|
163
|
-
storageAdapter,
|
|
164
|
-
clientInfoService,
|
|
165
|
-
config,
|
|
166
|
-
logger,
|
|
167
|
-
auditService,
|
|
168
|
-
);
|
|
169
|
-
|
|
170
|
-
// ============================================================================
|
|
171
|
-
// 5. Challenge Service
|
|
172
|
-
// ============================================================================
|
|
173
|
-
|
|
174
|
-
const challengeService = new ChallengeService(
|
|
175
|
-
repositories.challengeSessionRepository,
|
|
176
|
-
clientInfoService,
|
|
177
|
-
logger,
|
|
178
|
-
auditService,
|
|
179
|
-
config, // Pass config for maxAttempts
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
// ============================================================================
|
|
183
|
-
// 6. Email Provider and Verification Service
|
|
184
|
-
// ============================================================================
|
|
185
|
-
|
|
186
|
-
if (!emailProvider) {
|
|
187
|
-
throw new NAuthException(
|
|
188
|
-
AuthErrorCode.VALIDATION_FAILED,
|
|
189
|
-
'emailProvider is required. Install and configure an email package:\n' +
|
|
190
|
-
' yarn add @nauth-toolkit/email-console (for dev)\n' +
|
|
191
|
-
' yarn add @nauth-toolkit/email-nodemailer (for production)',
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Validate email provider has required method
|
|
196
|
-
if (typeof (emailProvider as any).sendVerificationEmail !== 'function') {
|
|
197
|
-
throw new NAuthException(
|
|
198
|
-
AuthErrorCode.VALIDATION_FAILED,
|
|
199
|
-
'emailProvider must implement sendVerificationEmail method',
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// Inject logger into email provider if it supports it
|
|
204
|
-
if (emailProvider && typeof (emailProvider as any).setLogger === 'function') {
|
|
205
|
-
(emailProvider as any).setLogger(logger);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Inject global variables from email config if provider supports it
|
|
209
|
-
if (emailProvider && typeof (emailProvider as any).setGlobalVariables === 'function' && config.email) {
|
|
210
|
-
const globalVars: Record<string, any> = {};
|
|
211
|
-
// Extract top-level branding fields
|
|
212
|
-
if (config.email.appName) globalVars.appName = config.email.appName;
|
|
213
|
-
if (config.email.companyName) globalVars.companyName = config.email.companyName;
|
|
214
|
-
if (config.email.logoUrl) globalVars.logoUrl = config.email.logoUrl;
|
|
215
|
-
if (config.email.supportEmail) globalVars.supportEmail = config.email.supportEmail;
|
|
216
|
-
if (config.email.dashboardUrl) globalVars.dashboardUrl = config.email.dashboardUrl;
|
|
217
|
-
if (config.email.brandColor) globalVars.brandColor = config.email.brandColor;
|
|
218
|
-
if (config.email.footerDisclaimer) globalVars.footerDisclaimer = config.email.footerDisclaimer;
|
|
219
|
-
// Merge with templates.globalVariables (templates.globalVariables takes precedence)
|
|
220
|
-
const mergedVars = {
|
|
221
|
-
...globalVars,
|
|
222
|
-
...(config.email.templates?.globalVariables || {}),
|
|
223
|
-
};
|
|
224
|
-
(emailProvider as any).setGlobalVariables(mergedVars);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const emailVerificationService = new EmailVerificationService(
|
|
228
|
-
repositories.verificationTokenRepository,
|
|
229
|
-
repositories.userRepository,
|
|
230
|
-
emailProvider as any,
|
|
231
|
-
storageAdapter,
|
|
232
|
-
config,
|
|
233
|
-
clientInfoService,
|
|
234
|
-
logger,
|
|
235
|
-
auditService,
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
// ============================================================================
|
|
239
|
-
// 7. SMS Provider and Phone Verification Service (Conditional)
|
|
240
|
-
// ============================================================================
|
|
241
|
-
|
|
242
|
-
let phoneVerificationService: PhoneVerificationService | undefined;
|
|
243
|
-
|
|
244
|
-
if (smsProvider) {
|
|
245
|
-
// Inject logger into SMS provider if it supports it
|
|
246
|
-
if (smsProvider && typeof (smsProvider as any).setLogger === 'function') {
|
|
247
|
-
(smsProvider as any).setLogger(logger);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
phoneVerificationService = new PhoneVerificationService(
|
|
251
|
-
repositories.verificationTokenRepository,
|
|
252
|
-
repositories.userRepository,
|
|
253
|
-
smsProvider as any,
|
|
254
|
-
storageAdapter,
|
|
255
|
-
config,
|
|
256
|
-
clientInfoService,
|
|
257
|
-
logger,
|
|
258
|
-
auditService,
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// ============================================================================
|
|
263
|
-
// 8. Trusted Device Service (Conditional)
|
|
264
|
-
// ============================================================================
|
|
265
|
-
|
|
266
|
-
const trustedDeviceService = repositories.trustedDeviceRepository
|
|
267
|
-
? new TrustedDeviceService(config, logger, repositories.trustedDeviceRepository)
|
|
268
|
-
: undefined;
|
|
269
|
-
|
|
270
|
-
// ============================================================================
|
|
271
|
-
// 9. Auth Flow Services
|
|
272
|
-
// ============================================================================
|
|
273
|
-
|
|
274
|
-
const authFlowContextBuilder = new AuthFlowContextBuilder(
|
|
275
|
-
trustedDeviceService,
|
|
276
|
-
undefined, // adaptiveMFADecisionService - will be set later
|
|
277
|
-
clientInfoService,
|
|
278
|
-
logger,
|
|
279
|
-
);
|
|
280
|
-
|
|
281
|
-
const authFlowStateMachine = new AuthFlowStateMachineService(authFlowContextBuilder, logger);
|
|
282
|
-
|
|
283
|
-
const authChallengeHelperService = new AuthChallengeHelperService(
|
|
284
|
-
challengeService,
|
|
285
|
-
jwtService,
|
|
286
|
-
sessionService,
|
|
287
|
-
repositories.mfaDeviceRepository,
|
|
288
|
-
logger,
|
|
289
|
-
authFlowStateMachine,
|
|
290
|
-
authFlowContextBuilder,
|
|
291
|
-
clientInfoService,
|
|
292
|
-
emailVerificationService,
|
|
293
|
-
phoneVerificationService,
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
// ============================================================================
|
|
297
|
-
// 10. MFA Service (Conditional)
|
|
298
|
-
// ============================================================================
|
|
299
|
-
|
|
300
|
-
const mfaService = new MFAService(
|
|
301
|
-
repositories.mfaDeviceRepository,
|
|
302
|
-
repositories.userRepository,
|
|
303
|
-
challengeService,
|
|
304
|
-
config,
|
|
305
|
-
logger,
|
|
306
|
-
auditService,
|
|
307
|
-
clientInfoService,
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
// ============================================================================
|
|
311
|
-
// 11. Auth Service
|
|
312
|
-
// ============================================================================
|
|
313
|
-
|
|
314
|
-
const authService = new AuthService(
|
|
315
|
-
repositories.userRepository,
|
|
316
|
-
repositories.loginAttemptRepository,
|
|
317
|
-
passwordService,
|
|
318
|
-
jwtService,
|
|
319
|
-
sessionService,
|
|
320
|
-
challengeService,
|
|
321
|
-
authChallengeHelperService,
|
|
322
|
-
emailVerificationService,
|
|
323
|
-
clientInfoService,
|
|
324
|
-
accountLockoutStorageService,
|
|
325
|
-
config,
|
|
326
|
-
logger,
|
|
327
|
-
auditService,
|
|
328
|
-
phoneVerificationService,
|
|
329
|
-
mfaService,
|
|
330
|
-
repositories.mfaDeviceRepository,
|
|
331
|
-
trustedDeviceService,
|
|
332
|
-
);
|
|
333
|
-
|
|
334
|
-
// ============================================================================
|
|
335
|
-
// 12. Social Auth Services
|
|
336
|
-
// ============================================================================
|
|
337
|
-
|
|
338
|
-
const socialProviderRegistry = new SocialProviderRegistry();
|
|
339
|
-
|
|
340
|
-
const socialAuthService = new SocialAuthService(
|
|
341
|
-
socialProviderRegistry,
|
|
342
|
-
repositories.userRepository,
|
|
343
|
-
repositories.socialAccountRepository,
|
|
344
|
-
authService,
|
|
345
|
-
logger,
|
|
346
|
-
auditService,
|
|
347
|
-
);
|
|
348
|
-
|
|
349
|
-
// ============================================================================
|
|
350
|
-
// 13. GeoLocation Service (Conditional)
|
|
351
|
-
// ============================================================================
|
|
352
|
-
|
|
353
|
-
let geoLocationService: GeoLocationService | undefined;
|
|
354
|
-
|
|
355
|
-
if (config.geoLocation?.maxMind) {
|
|
356
|
-
try {
|
|
357
|
-
// Try to load MaxMind module (optional peer dependency)
|
|
358
|
-
const maxMindModule = require('@maxmind/geoip2-node');
|
|
359
|
-
geoLocationService = new GeoLocationService(config, storageAdapter, maxMindModule, logger);
|
|
360
|
-
} catch {
|
|
361
|
-
// MaxMind module not installed - service remains undefined
|
|
362
|
-
logger?.warn?.('MaxMind GeoIP2 module not installed. Geolocation features will be disabled.');
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
// ============================================================================
|
|
367
|
-
// 14. Risk Detection and Adaptive MFA Services (Conditional)
|
|
368
|
-
// ============================================================================
|
|
369
|
-
|
|
370
|
-
// Always create risk services (needed for adaptive MFA)
|
|
371
|
-
const riskDetectionService = new RiskDetectionService(
|
|
372
|
-
repositories.sessionRepository,
|
|
373
|
-
repositories.authAuditRepository,
|
|
374
|
-
config,
|
|
375
|
-
logger,
|
|
376
|
-
trustedDeviceService,
|
|
377
|
-
);
|
|
378
|
-
|
|
379
|
-
const riskScoringService = new RiskScoringService(config, logger);
|
|
380
|
-
|
|
381
|
-
const adaptiveMFADecisionService = new AdaptiveMFADecisionService(
|
|
382
|
-
riskDetectionService,
|
|
383
|
-
riskScoringService,
|
|
384
|
-
storageAdapter,
|
|
385
|
-
clientInfoService,
|
|
386
|
-
config,
|
|
387
|
-
logger,
|
|
388
|
-
auditService,
|
|
389
|
-
);
|
|
390
|
-
|
|
391
|
-
// Now inject adaptiveMFADecisionService into authFlowContextBuilder
|
|
392
|
-
(authFlowContextBuilder as any).adaptiveMFADecisionService = adaptiveMFADecisionService;
|
|
393
|
-
|
|
394
|
-
// ============================================================================
|
|
395
|
-
// Return Service Container
|
|
396
|
-
// ============================================================================
|
|
397
|
-
|
|
398
|
-
return {
|
|
399
|
-
passwordService,
|
|
400
|
-
jwtService,
|
|
401
|
-
clientInfoService,
|
|
402
|
-
rateLimitStorageService,
|
|
403
|
-
accountLockoutStorageService,
|
|
404
|
-
sessionService,
|
|
405
|
-
challengeService,
|
|
406
|
-
emailVerificationService,
|
|
407
|
-
authFlowContextBuilder,
|
|
408
|
-
authFlowStateMachine,
|
|
409
|
-
authChallengeHelperService,
|
|
410
|
-
authService,
|
|
411
|
-
socialProviderRegistry,
|
|
412
|
-
socialAuthService,
|
|
413
|
-
auditService,
|
|
414
|
-
phoneVerificationService,
|
|
415
|
-
trustedDeviceService,
|
|
416
|
-
mfaService,
|
|
417
|
-
geoLocationService,
|
|
418
|
-
riskDetectionService,
|
|
419
|
-
riskScoringService,
|
|
420
|
-
adaptiveMFADecisionService,
|
|
421
|
-
};
|
|
422
|
-
}
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Social Authentication Provider Initialization
|
|
3
|
-
*
|
|
4
|
-
* Dynamically loads and initializes social auth providers based on configuration.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Public API imports
|
|
8
|
-
import {
|
|
9
|
-
NAuthConfig,
|
|
10
|
-
NAuthLogger,
|
|
11
|
-
SocialAuthService,
|
|
12
|
-
AuthService,
|
|
13
|
-
ClientInfoService,
|
|
14
|
-
AuthAuditService,
|
|
15
|
-
PhoneVerificationService,
|
|
16
|
-
BaseUser,
|
|
17
|
-
} from '../../index';
|
|
18
|
-
// Internal API imports (for framework adapter use only)
|
|
19
|
-
import {
|
|
20
|
-
JwtService,
|
|
21
|
-
SessionService,
|
|
22
|
-
AuthChallengeHelperService,
|
|
23
|
-
SocialProviderRegistry,
|
|
24
|
-
TrustedDeviceService,
|
|
25
|
-
} from '../../internal';
|
|
26
|
-
import { Repository } from 'typeorm';
|
|
27
|
-
|
|
28
|
-
export interface NAuthSocialProviders {
|
|
29
|
-
googleAuth?: any;
|
|
30
|
-
appleAuth?: any;
|
|
31
|
-
facebookAuth?: any;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Initialize and register social authentication providers
|
|
36
|
-
*
|
|
37
|
-
* Dynamically imports social provider packages based on configuration.
|
|
38
|
-
* Each provider is initialized with all required services and registered
|
|
39
|
-
* with the SocialAuthService registry.
|
|
40
|
-
*
|
|
41
|
-
* @param config - NAuth configuration
|
|
42
|
-
* @param providerRegistry - Social provider registry (internal)
|
|
43
|
-
* @param authService - Core authentication service
|
|
44
|
-
* @param socialAuthService - Social authentication service
|
|
45
|
-
* @param jwtService - JWT token service
|
|
46
|
-
* @param sessionService - Session management service
|
|
47
|
-
* @param challengeHelper - Auth challenge helper service
|
|
48
|
-
* @param clientInfoService - Client information service
|
|
49
|
-
* @param logger - Logger instance
|
|
50
|
-
* @param socialAuthStateStore - Shared state store for OAuth CSRF protection
|
|
51
|
-
* @param phoneVerificationService - Phone verification service (optional)
|
|
52
|
-
* @param auditService - Audit logging service (optional)
|
|
53
|
-
* @param trustedDeviceService - Trusted device service (optional)
|
|
54
|
-
* @returns Object containing initialized social providers
|
|
55
|
-
*/
|
|
56
|
-
export async function initSocialAuth(
|
|
57
|
-
config: NAuthConfig,
|
|
58
|
-
providerRegistry: SocialProviderRegistry,
|
|
59
|
-
authService: AuthService,
|
|
60
|
-
socialAuthService: SocialAuthService,
|
|
61
|
-
jwtService: JwtService,
|
|
62
|
-
sessionService: SessionService,
|
|
63
|
-
challengeHelper: AuthChallengeHelperService,
|
|
64
|
-
clientInfoService: ClientInfoService,
|
|
65
|
-
logger: NAuthLogger,
|
|
66
|
-
socialAuthStateStore: Map<string, { timestamp: number; provider: string }>,
|
|
67
|
-
userRepository: Repository<BaseUser>,
|
|
68
|
-
phoneVerificationService?: PhoneVerificationService,
|
|
69
|
-
auditService?: AuthAuditService,
|
|
70
|
-
trustedDeviceService?: TrustedDeviceService,
|
|
71
|
-
): Promise<NAuthSocialProviders> {
|
|
72
|
-
const providers: NAuthSocialProviders = {};
|
|
73
|
-
|
|
74
|
-
// ============================================================================
|
|
75
|
-
// Google OAuth Provider
|
|
76
|
-
// ============================================================================
|
|
77
|
-
if (config.social?.google?.enabled) {
|
|
78
|
-
try {
|
|
79
|
-
// @ts-ignore - Optional peer dependency, may not be installed
|
|
80
|
-
const { GoogleSocialAuthService, TokenVerifierService } = await import('@nauth-toolkit/social-google');
|
|
81
|
-
|
|
82
|
-
// Create token verifier for native mobile token validation
|
|
83
|
-
const tokenVerifier = new TokenVerifierService(config);
|
|
84
|
-
|
|
85
|
-
providers.googleAuth = new GoogleSocialAuthService(
|
|
86
|
-
config,
|
|
87
|
-
logger,
|
|
88
|
-
authService,
|
|
89
|
-
socialAuthService,
|
|
90
|
-
jwtService,
|
|
91
|
-
sessionService,
|
|
92
|
-
challengeHelper,
|
|
93
|
-
clientInfoService,
|
|
94
|
-
socialAuthStateStore,
|
|
95
|
-
userRepository,
|
|
96
|
-
phoneVerificationService,
|
|
97
|
-
auditService,
|
|
98
|
-
trustedDeviceService,
|
|
99
|
-
tokenVerifier,
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
// Register with registry
|
|
103
|
-
providerRegistry.registerProvider(providers.googleAuth);
|
|
104
|
-
logger?.debug?.('Google OAuth provider initialized');
|
|
105
|
-
} catch (error) {
|
|
106
|
-
logger?.warn?.(
|
|
107
|
-
'Google OAuth provider not available. Install @nauth-toolkit/social-google to enable Google authentication.',
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// ============================================================================
|
|
113
|
-
// Apple Sign-In Provider
|
|
114
|
-
// ============================================================================
|
|
115
|
-
if (config.social?.apple?.enabled) {
|
|
116
|
-
try {
|
|
117
|
-
// @ts-ignore - Optional peer dependency, may not be installed
|
|
118
|
-
const { AppleSocialAuthService, TokenVerifierService } = await import('@nauth-toolkit/social-apple');
|
|
119
|
-
|
|
120
|
-
// Create token verifier for native mobile token validation
|
|
121
|
-
const tokenVerifier = new TokenVerifierService(config);
|
|
122
|
-
|
|
123
|
-
providers.appleAuth = new AppleSocialAuthService(
|
|
124
|
-
config,
|
|
125
|
-
logger,
|
|
126
|
-
authService,
|
|
127
|
-
socialAuthService,
|
|
128
|
-
jwtService,
|
|
129
|
-
sessionService,
|
|
130
|
-
challengeHelper,
|
|
131
|
-
clientInfoService,
|
|
132
|
-
socialAuthStateStore,
|
|
133
|
-
userRepository,
|
|
134
|
-
phoneVerificationService,
|
|
135
|
-
auditService,
|
|
136
|
-
trustedDeviceService,
|
|
137
|
-
tokenVerifier,
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
// Register with registry
|
|
141
|
-
providerRegistry.registerProvider(providers.appleAuth);
|
|
142
|
-
logger?.debug?.('Apple Sign-In provider initialized');
|
|
143
|
-
} catch (error) {
|
|
144
|
-
logger?.warn?.(
|
|
145
|
-
'Apple Sign-In provider not available. Install @nauth-toolkit/social-apple to enable Apple authentication.',
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// ============================================================================
|
|
151
|
-
// Facebook OAuth Provider
|
|
152
|
-
// ============================================================================
|
|
153
|
-
if (config.social?.facebook?.enabled) {
|
|
154
|
-
try {
|
|
155
|
-
// @ts-ignore - Optional peer dependency, may not be installed
|
|
156
|
-
const { FacebookSocialAuthService, TokenVerifierService } = await import('@nauth-toolkit/social-facebook');
|
|
157
|
-
|
|
158
|
-
// Create token verifier for native mobile token validation
|
|
159
|
-
const tokenVerifier = new TokenVerifierService(config);
|
|
160
|
-
|
|
161
|
-
providers.facebookAuth = new FacebookSocialAuthService(
|
|
162
|
-
config,
|
|
163
|
-
logger,
|
|
164
|
-
authService,
|
|
165
|
-
socialAuthService,
|
|
166
|
-
jwtService,
|
|
167
|
-
sessionService,
|
|
168
|
-
challengeHelper,
|
|
169
|
-
clientInfoService,
|
|
170
|
-
socialAuthStateStore,
|
|
171
|
-
userRepository,
|
|
172
|
-
phoneVerificationService,
|
|
173
|
-
auditService,
|
|
174
|
-
trustedDeviceService,
|
|
175
|
-
tokenVerifier,
|
|
176
|
-
);
|
|
177
|
-
|
|
178
|
-
// Register with registry
|
|
179
|
-
providerRegistry.registerProvider(providers.facebookAuth);
|
|
180
|
-
logger?.debug?.('Facebook OAuth provider initialized');
|
|
181
|
-
} catch (error) {
|
|
182
|
-
logger?.warn?.(
|
|
183
|
-
'Facebook OAuth provider not available. Install @nauth-toolkit/social-facebook to enable Facebook authentication.',
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return providers;
|
|
189
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Storage Adapter Initialization Helper
|
|
3
|
-
*
|
|
4
|
-
* Initializes storage adapter with repository injection and proper error handling.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { Repository } from 'typeorm';
|
|
8
|
-
import { StorageAdapter, LoggerService, NAuthConfig, NAuthException, AuthErrorCode } from '../../index';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Initialize storage adapter
|
|
12
|
-
*
|
|
13
|
-
* Handles:
|
|
14
|
-
* - Logger injection (if adapter supports it)
|
|
15
|
-
* - Repository injection (for DatabaseStorageAdapter)
|
|
16
|
-
* - Adapter initialization
|
|
17
|
-
* - Fallback to DatabaseStorageAdapter if no adapter provided and repositories available
|
|
18
|
-
* - Error if no adapter and no repositories (prevents unsafe defaults)
|
|
19
|
-
*
|
|
20
|
-
* @param config - NAuth configuration
|
|
21
|
-
* @param rateLimitRepo - RateLimit repository (nullable)
|
|
22
|
-
* @param storageLockRepo - StorageLock repository (nullable)
|
|
23
|
-
* @param logger - Logger instance
|
|
24
|
-
* @returns Initialized StorageAdapter
|
|
25
|
-
* @throws {NAuthException} If no adapter provided and DatabaseStorageAdapter cannot be created
|
|
26
|
-
*/
|
|
27
|
-
export async function initStorage(
|
|
28
|
-
config: NAuthConfig,
|
|
29
|
-
rateLimitRepo: Repository<any> | null,
|
|
30
|
-
storageLockRepo: Repository<any> | null,
|
|
31
|
-
logger: LoggerService,
|
|
32
|
-
): Promise<StorageAdapter> {
|
|
33
|
-
// If storage adapter explicitly provided, use it
|
|
34
|
-
if (config.storageAdapter) {
|
|
35
|
-
const adapter = config.storageAdapter;
|
|
36
|
-
|
|
37
|
-
// Inject logger if adapter supports it
|
|
38
|
-
if (adapter && typeof (adapter as any).setLogger === 'function') {
|
|
39
|
-
(adapter as any).setLogger(logger);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Inject repositories into DatabaseStorageAdapter if it supports it
|
|
43
|
-
if (adapter && typeof (adapter as any).setRepositories === 'function') {
|
|
44
|
-
if (rateLimitRepo && storageLockRepo) {
|
|
45
|
-
(adapter as any).setRepositories(rateLimitRepo, storageLockRepo);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
await adapter.initialize();
|
|
50
|
-
return adapter;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// No storage adapter provided - try to use DatabaseStorageAdapter if repositories available
|
|
54
|
-
if (rateLimitRepo && storageLockRepo) {
|
|
55
|
-
try {
|
|
56
|
-
// Lazy import to avoid bundling if not used
|
|
57
|
-
// @ts-ignore - Dynamic import of optional peer dependency
|
|
58
|
-
const { DatabaseStorageAdapter } = await import('@nauth-toolkit/storage-database');
|
|
59
|
-
const adapter = new DatabaseStorageAdapter(null, null, logger as any);
|
|
60
|
-
adapter.setRepositories(rateLimitRepo as any, storageLockRepo as any);
|
|
61
|
-
await adapter.initialize();
|
|
62
|
-
|
|
63
|
-
logger?.warn?.(
|
|
64
|
-
'WARNING: Storage adapter not provided. Using DatabaseStorageAdapter as default. ' +
|
|
65
|
-
'For production, explicitly configure storageAdapter in your config.',
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
return adapter;
|
|
69
|
-
} catch (error) {
|
|
70
|
-
// If DatabaseStorageAdapter import fails, fall through to error
|
|
71
|
-
logger?.error?.(
|
|
72
|
-
'Failed to create DatabaseStorageAdapter. Please explicitly configure storageAdapter in your config.',
|
|
73
|
-
{ error },
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// No storage adapter provided and no repositories available - REQUIRE explicit configuration
|
|
79
|
-
throw new NAuthException(
|
|
80
|
-
AuthErrorCode.VALIDATION_FAILED,
|
|
81
|
-
'Storage adapter is REQUIRED for production deployments. ' +
|
|
82
|
-
'MemoryStorageAdapter is NOT safe for production (data lost on restart, not shared across instances). ' +
|
|
83
|
-
'Please configure storageAdapter in your NAuthConfig:\n\n' +
|
|
84
|
-
'Option 1: DatabaseStorageAdapter (recommended if you have a database)\n' +
|
|
85
|
-
' import { createDatabaseStorageAdapter } from "@nauth-toolkit/express";\n' +
|
|
86
|
-
' storageAdapter: createDatabaseStorageAdapter()\n\n' +
|
|
87
|
-
'Option 2: RedisStorageAdapter (for high-performance multi-server deployments)\n' +
|
|
88
|
-
' import { createRedisStorageAdapter } from "@nauth-toolkit/express";\n' +
|
|
89
|
-
' storageAdapter: createRedisStorageAdapter(process.env.REDIS_URL)\n\n' +
|
|
90
|
-
'Make sure to include storage entities in your DataSource configuration:\n' +
|
|
91
|
-
' import { getNAuthStorageEntities } from "@nauth-toolkit/database-typeorm-postgres";\n' +
|
|
92
|
-
' entities: [...getNAuthEntities(), ...getNAuthStorageEntities()]',
|
|
93
|
-
);
|
|
94
|
-
}
|