@ahoo-wang/fetcher-cosec 3.9.9 → 3.10.2
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/cosecConfigurer.d.ts +377 -79
- package/dist/cosecConfigurer.d.ts.map +1 -1
- package/dist/cosecRequestInterceptor.d.ts +289 -31
- package/dist/cosecRequestInterceptor.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +372 -162
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/spaceIdProvider.d.ts +383 -0
- package/dist/spaceIdProvider.d.ts.map +1 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -4,165 +4,463 @@ import { JwtTokenManager } from './jwtTokenManager';
|
|
|
4
4
|
import { TokenRefresher } from './tokenRefresher';
|
|
5
5
|
import { TokenStorage } from './tokenStorage';
|
|
6
6
|
import { AppIdCapable, DeviceIdStorageCapable } from './types';
|
|
7
|
+
import { SpaceIdProvider } from './spaceIdProvider';
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
9
|
+
* Configuration interface for CoSec security features.
|
|
10
|
+
*
|
|
11
|
+
* This interface provides a simplified, flexible configuration for setting up
|
|
12
|
+
* CoSec authentication and security features. It supports both full JWT
|
|
13
|
+
* authentication setups and minimal header injection configurations.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* **Required Properties:**
|
|
17
|
+
* - appId: Your application's unique identifier in the CoSec system
|
|
18
|
+
*
|
|
19
|
+
* **Optional Properties:**
|
|
20
|
+
* - tokenStorage: Custom token persistence (defaults to TokenStorage)
|
|
21
|
+
* - deviceIdStorage: Custom device ID management (defaults to DeviceIdStorage)
|
|
22
|
+
* - tokenRefresher: JWT token refresh handler (enables authentication)
|
|
23
|
+
* - spaceIdProvider: Space identifier resolver (enables multi-tenant)
|
|
24
|
+
* - onUnauthorized: Custom 401 error handler
|
|
25
|
+
* - onForbidden: Custom 403 error handler
|
|
26
|
+
*
|
|
27
|
+
* **Configuration Levels:**
|
|
28
|
+
* - Minimal: Only appId (headers only, no authentication)
|
|
29
|
+
* - Standard: appId + tokenRefresher (full JWT auth)
|
|
30
|
+
* - Enterprise: All options (multi-tenant with custom handlers)
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Minimal: Headers only, no authentication
|
|
35
|
+
* const minimalConfig: CoSecConfig = {
|
|
36
|
+
* appId: 'my-web-app'
|
|
37
|
+
* };
|
|
38
|
+
*
|
|
39
|
+
* // Standard: Full JWT authentication
|
|
40
|
+
* const standardConfig: CoSecConfig = {
|
|
41
|
+
* appId: 'my-web-app',
|
|
42
|
+
* tokenRefresher: {
|
|
43
|
+
* refresh: async (token) => refreshMyToken(token)
|
|
44
|
+
* },
|
|
45
|
+
* onUnauthorized: () => window.location.href = '/login',
|
|
46
|
+
* onForbidden: () => showError('Access denied')
|
|
47
|
+
* };
|
|
48
|
+
*
|
|
49
|
+
* // Enterprise: Multi-tenant with custom storage
|
|
50
|
+
* const enterpriseConfig: CoSecConfig = {
|
|
51
|
+
* appId: 'my-saas-app',
|
|
52
|
+
* tokenStorage: myCustomTokenStorage,
|
|
53
|
+
* deviceIdStorage: myCustomDeviceStorage,
|
|
54
|
+
* tokenRefresher: myTokenRefresher,
|
|
55
|
+
* spaceIdProvider: mySpaceProvider,
|
|
56
|
+
* onUnauthorized: handle401,
|
|
57
|
+
* onForbidden: handle403
|
|
58
|
+
* };
|
|
59
|
+
* ```
|
|
10
60
|
*/
|
|
11
61
|
export interface CoSecConfig extends AppIdCapable, Partial<DeviceIdStorageCapable> {
|
|
12
62
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
63
|
+
* Your application's unique identifier in the CoSec authentication system.
|
|
64
|
+
*
|
|
65
|
+
* This ID is sent with every request in the `CoSec-App-Id` header and is
|
|
66
|
+
* used to identify which application is making the request. Obtain this
|
|
67
|
+
* value from your CoSec administration console.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* **Requirements:**
|
|
71
|
+
* - Must be a non-empty string
|
|
72
|
+
* - Must be registered in the CoSec system
|
|
73
|
+
* - Should be unique per application/deployment
|
|
74
|
+
*
|
|
75
|
+
* **Usage:**
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const config: CoSecConfig = {
|
|
78
|
+
* appId: 'my-production-app-001' // Registered app ID
|
|
79
|
+
* };
|
|
80
|
+
* ```
|
|
15
81
|
*/
|
|
16
|
-
appId: string;
|
|
82
|
+
readonly appId: string;
|
|
17
83
|
/**
|
|
18
|
-
* Custom
|
|
19
|
-
*
|
|
20
|
-
*
|
|
84
|
+
* Custom implementation for storing and retrieving JWT tokens.
|
|
85
|
+
*
|
|
86
|
+
* If not provided, a default TokenStorage instance will be created
|
|
87
|
+
* using browser localStorage. Use this property to provide custom
|
|
88
|
+
* storage backends or testing implementations.
|
|
89
|
+
*
|
|
90
|
+
* @defaultValue new TokenStorage()
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* // Custom encrypted storage
|
|
95
|
+
* const encryptedStorage = new EncryptedTokenStorage();
|
|
96
|
+
* const config: CoSecConfig = {
|
|
97
|
+
* appId: 'my-app',
|
|
98
|
+
* tokenStorage: encryptedStorage
|
|
99
|
+
* };
|
|
100
|
+
*
|
|
101
|
+
* // Memory storage for testing
|
|
102
|
+
* const memoryStorage = new InMemoryTokenStorage();
|
|
103
|
+
* const config: CoSecConfig = {
|
|
104
|
+
* appId: 'my-app',
|
|
105
|
+
* tokenStorage: memoryStorage
|
|
106
|
+
* };
|
|
107
|
+
* ```
|
|
21
108
|
*/
|
|
22
|
-
tokenStorage?: TokenStorage;
|
|
109
|
+
readonly tokenStorage?: TokenStorage;
|
|
23
110
|
/**
|
|
24
|
-
* Custom
|
|
25
|
-
*
|
|
26
|
-
*
|
|
111
|
+
* Custom implementation for storing and retrieving device identifiers.
|
|
112
|
+
*
|
|
113
|
+
* If not provided, a default DeviceIdStorage instance will be created
|
|
114
|
+
* using browser localStorage with cross-tab synchronization. Use this
|
|
115
|
+
* property for custom device identification strategies.
|
|
116
|
+
*
|
|
117
|
+
* @defaultValue new DeviceIdStorage()
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // Custom device ID with custom key
|
|
122
|
+
* const customDeviceStorage = new DeviceIdStorage({
|
|
123
|
+
* key: 'my-app-device-id',
|
|
124
|
+
* storage: sessionStorage
|
|
125
|
+
* });
|
|
126
|
+
* const config: CoSecConfig = {
|
|
127
|
+
* appId: 'my-app',
|
|
128
|
+
* deviceIdStorage: customDeviceStorage
|
|
129
|
+
* };
|
|
130
|
+
* ```
|
|
27
131
|
*/
|
|
28
|
-
deviceIdStorage?: DeviceIdStorage;
|
|
132
|
+
readonly deviceIdStorage?: DeviceIdStorage;
|
|
29
133
|
/**
|
|
30
|
-
* Token
|
|
31
|
-
*
|
|
32
|
-
*
|
|
134
|
+
* Token refresh handler for automatic JWT token renewal.
|
|
135
|
+
*
|
|
136
|
+
* When provided, enables full JWT authentication including:
|
|
137
|
+
* - Automatic Bearer token injection in requests
|
|
138
|
+
* - Token refresh on 401 responses
|
|
139
|
+
* - Persistent token storage
|
|
140
|
+
*
|
|
141
|
+
* When not provided, no authentication interceptors are added.
|
|
142
|
+
*
|
|
143
|
+
* @defaultValue undefined (no authentication)
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* // Basic refresh implementation
|
|
148
|
+
* const myRefresher: TokenRefresher = {
|
|
149
|
+
* refresh: async (token) => {
|
|
150
|
+
* const response = await fetch('/api/auth/refresh', {
|
|
151
|
+
* method: 'POST',
|
|
152
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
153
|
+
* body: JSON.stringify({ refreshToken: token.refreshToken })
|
|
154
|
+
* });
|
|
155
|
+
* if (!response.ok) throw new Error('Refresh failed');
|
|
156
|
+
* return response.json();
|
|
157
|
+
* }
|
|
158
|
+
* };
|
|
159
|
+
*
|
|
160
|
+
* const config: CoSecConfig = {
|
|
161
|
+
* appId: 'my-app',
|
|
162
|
+
* tokenRefresher: myRefresher
|
|
163
|
+
* };
|
|
164
|
+
* ```
|
|
33
165
|
*/
|
|
34
|
-
tokenRefresher?: TokenRefresher;
|
|
166
|
+
readonly tokenRefresher?: TokenRefresher;
|
|
35
167
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
168
|
+
* Provider for resolving space identifiers from requests.
|
|
169
|
+
*
|
|
170
|
+
* Used for multi-tenant applications to scope requests to specific
|
|
171
|
+
* spaces within a tenant. When provided, the CoSec-Space-Id header
|
|
172
|
+
* will be added to requests for space-scoped resources.
|
|
173
|
+
*
|
|
174
|
+
* @defaultValue NoneSpaceIdProvider (no space identification)
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* // Multi-tenant space provider
|
|
179
|
+
* const spaceProvider: SpaceIdProvider = {
|
|
180
|
+
* resolveSpaceId: (exchange) => {
|
|
181
|
+
* return exchange.request.headers['X-Current-Space'] || null;
|
|
182
|
+
* }
|
|
183
|
+
* };
|
|
184
|
+
*
|
|
185
|
+
* const config: CoSecConfig = {
|
|
186
|
+
* appId: 'my-saas-app',
|
|
187
|
+
* tokenRefresher: myRefresher,
|
|
188
|
+
* spaceIdProvider: spaceProvider
|
|
189
|
+
* };
|
|
190
|
+
* ```
|
|
38
191
|
*/
|
|
39
|
-
|
|
192
|
+
readonly spaceIdProvider?: SpaceIdProvider;
|
|
40
193
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
194
|
+
* Handler invoked when an HTTP 401 Unauthorized response is received.
|
|
195
|
+
*
|
|
196
|
+
* This callback is triggered when the server returns a 401 status code,
|
|
197
|
+
* typically indicating expired or invalid authentication. Use this to
|
|
198
|
+
* implement custom error handling such as redirecting to login.
|
|
199
|
+
*
|
|
200
|
+
* @param exchange - The fetch exchange containing the failed request
|
|
201
|
+
* @returns Promise<void> or void
|
|
202
|
+
*
|
|
203
|
+
* @defaultValue undefined (401 errors not intercepted)
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* // Redirect to login
|
|
208
|
+
* const config: CoSecConfig = {
|
|
209
|
+
* appId: 'my-app',
|
|
210
|
+
* tokenRefresher: myRefresher,
|
|
211
|
+
* onUnauthorized: async (exchange) => {
|
|
212
|
+
* await authService.logout();
|
|
213
|
+
* window.location.href = '/login?reason=session_expired';
|
|
214
|
+
* }
|
|
215
|
+
* };
|
|
216
|
+
*
|
|
217
|
+
* // Silent token refresh
|
|
218
|
+
* const config: CoSecConfig = {
|
|
219
|
+
* appId: 'my-app',
|
|
220
|
+
* tokenRefresher: myRefresher,
|
|
221
|
+
* onUnauthorized: async (exchange) => {
|
|
222
|
+
* // Force logout without redirect
|
|
223
|
+
* authService.clearSession();
|
|
224
|
+
* dispatch(logoutAction());
|
|
225
|
+
* }
|
|
226
|
+
* };
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
readonly onUnauthorized?: (exchange: FetchExchange) => Promise<void> | void;
|
|
230
|
+
/**
|
|
231
|
+
* Handler invoked when an HTTP 403 Forbidden response is received.
|
|
232
|
+
*
|
|
233
|
+
* This callback is triggered when the server returns a 403 status code,
|
|
234
|
+
* indicating the authenticated user lacks permission for the requested
|
|
235
|
+
* resource. Use this to implement custom access denial handling.
|
|
236
|
+
*
|
|
237
|
+
* @param exchange - The fetch exchange containing the failed request
|
|
238
|
+
* @returns Promise<void>
|
|
239
|
+
*
|
|
240
|
+
* @defaultValue undefined (403 errors not intercepted)
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* // Show permission error
|
|
245
|
+
* const config: CoSecConfig = {
|
|
246
|
+
* appId: 'my-app',
|
|
247
|
+
* tokenRefresher: myRefresher,
|
|
248
|
+
* onForbidden: async (exchange) => {
|
|
249
|
+
* notification.error({
|
|
250
|
+
* message: 'Access Denied',
|
|
251
|
+
* description: 'You do not have permission to access this resource.'
|
|
252
|
+
* });
|
|
253
|
+
* }
|
|
254
|
+
* };
|
|
255
|
+
*
|
|
256
|
+
* // Redirect to access denied page
|
|
257
|
+
* const config: CoSecConfig = {
|
|
258
|
+
* appId: 'my-app',
|
|
259
|
+
* onForbidden: async (exchange) => {
|
|
260
|
+
* router.push('/access-denied');
|
|
261
|
+
* }
|
|
262
|
+
* };
|
|
263
|
+
* ```
|
|
43
264
|
*/
|
|
44
|
-
onForbidden?: (exchange: FetchExchange) => Promise<void>;
|
|
265
|
+
readonly onForbidden?: (exchange: FetchExchange) => Promise<void>;
|
|
45
266
|
}
|
|
46
267
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
268
|
+
* Configurer class that applies CoSec security interceptors to a Fetcher instance.
|
|
269
|
+
*
|
|
270
|
+
* This class provides a simplified, declarative way to configure all CoSec
|
|
271
|
+
* authentication and security features. It implements FetcherConfigurer and
|
|
272
|
+
* handles the conditional creation and registration of interceptors based
|
|
273
|
+
* on the provided configuration.
|
|
49
274
|
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
275
|
+
* @remarks
|
|
276
|
+
* **Architecture:**
|
|
277
|
+
* The configurer acts as a composition root for all CoSec interceptors,
|
|
278
|
+
* managing their lifecycle and ensuring proper ordering in the interceptor
|
|
279
|
+
* chain. This approach provides a clean separation between configuration
|
|
280
|
+
* and implementation.
|
|
53
281
|
*
|
|
54
|
-
*
|
|
282
|
+
* **Interceptor Ordering:**
|
|
283
|
+
* Interceptors are registered in a specific order to ensure correct
|
|
284
|
+
* header injection and error handling:
|
|
285
|
+
* 1. CoSecRequestInterceptor (request) - Adds security headers
|
|
286
|
+
* 2. ResourceAttributionRequestInterceptor (request) - Adds tenant attribution
|
|
287
|
+
* 3. AuthorizationRequestInterceptor (request) - Adds Bearer token [conditional]
|
|
288
|
+
* 4. AuthorizationResponseInterceptor (response) - Handles 401 refresh [conditional]
|
|
289
|
+
* 5. UnauthorizedErrorInterceptor (error) - Handles 401 errors [conditional]
|
|
290
|
+
* 6. ForbiddenErrorInterceptor (error) - Handles 403 errors [conditional]
|
|
291
|
+
*
|
|
292
|
+
* **Conditional Features:**
|
|
293
|
+
* - Authentication (tokenRefresher): Enables JWT Bearer token injection and refresh
|
|
294
|
+
* - Space Support (spaceIdProvider): Enables multi-tenant space scoping
|
|
295
|
+
* - Error Handlers (onUnauthorized/onForbidden): Enables custom error handling
|
|
55
296
|
*
|
|
56
297
|
* @example
|
|
57
|
-
* Full authentication setup with custom storage:
|
|
58
298
|
* ```typescript
|
|
299
|
+
* import { Fetcher } from '@ahoo-wang/fetcher';
|
|
300
|
+
* import { CoSecConfigurer } from '@ahoo-wang/fetcher-cosec';
|
|
301
|
+
*
|
|
302
|
+
* // Create and configure
|
|
59
303
|
* const configurer = new CoSecConfigurer({
|
|
60
|
-
* appId: 'my-app
|
|
61
|
-
* tokenStorage: new CustomTokenStorage(),
|
|
62
|
-
* deviceIdStorage: new CustomDeviceStorage(),
|
|
304
|
+
* appId: 'my-saas-app',
|
|
63
305
|
* tokenRefresher: {
|
|
64
|
-
* refresh: async (token
|
|
65
|
-
* const
|
|
306
|
+
* refresh: async (token) => {
|
|
307
|
+
* const res = await fetch('/api/refresh', {
|
|
66
308
|
* method: 'POST',
|
|
67
|
-
* body: JSON.stringify({ refreshToken: token.refreshToken })
|
|
309
|
+
* body: JSON.stringify({ refreshToken: token.refreshToken })
|
|
68
310
|
* });
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* accessToken: newTokens.accessToken,
|
|
72
|
-
* refreshToken: newTokens.refreshToken,
|
|
73
|
-
* };
|
|
74
|
-
* },
|
|
311
|
+
* return res.json();
|
|
312
|
+
* }
|
|
75
313
|
* },
|
|
76
|
-
* onUnauthorized: (
|
|
77
|
-
* onForbidden: (
|
|
314
|
+
* onUnauthorized: () => window.location.href = '/login',
|
|
315
|
+
* onForbidden: () => showAccessDenied()
|
|
78
316
|
* });
|
|
79
317
|
*
|
|
318
|
+
* // Apply to fetcher
|
|
319
|
+
* const fetcher = new Fetcher({ baseUrl: 'https://api.example.com' });
|
|
80
320
|
* configurer.applyTo(fetcher);
|
|
321
|
+
*
|
|
322
|
+
* // All requests now have CoSec headers and JWT auth
|
|
323
|
+
* const users = await fetcher.get('/api/users');
|
|
81
324
|
* ```
|
|
82
325
|
*
|
|
83
326
|
* @example
|
|
84
|
-
* Minimal setup with only CoSec headers (no authentication):
|
|
85
327
|
* ```typescript
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
328
|
+
* // Minimal setup - headers only, no authentication
|
|
329
|
+
* const fetcher = new Fetcher();
|
|
330
|
+
* new CoSecConfigurer({
|
|
331
|
+
* appId: 'my-tracking-app'
|
|
332
|
+
* }).applyTo(fetcher);
|
|
90
333
|
*
|
|
91
|
-
*
|
|
334
|
+
* // Requests will include:
|
|
335
|
+
* // - CoSec-App-Id: my-tracking-app
|
|
336
|
+
* // - CoSec-Device-Id: <generated>
|
|
337
|
+
* // - CoSec-Request-Id: <unique-per-request>
|
|
92
338
|
* ```
|
|
93
339
|
*/
|
|
94
340
|
export declare class CoSecConfigurer implements FetcherConfigurer {
|
|
95
341
|
readonly config: CoSecConfig;
|
|
96
342
|
/**
|
|
97
|
-
* Token storage instance
|
|
343
|
+
* Token storage instance for JWT token persistence.
|
|
344
|
+
*
|
|
345
|
+
* Created from config.tokenStorage or instantiated with default TokenStorage.
|
|
346
|
+
* Used by authorization interceptors to retrieve and store JWT tokens.
|
|
98
347
|
*/
|
|
99
348
|
readonly tokenStorage: TokenStorage;
|
|
100
349
|
/**
|
|
101
|
-
* Device ID storage instance
|
|
350
|
+
* Device ID storage instance for device identifier management.
|
|
351
|
+
*
|
|
352
|
+
* Created from config.deviceIdStorage or instantiated with default DeviceIdStorage.
|
|
353
|
+
* Provides persistent device identification with cross-tab synchronization.
|
|
102
354
|
*/
|
|
103
355
|
readonly deviceIdStorage: DeviceIdStorage;
|
|
104
356
|
/**
|
|
105
|
-
* JWT token manager
|
|
106
|
-
*
|
|
357
|
+
* JWT token manager for authentication operations.
|
|
358
|
+
*
|
|
359
|
+
* Only created when tokenRefresher is provided in the config.
|
|
360
|
+
* When undefined, no authentication interceptors are registered.
|
|
107
361
|
*/
|
|
108
362
|
readonly tokenManager?: JwtTokenManager;
|
|
363
|
+
/**
|
|
364
|
+
* Space identifier provider for multi-tenant support.
|
|
365
|
+
*
|
|
366
|
+
* Created from config.spaceIdProvider or defaulted to NoneSpaceIdProvider.
|
|
367
|
+
* Resolves space IDs from requests for tenant-scoped resource access.
|
|
368
|
+
*/
|
|
369
|
+
readonly spaceIdProvider?: SpaceIdProvider;
|
|
109
370
|
/**
|
|
110
371
|
* Creates a new CoSecConfigurer instance with the provided configuration.
|
|
111
372
|
*
|
|
112
|
-
*
|
|
113
|
-
* -
|
|
114
|
-
* -
|
|
373
|
+
* The constructor performs dependency initialization:
|
|
374
|
+
* - Creates or wraps tokenStorage and deviceIdStorage with defaults
|
|
375
|
+
* - Initializes spaceIdProvider (defaults to NoneSpaceIdProvider)
|
|
376
|
+
* - Conditionally creates tokenManager only if tokenRefresher is provided
|
|
377
|
+
*
|
|
378
|
+
* @param config - CoSec configuration object containing all settings
|
|
115
379
|
*
|
|
116
|
-
* @
|
|
380
|
+
* @throws Error if appId is not provided
|
|
381
|
+
* @throws Error if provided dependencies are invalid
|
|
117
382
|
*
|
|
118
383
|
* @example
|
|
119
384
|
* ```typescript
|
|
120
|
-
* // Full
|
|
385
|
+
* // Full configuration
|
|
121
386
|
* const configurer = new CoSecConfigurer({
|
|
122
387
|
* appId: 'my-app',
|
|
123
|
-
*
|
|
388
|
+
* tokenStorage: customTokenStorage,
|
|
389
|
+
* deviceIdStorage: customDeviceStorage,
|
|
390
|
+
* tokenRefresher: myRefresher,
|
|
391
|
+
* spaceIdProvider: mySpaceProvider,
|
|
392
|
+
* onUnauthorized: handle401,
|
|
393
|
+
* onForbidden: handle403
|
|
394
|
+
* });
|
|
395
|
+
*
|
|
396
|
+
* // Minimal configuration
|
|
397
|
+
* const configurer = new CoSecConfigurer({
|
|
398
|
+
* appId: 'my-app'
|
|
124
399
|
* });
|
|
125
400
|
*
|
|
126
|
-
* //
|
|
401
|
+
* // Custom storage with default refresh
|
|
127
402
|
* const configurer = new CoSecConfigurer({
|
|
128
403
|
* appId: 'my-app',
|
|
129
|
-
* tokenStorage:
|
|
130
|
-
* deviceIdStorage: customDeviceStorage
|
|
404
|
+
* tokenStorage: encryptedStorage,
|
|
405
|
+
* deviceIdStorage: customDeviceStorage
|
|
131
406
|
* });
|
|
132
407
|
* ```
|
|
133
408
|
*/
|
|
134
409
|
constructor(config: CoSecConfig);
|
|
135
410
|
/**
|
|
136
|
-
* Applies CoSec interceptors to the
|
|
411
|
+
* Applies CoSec interceptors to the specified Fetcher instance.
|
|
137
412
|
*
|
|
138
|
-
* This method
|
|
413
|
+
* This method registers all configured interceptors with the Fetcher's
|
|
414
|
+
* interceptor chain. The registration is conditional based on the config:
|
|
139
415
|
*
|
|
140
|
-
* Always
|
|
141
|
-
*
|
|
142
|
-
*
|
|
416
|
+
* **Always Registered (Headers & Attribution):**
|
|
417
|
+
* - CoSecRequestInterceptor: Injects security headers
|
|
418
|
+
* - ResourceAttributionRequestInterceptor: Adds tenant attribution parameters
|
|
143
419
|
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
420
|
+
* **Conditional Registration (Authentication):**
|
|
421
|
+
* - AuthorizationRequestInterceptor: Adds Bearer token [requires tokenRefresher]
|
|
422
|
+
* - AuthorizationResponseInterceptor: Handles 401 refresh [requires tokenRefresher]
|
|
147
423
|
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
424
|
+
* **Conditional Registration (Error Handling):**
|
|
425
|
+
* - UnauthorizedErrorInterceptor: Custom 401 handling [requires onUnauthorized]
|
|
426
|
+
* - ForbiddenErrorInterceptor: Custom 403 handling [requires onForbidden]
|
|
151
427
|
*
|
|
152
|
-
* @param fetcher - The Fetcher instance to configure
|
|
428
|
+
* @param fetcher - The Fetcher instance to configure with CoSec interceptors
|
|
429
|
+
*
|
|
430
|
+
* @throws Error if fetcher is null or undefined
|
|
153
431
|
*
|
|
154
432
|
* @example
|
|
155
433
|
* ```typescript
|
|
156
|
-
* const fetcher = new Fetcher({
|
|
434
|
+
* const fetcher = new Fetcher({ baseUrl: 'https://api.example.com' });
|
|
435
|
+
*
|
|
157
436
|
* const configurer = new CoSecConfigurer({
|
|
158
437
|
* appId: 'my-app',
|
|
159
|
-
* tokenRefresher:
|
|
160
|
-
* onUnauthorized:
|
|
161
|
-
* onForbidden:
|
|
438
|
+
* tokenRefresher: myRefresher,
|
|
439
|
+
* onUnauthorized: () => redirectToLogin(),
|
|
440
|
+
* onForbidden: () => showError()
|
|
162
441
|
* });
|
|
163
442
|
*
|
|
164
443
|
* configurer.applyTo(fetcher);
|
|
444
|
+
*
|
|
165
445
|
* // Now fetcher has all CoSec interceptors configured
|
|
446
|
+
* const data = await fetcher.get('/api/data');
|
|
447
|
+
* ```
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* ```typescript
|
|
451
|
+
* // Applying to multiple fetchers
|
|
452
|
+
* const apiFetcher = new Fetcher({ baseUrl: 'https://api.example.com' });
|
|
453
|
+
* const adminFetcher = new Fetcher({ baseUrl: 'https://admin.example.com' });
|
|
454
|
+
*
|
|
455
|
+
* const configurer = new CoSecConfigurer({
|
|
456
|
+
* appId: 'my-app',
|
|
457
|
+
* tokenRefresher: myRefresher,
|
|
458
|
+
* onForbidden: showAccessDenied
|
|
459
|
+
* });
|
|
460
|
+
*
|
|
461
|
+
* // Apply same configuration to multiple fetchers
|
|
462
|
+
* configurer.applyTo(apiFetcher);
|
|
463
|
+
* configurer.applyTo(adminFetcher);
|
|
166
464
|
* ```
|
|
167
465
|
*/
|
|
168
466
|
applyTo(fetcher: Fetcher): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cosecConfigurer.d.ts","sourceRoot":"","sources":["../src/cosecConfigurer.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAI/E,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,OAAO,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"cosecConfigurer.d.ts","sourceRoot":"","sources":["../src/cosecConfigurer.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAI/E,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,OAAO,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,EAAuB,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,MAAM,WAAW,WACf,SAAQ,YAAY,EAAE,OAAO,CAAC,sBAAsB,CAAC;IACrD;;;;;;;;;;;;;;;;;;;OAmBG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,YAAY,CAAC;IAErC;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,eAAe,CAAC;IAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgCG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,cAAc,CAAC;IAEzC;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,eAAe,CAAC;IAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,QAAQ,EAAE,aAAa,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAE5E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAkCG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,QAAQ,EAAE,aAAa,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACnE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwEG;AACH,qBAAa,eAAgB,YAAW,iBAAiB;aAwE3B,MAAM,EAAE,WAAW;IAvE/C;;;;;OAKG;IACH,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAC;IAEpC;;;;;OAKG;IACH,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;IAE1C;;;;;OAKG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,eAAe,CAAC;IAExC;;;;;OAKG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,eAAe,CAAC;IAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAsCG;gBACyB,MAAM,EAAE,WAAW;IAe/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuDG;IACH,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;CAmDhC"}
|