@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.
Files changed (184) hide show
  1. package/LICENSE +90 -0
  2. package/README.md +9 -0
  3. package/package.json +8 -3
  4. package/jest.config.js +0 -15
  5. package/jest.setup.ts +0 -6
  6. package/src/adapters/database-columns.ts +0 -165
  7. package/src/adapters/express.adapter.ts +0 -385
  8. package/src/adapters/fastify.adapter.ts +0 -416
  9. package/src/adapters/index.ts +0 -16
  10. package/src/adapters/storage.factory.ts +0 -143
  11. package/src/bootstrap.ts +0 -374
  12. package/src/dto/auth-challenge.dto.ts +0 -231
  13. package/src/dto/auth-response.dto.ts +0 -253
  14. package/src/dto/challenge-response.dto.ts +0 -234
  15. package/src/dto/change-password-request.dto.ts +0 -50
  16. package/src/dto/change-password-response.dto.ts +0 -29
  17. package/src/dto/change-password.dto.ts +0 -57
  18. package/src/dto/error-response.dto.ts +0 -136
  19. package/src/dto/get-available-methods.dto.ts +0 -55
  20. package/src/dto/get-challenge-data-response.dto.ts +0 -28
  21. package/src/dto/get-challenge-data.dto.ts +0 -69
  22. package/src/dto/get-client-info.dto.ts +0 -104
  23. package/src/dto/get-device-token-response.dto.ts +0 -25
  24. package/src/dto/get-events-by-type.dto.ts +0 -76
  25. package/src/dto/get-ip-address-response.dto.ts +0 -24
  26. package/src/dto/get-mfa-status.dto.ts +0 -94
  27. package/src/dto/get-risk-assessment-history.dto.ts +0 -39
  28. package/src/dto/get-session-id-response.dto.ts +0 -25
  29. package/src/dto/get-setup-data-response.dto.ts +0 -31
  30. package/src/dto/get-setup-data.dto.ts +0 -75
  31. package/src/dto/get-suspicious-activity.dto.ts +0 -42
  32. package/src/dto/get-user-agent-response.dto.ts +0 -23
  33. package/src/dto/get-user-auth-history.dto.ts +0 -95
  34. package/src/dto/get-user-by-email.dto.ts +0 -61
  35. package/src/dto/get-user-by-id.dto.ts +0 -46
  36. package/src/dto/get-user-devices.dto.ts +0 -53
  37. package/src/dto/get-user-response.dto.ts +0 -17
  38. package/src/dto/has-provider.dto.ts +0 -56
  39. package/src/dto/index.ts +0 -57
  40. package/src/dto/is-trusted-device-response.dto.ts +0 -34
  41. package/src/dto/list-providers-response.dto.ts +0 -23
  42. package/src/dto/login.dto.ts +0 -95
  43. package/src/dto/logout-all-response.dto.ts +0 -24
  44. package/src/dto/logout-all.dto.ts +0 -65
  45. package/src/dto/logout-response.dto.ts +0 -25
  46. package/src/dto/logout.dto.ts +0 -64
  47. package/src/dto/refresh-token.dto.ts +0 -36
  48. package/src/dto/remove-devices.dto.ts +0 -85
  49. package/src/dto/resend-code-response.dto.ts +0 -32
  50. package/src/dto/resend-code.dto.ts +0 -51
  51. package/src/dto/reset-password.dto.ts +0 -115
  52. package/src/dto/respond-challenge.dto.ts +0 -272
  53. package/src/dto/set-mfa-exemption.dto.ts +0 -112
  54. package/src/dto/set-must-change-password-response.dto.ts +0 -27
  55. package/src/dto/set-must-change-password.dto.ts +0 -46
  56. package/src/dto/set-preferred-method.dto.ts +0 -80
  57. package/src/dto/setup-mfa.dto.ts +0 -98
  58. package/src/dto/signup.dto.ts +0 -174
  59. package/src/dto/social-auth.dto.ts +0 -422
  60. package/src/dto/trust-device-response.dto.ts +0 -30
  61. package/src/dto/trust-device.dto.ts +0 -9
  62. package/src/dto/update-user-attributes-request.dto.ts +0 -51
  63. package/src/dto/user-response.dto.ts +0 -138
  64. package/src/dto/user-update.dto.ts +0 -222
  65. package/src/dto/verify-email.dto.ts +0 -313
  66. package/src/dto/verify-mfa-code.dto.ts +0 -103
  67. package/src/dto/verify-phone-by-sub.dto.ts +0 -78
  68. package/src/dto/verify-phone.dto.ts +0 -245
  69. package/src/entities/auth-audit.entity.ts +0 -232
  70. package/src/entities/challenge-session.entity.ts +0 -116
  71. package/src/entities/index.ts +0 -29
  72. package/src/entities/login-attempt.entity.ts +0 -64
  73. package/src/entities/mfa-device.entity.ts +0 -151
  74. package/src/entities/rate-limit.entity.ts +0 -44
  75. package/src/entities/session.entity.ts +0 -180
  76. package/src/entities/social-account.entity.ts +0 -96
  77. package/src/entities/storage-lock.entity.ts +0 -39
  78. package/src/entities/trusted-device.entity.ts +0 -112
  79. package/src/entities/user.entity.ts +0 -243
  80. package/src/entities/verification-token.entity.ts +0 -141
  81. package/src/enums/auth-audit-event-type.enum.ts +0 -360
  82. package/src/enums/error-codes.enum.ts +0 -420
  83. package/src/enums/mfa-method.enum.ts +0 -97
  84. package/src/enums/risk-factor.enum.ts +0 -111
  85. package/src/exceptions/nauth.exception.ts +0 -231
  86. package/src/handlers/auth.handler.ts +0 -260
  87. package/src/handlers/client-info.handler.ts +0 -101
  88. package/src/handlers/csrf.handler.ts +0 -156
  89. package/src/handlers/token-delivery.handler.ts +0 -118
  90. package/src/index.ts +0 -118
  91. package/src/interfaces/client-info.interface.ts +0 -85
  92. package/src/interfaces/config.interface.ts +0 -2135
  93. package/src/interfaces/entities.interface.ts +0 -226
  94. package/src/interfaces/index.ts +0 -15
  95. package/src/interfaces/logger.interface.ts +0 -283
  96. package/src/interfaces/mfa-provider.interface.ts +0 -154
  97. package/src/interfaces/oauth.interface.ts +0 -148
  98. package/src/interfaces/provider.interface.ts +0 -47
  99. package/src/interfaces/social-auth-provider.interface.ts +0 -131
  100. package/src/interfaces/storage-adapter.interface.ts +0 -82
  101. package/src/interfaces/template.interface.ts +0 -510
  102. package/src/interfaces/token-verifier.interface.ts +0 -110
  103. package/src/internal.ts +0 -178
  104. package/src/platform/interfaces.ts +0 -299
  105. package/src/schemas/auth-config.schema.ts +0 -646
  106. package/src/services/adaptive-mfa-decision.service.spec.ts +0 -1058
  107. package/src/services/adaptive-mfa-decision.service.ts +0 -457
  108. package/src/services/auth-audit.service.spec.ts +0 -675
  109. package/src/services/auth-audit.service.ts +0 -558
  110. package/src/services/auth-challenge-helper.service.spec.ts +0 -3227
  111. package/src/services/auth-challenge-helper.service.ts +0 -825
  112. package/src/services/auth-flow-context-builder.service.ts +0 -520
  113. package/src/services/auth-flow-rules.ts +0 -202
  114. package/src/services/auth-flow-state-definitions.ts +0 -190
  115. package/src/services/auth-flow-state-machine.service.ts +0 -207
  116. package/src/services/auth-flow-state-machine.types.ts +0 -316
  117. package/src/services/auth.service.spec.ts +0 -4195
  118. package/src/services/auth.service.ts +0 -3727
  119. package/src/services/challenge.service.spec.ts +0 -1363
  120. package/src/services/challenge.service.ts +0 -696
  121. package/src/services/client-info.service.spec.ts +0 -572
  122. package/src/services/client-info.service.ts +0 -374
  123. package/src/services/csrf.service.ts +0 -54
  124. package/src/services/email-verification.service.spec.ts +0 -1229
  125. package/src/services/email-verification.service.ts +0 -578
  126. package/src/services/geo-location.service.spec.ts +0 -603
  127. package/src/services/geo-location.service.ts +0 -599
  128. package/src/services/index.ts +0 -13
  129. package/src/services/jwt.service.spec.ts +0 -882
  130. package/src/services/jwt.service.ts +0 -621
  131. package/src/services/mfa-base.service.spec.ts +0 -246
  132. package/src/services/mfa-base.service.ts +0 -611
  133. package/src/services/mfa.service.spec.ts +0 -693
  134. package/src/services/mfa.service.ts +0 -960
  135. package/src/services/password.service.spec.ts +0 -166
  136. package/src/services/password.service.ts +0 -309
  137. package/src/services/phone-verification.service.spec.ts +0 -1120
  138. package/src/services/phone-verification.service.ts +0 -751
  139. package/src/services/risk-detection.service.spec.ts +0 -1292
  140. package/src/services/risk-detection.service.ts +0 -1012
  141. package/src/services/risk-scoring.service.spec.ts +0 -204
  142. package/src/services/risk-scoring.service.ts +0 -131
  143. package/src/services/session.service.spec.ts +0 -1293
  144. package/src/services/session.service.ts +0 -803
  145. package/src/services/social-account.service.spec.ts +0 -725
  146. package/src/services/social-auth-base.service.spec.ts +0 -418
  147. package/src/services/social-auth-base.service.ts +0 -581
  148. package/src/services/social-auth.service.spec.ts +0 -238
  149. package/src/services/social-auth.service.ts +0 -436
  150. package/src/services/social-provider-registry.service.spec.ts +0 -238
  151. package/src/services/social-provider-registry.service.ts +0 -122
  152. package/src/services/trusted-device.service.spec.ts +0 -505
  153. package/src/services/trusted-device.service.ts +0 -339
  154. package/src/storage/account-lockout-storage.service.spec.ts +0 -310
  155. package/src/storage/account-lockout-storage.service.ts +0 -89
  156. package/src/storage/index.ts +0 -3
  157. package/src/storage/memory-storage.adapter.ts +0 -443
  158. package/src/storage/rate-limit-storage.service.spec.ts +0 -247
  159. package/src/storage/rate-limit-storage.service.ts +0 -38
  160. package/src/templates/html-template.engine.spec.ts +0 -161
  161. package/src/templates/html-template.engine.ts +0 -688
  162. package/src/templates/index.ts +0 -7
  163. package/src/utils/common-passwords.spec.ts +0 -230
  164. package/src/utils/common-passwords.ts +0 -170
  165. package/src/utils/context-storage.ts +0 -188
  166. package/src/utils/cookie-names.util.ts +0 -67
  167. package/src/utils/cookies.util.ts +0 -94
  168. package/src/utils/index.ts +0 -12
  169. package/src/utils/ip-extractor.spec.ts +0 -330
  170. package/src/utils/ip-extractor.ts +0 -220
  171. package/src/utils/nauth-logger.spec.ts +0 -388
  172. package/src/utils/nauth-logger.ts +0 -215
  173. package/src/utils/pii-redactor.spec.ts +0 -130
  174. package/src/utils/pii-redactor.ts +0 -288
  175. package/src/utils/setup/get-repositories.ts +0 -140
  176. package/src/utils/setup/init-services.ts +0 -422
  177. package/src/utils/setup/init-social.ts +0 -189
  178. package/src/utils/setup/init-storage.ts +0 -94
  179. package/src/utils/setup/register-mfa.ts +0 -165
  180. package/src/utils/setup/run-nauth-migrations.ts +0 -61
  181. package/src/utils/token-delivery-policy.ts +0 -38
  182. package/src/validators/template.validator.ts +0 -219
  183. package/tsconfig.json +0 -37
  184. package/tsconfig.lint.json +0 -6
package/src/internal.ts DELETED
@@ -1,178 +0,0 @@
1
- /**
2
- * @packageDocumentation
3
- * @internal
4
- *
5
- * Internal Services - Framework Adapters Only
6
- *
7
- * This module exports internal implementation services that are used by
8
- * framework adapters (NestJS, Express) but should NOT be used directly
9
- * by consumer applications.
10
- *
11
- * **WARNING:** These APIs are considered internal implementation details
12
- * and may change without notice. Use the public API instead:
13
- * - `AuthService` - Main authentication API
14
- * - `MFAService` - MFA operations
15
- * - `SocialAuthService` - Social authentication
16
- * - `EmailVerificationService` - Email verification
17
- * - `PhoneVerificationService` - Phone verification
18
- * - `ClientInfoService` - Access client context
19
- *
20
- * **When to use this module:**
21
- * - You are building a new framework adapter (e.g., Fastify, Hapi)
22
- * - You need access to low-level services for dependency injection
23
- *
24
- * **When NOT to use this module:**
25
- * - You are building a consumer application
26
- * - You are implementing authentication in your app
27
- * - You need authentication features (use public API instead)
28
- *
29
- * @example
30
- * ```typescript
31
- * // ✅ Framework adapter usage
32
- * import { ChallengeService, PasswordService } from '@nauth-toolkit/core/internal';
33
- *
34
- * // Inject internal services in adapter setup
35
- * const challengeService = new ChallengeService(...);
36
- * const authService = new AuthService(..., challengeService, ...);
37
- * ```
38
- */
39
-
40
- // ============================================================================
41
- // Challenge System (Internal Orchestration)
42
- // ============================================================================
43
-
44
- /**
45
- * Challenge session management service
46
- * @internal
47
- */
48
- export { ChallengeService } from './services/challenge.service';
49
-
50
- /**
51
- * Challenge orchestration helper service
52
- * @internal
53
- */
54
- export { AuthChallengeHelperService } from './services/auth-challenge-helper.service';
55
-
56
- // ============================================================================
57
- // Authentication Flow State Machine (Internal)
58
- // ============================================================================
59
-
60
- /**
61
- * State machine service for authentication flows
62
- * @internal
63
- */
64
- export { AuthFlowStateMachineService } from './services/auth-flow-state-machine.service';
65
-
66
- /**
67
- * Context builder for state machine
68
- * @internal
69
- */
70
- export { AuthFlowContextBuilder } from './services/auth-flow-context-builder.service';
71
-
72
- /**
73
- * State machine types
74
- * @internal
75
- */
76
- export * from './services/auth-flow-state-machine.types';
77
-
78
- /**
79
- * State machine rules
80
- * @internal
81
- */
82
- export * from './services/auth-flow-rules';
83
-
84
- /**
85
- * State definitions
86
- * @internal
87
- */
88
- export * from './services/auth-flow-state-definitions';
89
-
90
- // ============================================================================
91
- // Low-Level Service Primitives (Internal)
92
- // ============================================================================
93
-
94
- /**
95
- * Password hashing and validation service
96
- * @internal
97
- */
98
- export { PasswordService } from './services/password.service';
99
-
100
- /**
101
- * JWT token generation and validation service
102
- * @internal
103
- */
104
- export { JwtService } from './services/jwt.service';
105
-
106
- /**
107
- * Session management service
108
- * @internal
109
- */
110
- export { SessionService } from './services/session.service';
111
-
112
- /**
113
- * Trusted device management service
114
- * @internal
115
- */
116
- export { TrustedDeviceService } from './services/trusted-device.service';
117
-
118
- /**
119
- * Geolocation service for IP-based location detection
120
- * @internal
121
- */
122
- export { GeoLocationService } from './services/geo-location.service';
123
-
124
- // ============================================================================
125
- // Risk & Adaptive Security (Internal)
126
- // ============================================================================
127
-
128
- /**
129
- * Risk detection service - analyzes authentication attempts
130
- * @internal
131
- */
132
- export { RiskDetectionService } from './services/risk-detection.service';
133
-
134
- /**
135
- * Risk scoring service - calculates risk scores
136
- * @internal
137
- */
138
- export { RiskScoringService } from './services/risk-scoring.service';
139
-
140
- /**
141
- * Adaptive MFA decision service - determines MFA requirements
142
- * @internal
143
- */
144
- export { AdaptiveMFADecisionService } from './services/adaptive-mfa-decision.service';
145
-
146
- // ============================================================================
147
- // Base Classes (Internal - for Provider Implementations)
148
- // ============================================================================
149
-
150
- /**
151
- * Base class for MFA provider implementations
152
- * @internal
153
- */
154
- export { BaseMFAProviderService } from './services/mfa-base.service';
155
-
156
- /**
157
- * Base class for social authentication provider implementations
158
- * @internal
159
- */
160
- export { BaseSocialAuthProviderService } from './services/social-auth-base.service';
161
-
162
- /**
163
- * Social provider registry service
164
- * Internal registry for managing social auth provider instances
165
- * @internal
166
- */
167
- export { SocialProviderRegistry } from './services/social-provider-registry.service';
168
-
169
- // ============================================================================
170
- // Audit Service (Internal - with recordEvent)
171
- // ============================================================================
172
-
173
- /**
174
- * Authentication audit service with event recording
175
- * Internal version that includes recordEvent() method for framework use
176
- * @internal
177
- */
178
- export { InternalAuthAuditService as AuthAuditService } from './services/auth-audit.service';
@@ -1,299 +0,0 @@
1
- /**
2
- * Platform Abstraction Interfaces
3
- *
4
- * Defines generic interfaces for HTTP requests and responses to decouple
5
- * the core logic from specific frameworks (Express, Fastify, Koa, etc.).
6
- *
7
- * **Design Principles:**
8
- * - Handlers MUST NOT access `raw` directly - all needed properties exposed via interface
9
- * - Adapters are responsible for context management (AsyncLocalStorage)
10
- * - Handlers assume context is available and focus purely on business logic
11
- */
12
-
13
- // ============================================================================
14
- // Request Interface
15
- // ============================================================================
16
-
17
- /**
18
- * Standardized HTTP Request interface
19
- *
20
- * Provides framework-agnostic access to request data. Handlers should use
21
- * these properties instead of accessing `raw` directly.
22
- */
23
- export interface NAuthRequest {
24
- /** HTTP method (GET, POST, PUT, DELETE, etc.) - always uppercase */
25
- readonly method: string;
26
-
27
- /** Request path without query string (e.g., /api/auth/login) */
28
- readonly path: string;
29
-
30
- /** Full request URL including query string */
31
- readonly url: string;
32
-
33
- /** Request body (parsed JSON/form data) */
34
- readonly body: Record<string, unknown>;
35
-
36
- /** URL query parameters */
37
- readonly query: Record<string, unknown>;
38
-
39
- /** URL path parameters (e.g., :id in /users/:id) */
40
- readonly params: Record<string, string>;
41
-
42
- /** HTTP Headers (lowercase keys) */
43
- readonly headers: Record<string, string | string[] | undefined>;
44
-
45
- /** Parsed cookies */
46
- readonly cookies: Record<string, string | undefined>;
47
-
48
- /** Client IP address */
49
- readonly ip: string;
50
-
51
- /**
52
- * Request-scoped attribute storage
53
- *
54
- * Used for passing data between handlers (e.g., user, token, clientInfo).
55
- * This is isolated storage managed by the adapter, NOT the raw request object.
56
- */
57
- readonly attributes: NAuthRequestAttributes;
58
-
59
- /**
60
- * Underlying framework request object (escape hatch)
61
- *
62
- * @deprecated Avoid using raw - add needed properties to NAuthRequest interface instead.
63
- * Only use for framework-specific edge cases.
64
- */
65
- readonly raw: unknown;
66
-
67
- /**
68
- * Get a header value in a case-insensitive way
69
- *
70
- * @param name - Header name (case-insensitive)
71
- * @returns Header value as string, or undefined if not found
72
- */
73
- getHeader(name: string): string | undefined;
74
- }
75
-
76
- /**
77
- * Request attributes storage interface
78
- *
79
- * Provides type-safe access to common NAuth attributes.
80
- */
81
- export interface NAuthRequestAttributes {
82
- /** Current authenticated user (set by AuthHandler) */
83
- user?: unknown;
84
-
85
- /** JWT payload (set by AuthHandler) */
86
- token?: unknown;
87
-
88
- /** Client info extracted by ClientInfoHandler */
89
- clientInfo?: unknown;
90
-
91
- /** Route marked as public (bypasses CSRF) */
92
- nauthPublic?: boolean;
93
-
94
- /** Deferred CSRF validation error */
95
- nauthCsrfError?: Error;
96
-
97
- /** Token delivery mode override */
98
- nauthTokenDelivery?: 'json' | 'cookies';
99
-
100
- /** Allow arbitrary string keys for extensibility */
101
- [key: string]: unknown;
102
- }
103
-
104
- // ============================================================================
105
- // Response Interface
106
- // ============================================================================
107
-
108
- /**
109
- * Cookie options for setCookie/clearCookie
110
- */
111
- export interface NAuthCookieOptions {
112
- httpOnly?: boolean;
113
- secure?: boolean;
114
- sameSite?: 'strict' | 'lax' | 'none' | boolean;
115
- domain?: string;
116
- path?: string;
117
- maxAge?: number;
118
- expires?: Date;
119
- }
120
-
121
- /**
122
- * Standardized HTTP Response interface
123
- *
124
- * Provides framework-agnostic response methods.
125
- */
126
- export interface NAuthResponse {
127
- /**
128
- * Underlying framework response object (escape hatch)
129
- *
130
- * @deprecated Avoid using raw - add needed methods to NAuthResponse interface instead.
131
- */
132
- readonly raw: unknown;
133
-
134
- /**
135
- * Set HTTP status code
136
- *
137
- * @param code - Status code (e.g., 200, 401, 500)
138
- * @returns this for method chaining
139
- */
140
- status(code: number): this;
141
-
142
- /**
143
- * Set HTTP header
144
- *
145
- * @param name - Header name
146
- * @param value - Header value(s)
147
- * @returns this for method chaining
148
- */
149
- header(name: string, value: string | string[]): this;
150
-
151
- /**
152
- * Set a cookie
153
- *
154
- * @param name - Cookie name
155
- * @param value - Cookie value
156
- * @param options - Cookie options
157
- * @returns this for method chaining
158
- */
159
- setCookie(name: string, value: string, options?: NAuthCookieOptions): this;
160
-
161
- /**
162
- * Clear a cookie
163
- *
164
- * @param name - Cookie name
165
- * @param options - Cookie options (path/domain must match original)
166
- * @returns this for method chaining
167
- */
168
- clearCookie(name: string, options?: NAuthCookieOptions): this;
169
-
170
- /**
171
- * Send response body
172
- *
173
- * @param body - Response body (string, object, buffer)
174
- */
175
- send(body: unknown): void;
176
-
177
- /**
178
- * Send JSON response
179
- *
180
- * @param body - Object to serialize as JSON
181
- */
182
- json(body: unknown): void;
183
-
184
- /**
185
- * Redirect to URL
186
- *
187
- * @param url - Target URL
188
- * @param status - HTTP status code (default: 302)
189
- */
190
- redirect(url: string, status?: number): void;
191
-
192
- /**
193
- * Check if response has been sent
194
- *
195
- * @returns true if headers have been sent
196
- */
197
- isSent(): boolean;
198
- }
199
-
200
- // ============================================================================
201
- // Adapter Interface
202
- // ============================================================================
203
-
204
- /**
205
- * Handler function signature for middleware
206
- */
207
- export type NAuthMiddlewareHandler = (
208
- req: NAuthRequest,
209
- res: NAuthResponse,
210
- next: () => Promise<void> | void,
211
- ) => Promise<void> | void;
212
-
213
- /**
214
- * Handler function signature for response interceptors
215
- */
216
- export type NAuthResponseInterceptorHandler = (
217
- req: NAuthRequest,
218
- res: NAuthResponse,
219
- body: unknown,
220
- ) => Promise<unknown>;
221
-
222
- /**
223
- * Handler function signature for route handlers
224
- */
225
- export type NAuthRouteHandler<T = unknown> = (req: NAuthRequest, res: NAuthResponse) => Promise<T> | T;
226
-
227
- /**
228
- * Platform Adapter Interface
229
- *
230
- * Implemented by framework-specific adapters (ExpressAdapter, FastifyAdapter, etc.).
231
- *
232
- * **Responsibilities:**
233
- * 1. Wrap framework req/res into NAuthRequest/NAuthResponse
234
- * 2. Manage AsyncLocalStorage context (initialize, preserve across hooks)
235
- * 3. Convert handlers to framework-specific middleware/hooks
236
- * 4. Handle errors appropriately for the framework
237
- *
238
- * **Context Management Contract:**
239
- * - The FIRST middleware registered (clientInfo) initializes the context
240
- * - Subsequent middleware must have access to the same context
241
- * - Route handlers wrapped with `wrapRouteHandler` must have context access
242
- */
243
- export interface NAuthAdapter {
244
- /**
245
- * Register a middleware/hook handler
246
- *
247
- * The adapter is responsible for:
248
- * - Wrapping req/res into NAuthRequest/NAuthResponse
249
- * - Managing AsyncLocalStorage context
250
- * - Calling handler with wrapped objects
251
- * - Handling errors
252
- *
253
- * @param name - Handler name (e.g., 'clientInfo', 'auth', 'csrf')
254
- * @param handler - The generic handler function
255
- * @param options - Optional configuration for this middleware
256
- * @returns Framework-specific middleware/hook
257
- */
258
- registerMiddleware(name: string, handler: NAuthMiddlewareHandler, options?: MiddlewareOptions): unknown;
259
-
260
- /**
261
- * Register a response interceptor (for Token Delivery)
262
- *
263
- * The interceptor receives the response body before it's sent and can modify it.
264
- * Used to inject tokens into cookies or modify response payload.
265
- *
266
- * @param handler - Handler that receives body and returns modified body
267
- * @returns Framework-specific response interceptor
268
- */
269
- registerResponseInterceptor(handler: NAuthResponseInterceptorHandler): unknown;
270
-
271
- /**
272
- * Wrap a route handler to ensure context is available
273
- *
274
- * For frameworks like Fastify where handlers run outside the middleware context,
275
- * this wrapper ensures AsyncLocalStorage context is restored.
276
- *
277
- * For Express, this may be a no-op or provide additional functionality.
278
- *
279
- * @param handler - Route handler function
280
- * @returns Framework-specific wrapped handler
281
- */
282
- wrapRouteHandler<T>(handler: NAuthRouteHandler<T>): unknown;
283
-
284
- /**
285
- * Get the adapter name for logging/debugging
286
- */
287
- readonly name: string;
288
- }
289
-
290
- /**
291
- * Options for middleware registration
292
- */
293
- export interface MiddlewareOptions {
294
- /**
295
- * Whether this middleware initializes the context
296
- * Only the first middleware (clientInfo) should set this to true
297
- */
298
- initializesContext?: boolean;
299
- }