@nauth-toolkit/core 0.1.39 → 0.1.40

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 (33) hide show
  1. package/dist/dto/get-user-sessions-response.dto.d.ts +88 -0
  2. package/dist/dto/get-user-sessions-response.dto.d.ts.map +1 -0
  3. package/dist/dto/get-user-sessions-response.dto.js +181 -0
  4. package/dist/dto/get-user-sessions-response.dto.js.map +1 -0
  5. package/dist/dto/get-user-sessions.dto.d.ts +17 -0
  6. package/dist/dto/get-user-sessions.dto.d.ts.map +1 -0
  7. package/dist/dto/get-user-sessions.dto.js +38 -0
  8. package/dist/dto/get-user-sessions.dto.js.map +1 -0
  9. package/dist/dto/index.d.ts +4 -0
  10. package/dist/dto/index.d.ts.map +1 -1
  11. package/dist/dto/index.js +4 -0
  12. package/dist/dto/index.js.map +1 -1
  13. package/dist/dto/logout-session-response.dto.d.ts +20 -0
  14. package/dist/dto/logout-session-response.dto.d.ts.map +1 -0
  15. package/dist/dto/logout-session-response.dto.js +42 -0
  16. package/dist/dto/logout-session-response.dto.js.map +1 -0
  17. package/dist/dto/logout-session.dto.d.ts +22 -0
  18. package/dist/dto/logout-session.dto.d.ts.map +1 -0
  19. package/dist/dto/logout-session.dto.js +48 -0
  20. package/dist/dto/logout-session.dto.js.map +1 -0
  21. package/dist/services/auth-service-internal-helpers.d.ts +229 -0
  22. package/dist/services/auth-service-internal-helpers.d.ts.map +1 -0
  23. package/dist/services/auth-service-internal-helpers.js +1004 -0
  24. package/dist/services/auth-service-internal-helpers.js.map +1 -0
  25. package/dist/services/auth.service.d.ts +178 -156
  26. package/dist/services/auth.service.d.ts.map +1 -1
  27. package/dist/services/auth.service.js +486 -2308
  28. package/dist/services/auth.service.js.map +1 -1
  29. package/dist/services/user.service.d.ts +274 -0
  30. package/dist/services/user.service.d.ts.map +1 -0
  31. package/dist/services/user.service.js +1327 -0
  32. package/dist/services/user.service.js.map +1 -0
  33. package/package.json +1 -1
@@ -0,0 +1,229 @@
1
+ import { Repository } from 'typeorm';
2
+ import { IUser } from '../interfaces/entities.interface';
3
+ import { BaseUser, BaseLoginAttempt, BaseChallengeSession } from '../entities';
4
+ import { PasswordService } from './password.service';
5
+ import { SessionService } from './session.service';
6
+ import { EmailVerificationService } from './email-verification.service';
7
+ import { PhoneVerificationService } from './phone-verification.service';
8
+ import { ClientInfoService } from './client-info.service';
9
+ import { ChallengeService } from './challenge.service';
10
+ import { AuthChallengeHelperService } from './auth-challenge-helper.service';
11
+ import { AccountLockoutStorageService } from '../storage/account-lockout-storage.service';
12
+ import { InternalAuthAuditService as AuthAuditService } from './auth-audit.service';
13
+ import { TrustedDeviceService } from './trusted-device.service';
14
+ import { MFAService } from './mfa.service';
15
+ import { AuthAuditEventType } from '../enums/auth-audit-event-type.enum';
16
+ import { ChallengeResponseData, CollectPhoneResponse, VerifyPhoneResponse, VerifyMFACodeResponse, VerifyMFAPasskeyResponse, MFASetupResponse } from '../dto/challenge-response.dto';
17
+ import { AuthResponseDTO } from '../dto/auth-response.dto';
18
+ import { UpdateUserAttributesRequestDTO } from '../dto/update-user-attributes-request.dto';
19
+ import { NAuthConfig } from '../interfaces/config.interface';
20
+ import { NAuthLogger } from '../utils/nauth-logger';
21
+ /**
22
+ * Internal helper service for AuthService
23
+ *
24
+ * Contains private utility methods for challenge handling, validation,
25
+ * password management, and login tracking. This class is NOT exported from
26
+ * the package and should only be used internally by AuthService.
27
+ *
28
+ * INTERNAL USE ONLY - DO NOT IMPORT DIRECTLY
29
+ *
30
+ * @internal
31
+ */
32
+ export declare class AuthServiceInternalHelpers {
33
+ private readonly userRepository;
34
+ private readonly loginAttemptRepository;
35
+ private readonly emailVerificationService;
36
+ private readonly phoneVerificationService;
37
+ private readonly challengeService;
38
+ private readonly challengeHelper;
39
+ private readonly clientInfoService;
40
+ private readonly sessionService;
41
+ private readonly accountLockoutStorage;
42
+ private readonly config;
43
+ private readonly logger;
44
+ constructor(userRepository: Repository<BaseUser>, loginAttemptRepository: Repository<BaseLoginAttempt>, emailVerificationService: EmailVerificationService, phoneVerificationService: PhoneVerificationService | undefined, challengeService: ChallengeService, challengeHelper: AuthChallengeHelperService, clientInfoService: ClientInfoService, sessionService: SessionService, accountLockoutStorage: AccountLockoutStorageService, config: NAuthConfig, logger: NAuthLogger);
45
+ /**
46
+ * Handle VERIFY_EMAIL challenge
47
+ *
48
+ * @param challengeSession - Challenge session with user
49
+ * @param code - Email verification code
50
+ * @returns Authentication response with tokens or next challenge
51
+ */
52
+ handleVerifyEmail(challengeSession: BaseChallengeSession & {
53
+ user?: BaseUser;
54
+ }, code: string): Promise<AuthResponseDTO>;
55
+ /**
56
+ * Handle VERIFY_PHONE challenge
57
+ *
58
+ * @param challengeSession - Challenge session with user
59
+ * @param data - Phone verification data (phone number or code)
60
+ * @returns Authentication response with tokens or next challenge
61
+ */
62
+ handleVerifyPhone(challengeSession: BaseChallengeSession & {
63
+ user?: BaseUser;
64
+ }, data: VerifyPhoneResponse | CollectPhoneResponse): Promise<AuthResponseDTO>;
65
+ /**
66
+ * Handle MFA_REQUIRED challenge
67
+ *
68
+ * @param challengeSession - Challenge session with user
69
+ * @param data - MFA verification data
70
+ * @param mfaService - MFA service (passed from AuthService)
71
+ * @param trustedDeviceService - Trusted device service (optional, passed from AuthService)
72
+ * @param auditService - Audit service (optional, passed from AuthService)
73
+ * @returns Authentication response with tokens or next challenge
74
+ */
75
+ handleMFAVerification(challengeSession: BaseChallengeSession & {
76
+ user?: BaseUser;
77
+ }, data: VerifyMFACodeResponse | VerifyMFAPasskeyResponse, mfaService: MFAService | undefined, trustedDeviceService: TrustedDeviceService | undefined, auditService: AuthAuditService | undefined): Promise<AuthResponseDTO>;
78
+ /**
79
+ * Handle FORCE_CHANGE_PASSWORD challenge
80
+ *
81
+ * @param challengeSession - Challenge session with user
82
+ * @param newPassword - New password
83
+ * @param passwordService - Password service (passed from AuthService)
84
+ * @param auditService - Audit service (optional, passed from AuthService)
85
+ * @returns Authentication response with tokens or next challenge
86
+ */
87
+ handleForceChangePassword(challengeSession: BaseChallengeSession & {
88
+ user?: BaseUser;
89
+ }, newPassword: string, passwordService: PasswordService, auditService: AuthAuditService | undefined): Promise<AuthResponseDTO>;
90
+ /**
91
+ * Handle MFA_SETUP_REQUIRED challenge
92
+ *
93
+ * @param challengeSession - Challenge session with user
94
+ * @param data - MFA setup data
95
+ * @param mfaService - MFA service (passed from AuthService)
96
+ * @param auditService - Audit service (optional, passed from AuthService)
97
+ * @returns Authentication response with tokens or next challenge
98
+ */
99
+ handleMFASetup(challengeSession: BaseChallengeSession & {
100
+ user?: BaseUser;
101
+ }, data: MFASetupResponse, mfaService: MFAService | undefined, _auditService: AuthAuditService | undefined): Promise<AuthResponseDTO>;
102
+ /**
103
+ * Validate that response type matches expected challenge type
104
+ *
105
+ * @param expected - Expected challenge type
106
+ * @param provided - Provided challenge type
107
+ * @throws {NAuthException} If types don't match
108
+ */
109
+ validateChallengeTypeMatch(expected: string, provided: string): void;
110
+ /**
111
+ * Validate parameters for challenge type
112
+ *
113
+ * Service-level validation ensures Express/other frameworks get same validation as NestJS.
114
+ * This is critical for non-DTO-based applications.
115
+ *
116
+ * @param type - Challenge type
117
+ * @param data - Challenge response data
118
+ * @throws {NAuthException} If validation fails
119
+ */
120
+ validateChallengeParams(type: string, data: ChallengeResponseData): void;
121
+ /**
122
+ * Checks if the login identifier matches the specified allowed type.
123
+ *
124
+ * Determines if the given identifier is a valid email, username, phone, or allowed hybrid,
125
+ * according to the configured identifier type restriction.
126
+ *
127
+ * @param identifier - The login identifier to check (email, username, or phone)
128
+ * @param allowedType - The permitted identifier type ('email', 'username', 'phone', or 'email_or_username')
129
+ * @returns True if the identifier conforms to the allowed type, otherwise false
130
+ */
131
+ validateIdentifierType(identifier: string, allowedType: 'email' | 'username' | 'phone' | 'email_or_username'): boolean;
132
+ /**
133
+ * Ensures email, phone, and username are unique for other users before update.
134
+ *
135
+ * Throws if another user already has the specified email, phone, or username.
136
+ *
137
+ * @param userId - Internal numeric user ID (excluded from check)
138
+ * @param updateData - User fields to check for uniqueness
139
+ * @throws {NAuthException} If a unique constraint is violated for email, phone, or username
140
+ */
141
+ validateUniquenessConstraints(userId: number, updateData: UpdateUserAttributesRequestDTO): Promise<void>;
142
+ /**
143
+ * Retrieves a user entity by login identifier.
144
+ *
145
+ * Performs a lookup for a user by email, username, or phone number.
146
+ * The search respects the identifierType restriction when provided, limiting which fields are queried.
147
+ *
148
+ * @param identifier - Login credential (email, username, or phone)
149
+ * @param identifierType - Restricts search to a specific identifier type ('email', 'username', 'phone', or 'email_or_username')
150
+ * @returns The user entity if found, otherwise null
151
+ */
152
+ findUserByIdentifier(identifier: string, identifierType?: 'email' | 'username' | 'phone' | 'email_or_username'): Promise<IUser | null>;
153
+ /**
154
+ * Centralized password update flow used by:
155
+ * - changePassword()
156
+ * - confirmForgotPassword()
157
+ * - adminSetPassword()
158
+ * - FORCE_CHANGE_PASSWORD challenge handler
159
+ *
160
+ * WHY:
161
+ * - Prevent logic drift between different password-changing entrypoints
162
+ * - Ensure consistent validation, history enforcement, persistence, session revocation, and audit trails
163
+ *
164
+ * @param params - Password update parameters
165
+ * @param passwordService - Password service (passed from AuthService)
166
+ * @param auditService - Audit service (optional, passed from AuthService)
167
+ * @returns Sessions revoked count (0 when not revoked)
168
+ * @throws {NAuthException} WEAK_PASSWORD | PASSWORD_REUSED | NOT_FOUND
169
+ */
170
+ updateUserPassword(params: {
171
+ user: IUser;
172
+ newPassword: string;
173
+ mustChangePassword: boolean;
174
+ revokeSessions: boolean;
175
+ revokeReason: string;
176
+ beforePersist?: () => Promise<void>;
177
+ audit?: {
178
+ eventType: AuthAuditEventType;
179
+ eventStatus: 'SUCCESS' | 'FAILURE' | 'INFO' | 'SUSPICIOUS';
180
+ reason?: string;
181
+ description?: string;
182
+ authMethod?: string;
183
+ metadata?: Record<string, unknown>;
184
+ };
185
+ }, passwordService: PasswordService, auditService: AuthAuditService | undefined): Promise<{
186
+ sessionsRevoked: number;
187
+ }>;
188
+ /**
189
+ * Handles a failed login by recording the attempt, applying IP-based lockout policy,
190
+ * and invoking relevant hooks.
191
+ *
192
+ * @param identifier - User identifier (email/username/phone)
193
+ * @param reason - Optional reason for failure
194
+ */
195
+ handleFailedLogin(identifier: string, reason?: string): Promise<void>;
196
+ /**
197
+ * Records a login attempt with client context.
198
+ *
199
+ * @param email - User's email address
200
+ * @param success - True if login succeeded, false if failed
201
+ * @param failureReason - Optional reason for failure
202
+ * @param userId - Optional internal user ID (only for successful logins)
203
+ */
204
+ recordLoginAttempt(email: string, success: boolean, failureReason?: string, userId?: number): Promise<void>;
205
+ /**
206
+ * Clear authentication cookies from response
207
+ *
208
+ * @param response - HTTP response object with clearCookie method
209
+ * @param forgetDevice - Whether to also clear device token cookie
210
+ */
211
+ clearAuthCookies(response: {
212
+ clearCookie?: (name: string, options?: unknown) => void;
213
+ }, forgetDevice: boolean): void;
214
+ /**
215
+ * Mask email address for privacy (show first char and domain)
216
+ *
217
+ * @param email - Email address to mask
218
+ * @returns Masked email (e.g., 'u***r@example.com')
219
+ */
220
+ maskEmail(email: string): string;
221
+ /**
222
+ * Mask phone number for privacy (show last 4 digits)
223
+ *
224
+ * @param phone - Phone number to mask
225
+ * @returns Masked phone (e.g., '***-***-1234')
226
+ */
227
+ maskPhone(phone: string): string;
228
+ }
229
+ //# sourceMappingURL=auth-service-internal-helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-service-internal-helpers.d.ts","sourceRoot":"","sources":["../../src/services/auth-service-internal-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,kCAAkC,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAC/E,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,c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