@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.
- package/LICENSE +90 -0
- package/README.md +30 -0
- package/package.json +7 -2
- 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,621 +0,0 @@
|
|
|
1
|
-
import * as jose from 'jose';
|
|
2
|
-
import { JwtConfig } from '../interfaces/config.interface';
|
|
3
|
-
import { NAuthException } from '../exceptions/nauth.exception';
|
|
4
|
-
import { AuthErrorCode } from '../enums/error-codes.enum';
|
|
5
|
-
import * as crypto from 'crypto';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* JWT Payload structure
|
|
9
|
-
*/
|
|
10
|
-
export interface JwtPayload {
|
|
11
|
-
/** User ID */
|
|
12
|
-
sub: string;
|
|
13
|
-
|
|
14
|
-
/** User email */
|
|
15
|
-
email: string;
|
|
16
|
-
|
|
17
|
-
/** Token type (access or refresh) */
|
|
18
|
-
type: 'access' | 'refresh';
|
|
19
|
-
|
|
20
|
-
/** Session ID */
|
|
21
|
-
sessionId: string;
|
|
22
|
-
|
|
23
|
-
/** Token family ID (for rotation detection) */
|
|
24
|
-
tokenFamily?: string;
|
|
25
|
-
|
|
26
|
-
/** Issued at timestamp */
|
|
27
|
-
iat: number;
|
|
28
|
-
|
|
29
|
-
/** Expiration timestamp */
|
|
30
|
-
exp: number;
|
|
31
|
-
|
|
32
|
-
/** Issuer */
|
|
33
|
-
iss?: string;
|
|
34
|
-
|
|
35
|
-
/** Audience */
|
|
36
|
-
aud?: string | string[];
|
|
37
|
-
|
|
38
|
-
/** Device ID */
|
|
39
|
-
deviceId?: string;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Token validation result
|
|
44
|
-
*/
|
|
45
|
-
export interface TokenValidationResult {
|
|
46
|
-
/** Whether token is valid */
|
|
47
|
-
valid: boolean;
|
|
48
|
-
|
|
49
|
-
/** Decoded payload if valid */
|
|
50
|
-
payload?: JwtPayload;
|
|
51
|
-
|
|
52
|
-
/** Error message if invalid */
|
|
53
|
-
error?: string;
|
|
54
|
-
|
|
55
|
-
/** Error type for specific handling */
|
|
56
|
-
errorType?: 'expired' | 'invalid' | 'malformed' | 'blacklisted';
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Token pair (access + refresh)
|
|
61
|
-
*/
|
|
62
|
-
export interface TokenPair {
|
|
63
|
-
/** Short-lived access token */
|
|
64
|
-
accessToken: string;
|
|
65
|
-
|
|
66
|
-
/** Long-lived refresh token */
|
|
67
|
-
refreshToken: string;
|
|
68
|
-
|
|
69
|
-
/** Access token expiration in seconds */
|
|
70
|
-
expiresIn: number;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* JWT Service (Platform-Agnostic)
|
|
75
|
-
*
|
|
76
|
-
* Handles all JWT token operations using jose library for platform independence.
|
|
77
|
-
*
|
|
78
|
-
* **Features:**
|
|
79
|
-
* - Platform-agnostic (no framework dependencies)
|
|
80
|
-
* - Support for multiple algorithms (HS256, HS384, HS512, RS256, RS384, RS512)
|
|
81
|
-
* - Token rotation with family tracking
|
|
82
|
-
* - Token reuse detection
|
|
83
|
-
* - Symmetric and asymmetric key support
|
|
84
|
-
*
|
|
85
|
-
* **Security Features:**
|
|
86
|
-
* - HS256 as default algorithm (symmetric key)
|
|
87
|
-
* - HS256/HS384/HS512 for symmetric keys
|
|
88
|
-
* - RS256/RS384/RS512 for asymmetric keys
|
|
89
|
-
* - Token rotation on refresh
|
|
90
|
-
* - Token family tracking for reuse detection
|
|
91
|
-
* - Configurable expiration times
|
|
92
|
-
* - Standard JWT claims (iss, aud, sub, exp, iat)
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
|
-
* ```typescript
|
|
96
|
-
* const jwtService = new JwtService(config);
|
|
97
|
-
*
|
|
98
|
-
* // Generate token pair
|
|
99
|
-
* const tokens = await jwtService.generateTokenPair({
|
|
100
|
-
* userId: 'user-123',
|
|
101
|
-
* email: 'user@example.com',
|
|
102
|
-
* sessionId: 'session-456',
|
|
103
|
-
* });
|
|
104
|
-
*
|
|
105
|
-
* // Validate token
|
|
106
|
-
* const result = await jwtService.validateAccessToken(tokens.accessToken);
|
|
107
|
-
* if (result.valid) {
|
|
108
|
-
* console.log('User ID:', result.payload.sub);
|
|
109
|
-
* }
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
export class JwtService {
|
|
113
|
-
/** JWT configuration */
|
|
114
|
-
private readonly config: JwtConfig;
|
|
115
|
-
|
|
116
|
-
/** Cached access token key (for performance) */
|
|
117
|
-
private accessTokenKey: Uint8Array | crypto.KeyObject | null = null;
|
|
118
|
-
|
|
119
|
-
/** Cached refresh token key (for performance) */
|
|
120
|
-
private refreshTokenKey: Uint8Array | crypto.KeyObject | null = null;
|
|
121
|
-
|
|
122
|
-
constructor(jwtConfig: JwtConfig) {
|
|
123
|
-
this.config = jwtConfig;
|
|
124
|
-
this.prepareKeys();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// ============================================================================
|
|
128
|
-
// Key Preparation
|
|
129
|
-
// ============================================================================
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Prepare and cache signing keys for better performance
|
|
133
|
-
* @private
|
|
134
|
-
*/
|
|
135
|
-
private prepareKeys(): void {
|
|
136
|
-
// Access token key
|
|
137
|
-
if (this.config.accessToken.privateKey) {
|
|
138
|
-
// Use private key (for RS256, RS384, RS512)
|
|
139
|
-
this.accessTokenKey = crypto.createPrivateKey(this.config.accessToken.privateKey);
|
|
140
|
-
} else if (this.config.accessToken.secret) {
|
|
141
|
-
// For symmetric algorithms (HS256, HS384, HS512), use secret as Uint8Array
|
|
142
|
-
this.accessTokenKey = new TextEncoder().encode(this.config.accessToken.secret);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Refresh token key (always uses secret for symmetric algorithms)
|
|
146
|
-
if (this.config.refreshToken.secret) {
|
|
147
|
-
this.refreshTokenKey = new TextEncoder().encode(this.config.refreshToken.secret);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Get algorithm for signing access tokens
|
|
153
|
-
*
|
|
154
|
-
* Automatically selects appropriate algorithm based on key material:
|
|
155
|
-
* - If privateKey is provided → uses configured algorithm (RS256, RS384, RS512)
|
|
156
|
-
* - If only secret is provided → uses configured algorithm or defaults to HS256
|
|
157
|
-
*
|
|
158
|
-
* @private
|
|
159
|
-
*/
|
|
160
|
-
private getAlgorithm(): string {
|
|
161
|
-
// Default to HS256 if no algorithm is configured
|
|
162
|
-
return this.config.algorithm || 'HS256';
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Get algorithm for signing refresh tokens
|
|
167
|
-
*
|
|
168
|
-
* Refresh tokens only support symmetric algorithms (HS256/HS384/HS512)
|
|
169
|
-
* because RefreshTokenConfig only provides a secret, not a privateKey.
|
|
170
|
-
*
|
|
171
|
-
* Automatically selects appropriate symmetric algorithm:
|
|
172
|
-
* - If configured algorithm is symmetric (HS256/HS384/HS512) → uses it
|
|
173
|
-
* - If configured algorithm is asymmetric (RS256, RS384, RS512) → falls back to HS256
|
|
174
|
-
* - Defaults to HS256 if no algorithm is configured
|
|
175
|
-
*
|
|
176
|
-
* @private
|
|
177
|
-
*/
|
|
178
|
-
private getRefreshTokenAlgorithm(): string {
|
|
179
|
-
const configuredAlgorithm = this.config.algorithm || 'HS256';
|
|
180
|
-
|
|
181
|
-
// Refresh tokens only support symmetric algorithms (HS256, HS384, HS512)
|
|
182
|
-
// because RefreshTokenConfig only has a secret, not a privateKey
|
|
183
|
-
if (configuredAlgorithm === 'HS256' || configuredAlgorithm === 'HS384' || configuredAlgorithm === 'HS512') {
|
|
184
|
-
return configuredAlgorithm;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// For asymmetric algorithms (RS256, RS384, RS512), fall back to HS256
|
|
188
|
-
// This ensures compatibility with the symmetric refreshTokenKey
|
|
189
|
-
return 'HS256';
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// ============================================================================
|
|
193
|
-
// Token Generation
|
|
194
|
-
// ============================================================================
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Generate both access and refresh tokens
|
|
198
|
-
*
|
|
199
|
-
* Creates a pair of tokens with the same token family for rotation tracking.
|
|
200
|
-
* The token family allows detection of token reuse attacks.
|
|
201
|
-
*
|
|
202
|
-
* @param data - User and session information
|
|
203
|
-
* @returns Token pair with access and refresh tokens
|
|
204
|
-
*
|
|
205
|
-
* @example
|
|
206
|
-
* ```typescript
|
|
207
|
-
* const tokens = await jwtService.generateTokenPair({
|
|
208
|
-
* userId: 'user-123',
|
|
209
|
-
* email: 'user@example.com',
|
|
210
|
-
* sessionId: 'session-456',
|
|
211
|
-
* });
|
|
212
|
-
*
|
|
213
|
-
* // Store tokens and send to client
|
|
214
|
-
* res.json({
|
|
215
|
-
* accessToken: tokens.accessToken,
|
|
216
|
-
* refreshToken: tokens.refreshToken,
|
|
217
|
-
* expiresIn: tokens.expiresIn,
|
|
218
|
-
* });
|
|
219
|
-
* ```
|
|
220
|
-
*/
|
|
221
|
-
async generateTokenPair(data: {
|
|
222
|
-
userId: string;
|
|
223
|
-
email: string;
|
|
224
|
-
sessionId: string;
|
|
225
|
-
tokenFamily?: string;
|
|
226
|
-
}): Promise<TokenPair> {
|
|
227
|
-
// Generate or reuse token family ID for rotation tracking
|
|
228
|
-
const tokenFamily = data.tokenFamily || this.generateTokenFamily();
|
|
229
|
-
|
|
230
|
-
// Generate access token (short-lived)
|
|
231
|
-
const accessToken = await this.generateAccessToken({
|
|
232
|
-
...data,
|
|
233
|
-
tokenFamily,
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
// Generate refresh token (long-lived)
|
|
237
|
-
const refreshToken = await this.generateRefreshToken({
|
|
238
|
-
...data,
|
|
239
|
-
tokenFamily,
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
// Calculate expiration time in seconds
|
|
243
|
-
const expiresIn = this.parseExpiresIn(this.config.accessToken.expiresIn);
|
|
244
|
-
|
|
245
|
-
return {
|
|
246
|
-
accessToken,
|
|
247
|
-
refreshToken,
|
|
248
|
-
expiresIn,
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Generate an access token
|
|
254
|
-
*
|
|
255
|
-
* Access tokens are short-lived (typically 15 minutes) and used for API authentication.
|
|
256
|
-
* They contain user identity and authorization information.
|
|
257
|
-
*
|
|
258
|
-
* @param data - Token payload data
|
|
259
|
-
* @returns Signed JWT access token
|
|
260
|
-
*/
|
|
261
|
-
async generateAccessToken(data: {
|
|
262
|
-
userId: string;
|
|
263
|
-
email: string;
|
|
264
|
-
sessionId: string;
|
|
265
|
-
tokenFamily: string;
|
|
266
|
-
}): Promise<string> {
|
|
267
|
-
if (!this.accessTokenKey) {
|
|
268
|
-
throw new NAuthException(
|
|
269
|
-
AuthErrorCode.INTERNAL_ERROR,
|
|
270
|
-
'Access token key not configured. Provide secret or privateKey.',
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const algorithm = this.getAlgorithm();
|
|
275
|
-
let jwt = new jose.SignJWT({
|
|
276
|
-
sub: data.userId,
|
|
277
|
-
email: data.email,
|
|
278
|
-
type: 'access',
|
|
279
|
-
sessionId: data.sessionId,
|
|
280
|
-
tokenFamily: data.tokenFamily,
|
|
281
|
-
})
|
|
282
|
-
.setProtectedHeader({ alg: algorithm })
|
|
283
|
-
.setIssuedAt()
|
|
284
|
-
.setExpirationTime(this.config.accessToken.expiresIn);
|
|
285
|
-
|
|
286
|
-
// Add issuer if configured
|
|
287
|
-
if (this.config.issuer) {
|
|
288
|
-
jwt = jwt.setIssuer(this.config.issuer);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// Add audience if configured
|
|
292
|
-
if (this.config.audience) {
|
|
293
|
-
if (Array.isArray(this.config.audience)) {
|
|
294
|
-
jwt = jwt.setAudience(this.config.audience);
|
|
295
|
-
} else {
|
|
296
|
-
jwt = jwt.setAudience(this.config.audience);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
return await jwt.sign(this.accessTokenKey);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Generate a refresh token
|
|
305
|
-
*
|
|
306
|
-
* Refresh tokens are long-lived (typically 30 days) and used to obtain new access tokens.
|
|
307
|
-
* They should be stored securely and rotated on each use.
|
|
308
|
-
*
|
|
309
|
-
* ⚠️ NOTE: Refresh tokens always use a symmetric algorithm (HS256/HS384/HS512)
|
|
310
|
-
* because RefreshTokenConfig only provides a secret, not a privateKey.
|
|
311
|
-
* This ensures compatibility between the algorithm and key type.
|
|
312
|
-
*
|
|
313
|
-
* @param data - Token payload data
|
|
314
|
-
* @returns Signed JWT refresh token
|
|
315
|
-
*/
|
|
316
|
-
async generateRefreshToken(data: {
|
|
317
|
-
userId: string;
|
|
318
|
-
email: string;
|
|
319
|
-
sessionId: string;
|
|
320
|
-
tokenFamily: string;
|
|
321
|
-
}): Promise<string> {
|
|
322
|
-
if (!this.refreshTokenKey) {
|
|
323
|
-
throw new NAuthException(AuthErrorCode.INTERNAL_ERROR, 'Refresh token secret not configured.');
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// Use refresh token-specific algorithm (always symmetric)
|
|
327
|
-
const algorithm = this.getRefreshTokenAlgorithm();
|
|
328
|
-
const jwt = new jose.SignJWT({
|
|
329
|
-
sub: data.userId,
|
|
330
|
-
email: data.email,
|
|
331
|
-
type: 'refresh',
|
|
332
|
-
sessionId: data.sessionId,
|
|
333
|
-
tokenFamily: data.tokenFamily,
|
|
334
|
-
})
|
|
335
|
-
.setProtectedHeader({ alg: algorithm })
|
|
336
|
-
.setIssuedAt()
|
|
337
|
-
.setExpirationTime(this.config.refreshToken.expiresIn);
|
|
338
|
-
|
|
339
|
-
return await jwt.sign(this.refreshTokenKey);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// ============================================================================
|
|
343
|
-
// Token Validation
|
|
344
|
-
// ============================================================================
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Validate an access token
|
|
348
|
-
*
|
|
349
|
-
* Verifies:
|
|
350
|
-
* - Token signature is valid
|
|
351
|
-
* - Token hasn't expired
|
|
352
|
-
* - Token type is 'access'
|
|
353
|
-
* - Token structure is correct
|
|
354
|
-
*
|
|
355
|
-
* @param token - JWT access token to validate
|
|
356
|
-
* @returns Validation result with payload or error
|
|
357
|
-
*
|
|
358
|
-
* @example
|
|
359
|
-
* ```typescript
|
|
360
|
-
* const result = await jwtService.validateAccessToken(token);
|
|
361
|
-
*
|
|
362
|
-
* if (!result.valid) {
|
|
363
|
-
* if (result.errorType === 'expired') {
|
|
364
|
-
* // Attempt to refresh token
|
|
365
|
-
* } else {
|
|
366
|
-
* // Invalid token, reject request
|
|
367
|
-
* }
|
|
368
|
-
* }
|
|
369
|
-
* ```
|
|
370
|
-
*/
|
|
371
|
-
async validateAccessToken(token: string): Promise<TokenValidationResult> {
|
|
372
|
-
try {
|
|
373
|
-
// Determine key for verification
|
|
374
|
-
let verificationKey: Uint8Array | crypto.KeyObject;
|
|
375
|
-
|
|
376
|
-
if (this.config.accessToken.publicKey) {
|
|
377
|
-
// Use public key for asymmetric verification (RS256, RS384, RS512)
|
|
378
|
-
verificationKey = crypto.createPublicKey(this.config.accessToken.publicKey);
|
|
379
|
-
} else if (this.accessTokenKey) {
|
|
380
|
-
// Use secret for symmetric verification (HS256, HS512)
|
|
381
|
-
verificationKey = this.accessTokenKey;
|
|
382
|
-
} else {
|
|
383
|
-
throw new Error('No verification key available');
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// Verify and decode token
|
|
387
|
-
const { payload } = await jose.jwtVerify(token, verificationKey, {
|
|
388
|
-
issuer: this.config.issuer,
|
|
389
|
-
audience: this.config.audience,
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
// Cast payload to JwtPayload
|
|
393
|
-
const jwtPayload = payload as unknown as JwtPayload;
|
|
394
|
-
|
|
395
|
-
// Ensure token type is correct
|
|
396
|
-
if (jwtPayload.type !== 'access') {
|
|
397
|
-
return {
|
|
398
|
-
valid: false,
|
|
399
|
-
error: 'Invalid token type',
|
|
400
|
-
errorType: 'invalid',
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
return {
|
|
405
|
-
valid: true,
|
|
406
|
-
payload: jwtPayload,
|
|
407
|
-
};
|
|
408
|
-
} catch (error) {
|
|
409
|
-
return this.handleValidationError(error);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
* Validate a refresh token
|
|
415
|
-
*
|
|
416
|
-
* Similar to access token validation but checks for 'refresh' type.
|
|
417
|
-
* Also verifies token hasn't been used before (if rotation is enabled).
|
|
418
|
-
*
|
|
419
|
-
* @param token - JWT refresh token to validate
|
|
420
|
-
* @returns Validation result with payload or error
|
|
421
|
-
*/
|
|
422
|
-
async validateRefreshToken(token: string): Promise<TokenValidationResult> {
|
|
423
|
-
try {
|
|
424
|
-
if (!this.refreshTokenKey) {
|
|
425
|
-
throw new Error('Refresh token key not configured');
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
// Verify and decode token
|
|
429
|
-
const { payload } = await jose.jwtVerify(token, this.refreshTokenKey);
|
|
430
|
-
|
|
431
|
-
// Cast payload to JwtPayload
|
|
432
|
-
const jwtPayload = payload as unknown as JwtPayload;
|
|
433
|
-
|
|
434
|
-
// Ensure token type is correct
|
|
435
|
-
if (jwtPayload.type !== 'refresh') {
|
|
436
|
-
return {
|
|
437
|
-
valid: false,
|
|
438
|
-
error: 'Invalid token type',
|
|
439
|
-
errorType: 'invalid',
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
return {
|
|
444
|
-
valid: true,
|
|
445
|
-
payload: jwtPayload,
|
|
446
|
-
};
|
|
447
|
-
} catch (error) {
|
|
448
|
-
return this.handleValidationError(error);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
/**
|
|
453
|
-
* Decode a token without verification
|
|
454
|
-
*
|
|
455
|
-
* ⚠️ WARNING: This method does NOT validate the token signature or expiration.
|
|
456
|
-
* Only use for non-security-critical operations like logging or analytics.
|
|
457
|
-
*
|
|
458
|
-
* @param token - JWT token to decode
|
|
459
|
-
* @returns Decoded payload or null if malformed
|
|
460
|
-
*/
|
|
461
|
-
decodeToken(token: string): JwtPayload | null {
|
|
462
|
-
try {
|
|
463
|
-
const payload = jose.decodeJwt(token);
|
|
464
|
-
// Convert jose.JWTPayload to our JwtPayload via unknown
|
|
465
|
-
return payload as unknown as JwtPayload;
|
|
466
|
-
} catch {
|
|
467
|
-
return null;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// ============================================================================
|
|
472
|
-
// Token Utilities
|
|
473
|
-
// ============================================================================
|
|
474
|
-
|
|
475
|
-
/**
|
|
476
|
-
* Generate a unique token family identifier
|
|
477
|
-
*
|
|
478
|
-
* Token families are used to track token rotation and detect reuse attacks.
|
|
479
|
-
* All tokens in the same "family" (original + rotated versions) share this ID.
|
|
480
|
-
*
|
|
481
|
-
* ⚠️ SECURITY FIX #10: Increased from 16 bytes (128 bits) to 32 bytes (256 bits)
|
|
482
|
-
*
|
|
483
|
-
* @returns Random token family ID (256 bits)
|
|
484
|
-
*/
|
|
485
|
-
generateTokenFamily(): string {
|
|
486
|
-
return crypto.randomBytes(32).toString('hex'); // 256 bits
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
/**
|
|
490
|
-
* Hash a token for storage
|
|
491
|
-
*
|
|
492
|
-
* Tokens should be hashed before storing in the database for security.
|
|
493
|
-
* This prevents token exposure if the database is compromised.
|
|
494
|
-
*
|
|
495
|
-
* @param token - Token to hash
|
|
496
|
-
* @returns SHA-256 hash of the token
|
|
497
|
-
*/
|
|
498
|
-
hashToken(token: string): string {
|
|
499
|
-
return crypto.createHash('sha256').update(token).digest('hex');
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
/**
|
|
503
|
-
* Get access token expiry time in seconds
|
|
504
|
-
*
|
|
505
|
-
* @returns Access token expiry time in seconds
|
|
506
|
-
*
|
|
507
|
-
* @example
|
|
508
|
-
* ```typescript
|
|
509
|
-
* const expiry = jwtService.getAccessTokenExpiry();
|
|
510
|
-
* console.log(expiry); // 900 (15 minutes)
|
|
511
|
-
* ```
|
|
512
|
-
*/
|
|
513
|
-
getAccessTokenExpiry(): number {
|
|
514
|
-
return this.parseExpiresIn(this.config.accessToken.expiresIn);
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* Get refresh token TTL in seconds
|
|
519
|
-
*
|
|
520
|
-
* Used for setting expiration on used-token tracking in storage.
|
|
521
|
-
*
|
|
522
|
-
* @returns TTL in seconds
|
|
523
|
-
*/
|
|
524
|
-
getRefreshTokenTTL(): number {
|
|
525
|
-
return this.parseExpiresIn(this.config.refreshToken.expiresIn);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* Extract token from Authorization header
|
|
530
|
-
*
|
|
531
|
-
* Supports standard "Bearer <token>" format
|
|
532
|
-
*
|
|
533
|
-
* @param authHeader - Authorization header value
|
|
534
|
-
* @returns Extracted token or null
|
|
535
|
-
*
|
|
536
|
-
* @example
|
|
537
|
-
* ```typescript
|
|
538
|
-
* const token = jwtService.extractTokenFromHeader('Bearer eyJhbGc...');
|
|
539
|
-
* // Returns: 'eyJhbGc...'
|
|
540
|
-
* ```
|
|
541
|
-
*/
|
|
542
|
-
extractTokenFromHeader(authHeader?: string): string | null {
|
|
543
|
-
if (!authHeader) return null;
|
|
544
|
-
|
|
545
|
-
const [type, token] = authHeader.split(' ');
|
|
546
|
-
|
|
547
|
-
// Verify Bearer scheme
|
|
548
|
-
if (type !== 'Bearer') return null;
|
|
549
|
-
|
|
550
|
-
return token || null;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
// ============================================================================
|
|
554
|
-
// Private Helper Methods
|
|
555
|
-
// ============================================================================
|
|
556
|
-
|
|
557
|
-
/**
|
|
558
|
-
* Parse expiration time from string or number
|
|
559
|
-
* @param expiresIn - Expiration time (e.g., '15m', 900, '1h')
|
|
560
|
-
* @returns Expiration time in seconds
|
|
561
|
-
*/
|
|
562
|
-
private parseExpiresIn(expiresIn: string | number): number {
|
|
563
|
-
if (typeof expiresIn === 'number') {
|
|
564
|
-
return expiresIn;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
// Parse time strings (e.g., '15m', '1h', '30d')
|
|
568
|
-
const units: Record<string, number> = {
|
|
569
|
-
s: 1,
|
|
570
|
-
m: 60,
|
|
571
|
-
h: 3600,
|
|
572
|
-
d: 86400,
|
|
573
|
-
};
|
|
574
|
-
|
|
575
|
-
const match = expiresIn.match(/^(\d+)([smhd])$/);
|
|
576
|
-
if (!match) {
|
|
577
|
-
throw new NAuthException(AuthErrorCode.VALIDATION_FAILED, `Invalid expiresIn format: ${expiresIn}`);
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
const [, value, unit] = match;
|
|
581
|
-
return parseInt(value, 10) * units[unit];
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
/**
|
|
585
|
-
* Handle JWT validation errors and convert to standardized result
|
|
586
|
-
* @param error - Error from JWT verification
|
|
587
|
-
* @returns Standardized validation result
|
|
588
|
-
*/
|
|
589
|
-
private handleValidationError(error: unknown): TokenValidationResult {
|
|
590
|
-
if (error instanceof Error) {
|
|
591
|
-
// jose errors have a 'code' property
|
|
592
|
-
const errorWithCode = error as Error & { code?: string };
|
|
593
|
-
const errorCode = errorWithCode.code;
|
|
594
|
-
|
|
595
|
-
// Token expired (jose errors)
|
|
596
|
-
if (error.message.includes('expired') || errorCode === 'ERR_JWT_EXPIRED') {
|
|
597
|
-
return {
|
|
598
|
-
valid: false,
|
|
599
|
-
error: 'Token has expired',
|
|
600
|
-
errorType: 'expired',
|
|
601
|
-
};
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
// Invalid signature or malformed token
|
|
605
|
-
if (error.message.includes('signature') || error.message.includes('invalid') || errorCode === 'ERR_JWT_INVALID') {
|
|
606
|
-
return {
|
|
607
|
-
valid: false,
|
|
608
|
-
error: 'Invalid token',
|
|
609
|
-
errorType: 'invalid',
|
|
610
|
-
};
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
// Unknown error
|
|
615
|
-
return {
|
|
616
|
-
valid: false,
|
|
617
|
-
error: 'Token validation failed',
|
|
618
|
-
errorType: 'malformed',
|
|
619
|
-
};
|
|
620
|
-
}
|
|
621
|
-
}
|