@digilogiclabs/platform-core 1.5.0 → 1.6.0
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/dist/auth.d.mts +2 -0
- package/dist/auth.d.ts +2 -0
- package/dist/auth.js +1268 -0
- package/dist/auth.js.map +1 -0
- package/dist/auth.mjs +1175 -0
- package/dist/auth.mjs.map +1 -0
- package/dist/index-CkyVz0hQ.d.mts +1367 -0
- package/dist/index-CkyVz0hQ.d.ts +1367 -0
- package/dist/index.d.mts +6 -1369
- package/dist/index.d.ts +6 -1369
- package/dist/index.js +95 -100
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +95 -100
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,1367 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Security Utilities
|
|
5
|
+
*
|
|
6
|
+
* HTML escaping, input detection, sanitization helpers,
|
|
7
|
+
* timing-safe comparison, error sanitization, and request
|
|
8
|
+
* correlation for safe, consistent security across all apps.
|
|
9
|
+
*/
|
|
10
|
+
/** Regex for protocol-prefixed URLs */
|
|
11
|
+
declare const URL_PROTOCOL_PATTERN: RegExp;
|
|
12
|
+
/** Regex for bare domain names with common TLDs */
|
|
13
|
+
declare const URL_DOMAIN_PATTERN: RegExp;
|
|
14
|
+
/** Regex for HTML tags */
|
|
15
|
+
declare const HTML_TAG_PATTERN: RegExp;
|
|
16
|
+
/**
|
|
17
|
+
* Escape HTML special characters to prevent injection.
|
|
18
|
+
* Use when inserting user content into HTML email templates or rendered HTML.
|
|
19
|
+
*/
|
|
20
|
+
declare function escapeHtml(str: string): string;
|
|
21
|
+
/** Check if a string contains protocol-prefixed URLs or bare domains */
|
|
22
|
+
declare function containsUrls(str: string): boolean;
|
|
23
|
+
/** Check if a string contains HTML tags */
|
|
24
|
+
declare function containsHtml(str: string): boolean;
|
|
25
|
+
/** Strip all HTML tags from a string */
|
|
26
|
+
declare function stripHtml(str: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* Defang URLs to prevent auto-linking in email clients.
|
|
29
|
+
* Converts https://evil.com → hxxps://evil[.]com
|
|
30
|
+
*/
|
|
31
|
+
declare function defangUrl(str: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Sanitize user content for safe insertion into HTML email templates.
|
|
34
|
+
* Escapes HTML entities AND defangs any URLs that slipped through validation.
|
|
35
|
+
*/
|
|
36
|
+
declare function sanitizeForEmail(str: string): string;
|
|
37
|
+
/**
|
|
38
|
+
* Constant-time string comparison to prevent timing side-channel attacks.
|
|
39
|
+
* Use for comparing secrets, tokens, API keys, HMAC signatures, etc.
|
|
40
|
+
*
|
|
41
|
+
* Returns false (not throws) for length mismatches — still constant-time
|
|
42
|
+
* relative to the shorter string to avoid leaking length info.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* if (!constantTimeEqual(providedToken, expectedSecret)) {
|
|
47
|
+
* return { status: 401, error: 'Invalid token' }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function constantTimeEqual(a: string, b: string): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Sanitize an error for client-facing API responses.
|
|
54
|
+
*
|
|
55
|
+
* - 4xx errors: returns the actual message (client needs to know what went wrong)
|
|
56
|
+
* - 5xx errors: returns a generic message (never leak internals to clients)
|
|
57
|
+
* - Development mode: optionally includes stack trace for debugging
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* catch (error) {
|
|
62
|
+
* const { message, code } = sanitizeApiError(error, 500)
|
|
63
|
+
* return Response.json({ error: message, code }, { status: 500 })
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare function sanitizeApiError(error: unknown, statusCode: number, isDevelopment?: boolean): {
|
|
68
|
+
message: string;
|
|
69
|
+
code?: string;
|
|
70
|
+
stack?: string;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Extract a correlation/request ID from standard headers, or generate one.
|
|
74
|
+
*
|
|
75
|
+
* Checks (in order): X-Request-ID, X-Correlation-ID, then falls back to
|
|
76
|
+
* crypto.randomUUID(). Works with any headers-like object (plain object,
|
|
77
|
+
* Headers API, or a getter function).
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* // With Next.js request
|
|
82
|
+
* const id = getCorrelationId((name) => request.headers.get(name))
|
|
83
|
+
*
|
|
84
|
+
* // With plain object
|
|
85
|
+
* const id = getCorrelationId({ 'x-request-id': 'abc-123' })
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
declare function getCorrelationId(headers: Record<string, string | string[] | undefined> | ((name: string) => string | null | undefined)): string;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* API Utilities
|
|
92
|
+
*
|
|
93
|
+
* Framework-agnostic error codes, error class, response types,
|
|
94
|
+
* and database error mapping for consistent API behavior across apps.
|
|
95
|
+
*/
|
|
96
|
+
/** Standard API error codes */
|
|
97
|
+
declare const ApiErrorCode: {
|
|
98
|
+
readonly VALIDATION_ERROR: "VALIDATION_ERROR";
|
|
99
|
+
readonly UNAUTHORIZED: "UNAUTHORIZED";
|
|
100
|
+
readonly FORBIDDEN: "FORBIDDEN";
|
|
101
|
+
readonly NOT_FOUND: "NOT_FOUND";
|
|
102
|
+
readonly CONFLICT: "CONFLICT";
|
|
103
|
+
readonly RATE_LIMIT_EXCEEDED: "RATE_LIMIT_EXCEEDED";
|
|
104
|
+
readonly INTERNAL_ERROR: "INTERNAL_ERROR";
|
|
105
|
+
readonly DATABASE_ERROR: "DATABASE_ERROR";
|
|
106
|
+
readonly EXTERNAL_SERVICE_ERROR: "EXTERNAL_SERVICE_ERROR";
|
|
107
|
+
readonly CONFIGURATION_ERROR: "CONFIGURATION_ERROR";
|
|
108
|
+
};
|
|
109
|
+
type ApiErrorCodeType = (typeof ApiErrorCode)[keyof typeof ApiErrorCode];
|
|
110
|
+
/** Custom API Error class with status code and error code */
|
|
111
|
+
declare class ApiError extends Error {
|
|
112
|
+
readonly statusCode: number;
|
|
113
|
+
readonly code: ApiErrorCodeType;
|
|
114
|
+
readonly details?: unknown;
|
|
115
|
+
constructor(statusCode: number, message: string, code?: ApiErrorCodeType, details?: unknown);
|
|
116
|
+
}
|
|
117
|
+
/** Type guard for ApiError */
|
|
118
|
+
declare function isApiError(error: unknown): error is ApiError;
|
|
119
|
+
/** Pre-built error factories */
|
|
120
|
+
declare const CommonApiErrors: {
|
|
121
|
+
unauthorized: (msg?: string) => ApiError;
|
|
122
|
+
forbidden: (msg?: string) => ApiError;
|
|
123
|
+
notFound: (resource?: string) => ApiError;
|
|
124
|
+
conflict: (msg?: string) => ApiError;
|
|
125
|
+
rateLimitExceeded: (msg?: string) => ApiError;
|
|
126
|
+
validationError: (details?: unknown) => ApiError;
|
|
127
|
+
internalError: (msg?: string) => ApiError;
|
|
128
|
+
};
|
|
129
|
+
/** PostgreSQL error code → HTTP status mapping */
|
|
130
|
+
declare const PG_ERROR_MAP: Record<string, {
|
|
131
|
+
status: number;
|
|
132
|
+
code: ApiErrorCodeType;
|
|
133
|
+
message: string;
|
|
134
|
+
}>;
|
|
135
|
+
/**
|
|
136
|
+
* Classify any error into a standardized shape.
|
|
137
|
+
* Framework-agnostic — returns a plain object, not an HTTP response.
|
|
138
|
+
*/
|
|
139
|
+
declare function classifyError(error: unknown, isDev?: boolean): {
|
|
140
|
+
status: number;
|
|
141
|
+
body: {
|
|
142
|
+
error: string;
|
|
143
|
+
code: string;
|
|
144
|
+
details?: unknown;
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
/** Standard success response shape */
|
|
148
|
+
interface ApiSuccessResponse<T = unknown> {
|
|
149
|
+
success: true;
|
|
150
|
+
data: T;
|
|
151
|
+
message?: string;
|
|
152
|
+
timestamp?: string;
|
|
153
|
+
}
|
|
154
|
+
/** Standard paginated response shape */
|
|
155
|
+
interface ApiPaginatedResponse<T = unknown> extends ApiSuccessResponse<T[]> {
|
|
156
|
+
pagination: {
|
|
157
|
+
page: number;
|
|
158
|
+
limit: number;
|
|
159
|
+
total: number;
|
|
160
|
+
totalPages: number;
|
|
161
|
+
hasMore: boolean;
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/** Build a pagination metadata object */
|
|
165
|
+
declare function buildPagination(page: number, limit: number, total: number): {
|
|
166
|
+
page: number;
|
|
167
|
+
limit: number;
|
|
168
|
+
total: number;
|
|
169
|
+
totalPages: number;
|
|
170
|
+
hasMore: boolean;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Keycloak Authentication Utilities
|
|
175
|
+
*
|
|
176
|
+
* Framework-agnostic helpers for working with Keycloak OIDC tokens.
|
|
177
|
+
* Used by Next.js apps (Auth.js middleware + API routes) and .NET services
|
|
178
|
+
* share the same role model — these helpers ensure consistent behavior.
|
|
179
|
+
*
|
|
180
|
+
* Edge-runtime compatible: uses atob() for JWT decoding, not Buffer.
|
|
181
|
+
*/
|
|
182
|
+
/**
|
|
183
|
+
* Keycloak provider configuration for Auth.js / NextAuth.
|
|
184
|
+
* Everything needed to configure the Keycloak OIDC provider.
|
|
185
|
+
*/
|
|
186
|
+
interface KeycloakConfig {
|
|
187
|
+
/** Keycloak issuer URL (e.g. https://auth.example.com/realms/my-realm) */
|
|
188
|
+
issuer: string;
|
|
189
|
+
/** OAuth client ID registered in Keycloak */
|
|
190
|
+
clientId: string;
|
|
191
|
+
/** OAuth client secret */
|
|
192
|
+
clientSecret: string;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Token set returned by Keycloak after authentication or refresh.
|
|
196
|
+
*/
|
|
197
|
+
interface KeycloakTokenSet {
|
|
198
|
+
accessToken: string;
|
|
199
|
+
refreshToken?: string;
|
|
200
|
+
idToken?: string;
|
|
201
|
+
/** Expiry as epoch milliseconds */
|
|
202
|
+
expiresAt: number;
|
|
203
|
+
/** Parsed realm roles (filtered, no Keycloak defaults) */
|
|
204
|
+
roles: string[];
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Result of a token refresh attempt.
|
|
208
|
+
*/
|
|
209
|
+
type TokenRefreshResult = {
|
|
210
|
+
ok: true;
|
|
211
|
+
tokens: KeycloakTokenSet;
|
|
212
|
+
} | {
|
|
213
|
+
ok: false;
|
|
214
|
+
error: string;
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* Default Keycloak roles that should be filtered out when checking
|
|
218
|
+
* application-level roles. These are always present and not meaningful
|
|
219
|
+
* for authorization decisions.
|
|
220
|
+
*/
|
|
221
|
+
declare const KEYCLOAK_DEFAULT_ROLES: readonly ["offline_access", "uma_authorization"];
|
|
222
|
+
/**
|
|
223
|
+
* Parse realm roles from a Keycloak JWT access token.
|
|
224
|
+
*
|
|
225
|
+
* Supports two token formats:
|
|
226
|
+
* - `realm_roles` (flat array) — Custom client mapper configuration
|
|
227
|
+
* - `realm_access.roles` (nested) — Keycloak default format
|
|
228
|
+
*
|
|
229
|
+
* Uses atob() for Edge runtime compatibility (not Buffer.from).
|
|
230
|
+
* Filters out Keycloak default roles automatically.
|
|
231
|
+
*
|
|
232
|
+
* @param accessToken - Raw JWT string from Keycloak
|
|
233
|
+
* @param additionalDefaultRoles - Extra role names to filter (e.g. realm-specific defaults)
|
|
234
|
+
* @returns Array of meaningful role names
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```typescript
|
|
238
|
+
* const roles = parseKeycloakRoles(account.access_token)
|
|
239
|
+
* // ['admin', 'editor']
|
|
240
|
+
*
|
|
241
|
+
* // With realm-specific defaults
|
|
242
|
+
* const roles = parseKeycloakRoles(token, ['default-roles-my-realm'])
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
declare function parseKeycloakRoles(accessToken: string | undefined | null, additionalDefaultRoles?: string[]): string[];
|
|
246
|
+
/**
|
|
247
|
+
* Check if a user has a specific role.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* if (hasRole(session.user.roles, 'admin')) {
|
|
252
|
+
* // grant access
|
|
253
|
+
* }
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
declare function hasRole(roles: string[] | undefined | null, role: string): boolean;
|
|
257
|
+
/**
|
|
258
|
+
* Check if a user has ANY of the specified roles (OR logic).
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```typescript
|
|
262
|
+
* if (hasAnyRole(session.user.roles, ['admin', 'editor'])) {
|
|
263
|
+
* // grant access
|
|
264
|
+
* }
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
declare function hasAnyRole(roles: string[] | undefined | null, requiredRoles: string[]): boolean;
|
|
268
|
+
/**
|
|
269
|
+
* Check if a user has ALL of the specified roles (AND logic).
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* if (hasAllRoles(session.user.roles, ['admin', 'billing'])) {
|
|
274
|
+
* // grant access to billing admin
|
|
275
|
+
* }
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
declare function hasAllRoles(roles: string[] | undefined | null, requiredRoles: string[]): boolean;
|
|
279
|
+
/**
|
|
280
|
+
* Check if a token needs refreshing.
|
|
281
|
+
*
|
|
282
|
+
* @param expiresAt - Token expiry as epoch milliseconds
|
|
283
|
+
* @param bufferMs - Refresh this many ms before actual expiry (default: 60s)
|
|
284
|
+
* @returns true if the token should be refreshed
|
|
285
|
+
*/
|
|
286
|
+
declare function isTokenExpired(expiresAt: number | undefined | null, bufferMs?: number): boolean;
|
|
287
|
+
/**
|
|
288
|
+
* Build the URLSearchParams for a Keycloak token refresh request.
|
|
289
|
+
*
|
|
290
|
+
* This is the body for POST to `{issuer}/protocol/openid-connect/token`.
|
|
291
|
+
* Framework-agnostic — use with fetch(), axios, or any HTTP client.
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```typescript
|
|
295
|
+
* const params = buildTokenRefreshParams(config, refreshToken)
|
|
296
|
+
* const response = await fetch(`${config.issuer}/protocol/openid-connect/token`, {
|
|
297
|
+
* method: 'POST',
|
|
298
|
+
* headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
299
|
+
* body: params,
|
|
300
|
+
* })
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
declare function buildTokenRefreshParams(config: KeycloakConfig, refreshToken: string): URLSearchParams;
|
|
304
|
+
/**
|
|
305
|
+
* Get the Keycloak token endpoint URL for a given issuer.
|
|
306
|
+
*/
|
|
307
|
+
declare function getTokenEndpoint(issuer: string): string;
|
|
308
|
+
/**
|
|
309
|
+
* Get the Keycloak end session (logout) endpoint URL.
|
|
310
|
+
*/
|
|
311
|
+
declare function getEndSessionEndpoint(issuer: string): string;
|
|
312
|
+
/**
|
|
313
|
+
* Perform a token refresh against Keycloak and parse the result.
|
|
314
|
+
*
|
|
315
|
+
* Returns a discriminated union — check `result.ok` before accessing tokens.
|
|
316
|
+
* Automatically parses roles from the new access token.
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* ```typescript
|
|
320
|
+
* const result = await refreshKeycloakToken(config, currentRefreshToken)
|
|
321
|
+
* if (result.ok) {
|
|
322
|
+
* token.accessToken = result.tokens.accessToken
|
|
323
|
+
* token.roles = result.tokens.roles
|
|
324
|
+
* } else {
|
|
325
|
+
* // Force re-login
|
|
326
|
+
* token.error = 'RefreshTokenError'
|
|
327
|
+
* }
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
declare function refreshKeycloakToken(config: KeycloakConfig, refreshToken: string, additionalDefaultRoles?: string[]): Promise<TokenRefreshResult>;
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Next.js Auth.js + Keycloak Configuration Builder
|
|
334
|
+
*
|
|
335
|
+
* Generates Auth.js callbacks for Keycloak OIDC integration.
|
|
336
|
+
* Handles JWT token storage, role parsing, token refresh, and session mapping.
|
|
337
|
+
*
|
|
338
|
+
* Edge-runtime compatible: uses only atob() and fetch(), no Node.js-only APIs.
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```typescript
|
|
342
|
+
* // auth.config.ts (Edge-compatible)
|
|
343
|
+
* import { buildKeycloakCallbacks } from '@digilogiclabs/platform-core'
|
|
344
|
+
* import Keycloak from 'next-auth/providers/keycloak'
|
|
345
|
+
*
|
|
346
|
+
* const callbacks = buildKeycloakCallbacks({
|
|
347
|
+
* issuer: process.env.AUTH_KEYCLOAK_ISSUER!,
|
|
348
|
+
* clientId: process.env.AUTH_KEYCLOAK_ID!,
|
|
349
|
+
* clientSecret: process.env.AUTH_KEYCLOAK_SECRET!,
|
|
350
|
+
* defaultRoles: ['default-roles-my-realm'],
|
|
351
|
+
* })
|
|
352
|
+
*
|
|
353
|
+
* export const authConfig = {
|
|
354
|
+
* providers: [Keycloak({ ... })],
|
|
355
|
+
* session: { strategy: 'jwt' },
|
|
356
|
+
* callbacks,
|
|
357
|
+
* }
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
interface KeycloakCallbacksConfig {
|
|
361
|
+
/** Keycloak issuer URL */
|
|
362
|
+
issuer: string;
|
|
363
|
+
/** OAuth client ID */
|
|
364
|
+
clientId: string;
|
|
365
|
+
/** OAuth client secret */
|
|
366
|
+
clientSecret: string;
|
|
367
|
+
/** Realm-specific default roles to filter (e.g. ['default-roles-my-realm']) */
|
|
368
|
+
defaultRoles?: string[];
|
|
369
|
+
/** Enable debug logging (default: NODE_ENV === 'development') */
|
|
370
|
+
debug?: boolean;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Extended JWT token shape used by the Keycloak callbacks.
|
|
374
|
+
* These fields are added to the Auth.js JWT token during sign-in and refresh.
|
|
375
|
+
*/
|
|
376
|
+
interface KeycloakJwtFields {
|
|
377
|
+
id?: string;
|
|
378
|
+
accessToken?: string;
|
|
379
|
+
refreshToken?: string;
|
|
380
|
+
idToken?: string;
|
|
381
|
+
accessTokenExpires?: number;
|
|
382
|
+
roles?: string[];
|
|
383
|
+
error?: string;
|
|
384
|
+
}
|
|
385
|
+
interface AuthCookiesConfig {
|
|
386
|
+
/** Production cookie domain (e.g. '.digilogiclabs.com'). Omit for default domain. */
|
|
387
|
+
domain?: string;
|
|
388
|
+
/** Include session token cookie config (default: true) */
|
|
389
|
+
sessionToken?: boolean;
|
|
390
|
+
/** Include callback URL cookie config (default: true) */
|
|
391
|
+
callbackUrl?: boolean;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Build Auth.js cookie configuration for cross-domain OIDC.
|
|
395
|
+
*
|
|
396
|
+
* Handles the common pattern of configuring cookies to work across
|
|
397
|
+
* www and non-www domains (e.g. digilogiclabs.com and www.digilogiclabs.com).
|
|
398
|
+
*
|
|
399
|
+
* PKCE and state cookies are always included for OAuth security.
|
|
400
|
+
* Session token and callback URL cookies are included by default.
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```typescript
|
|
404
|
+
* import { buildAuthCookies } from '@digilogiclabs/platform-core'
|
|
405
|
+
*
|
|
406
|
+
* export const authConfig = {
|
|
407
|
+
* cookies: buildAuthCookies({ domain: '.digilogiclabs.com' }),
|
|
408
|
+
* // ...
|
|
409
|
+
* }
|
|
410
|
+
* ```
|
|
411
|
+
*/
|
|
412
|
+
declare function buildAuthCookies(config?: AuthCookiesConfig): Record<string, {
|
|
413
|
+
name: string;
|
|
414
|
+
options: {
|
|
415
|
+
httpOnly: boolean;
|
|
416
|
+
sameSite: "lax";
|
|
417
|
+
path: string;
|
|
418
|
+
secure: boolean;
|
|
419
|
+
domain: string | undefined;
|
|
420
|
+
};
|
|
421
|
+
}>;
|
|
422
|
+
interface RedirectCallbackConfig {
|
|
423
|
+
/** Allow www variant redirects (e.g. www.example.com ↔ example.com) */
|
|
424
|
+
allowWwwVariant?: boolean;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Build a standard Auth.js redirect callback.
|
|
428
|
+
*
|
|
429
|
+
* Handles common redirect patterns:
|
|
430
|
+
* - Relative URLs → prefixed with baseUrl
|
|
431
|
+
* - Same-origin URLs → allowed
|
|
432
|
+
* - www variant URLs → optionally allowed
|
|
433
|
+
* - Everything else → baseUrl
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* ```typescript
|
|
437
|
+
* import { buildRedirectCallback } from '@digilogiclabs/platform-core'
|
|
438
|
+
*
|
|
439
|
+
* export const authConfig = {
|
|
440
|
+
* callbacks: {
|
|
441
|
+
* redirect: buildRedirectCallback({ allowWwwVariant: true }),
|
|
442
|
+
* },
|
|
443
|
+
* }
|
|
444
|
+
* ```
|
|
445
|
+
*/
|
|
446
|
+
declare function buildRedirectCallback(config?: RedirectCallbackConfig): ({ url, baseUrl }: {
|
|
447
|
+
url: string;
|
|
448
|
+
baseUrl: string;
|
|
449
|
+
}) => Promise<string>;
|
|
450
|
+
/**
|
|
451
|
+
* Build Auth.js JWT and session callbacks for Keycloak.
|
|
452
|
+
*
|
|
453
|
+
* Returns an object with `jwt` and `session` callbacks that handle:
|
|
454
|
+
* - Storing Keycloak tokens on initial sign-in
|
|
455
|
+
* - Parsing realm roles from the access token
|
|
456
|
+
* - Automatic token refresh when expired
|
|
457
|
+
* - Mapping tokens/roles to the session object
|
|
458
|
+
*
|
|
459
|
+
* The callbacks are Edge-runtime compatible.
|
|
460
|
+
*/
|
|
461
|
+
declare function buildKeycloakCallbacks(config: KeycloakCallbacksConfig): {
|
|
462
|
+
/**
|
|
463
|
+
* JWT callback — stores Keycloak tokens and handles refresh.
|
|
464
|
+
*
|
|
465
|
+
* Compatible with Auth.js v5 JWT callback signature.
|
|
466
|
+
*/
|
|
467
|
+
jwt({ token, user, account }: {
|
|
468
|
+
token: any;
|
|
469
|
+
user?: any;
|
|
470
|
+
account?: any;
|
|
471
|
+
}): Promise<any>;
|
|
472
|
+
/**
|
|
473
|
+
* Session callback — maps JWT fields to the session object.
|
|
474
|
+
*
|
|
475
|
+
* Compatible with Auth.js v5 session callback signature.
|
|
476
|
+
*/
|
|
477
|
+
session({ session, token }: {
|
|
478
|
+
session: any;
|
|
479
|
+
token: any;
|
|
480
|
+
}): Promise<any>;
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* API Security Types & Helpers
|
|
485
|
+
*
|
|
486
|
+
* Framework-agnostic types and utilities for building composable
|
|
487
|
+
* API security wrappers. These define the shared contract that
|
|
488
|
+
* framework-specific implementations (Next.js, Express, .NET) follow.
|
|
489
|
+
*
|
|
490
|
+
* The actual wrappers (withPublicApi, withAdminApi, etc.) live in each
|
|
491
|
+
* app because they depend on framework-specific request/response types.
|
|
492
|
+
* This module provides the shared types and logic they all use.
|
|
493
|
+
*/
|
|
494
|
+
/**
|
|
495
|
+
* How a request was authenticated.
|
|
496
|
+
* Used by audit logging and authorization decisions.
|
|
497
|
+
*/
|
|
498
|
+
type AuthMethod = "session" | "bearer_token" | "api_key" | "webhook_signature" | "cron_secret" | "none";
|
|
499
|
+
/**
|
|
500
|
+
* Audit configuration for a secured route.
|
|
501
|
+
* Tells the security wrapper what to log.
|
|
502
|
+
*/
|
|
503
|
+
interface RouteAuditConfig {
|
|
504
|
+
/** The action being performed (e.g. 'game.server.create') */
|
|
505
|
+
action: string;
|
|
506
|
+
/** The resource type being acted on (e.g. 'game_server') */
|
|
507
|
+
resourceType: string;
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Rate limit preset configuration.
|
|
511
|
+
* Matches the structure used by all apps.
|
|
512
|
+
*/
|
|
513
|
+
interface RateLimitPreset {
|
|
514
|
+
/** Maximum requests allowed in the window */
|
|
515
|
+
limit: number;
|
|
516
|
+
/** Window duration in seconds */
|
|
517
|
+
windowSeconds: number;
|
|
518
|
+
/** Higher limit for authenticated users */
|
|
519
|
+
authenticatedLimit?: number;
|
|
520
|
+
/** Block duration when limit exceeded (seconds) */
|
|
521
|
+
blockDurationSeconds?: number;
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Standard rate limit presets shared across all apps.
|
|
525
|
+
*
|
|
526
|
+
* Apps can extend with their own domain-specific presets:
|
|
527
|
+
* ```typescript
|
|
528
|
+
* const APP_RATE_LIMITS = {
|
|
529
|
+
* ...StandardRateLimitPresets,
|
|
530
|
+
* gameServerCreate: { limit: 5, windowSeconds: 3600, blockDurationSeconds: 3600 },
|
|
531
|
+
* }
|
|
532
|
+
* ```
|
|
533
|
+
*/
|
|
534
|
+
declare const StandardRateLimitPresets: {
|
|
535
|
+
/** General API: 100/min, 200/min authenticated */
|
|
536
|
+
readonly apiGeneral: {
|
|
537
|
+
readonly limit: 100;
|
|
538
|
+
readonly windowSeconds: 60;
|
|
539
|
+
readonly authenticatedLimit: 200;
|
|
540
|
+
};
|
|
541
|
+
/** Admin operations: 100/min (admins are trusted) */
|
|
542
|
+
readonly adminAction: {
|
|
543
|
+
readonly limit: 100;
|
|
544
|
+
readonly windowSeconds: 60;
|
|
545
|
+
};
|
|
546
|
+
/** AI/expensive operations: 20/hour, 50/hour authenticated */
|
|
547
|
+
readonly aiRequest: {
|
|
548
|
+
readonly limit: 20;
|
|
549
|
+
readonly windowSeconds: 3600;
|
|
550
|
+
readonly authenticatedLimit: 50;
|
|
551
|
+
};
|
|
552
|
+
/** Auth attempts: 5/15min with 15min block */
|
|
553
|
+
readonly authAttempt: {
|
|
554
|
+
readonly limit: 5;
|
|
555
|
+
readonly windowSeconds: 900;
|
|
556
|
+
readonly blockDurationSeconds: 900;
|
|
557
|
+
};
|
|
558
|
+
/** Contact/public forms: 10/hour */
|
|
559
|
+
readonly publicForm: {
|
|
560
|
+
readonly limit: 10;
|
|
561
|
+
readonly windowSeconds: 3600;
|
|
562
|
+
readonly blockDurationSeconds: 1800;
|
|
563
|
+
};
|
|
564
|
+
/** Checkout/billing: 10/hour with 1hr block */
|
|
565
|
+
readonly checkout: {
|
|
566
|
+
readonly limit: 10;
|
|
567
|
+
readonly windowSeconds: 3600;
|
|
568
|
+
readonly blockDurationSeconds: 3600;
|
|
569
|
+
};
|
|
570
|
+
};
|
|
571
|
+
/**
|
|
572
|
+
* Configuration for a secured API handler.
|
|
573
|
+
*
|
|
574
|
+
* This is the framework-agnostic config shape. Each framework's
|
|
575
|
+
* wrapper (withPublicApi, withAdminApi, etc.) maps to this structure.
|
|
576
|
+
*/
|
|
577
|
+
interface ApiSecurityConfig {
|
|
578
|
+
/** Whether authentication is required */
|
|
579
|
+
requireAuth: boolean;
|
|
580
|
+
/** Whether admin role is required */
|
|
581
|
+
requireAdmin: boolean;
|
|
582
|
+
/** Required roles (user must have at least one) */
|
|
583
|
+
requireRoles?: string[];
|
|
584
|
+
/** Allow legacy bearer token as alternative to session auth */
|
|
585
|
+
allowBearerToken?: boolean;
|
|
586
|
+
/** Rate limit preset name or custom config */
|
|
587
|
+
rateLimit?: string | RateLimitPreset;
|
|
588
|
+
/** Audit logging config */
|
|
589
|
+
audit?: RouteAuditConfig;
|
|
590
|
+
/** Human-readable operation name for logging */
|
|
591
|
+
operation?: string;
|
|
592
|
+
/** Skip rate limiting */
|
|
593
|
+
skipRateLimit?: boolean;
|
|
594
|
+
/** Skip audit logging */
|
|
595
|
+
skipAudit?: boolean;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Minimal session shape that all auth systems provide.
|
|
599
|
+
* Maps to Auth.js Session, .NET ClaimsPrincipal, etc.
|
|
600
|
+
*/
|
|
601
|
+
interface SecuritySession {
|
|
602
|
+
user: {
|
|
603
|
+
id: string;
|
|
604
|
+
email?: string | null;
|
|
605
|
+
name?: string | null;
|
|
606
|
+
roles?: string[];
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Context available to secured route handlers after all security
|
|
611
|
+
* checks have passed. Framework wrappers extend this with their
|
|
612
|
+
* own fields (e.g. NextRequest, validated body, etc.).
|
|
613
|
+
*/
|
|
614
|
+
interface ApiSecurityContext {
|
|
615
|
+
/** Authenticated session (null for public routes or token auth) */
|
|
616
|
+
session: SecuritySession | null;
|
|
617
|
+
/** How the request was authenticated */
|
|
618
|
+
authMethod: AuthMethod;
|
|
619
|
+
/** Whether the user has admin privileges */
|
|
620
|
+
isAdmin: boolean;
|
|
621
|
+
/** Request correlation ID */
|
|
622
|
+
requestId: string;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Determine the appropriate rate limit key for a request.
|
|
626
|
+
*
|
|
627
|
+
* Priority: user ID > email > IP address.
|
|
628
|
+
* Authenticated users get their own bucket (and potentially higher limits).
|
|
629
|
+
*
|
|
630
|
+
* @param session - Current session (if any)
|
|
631
|
+
* @param clientIp - Client IP address
|
|
632
|
+
* @returns { identifier, isAuthenticated }
|
|
633
|
+
*/
|
|
634
|
+
declare function resolveRateLimitIdentifier(session: SecuritySession | null, clientIp: string): {
|
|
635
|
+
identifier: string;
|
|
636
|
+
isAuthenticated: boolean;
|
|
637
|
+
};
|
|
638
|
+
/**
|
|
639
|
+
* Extract the client IP from standard proxy headers.
|
|
640
|
+
*
|
|
641
|
+
* Checks (in order): CF-Connecting-IP, X-Real-IP, X-Forwarded-For.
|
|
642
|
+
* Use this to ensure consistent IP extraction across all apps.
|
|
643
|
+
*
|
|
644
|
+
* @param getHeader - Header getter function
|
|
645
|
+
* @returns Client IP or 'unknown'
|
|
646
|
+
*/
|
|
647
|
+
declare function extractClientIp(getHeader: (name: string) => string | null | undefined): string;
|
|
648
|
+
/**
|
|
649
|
+
* Build the standard rate limit response headers.
|
|
650
|
+
*
|
|
651
|
+
* @returns Headers object with X-RateLimit-* headers
|
|
652
|
+
*/
|
|
653
|
+
declare function buildRateLimitHeaders(limit: number, remaining: number, resetAtMs: number): Record<string, string>;
|
|
654
|
+
/**
|
|
655
|
+
* Build a standard error response body.
|
|
656
|
+
* Ensures consistent error shape across all apps.
|
|
657
|
+
*/
|
|
658
|
+
declare function buildErrorBody(error: string, extra?: Record<string, unknown>): Record<string, unknown>;
|
|
659
|
+
/**
|
|
660
|
+
* Pre-built security configurations for common route types.
|
|
661
|
+
*
|
|
662
|
+
* Use these as the base for framework-specific wrappers:
|
|
663
|
+
* ```typescript
|
|
664
|
+
* // In your app's api-security.ts
|
|
665
|
+
* export function withPublicApi(config, handler) {
|
|
666
|
+
* return createSecureHandler({
|
|
667
|
+
* ...WrapperPresets.public,
|
|
668
|
+
* ...config,
|
|
669
|
+
* }, handler)
|
|
670
|
+
* }
|
|
671
|
+
* ```
|
|
672
|
+
*/
|
|
673
|
+
declare const WrapperPresets: {
|
|
674
|
+
/** Public route: no auth, rate limited */
|
|
675
|
+
readonly public: {
|
|
676
|
+
readonly requireAuth: false;
|
|
677
|
+
readonly requireAdmin: false;
|
|
678
|
+
readonly rateLimit: "apiGeneral";
|
|
679
|
+
};
|
|
680
|
+
/** Authenticated route: requires session */
|
|
681
|
+
readonly authenticated: {
|
|
682
|
+
readonly requireAuth: true;
|
|
683
|
+
readonly requireAdmin: false;
|
|
684
|
+
readonly rateLimit: "apiGeneral";
|
|
685
|
+
};
|
|
686
|
+
/** Admin route: requires session with admin role */
|
|
687
|
+
readonly admin: {
|
|
688
|
+
readonly requireAuth: true;
|
|
689
|
+
readonly requireAdmin: true;
|
|
690
|
+
readonly rateLimit: "adminAction";
|
|
691
|
+
};
|
|
692
|
+
/** Legacy admin: accepts session OR bearer token */
|
|
693
|
+
readonly legacyAdmin: {
|
|
694
|
+
readonly requireAuth: true;
|
|
695
|
+
readonly requireAdmin: true;
|
|
696
|
+
readonly allowBearerToken: true;
|
|
697
|
+
readonly rateLimit: "adminAction";
|
|
698
|
+
};
|
|
699
|
+
/** AI/expensive: requires auth, strict rate limit */
|
|
700
|
+
readonly ai: {
|
|
701
|
+
readonly requireAuth: true;
|
|
702
|
+
readonly requireAdmin: false;
|
|
703
|
+
readonly rateLimit: "aiRequest";
|
|
704
|
+
};
|
|
705
|
+
/** Cron: no rate limit, admin-level access */
|
|
706
|
+
readonly cron: {
|
|
707
|
+
readonly requireAuth: true;
|
|
708
|
+
readonly requireAdmin: false;
|
|
709
|
+
readonly skipRateLimit: true;
|
|
710
|
+
readonly skipAudit: false;
|
|
711
|
+
};
|
|
712
|
+
};
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Common Validation Schemas
|
|
716
|
+
*
|
|
717
|
+
* Reusable Zod schemas for request validation across all apps.
|
|
718
|
+
* These are the building blocks — apps compose them into
|
|
719
|
+
* route-specific schemas for their own domain logic.
|
|
720
|
+
*
|
|
721
|
+
* Requires `zod` as a peer dependency (already in platform-core).
|
|
722
|
+
*/
|
|
723
|
+
|
|
724
|
+
/** Validated, normalized email address */
|
|
725
|
+
declare const EmailSchema: z.ZodString;
|
|
726
|
+
/** Password with minimum security requirements */
|
|
727
|
+
declare const PasswordSchema: z.ZodString;
|
|
728
|
+
/** URL-safe slug (lowercase alphanumeric + hyphens) */
|
|
729
|
+
declare const SlugSchema: z.ZodString;
|
|
730
|
+
/** Phone number (international format, flexible) */
|
|
731
|
+
declare const PhoneSchema: z.ZodString;
|
|
732
|
+
/** Human name (letters, spaces, hyphens, apostrophes) */
|
|
733
|
+
declare const PersonNameSchema: z.ZodString;
|
|
734
|
+
/**
|
|
735
|
+
* Create a text schema that blocks HTML tags and links.
|
|
736
|
+
* Use for user-facing text fields (contact forms, comments, etc.)
|
|
737
|
+
*
|
|
738
|
+
* @param options - Customize min/max length and error messages
|
|
739
|
+
*
|
|
740
|
+
* @example
|
|
741
|
+
* ```typescript
|
|
742
|
+
* const MessageSchema = createSafeTextSchema({ min: 10, max: 1000 })
|
|
743
|
+
* const CommentSchema = createSafeTextSchema({ max: 500, allowUrls: true })
|
|
744
|
+
* ```
|
|
745
|
+
*/
|
|
746
|
+
declare function createSafeTextSchema(options?: {
|
|
747
|
+
min?: number;
|
|
748
|
+
max?: number;
|
|
749
|
+
allowHtml?: boolean;
|
|
750
|
+
allowUrls?: boolean;
|
|
751
|
+
fieldName?: string;
|
|
752
|
+
}): z.ZodType<string, z.ZodTypeDef, string>;
|
|
753
|
+
/** Standard pagination query parameters */
|
|
754
|
+
declare const PaginationSchema: z.ZodObject<{
|
|
755
|
+
page: z.ZodDefault<z.ZodNumber>;
|
|
756
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
757
|
+
sortBy: z.ZodOptional<z.ZodString>;
|
|
758
|
+
sortOrder: z.ZodDefault<z.ZodEnum<["asc", "desc"]>>;
|
|
759
|
+
}, "strip", z.ZodTypeAny, {
|
|
760
|
+
limit: number;
|
|
761
|
+
page: number;
|
|
762
|
+
sortOrder: "asc" | "desc";
|
|
763
|
+
sortBy?: string | undefined;
|
|
764
|
+
}, {
|
|
765
|
+
sortBy?: string | undefined;
|
|
766
|
+
limit?: number | undefined;
|
|
767
|
+
page?: number | undefined;
|
|
768
|
+
sortOrder?: "asc" | "desc" | undefined;
|
|
769
|
+
}>;
|
|
770
|
+
/** Date range filter (ISO 8601 datetime strings) */
|
|
771
|
+
declare const DateRangeSchema: z.ZodEffects<z.ZodObject<{
|
|
772
|
+
startDate: z.ZodString;
|
|
773
|
+
endDate: z.ZodString;
|
|
774
|
+
}, "strip", z.ZodTypeAny, {
|
|
775
|
+
startDate: string;
|
|
776
|
+
endDate: string;
|
|
777
|
+
}, {
|
|
778
|
+
startDate: string;
|
|
779
|
+
endDate: string;
|
|
780
|
+
}>, {
|
|
781
|
+
startDate: string;
|
|
782
|
+
endDate: string;
|
|
783
|
+
}, {
|
|
784
|
+
startDate: string;
|
|
785
|
+
endDate: string;
|
|
786
|
+
}>;
|
|
787
|
+
/** Search query with optional filters */
|
|
788
|
+
declare const SearchQuerySchema: z.ZodObject<{
|
|
789
|
+
query: z.ZodString;
|
|
790
|
+
page: z.ZodDefault<z.ZodNumber>;
|
|
791
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
792
|
+
}, "strip", z.ZodTypeAny, {
|
|
793
|
+
query: string;
|
|
794
|
+
limit: number;
|
|
795
|
+
page: number;
|
|
796
|
+
}, {
|
|
797
|
+
query: string;
|
|
798
|
+
limit?: number | undefined;
|
|
799
|
+
page?: number | undefined;
|
|
800
|
+
}>;
|
|
801
|
+
/** Login credentials */
|
|
802
|
+
declare const LoginSchema: z.ZodObject<{
|
|
803
|
+
email: z.ZodString;
|
|
804
|
+
password: z.ZodString;
|
|
805
|
+
}, "strip", z.ZodTypeAny, {
|
|
806
|
+
email: string;
|
|
807
|
+
password: string;
|
|
808
|
+
}, {
|
|
809
|
+
email: string;
|
|
810
|
+
password: string;
|
|
811
|
+
}>;
|
|
812
|
+
/** Signup with optional name */
|
|
813
|
+
declare const SignupSchema: z.ZodObject<{
|
|
814
|
+
email: z.ZodString;
|
|
815
|
+
password: z.ZodString;
|
|
816
|
+
name: z.ZodOptional<z.ZodString>;
|
|
817
|
+
}, "strip", z.ZodTypeAny, {
|
|
818
|
+
email: string;
|
|
819
|
+
password: string;
|
|
820
|
+
name?: string | undefined;
|
|
821
|
+
}, {
|
|
822
|
+
email: string;
|
|
823
|
+
password: string;
|
|
824
|
+
name?: string | undefined;
|
|
825
|
+
}>;
|
|
826
|
+
type EmailInput = z.infer<typeof EmailSchema>;
|
|
827
|
+
type PaginationInput = z.infer<typeof PaginationSchema>;
|
|
828
|
+
type DateRangeInput = z.infer<typeof DateRangeSchema>;
|
|
829
|
+
type SearchQueryInput = z.infer<typeof SearchQuerySchema>;
|
|
830
|
+
type LoginInput = z.infer<typeof LoginSchema>;
|
|
831
|
+
type SignupInput = z.infer<typeof SignupSchema>;
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Feature Flag System
|
|
835
|
+
*
|
|
836
|
+
* Generic, type-safe feature flag builder for staged rollout.
|
|
837
|
+
* Each app defines its own flags; this module provides the
|
|
838
|
+
* infrastructure for evaluating them by environment.
|
|
839
|
+
*
|
|
840
|
+
* @example
|
|
841
|
+
* ```typescript
|
|
842
|
+
* // Define your app's flags
|
|
843
|
+
* const flags = createFeatureFlags({
|
|
844
|
+
* STRIPE_PAYMENTS: {
|
|
845
|
+
* development: true,
|
|
846
|
+
* staging: true,
|
|
847
|
+
* production: { envVar: 'ENABLE_PAYMENTS' },
|
|
848
|
+
* },
|
|
849
|
+
* PUBLIC_SIGNUP: {
|
|
850
|
+
* development: true,
|
|
851
|
+
* staging: false,
|
|
852
|
+
* production: { envVar: 'ENABLE_PUBLIC_SIGNUP' },
|
|
853
|
+
* },
|
|
854
|
+
* AI_FEATURES: {
|
|
855
|
+
* development: true,
|
|
856
|
+
* staging: { envVar: 'ENABLE_AI' },
|
|
857
|
+
* production: false,
|
|
858
|
+
* },
|
|
859
|
+
* })
|
|
860
|
+
*
|
|
861
|
+
* // Evaluate at runtime
|
|
862
|
+
* const resolved = flags.resolve() // reads NODE_ENV + DEPLOYMENT_STAGE
|
|
863
|
+
* if (resolved.STRIPE_PAYMENTS) { ... }
|
|
864
|
+
*
|
|
865
|
+
* // Check a single flag
|
|
866
|
+
* if (flags.isEnabled('AI_FEATURES')) { ... }
|
|
867
|
+
* ```
|
|
868
|
+
*/
|
|
869
|
+
type DeploymentStage = "development" | "staging" | "preview" | "production";
|
|
870
|
+
/**
|
|
871
|
+
* How a flag is resolved in a given environment.
|
|
872
|
+
* - `true` / `false` — hardcoded on/off
|
|
873
|
+
* - `{ envVar: string }` — read from environment variable (truthy = "true")
|
|
874
|
+
* - `{ envVar: string, default: boolean }` — with fallback
|
|
875
|
+
*/
|
|
876
|
+
type FlagValue = boolean | {
|
|
877
|
+
envVar: string;
|
|
878
|
+
default?: boolean;
|
|
879
|
+
};
|
|
880
|
+
/**
|
|
881
|
+
* Flag definition across deployment stages.
|
|
882
|
+
*/
|
|
883
|
+
interface FlagDefinition {
|
|
884
|
+
development: FlagValue;
|
|
885
|
+
staging: FlagValue;
|
|
886
|
+
production: FlagValue;
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Map of flag name to definition.
|
|
890
|
+
*/
|
|
891
|
+
type FlagDefinitions<T extends string = string> = Record<T, FlagDefinition>;
|
|
892
|
+
/**
|
|
893
|
+
* Resolved flags — all booleans.
|
|
894
|
+
*/
|
|
895
|
+
type ResolvedFlags<T extends string = string> = Record<T, boolean>;
|
|
896
|
+
/**
|
|
897
|
+
* Allowlist configuration for tester-gated access.
|
|
898
|
+
*/
|
|
899
|
+
interface AllowlistConfig {
|
|
900
|
+
/** Environment variable containing comma-separated emails */
|
|
901
|
+
envVar?: string;
|
|
902
|
+
/** Hardcoded fallback emails */
|
|
903
|
+
fallback?: string[];
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* Detect the current deployment stage from environment variables.
|
|
907
|
+
*/
|
|
908
|
+
declare function detectStage(): DeploymentStage;
|
|
909
|
+
/**
|
|
910
|
+
* Create a type-safe feature flag system.
|
|
911
|
+
*
|
|
912
|
+
* @param definitions - Flag definitions per environment
|
|
913
|
+
* @returns Feature flag accessor with resolve() and isEnabled()
|
|
914
|
+
*/
|
|
915
|
+
declare function createFeatureFlags<T extends string>(definitions: FlagDefinitions<T>): {
|
|
916
|
+
/**
|
|
917
|
+
* Resolve all flags for the current environment.
|
|
918
|
+
* Call this once at startup or per-request for dynamic flags.
|
|
919
|
+
*/
|
|
920
|
+
resolve(stage?: DeploymentStage): ResolvedFlags<T>;
|
|
921
|
+
/**
|
|
922
|
+
* Check if a single flag is enabled.
|
|
923
|
+
*/
|
|
924
|
+
isEnabled(flag: T, stage?: DeploymentStage): boolean;
|
|
925
|
+
/**
|
|
926
|
+
* Get the flag definitions (for introspection/admin UI).
|
|
927
|
+
*/
|
|
928
|
+
definitions: FlagDefinitions<T>;
|
|
929
|
+
};
|
|
930
|
+
/**
|
|
931
|
+
* Build an allowlist from environment variable + fallback emails.
|
|
932
|
+
*
|
|
933
|
+
* @example
|
|
934
|
+
* ```typescript
|
|
935
|
+
* const testers = buildAllowlist({
|
|
936
|
+
* envVar: 'ADMIN_EMAILS',
|
|
937
|
+
* fallback: ['admin@example.com'],
|
|
938
|
+
* })
|
|
939
|
+
* if (testers.includes(userEmail)) { ... }
|
|
940
|
+
* ```
|
|
941
|
+
*/
|
|
942
|
+
declare function buildAllowlist(config: AllowlistConfig): string[];
|
|
943
|
+
/**
|
|
944
|
+
* Check if an email is in the allowlist (case-insensitive).
|
|
945
|
+
*/
|
|
946
|
+
declare function isAllowlisted(email: string, allowlist: string[]): boolean;
|
|
947
|
+
|
|
948
|
+
/**
|
|
949
|
+
* Standalone Rate Limiter
|
|
950
|
+
*
|
|
951
|
+
* Framework-agnostic sliding window rate limiter that works with
|
|
952
|
+
* any storage backend (Redis, memory, etc.). Apps provide a storage
|
|
953
|
+
* adapter; this module handles the algorithm and types.
|
|
954
|
+
*
|
|
955
|
+
* @example
|
|
956
|
+
* ```typescript
|
|
957
|
+
* import {
|
|
958
|
+
* checkRateLimit,
|
|
959
|
+
* createMemoryRateLimitStore,
|
|
960
|
+
* CommonRateLimits,
|
|
961
|
+
* } from '@digilogiclabs/platform-core'
|
|
962
|
+
*
|
|
963
|
+
* const store = createMemoryRateLimitStore()
|
|
964
|
+
*
|
|
965
|
+
* const result = await checkRateLimit('api-call', 'user:123', CommonRateLimits.apiGeneral, { store })
|
|
966
|
+
* if (!result.allowed) {
|
|
967
|
+
* // Return 429 with result.retryAfterSeconds
|
|
968
|
+
* }
|
|
969
|
+
* ```
|
|
970
|
+
*/
|
|
971
|
+
/** Configuration for a rate limit rule */
|
|
972
|
+
interface RateLimitRule {
|
|
973
|
+
/** Maximum requests allowed in the window */
|
|
974
|
+
limit: number;
|
|
975
|
+
/** Time window in seconds */
|
|
976
|
+
windowSeconds: number;
|
|
977
|
+
/** Different limit for authenticated users (optional) */
|
|
978
|
+
authenticatedLimit?: number;
|
|
979
|
+
/** Block duration in seconds when limit is exceeded (optional) */
|
|
980
|
+
blockDurationSeconds?: number;
|
|
981
|
+
}
|
|
982
|
+
/** Result of a rate limit check */
|
|
983
|
+
interface RateLimitCheckResult {
|
|
984
|
+
/** Whether the request is allowed */
|
|
985
|
+
allowed: boolean;
|
|
986
|
+
/** Remaining requests in the current window */
|
|
987
|
+
remaining: number;
|
|
988
|
+
/** Unix timestamp (ms) when the window resets */
|
|
989
|
+
resetAt: number;
|
|
990
|
+
/** Current request count in the window */
|
|
991
|
+
current: number;
|
|
992
|
+
/** The limit that was applied */
|
|
993
|
+
limit: number;
|
|
994
|
+
/** Seconds until retry is allowed (for 429 Retry-After header) */
|
|
995
|
+
retryAfterSeconds: number;
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* Storage backend for rate limiting.
|
|
999
|
+
*
|
|
1000
|
+
* Implementations should support sorted-set-like semantics for
|
|
1001
|
+
* accurate sliding window rate limiting.
|
|
1002
|
+
*/
|
|
1003
|
+
interface RateLimitStore {
|
|
1004
|
+
/**
|
|
1005
|
+
* Add an entry to the sliding window and return the current count.
|
|
1006
|
+
* Should atomically: remove entries older than windowStart,
|
|
1007
|
+
* add the new entry, and return the total count.
|
|
1008
|
+
*/
|
|
1009
|
+
increment(key: string, windowMs: number, now: number): Promise<{
|
|
1010
|
+
count: number;
|
|
1011
|
+
}>;
|
|
1012
|
+
/** Check if a key is blocked and return remaining TTL */
|
|
1013
|
+
isBlocked(key: string): Promise<{
|
|
1014
|
+
blocked: boolean;
|
|
1015
|
+
ttlMs: number;
|
|
1016
|
+
}>;
|
|
1017
|
+
/** Set a temporary block on a key */
|
|
1018
|
+
setBlock(key: string, durationSeconds: number): Promise<void>;
|
|
1019
|
+
/** Remove all entries for a key (for reset/testing) */
|
|
1020
|
+
reset(key: string): Promise<void>;
|
|
1021
|
+
}
|
|
1022
|
+
/** Options for checkRateLimit */
|
|
1023
|
+
interface RateLimitOptions {
|
|
1024
|
+
/** Storage backend (defaults to in-memory if not provided) */
|
|
1025
|
+
store?: RateLimitStore;
|
|
1026
|
+
/** Whether the user is authenticated (uses authenticatedLimit if available) */
|
|
1027
|
+
isAuthenticated?: boolean;
|
|
1028
|
+
/** Logger for warnings/errors (optional) */
|
|
1029
|
+
logger?: {
|
|
1030
|
+
warn: (msg: string, meta?: Record<string, unknown>) => void;
|
|
1031
|
+
error: (msg: string, meta?: Record<string, unknown>) => void;
|
|
1032
|
+
};
|
|
1033
|
+
}
|
|
1034
|
+
/**
|
|
1035
|
+
* Common rate limit rules for typical operations.
|
|
1036
|
+
* Apps can extend with domain-specific presets.
|
|
1037
|
+
*
|
|
1038
|
+
* @example
|
|
1039
|
+
* ```typescript
|
|
1040
|
+
* const APP_LIMITS = {
|
|
1041
|
+
* ...CommonRateLimits,
|
|
1042
|
+
* serverCreate: { limit: 5, windowSeconds: 3600, blockDurationSeconds: 3600 },
|
|
1043
|
+
* }
|
|
1044
|
+
* ```
|
|
1045
|
+
*/
|
|
1046
|
+
declare const CommonRateLimits: {
|
|
1047
|
+
/** General API: 100/min, 200/min authenticated */
|
|
1048
|
+
readonly apiGeneral: {
|
|
1049
|
+
readonly limit: 100;
|
|
1050
|
+
readonly windowSeconds: 60;
|
|
1051
|
+
readonly authenticatedLimit: 200;
|
|
1052
|
+
};
|
|
1053
|
+
/** Admin actions: 100/min */
|
|
1054
|
+
readonly adminAction: {
|
|
1055
|
+
readonly limit: 100;
|
|
1056
|
+
readonly windowSeconds: 60;
|
|
1057
|
+
};
|
|
1058
|
+
/** Auth attempts: 10/15min with 30min block */
|
|
1059
|
+
readonly authAttempt: {
|
|
1060
|
+
readonly limit: 10;
|
|
1061
|
+
readonly windowSeconds: 900;
|
|
1062
|
+
readonly blockDurationSeconds: 1800;
|
|
1063
|
+
};
|
|
1064
|
+
/** AI/expensive requests: 20/hour, 50/hour authenticated */
|
|
1065
|
+
readonly aiRequest: {
|
|
1066
|
+
readonly limit: 20;
|
|
1067
|
+
readonly windowSeconds: 3600;
|
|
1068
|
+
readonly authenticatedLimit: 50;
|
|
1069
|
+
};
|
|
1070
|
+
/** Public form submissions: 5/hour with 1hr block */
|
|
1071
|
+
readonly publicForm: {
|
|
1072
|
+
readonly limit: 5;
|
|
1073
|
+
readonly windowSeconds: 3600;
|
|
1074
|
+
readonly blockDurationSeconds: 3600;
|
|
1075
|
+
};
|
|
1076
|
+
/** Checkout/billing: 10/hour with 1hr block */
|
|
1077
|
+
readonly checkout: {
|
|
1078
|
+
readonly limit: 10;
|
|
1079
|
+
readonly windowSeconds: 3600;
|
|
1080
|
+
readonly blockDurationSeconds: 3600;
|
|
1081
|
+
};
|
|
1082
|
+
};
|
|
1083
|
+
/**
|
|
1084
|
+
* In-memory rate limit store for testing and graceful degradation.
|
|
1085
|
+
* Not suitable for multi-process or distributed environments.
|
|
1086
|
+
*/
|
|
1087
|
+
declare function createMemoryRateLimitStore(): RateLimitStore;
|
|
1088
|
+
/**
|
|
1089
|
+
* Check rate limit for an operation.
|
|
1090
|
+
*
|
|
1091
|
+
* @param operation - Name of the operation (e.g., 'server-create', 'api-call')
|
|
1092
|
+
* @param identifier - Who is making the request (e.g., 'user:123', 'ip:1.2.3.4')
|
|
1093
|
+
* @param rule - Rate limit configuration
|
|
1094
|
+
* @param options - Storage, auth status, logger
|
|
1095
|
+
* @returns Rate limit check result
|
|
1096
|
+
*/
|
|
1097
|
+
declare function checkRateLimit(operation: string, identifier: string, rule: RateLimitRule, options?: RateLimitOptions): Promise<RateLimitCheckResult>;
|
|
1098
|
+
/**
|
|
1099
|
+
* Get current rate limit status without incrementing the counter.
|
|
1100
|
+
*/
|
|
1101
|
+
declare function getRateLimitStatus(operation: string, identifier: string, rule: RateLimitRule, store?: RateLimitStore): Promise<RateLimitCheckResult | null>;
|
|
1102
|
+
/**
|
|
1103
|
+
* Reset rate limit for an identifier (for admin/testing).
|
|
1104
|
+
*/
|
|
1105
|
+
declare function resetRateLimitForKey(operation: string, identifier: string, store?: RateLimitStore): Promise<void>;
|
|
1106
|
+
/**
|
|
1107
|
+
* Build standard rate limit headers for HTTP responses.
|
|
1108
|
+
*/
|
|
1109
|
+
declare function buildRateLimitResponseHeaders(result: RateLimitCheckResult): Record<string, string>;
|
|
1110
|
+
/**
|
|
1111
|
+
* Resolve a rate limit identifier from a request-like context.
|
|
1112
|
+
* Prefers user ID > email > IP for most accurate limiting.
|
|
1113
|
+
*/
|
|
1114
|
+
declare function resolveIdentifier(session: {
|
|
1115
|
+
user?: {
|
|
1116
|
+
id?: string;
|
|
1117
|
+
email?: string;
|
|
1118
|
+
};
|
|
1119
|
+
} | null | undefined, clientIp?: string): {
|
|
1120
|
+
identifier: string;
|
|
1121
|
+
isAuthenticated: boolean;
|
|
1122
|
+
};
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
* Audit Logging System
|
|
1126
|
+
*
|
|
1127
|
+
* Framework-agnostic audit logging for security-sensitive operations.
|
|
1128
|
+
* Provides structured audit events with actor, action, resource, and
|
|
1129
|
+
* outcome tracking. Apps provide their own persistence (Redis, DB, etc.)
|
|
1130
|
+
* via a simple callback; this module handles the event model and helpers.
|
|
1131
|
+
*
|
|
1132
|
+
* @example
|
|
1133
|
+
* ```typescript
|
|
1134
|
+
* import { createAuditLogger, StandardAuditActions } from '@digilogiclabs/platform-core'
|
|
1135
|
+
*
|
|
1136
|
+
* const audit = createAuditLogger({
|
|
1137
|
+
* persist: async (record) => {
|
|
1138
|
+
* await redis.setex(`audit:${record.id}`, 90 * 86400, JSON.stringify(record))
|
|
1139
|
+
* },
|
|
1140
|
+
* })
|
|
1141
|
+
*
|
|
1142
|
+
* await audit.log({
|
|
1143
|
+
* actor: { id: userId, email, type: 'user' },
|
|
1144
|
+
* action: StandardAuditActions.LOGIN_SUCCESS,
|
|
1145
|
+
* outcome: 'success',
|
|
1146
|
+
* })
|
|
1147
|
+
* ```
|
|
1148
|
+
*/
|
|
1149
|
+
/** Who performed the action */
|
|
1150
|
+
interface OpsAuditActor {
|
|
1151
|
+
id: string;
|
|
1152
|
+
email?: string;
|
|
1153
|
+
type: "user" | "admin" | "system" | "api_key" | "anonymous";
|
|
1154
|
+
sessionId?: string;
|
|
1155
|
+
}
|
|
1156
|
+
/** What resource was affected */
|
|
1157
|
+
interface OpsAuditResource {
|
|
1158
|
+
type: string;
|
|
1159
|
+
id: string;
|
|
1160
|
+
name?: string;
|
|
1161
|
+
}
|
|
1162
|
+
/** The audit event to log */
|
|
1163
|
+
interface OpsAuditEvent {
|
|
1164
|
+
actor: OpsAuditActor;
|
|
1165
|
+
action: string;
|
|
1166
|
+
resource?: OpsAuditResource;
|
|
1167
|
+
outcome: "success" | "failure" | "blocked" | "pending";
|
|
1168
|
+
metadata?: Record<string, unknown>;
|
|
1169
|
+
reason?: string;
|
|
1170
|
+
}
|
|
1171
|
+
/** Complete audit record with context */
|
|
1172
|
+
interface OpsAuditRecord extends OpsAuditEvent {
|
|
1173
|
+
id: string;
|
|
1174
|
+
timestamp: string;
|
|
1175
|
+
ip?: string;
|
|
1176
|
+
userAgent?: string;
|
|
1177
|
+
requestId?: string;
|
|
1178
|
+
duration?: number;
|
|
1179
|
+
}
|
|
1180
|
+
/** Options for creating an audit logger */
|
|
1181
|
+
interface OpsAuditLoggerOptions {
|
|
1182
|
+
/**
|
|
1183
|
+
* Persist an audit record to storage (Redis, DB, etc.).
|
|
1184
|
+
* Called after console logging. Errors are caught and logged,
|
|
1185
|
+
* never thrown — audit failures must not break operations.
|
|
1186
|
+
*/
|
|
1187
|
+
persist?: (record: OpsAuditRecord) => Promise<void>;
|
|
1188
|
+
/**
|
|
1189
|
+
* Console logger for structured output.
|
|
1190
|
+
* Defaults to console.log/console.warn.
|
|
1191
|
+
*/
|
|
1192
|
+
logger?: {
|
|
1193
|
+
info: (msg: string, meta?: Record<string, unknown>) => void;
|
|
1194
|
+
warn: (msg: string, meta?: Record<string, unknown>) => void;
|
|
1195
|
+
error: (msg: string, meta?: Record<string, unknown>) => void;
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
/** Request-like object for extracting IP, user agent, request ID */
|
|
1199
|
+
interface AuditRequest {
|
|
1200
|
+
headers: {
|
|
1201
|
+
get(name: string): string | null;
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
/**
|
|
1205
|
+
* Standard audit action constants.
|
|
1206
|
+
* Apps should extend with domain-specific actions:
|
|
1207
|
+
*
|
|
1208
|
+
* @example
|
|
1209
|
+
* ```typescript
|
|
1210
|
+
* const AppAuditActions = {
|
|
1211
|
+
* ...StandardAuditActions,
|
|
1212
|
+
* SERVER_CREATE: 'game.server.create',
|
|
1213
|
+
* SERVER_DELETE: 'game.server.delete',
|
|
1214
|
+
* } as const
|
|
1215
|
+
* ```
|
|
1216
|
+
*/
|
|
1217
|
+
declare const StandardAuditActions: {
|
|
1218
|
+
readonly LOGIN_SUCCESS: "auth.login.success";
|
|
1219
|
+
readonly LOGIN_FAILURE: "auth.login.failure";
|
|
1220
|
+
readonly LOGOUT: "auth.logout";
|
|
1221
|
+
readonly SESSION_REFRESH: "auth.session.refresh";
|
|
1222
|
+
readonly PASSWORD_CHANGE: "auth.password.change";
|
|
1223
|
+
readonly PASSWORD_RESET: "auth.password.reset";
|
|
1224
|
+
readonly CHECKOUT_START: "billing.checkout.start";
|
|
1225
|
+
readonly CHECKOUT_COMPLETE: "billing.checkout.complete";
|
|
1226
|
+
readonly SUBSCRIPTION_CREATE: "billing.subscription.create";
|
|
1227
|
+
readonly SUBSCRIPTION_CANCEL: "billing.subscription.cancel";
|
|
1228
|
+
readonly SUBSCRIPTION_UPDATE: "billing.subscription.update";
|
|
1229
|
+
readonly PAYMENT_FAILED: "billing.payment.failed";
|
|
1230
|
+
readonly ADMIN_LOGIN: "admin.login";
|
|
1231
|
+
readonly ADMIN_USER_VIEW: "admin.user.view";
|
|
1232
|
+
readonly ADMIN_USER_UPDATE: "admin.user.update";
|
|
1233
|
+
readonly ADMIN_CONFIG_CHANGE: "admin.config.change";
|
|
1234
|
+
readonly RATE_LIMIT_EXCEEDED: "security.rate_limit.exceeded";
|
|
1235
|
+
readonly INVALID_INPUT: "security.input.invalid";
|
|
1236
|
+
readonly UNAUTHORIZED_ACCESS: "security.access.unauthorized";
|
|
1237
|
+
readonly OWNERSHIP_VIOLATION: "security.ownership.violation";
|
|
1238
|
+
readonly WEBHOOK_SIGNATURE_INVALID: "security.webhook.signature_invalid";
|
|
1239
|
+
readonly DATA_EXPORT: "data.export";
|
|
1240
|
+
readonly DATA_DELETE: "data.delete";
|
|
1241
|
+
readonly DATA_UPDATE: "data.update";
|
|
1242
|
+
};
|
|
1243
|
+
type StandardAuditActionType = (typeof StandardAuditActions)[keyof typeof StandardAuditActions];
|
|
1244
|
+
/**
|
|
1245
|
+
* Extract client IP from request headers.
|
|
1246
|
+
* Checks common proxy headers in order of reliability.
|
|
1247
|
+
*/
|
|
1248
|
+
declare function extractAuditIp(request?: AuditRequest): string | undefined;
|
|
1249
|
+
/**
|
|
1250
|
+
* Extract user agent from request.
|
|
1251
|
+
*/
|
|
1252
|
+
declare function extractAuditUserAgent(request?: AuditRequest): string | undefined;
|
|
1253
|
+
/**
|
|
1254
|
+
* Extract or generate request ID.
|
|
1255
|
+
*/
|
|
1256
|
+
declare function extractAuditRequestId(request?: AuditRequest): string;
|
|
1257
|
+
/**
|
|
1258
|
+
* Create an AuditActor from a session object.
|
|
1259
|
+
* Works with Auth.js/NextAuth session shape.
|
|
1260
|
+
*/
|
|
1261
|
+
declare function createAuditActor(session: {
|
|
1262
|
+
user?: {
|
|
1263
|
+
id?: string;
|
|
1264
|
+
email?: string | null;
|
|
1265
|
+
};
|
|
1266
|
+
} | null | undefined): OpsAuditActor;
|
|
1267
|
+
/**
|
|
1268
|
+
* Create an audit logger instance.
|
|
1269
|
+
*
|
|
1270
|
+
* @param options - Persistence callback and optional logger
|
|
1271
|
+
* @returns Audit logger with log() and createTimedAudit() methods
|
|
1272
|
+
*/
|
|
1273
|
+
declare function createAuditLogger(options?: OpsAuditLoggerOptions): {
|
|
1274
|
+
log: (event: OpsAuditEvent, request?: AuditRequest) => Promise<OpsAuditRecord>;
|
|
1275
|
+
createTimedAudit: (event: Omit<OpsAuditEvent, "outcome">, request?: AuditRequest) => {
|
|
1276
|
+
success: (metadata?: Record<string, unknown>) => Promise<OpsAuditRecord>;
|
|
1277
|
+
failure: (reason: string, metadata?: Record<string, unknown>) => Promise<OpsAuditRecord>;
|
|
1278
|
+
blocked: (reason: string, metadata?: Record<string, unknown>) => Promise<OpsAuditRecord>;
|
|
1279
|
+
};
|
|
1280
|
+
};
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* Environment Variable Helpers
|
|
1284
|
+
*
|
|
1285
|
+
* Type-safe, fail-fast utilities for reading environment variables.
|
|
1286
|
+
* Every app needs these — extracted from DLL, WIAN, and OSS patterns.
|
|
1287
|
+
*
|
|
1288
|
+
* @example
|
|
1289
|
+
* ```typescript
|
|
1290
|
+
* import { getRequiredEnv, getOptionalEnv, getBoolEnv, validateEnvVars } from '@digilogiclabs/platform-core'
|
|
1291
|
+
*
|
|
1292
|
+
* const config = {
|
|
1293
|
+
* stripe: { secretKey: getRequiredEnv('STRIPE_SECRET_KEY') },
|
|
1294
|
+
* redis: { url: getOptionalEnv('REDIS_URL', '') },
|
|
1295
|
+
* features: { debug: getBoolEnv('DEBUG', false) },
|
|
1296
|
+
* }
|
|
1297
|
+
*
|
|
1298
|
+
* // Validate at startup
|
|
1299
|
+
* validateEnvVars({
|
|
1300
|
+
* required: ['STRIPE_SECRET_KEY', 'DATABASE_URL'],
|
|
1301
|
+
* requireOneOf: [['DATABASE_URL', 'SUPABASE_URL']], // at least one
|
|
1302
|
+
* validators: {
|
|
1303
|
+
* DATABASE_URL: (v) => v.startsWith('postgres') || 'must start with postgres://',
|
|
1304
|
+
* ADMIN_SECRET: (v) => v.length >= 32 || 'must be at least 32 characters',
|
|
1305
|
+
* },
|
|
1306
|
+
* })
|
|
1307
|
+
* ```
|
|
1308
|
+
*/
|
|
1309
|
+
/**
|
|
1310
|
+
* Get a required environment variable.
|
|
1311
|
+
* Throws immediately if not set — fail-fast at startup.
|
|
1312
|
+
*/
|
|
1313
|
+
declare function getRequiredEnv(key: string): string;
|
|
1314
|
+
/**
|
|
1315
|
+
* Get an optional environment variable with a default value.
|
|
1316
|
+
*/
|
|
1317
|
+
declare function getOptionalEnv(key: string, defaultValue: string): string;
|
|
1318
|
+
/**
|
|
1319
|
+
* Get a boolean environment variable.
|
|
1320
|
+
* Treats "true" and "1" as true, everything else as false.
|
|
1321
|
+
*/
|
|
1322
|
+
declare function getBoolEnv(key: string, defaultValue?: boolean): boolean;
|
|
1323
|
+
/**
|
|
1324
|
+
* Get a numeric environment variable with a default.
|
|
1325
|
+
*/
|
|
1326
|
+
declare function getIntEnv(key: string, defaultValue: number): number;
|
|
1327
|
+
/**
|
|
1328
|
+
* Configuration for environment validation.
|
|
1329
|
+
*/
|
|
1330
|
+
interface EnvValidationConfig {
|
|
1331
|
+
/** Variables that must be set (non-empty) */
|
|
1332
|
+
required?: string[];
|
|
1333
|
+
/** Groups where at least one must be set (e.g., DATABASE_URL or SUPABASE_URL) */
|
|
1334
|
+
requireOneOf?: string[][];
|
|
1335
|
+
/** Custom validators — return true or an error message */
|
|
1336
|
+
validators?: Record<string, (value: string) => true | string>;
|
|
1337
|
+
}
|
|
1338
|
+
/**
|
|
1339
|
+
* Validation result with categorized errors.
|
|
1340
|
+
*/
|
|
1341
|
+
interface EnvValidationResult {
|
|
1342
|
+
valid: boolean;
|
|
1343
|
+
missing: string[];
|
|
1344
|
+
invalid: {
|
|
1345
|
+
key: string;
|
|
1346
|
+
reason: string;
|
|
1347
|
+
}[];
|
|
1348
|
+
missingOneOf: string[][];
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* Validate environment variables against a configuration.
|
|
1352
|
+
*
|
|
1353
|
+
* @throws Error with details about all missing/invalid variables
|
|
1354
|
+
*/
|
|
1355
|
+
declare function validateEnvVars(config: EnvValidationConfig): void;
|
|
1356
|
+
/**
|
|
1357
|
+
* Check environment variables without throwing.
|
|
1358
|
+
* Returns a result object for custom error handling.
|
|
1359
|
+
*/
|
|
1360
|
+
declare function checkEnvVars(config: EnvValidationConfig): EnvValidationResult;
|
|
1361
|
+
/**
|
|
1362
|
+
* Get a redacted config summary for debugging.
|
|
1363
|
+
* Shows which variables are configured without exposing values.
|
|
1364
|
+
*/
|
|
1365
|
+
declare function getEnvSummary(keys: string[]): Record<string, boolean>;
|
|
1366
|
+
|
|
1367
|
+
export { type ApiSecurityContext as $, ApiErrorCode as A, type KeycloakConfig as B, CommonApiErrors as C, type KeycloakTokenSet as D, buildKeycloakCallbacks as E, buildAuthCookies as F, buildRedirectCallback as G, HTML_TAG_PATTERN as H, type KeycloakCallbacksConfig as I, type KeycloakJwtFields as J, KEYCLOAK_DEFAULT_ROLES as K, type AuthCookiesConfig as L, resolveRateLimitIdentifier as M, extractClientIp as N, buildRateLimitHeaders as O, PG_ERROR_MAP as P, buildErrorBody as Q, type RedirectCallbackConfig as R, StandardRateLimitPresets as S, type TokenRefreshResult as T, URL_PROTOCOL_PATTERN as U, type AuthMethod as V, WrapperPresets as W, type RouteAuditConfig as X, type RateLimitPreset as Y, type ApiSecurityConfig as Z, type SecuritySession as _, containsHtml as a, EmailSchema as a0, PasswordSchema as a1, SlugSchema as a2, PhoneSchema as a3, PersonNameSchema as a4, PaginationSchema as a5, DateRangeSchema as a6, SearchQuerySchema as a7, LoginSchema as a8, SignupSchema as a9, type RateLimitStore as aA, type RateLimitOptions as aB, createAuditLogger as aC, createAuditActor as aD, extractAuditIp as aE, extractAuditUserAgent as aF, extractAuditRequestId as aG, StandardAuditActions as aH, type OpsAuditActor as aI, type OpsAuditResource as aJ, type OpsAuditEvent as aK, type OpsAuditRecord as aL, type OpsAuditLoggerOptions as aM, type AuditRequest as aN, type StandardAuditActionType as aO, getRequiredEnv as aP, getOptionalEnv as aQ, getBoolEnv as aR, getIntEnv as aS, validateEnvVars as aT, checkEnvVars as aU, getEnvSummary as aV, type EnvValidationConfig as aW, type EnvValidationResult as aX, createSafeTextSchema as aa, type EmailInput as ab, type PaginationInput as ac, type DateRangeInput as ad, type SearchQueryInput as ae, type LoginInput as af, type SignupInput as ag, createFeatureFlags as ah, detectStage as ai, buildAllowlist as aj, isAllowlisted as ak, type DeploymentStage as al, type FlagValue as am, type FlagDefinition as an, type FlagDefinitions as ao, type ResolvedFlags as ap, type AllowlistConfig as aq, checkRateLimit as ar, getRateLimitStatus as as, resetRateLimitForKey as at, createMemoryRateLimitStore as au, buildRateLimitResponseHeaders as av, resolveIdentifier as aw, CommonRateLimits as ax, type RateLimitRule as ay, type RateLimitCheckResult as az, sanitizeForEmail as b, containsUrls as c, defangUrl as d, escapeHtml as e, constantTimeEqual as f, sanitizeApiError as g, getCorrelationId as h, URL_DOMAIN_PATTERN as i, ApiError as j, isApiError as k, classifyError as l, buildPagination as m, type ApiErrorCodeType as n, type ApiSuccessResponse as o, type ApiPaginatedResponse as p, parseKeycloakRoles as q, hasRole as r, stripHtml as s, hasAnyRole as t, hasAllRoles as u, isTokenExpired as v, buildTokenRefreshParams as w, getTokenEndpoint as x, getEndSessionEndpoint as y, refreshKeycloakToken as z };
|