digitaltwin-core 0.14.3 → 1.0.1
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/README.md +218 -1
- package/dist/auth/apisix_parser.d.ts +56 -56
- package/dist/auth/apisix_parser.d.ts.map +1 -1
- package/dist/auth/apisix_parser.js +72 -86
- package/dist/auth/apisix_parser.js.map +1 -1
- package/dist/auth/auth_provider.d.ts +118 -0
- package/dist/auth/auth_provider.d.ts.map +1 -0
- package/dist/auth/auth_provider.js +8 -0
- package/dist/auth/auth_provider.js.map +1 -0
- package/dist/auth/auth_provider_factory.d.ts +91 -0
- package/dist/auth/auth_provider_factory.d.ts.map +1 -0
- package/dist/auth/auth_provider_factory.js +146 -0
- package/dist/auth/auth_provider_factory.js.map +1 -0
- package/dist/auth/index.d.ts +4 -1
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +3 -0
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/providers/gateway_auth_provider.d.ts +78 -0
- package/dist/auth/providers/gateway_auth_provider.d.ts.map +1 -0
- package/dist/auth/providers/gateway_auth_provider.js +109 -0
- package/dist/auth/providers/gateway_auth_provider.js.map +1 -0
- package/dist/auth/providers/index.d.ts +4 -0
- package/dist/auth/providers/index.d.ts.map +1 -0
- package/dist/auth/providers/index.js +4 -0
- package/dist/auth/providers/index.js.map +1 -0
- package/dist/auth/providers/jwt_auth_provider.d.ts +91 -0
- package/dist/auth/providers/jwt_auth_provider.d.ts.map +1 -0
- package/dist/auth/providers/jwt_auth_provider.js +204 -0
- package/dist/auth/providers/jwt_auth_provider.js.map +1 -0
- package/dist/auth/providers/no_auth_provider.d.ts +61 -0
- package/dist/auth/providers/no_auth_provider.d.ts.map +1 -0
- package/dist/auth/providers/no_auth_provider.js +76 -0
- package/dist/auth/providers/no_auth_provider.js.map +1 -0
- package/dist/auth/types.d.ts +5 -3
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/components/assets_manager.d.ts +1 -1
- package/dist/components/assets_manager.d.ts.map +1 -1
- package/dist/components/assets_manager.js +54 -48
- package/dist/components/assets_manager.js.map +1 -1
- package/dist/components/collector.d.ts.map +1 -1
- package/dist/components/collector.js +30 -18
- package/dist/components/collector.js.map +1 -1
- package/dist/components/custom_table_manager.d.ts.map +1 -1
- package/dist/components/custom_table_manager.js +36 -65
- package/dist/components/custom_table_manager.js.map +1 -1
- package/dist/components/global_assets_handler.d.ts +4 -2
- package/dist/components/global_assets_handler.d.ts.map +1 -1
- package/dist/components/global_assets_handler.js.map +1 -1
- package/dist/components/harvester.d.ts.map +1 -1
- package/dist/components/harvester.js +46 -33
- package/dist/components/harvester.js.map +1 -1
- package/dist/components/interfaces.d.ts +3 -2
- package/dist/components/interfaces.d.ts.map +1 -1
- package/dist/components/map_manager.d.ts.map +1 -1
- package/dist/components/map_manager.js.map +1 -1
- package/dist/components/tileset_manager.d.ts +2 -1
- package/dist/components/tileset_manager.d.ts.map +1 -1
- package/dist/components/tileset_manager.js +20 -15
- package/dist/components/tileset_manager.js.map +1 -1
- package/dist/database/adapters/knex_database_adapter.d.ts +6 -1
- package/dist/database/adapters/knex_database_adapter.d.ts.map +1 -1
- package/dist/database/adapters/knex_database_adapter.js +118 -36
- package/dist/database/adapters/knex_database_adapter.js.map +1 -1
- package/dist/database/database_adapter.d.ts +13 -1
- package/dist/database/database_adapter.d.ts.map +1 -1
- package/dist/database/database_adapter.js.map +1 -1
- package/dist/engine/component_types.d.ts +95 -0
- package/dist/engine/component_types.d.ts.map +1 -0
- package/dist/engine/component_types.js +93 -0
- package/dist/engine/component_types.js.map +1 -0
- package/dist/engine/digital_twin_engine.d.ts +121 -6
- package/dist/engine/digital_twin_engine.d.ts.map +1 -1
- package/dist/engine/digital_twin_engine.js +402 -74
- package/dist/engine/digital_twin_engine.js.map +1 -1
- package/dist/engine/endpoints.d.ts.map +1 -1
- package/dist/engine/endpoints.js +35 -3
- package/dist/engine/endpoints.js.map +1 -1
- package/dist/engine/error_handler.d.ts +20 -0
- package/dist/engine/error_handler.d.ts.map +1 -0
- package/dist/engine/error_handler.js +69 -0
- package/dist/engine/error_handler.js.map +1 -0
- package/dist/engine/events.d.ts +1 -1
- package/dist/engine/events.d.ts.map +1 -1
- package/dist/engine/events.js.map +1 -1
- package/dist/engine/health.d.ts +112 -0
- package/dist/engine/health.d.ts.map +1 -0
- package/dist/engine/health.js +190 -0
- package/dist/engine/health.js.map +1 -0
- package/dist/engine/initializer.d.ts.map +1 -1
- package/dist/engine/initializer.js +6 -4
- package/dist/engine/initializer.js.map +1 -1
- package/dist/engine/scheduler.d.ts.map +1 -1
- package/dist/engine/scheduler.js +17 -9
- package/dist/engine/scheduler.js.map +1 -1
- package/dist/engine/upload_processor.d.ts.map +1 -1
- package/dist/engine/upload_processor.js +24 -12
- package/dist/engine/upload_processor.js.map +1 -1
- package/dist/errors/index.d.ts +94 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +149 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -1
- package/dist/loader/component_loader.d.ts +128 -0
- package/dist/loader/component_loader.d.ts.map +1 -0
- package/dist/loader/component_loader.js +330 -0
- package/dist/loader/component_loader.js.map +1 -0
- package/dist/loader/index.d.ts +19 -0
- package/dist/loader/index.d.ts.map +1 -0
- package/dist/loader/index.js +19 -0
- package/dist/loader/index.js.map +1 -0
- package/dist/storage/adapters/local_storage_service.d.ts +6 -0
- package/dist/storage/adapters/local_storage_service.d.ts.map +1 -1
- package/dist/storage/adapters/local_storage_service.js +26 -4
- package/dist/storage/adapters/local_storage_service.js.map +1 -1
- package/dist/storage/adapters/ovh_storage_service.d.ts.map +1 -1
- package/dist/storage/adapters/ovh_storage_service.js +5 -6
- package/dist/storage/adapters/ovh_storage_service.js.map +1 -1
- package/dist/storage/storage_factory.d.ts.map +1 -1
- package/dist/storage/storage_factory.js +4 -1
- package/dist/storage/storage_factory.js.map +1 -1
- package/dist/storage/storage_service.d.ts.map +1 -1
- package/dist/storage/storage_service.js +6 -2
- package/dist/storage/storage_service.js.map +1 -1
- package/dist/types/http.d.ts +156 -0
- package/dist/types/http.d.ts.map +1 -0
- package/dist/types/http.js +8 -0
- package/dist/types/http.js.map +1 -0
- package/dist/utils/graceful_shutdown.d.ts +44 -0
- package/dist/utils/graceful_shutdown.d.ts.map +1 -0
- package/dist/utils/graceful_shutdown.js +79 -0
- package/dist/utils/graceful_shutdown.js.map +1 -0
- package/dist/utils/http_responses.d.ts +20 -0
- package/dist/utils/http_responses.d.ts.map +1 -1
- package/dist/utils/http_responses.js +28 -2
- package/dist/utils/http_responses.js.map +1 -1
- package/dist/utils/logger.d.ts +8 -8
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +8 -8
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/safe_async.d.ts +50 -0
- package/dist/utils/safe_async.d.ts.map +1 -0
- package/dist/utils/safe_async.js +90 -0
- package/dist/utils/safe_async.js.map +1 -0
- package/dist/validation/index.d.ts +3 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +7 -0
- package/dist/validation/index.js.map +1 -0
- package/dist/validation/schemas.d.ts +273 -0
- package/dist/validation/schemas.d.ts.map +1 -0
- package/dist/validation/schemas.js +82 -0
- package/dist/validation/schemas.js.map +1 -0
- package/dist/validation/validate.d.ts +49 -0
- package/dist/validation/validate.d.ts.map +1 -0
- package/dist/validation/validate.js +110 -0
- package/dist/validation/validate.js.map +1 -0
- package/package.json +14 -8
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@ Digital Twin Core is a minimalist TypeScript framework used to collect and proce
|
|
|
12
12
|
- **Storage adapters** – currently local filesystem and OVH Object Storage via S3 API.
|
|
13
13
|
- **Database adapter** – implemented with [Knex](https://knexjs.org/) to index metadata.
|
|
14
14
|
- **Engine** – orchestrates components, schedules jobs with BullMQ and exposes endpoints via Express.
|
|
15
|
+
- **Authentication** – pluggable authentication system supporting API gateway headers, JWT tokens, or no-auth mode.
|
|
15
16
|
|
|
16
17
|
## Installation
|
|
17
18
|
|
|
@@ -65,6 +66,70 @@ const engine = new DigitalTwinEngine({ storage, database });
|
|
|
65
66
|
engine.start();
|
|
66
67
|
```
|
|
67
68
|
|
|
69
|
+
## Developer Experience
|
|
70
|
+
|
|
71
|
+
### Auto-Discovery
|
|
72
|
+
|
|
73
|
+
The framework provides automatic component discovery to reduce boilerplate:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { DigitalTwinEngine, loadComponents } from 'digitaltwin-core'
|
|
77
|
+
|
|
78
|
+
// Before: manual imports for each component
|
|
79
|
+
// import { WeatherCollector } from './components/weather_collector.js'
|
|
80
|
+
// import { SensorCollector } from './components/sensor_collector.js'
|
|
81
|
+
// ...
|
|
82
|
+
|
|
83
|
+
// After: automatic discovery
|
|
84
|
+
const result = await loadComponents('./dist/components')
|
|
85
|
+
|
|
86
|
+
const engine = new DigitalTwinEngine({ storage, database })
|
|
87
|
+
engine.registerComponents(result)
|
|
88
|
+
await engine.start()
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The loader discovers components based on file naming conventions:
|
|
92
|
+
- `*_collector.js` → Collectors
|
|
93
|
+
- `*_harvester.js` → Harvesters
|
|
94
|
+
- `*_handler.js` → Handlers
|
|
95
|
+
- `*_assets_manager.js` → Assets Managers
|
|
96
|
+
- `*_custom_table.js` → Custom Table Managers
|
|
97
|
+
|
|
98
|
+
### Loading Options
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
const result = await loadComponents('./dist/components', {
|
|
102
|
+
recursive: true, // Scan subdirectories (default)
|
|
103
|
+
verbose: true, // Log discovered components
|
|
104
|
+
extensions: ['.js'], // File extensions
|
|
105
|
+
exclude: ['*.test.*'] // Exclusion patterns
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
// Result includes metadata
|
|
109
|
+
console.log(result.summary)
|
|
110
|
+
// { total: 5, collectors: 2, harvesters: 1, handlers: 2, errors: 0 }
|
|
111
|
+
|
|
112
|
+
// Check for loading errors
|
|
113
|
+
if (result.errors.length > 0) {
|
|
114
|
+
console.warn('Some components failed to load:', result.errors)
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Type Guards
|
|
119
|
+
|
|
120
|
+
Runtime type detection utilities:
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { isCollector, isHandler, detectComponentType } from 'digitaltwin-core'
|
|
124
|
+
|
|
125
|
+
if (isCollector(component)) {
|
|
126
|
+
// TypeScript narrowing works here
|
|
127
|
+
await component.collect()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const type = detectComponentType(component) // 'collector' | 'handler' | ...
|
|
131
|
+
```
|
|
132
|
+
|
|
68
133
|
## Components
|
|
69
134
|
|
|
70
135
|
### Collectors
|
|
@@ -326,6 +391,153 @@ class WMSLayersManager extends CustomTableManager {
|
|
|
326
391
|
- `update(id, data)` - Update existing record
|
|
327
392
|
- `delete(id)` - Delete record
|
|
328
393
|
|
|
394
|
+
## Request Validation
|
|
395
|
+
|
|
396
|
+
The framework includes VineJS integration for type-safe request validation:
|
|
397
|
+
|
|
398
|
+
```typescript
|
|
399
|
+
import { validate, AssetUploadSchema } from 'digitaltwin-core'
|
|
400
|
+
|
|
401
|
+
// In your component
|
|
402
|
+
async handleUpload(req: any) {
|
|
403
|
+
const data = await validate(AssetUploadSchema, req.body)
|
|
404
|
+
// data is now typed and validated
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
Validation errors return HTTP 422 with detailed error messages:
|
|
409
|
+
|
|
410
|
+
```json
|
|
411
|
+
{
|
|
412
|
+
"error": "Validation failed",
|
|
413
|
+
"details": [
|
|
414
|
+
{ "field": "description", "message": "The description field must be a string" }
|
|
415
|
+
]
|
|
416
|
+
}
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
## Error Handling
|
|
420
|
+
|
|
421
|
+
The framework provides custom error classes for structured error handling:
|
|
422
|
+
|
|
423
|
+
```typescript
|
|
424
|
+
import { CollectorError, ValidationError, StorageError } from 'digitaltwin-core'
|
|
425
|
+
|
|
426
|
+
// Errors include context
|
|
427
|
+
throw new CollectorError('Failed to fetch data', 'weather-collector', originalError)
|
|
428
|
+
|
|
429
|
+
// Validation errors return 422
|
|
430
|
+
throw new ValidationError('Invalid input', details)
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
All component errors are caught and logged with context (component name, stack trace). Non-critical operations use `safeAsync` to log errors without crashing:
|
|
434
|
+
|
|
435
|
+
```typescript
|
|
436
|
+
import { safeAsync } from 'digitaltwin-core'
|
|
437
|
+
|
|
438
|
+
// Won't throw, just logs on failure
|
|
439
|
+
await safeAsync(() => cleanup(), 'cleanup temporary files', logger)
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
## Production Features
|
|
443
|
+
|
|
444
|
+
### Graceful Shutdown
|
|
445
|
+
|
|
446
|
+
The engine supports graceful shutdown with configurable timeout:
|
|
447
|
+
|
|
448
|
+
```typescript
|
|
449
|
+
const engine = new DigitalTwinEngine({ database, storage })
|
|
450
|
+
|
|
451
|
+
// Configure shutdown timeout (default: 30s)
|
|
452
|
+
engine.setShutdownTimeout(60000)
|
|
453
|
+
|
|
454
|
+
// Check if shutting down
|
|
455
|
+
if (engine.isShuttingDown()) {
|
|
456
|
+
// Don't accept new work
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// Graceful stop
|
|
460
|
+
await engine.stop()
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
### Health Checks
|
|
464
|
+
|
|
465
|
+
Register custom health checks for monitoring:
|
|
466
|
+
|
|
467
|
+
```typescript
|
|
468
|
+
engine.registerHealthCheck('external-api', async () => {
|
|
469
|
+
const response = await fetch('https://api.example.com/health')
|
|
470
|
+
return { status: response.ok ? 'up' : 'down' }
|
|
471
|
+
})
|
|
472
|
+
|
|
473
|
+
// Built-in checks: database, redis (if configured)
|
|
474
|
+
const names = engine.getHealthCheckNames() // ['database', 'redis', 'external-api']
|
|
475
|
+
|
|
476
|
+
// Remove check
|
|
477
|
+
engine.removeHealthCheck('external-api')
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
### OpenAPI Specification
|
|
481
|
+
|
|
482
|
+
Generate OpenAPI 3.0 specs from your components:
|
|
483
|
+
|
|
484
|
+
```typescript
|
|
485
|
+
import { OpenAPIGenerator } from 'digitaltwin-core'
|
|
486
|
+
|
|
487
|
+
const spec = OpenAPIGenerator.generate({
|
|
488
|
+
info: { title: 'My API', version: '1.0.0' },
|
|
489
|
+
components: [collector, assetsManager, handler]
|
|
490
|
+
})
|
|
491
|
+
|
|
492
|
+
// Output as JSON or YAML
|
|
493
|
+
const json = OpenAPIGenerator.toJSON(spec)
|
|
494
|
+
const yaml = OpenAPIGenerator.toYAML(spec)
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
## Server Configuration
|
|
498
|
+
|
|
499
|
+
### HTTP Compression
|
|
500
|
+
|
|
501
|
+
HTTP compression is **disabled by default** because API gateways (Apache APISIX, Kong, Nginx, etc.) typically handle compression at the gateway level.
|
|
502
|
+
|
|
503
|
+
For standalone deployments without a gateway, enable compression via environment variable:
|
|
504
|
+
|
|
505
|
+
```bash
|
|
506
|
+
export DIGITALTWIN_ENABLE_COMPRESSION=true
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
When enabled, the server uses gzip compression for JSON responses larger than 1KB, reducing bandwidth by 60-80%.
|
|
510
|
+
|
|
511
|
+
## Authentication
|
|
512
|
+
|
|
513
|
+
The framework supports multiple authentication modes:
|
|
514
|
+
|
|
515
|
+
- **Gateway** (default): Uses headers from API gateways (Apache APISIX, KrakenD)
|
|
516
|
+
- **JWT**: Direct JWT token validation
|
|
517
|
+
- **None**: Disabled for development/testing
|
|
518
|
+
|
|
519
|
+
### Gateway Mode (Default)
|
|
520
|
+
|
|
521
|
+
No configuration needed. The framework reads `x-user-id` and `x-user-roles` headers set by your API gateway.
|
|
522
|
+
|
|
523
|
+
### JWT Mode
|
|
524
|
+
|
|
525
|
+
```bash
|
|
526
|
+
export AUTH_MODE=jwt
|
|
527
|
+
export JWT_SECRET=your-secret-key
|
|
528
|
+
# Or for RSA: JWT_PUBLIC_KEY or JWT_PUBLIC_KEY_FILE
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
### Disable Authentication
|
|
532
|
+
|
|
533
|
+
```bash
|
|
534
|
+
export DIGITALTWIN_DISABLE_AUTH=true
|
|
535
|
+
# Or
|
|
536
|
+
export AUTH_MODE=none
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
For detailed configuration options, see [src/auth/README.md](src/auth/README.md).
|
|
540
|
+
|
|
329
541
|
## Project Scaffolding
|
|
330
542
|
|
|
331
543
|
Use [create-digitaltwin](https://github.com/CePseudoBE/create-digitaltwin) to quickly bootstrap new projects:
|
|
@@ -348,11 +560,16 @@ node dt make:harvester DataProcessor --source weather-collector
|
|
|
348
560
|
## Folder structure
|
|
349
561
|
|
|
350
562
|
- `src/` – framework sources
|
|
563
|
+
- `auth/` – authentication providers and user management
|
|
351
564
|
- `components/` – base classes for collectors, harvesters, handlers and assets manager
|
|
352
|
-
- `engine/` – orchestration logic
|
|
565
|
+
- `engine/` – orchestration logic (includes component type guards)
|
|
566
|
+
- `loader/` – component auto-discovery utilities
|
|
353
567
|
- `storage/` – storage service abstractions and adapters
|
|
354
568
|
- `database/` – metadata database adapter
|
|
355
569
|
- `env/` – environment configuration helper
|
|
570
|
+
- `errors/` – custom error classes
|
|
571
|
+
- `validation/` – request validation with VineJS
|
|
572
|
+
- `utils/` – utility functions (graceful shutdown, HTTP responses, etc.)
|
|
356
573
|
- `tests/` – unit tests
|
|
357
574
|
|
|
358
575
|
---
|
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
import type { AuthenticatedUser } from './types.js';
|
|
2
|
+
import type { AuthProvider } from './auth_provider.js';
|
|
3
|
+
/**
|
|
4
|
+
* Headers type that accepts both Express IncomingHttpHeaders and Record<string, string>
|
|
5
|
+
*/
|
|
6
|
+
export type HeadersLike = Record<string, string | string[] | undefined>;
|
|
2
7
|
/**
|
|
3
8
|
* Parses authentication information from Apache APISIX headers set after Keycloak authentication.
|
|
4
9
|
*
|
|
5
|
-
* This class
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
+
* This class provides a static API for backward compatibility while internally using
|
|
11
|
+
* the AuthProvider system. It automatically handles:
|
|
12
|
+
* - Gateway mode (x-user-id, x-user-roles headers)
|
|
13
|
+
* - JWT mode (Authorization: Bearer token)
|
|
14
|
+
* - No-auth mode (DIGITALTWIN_DISABLE_AUTH=true)
|
|
10
15
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
16
|
+
* For new code, consider using AuthProviderFactory directly:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const authProvider = AuthProviderFactory.fromEnv()
|
|
19
|
+
* const user = authProvider.parseRequest(req)
|
|
20
|
+
* ```
|
|
14
21
|
*
|
|
15
22
|
* @example
|
|
16
23
|
* ```typescript
|
|
@@ -24,18 +31,39 @@ import type { AuthenticatedUser } from './types.js';
|
|
|
24
31
|
* ```
|
|
25
32
|
*/
|
|
26
33
|
export declare class ApisixAuthParser {
|
|
34
|
+
private static _provider;
|
|
35
|
+
/**
|
|
36
|
+
* Get the authentication provider instance.
|
|
37
|
+
* Creates it on first use based on environment configuration.
|
|
38
|
+
*/
|
|
39
|
+
private static getProvider;
|
|
40
|
+
/**
|
|
41
|
+
* Reset the provider instance (useful for testing).
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
static _resetProvider(): void;
|
|
45
|
+
/**
|
|
46
|
+
* Set a custom provider (useful for testing).
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
static _setProvider(provider: AuthProvider): void;
|
|
50
|
+
/**
|
|
51
|
+
* Create a request-like object from headers for the AuthProvider.
|
|
52
|
+
* Normalizes headers by taking only the first value for array headers.
|
|
53
|
+
*/
|
|
54
|
+
private static toAuthRequest;
|
|
27
55
|
/**
|
|
28
|
-
* Extracts user information from
|
|
56
|
+
* Extracts user information from authentication headers.
|
|
29
57
|
*
|
|
30
|
-
* Parses the authentication headers
|
|
31
|
-
* - `x-user-id
|
|
32
|
-
* - `
|
|
58
|
+
* Parses the authentication headers (gateway mode) or JWT token (jwt mode):
|
|
59
|
+
* - Gateway: `x-user-id` and `x-user-roles` headers
|
|
60
|
+
* - JWT: `Authorization: Bearer <token>` header
|
|
33
61
|
*
|
|
34
62
|
* When authentication is disabled (DIGITALTWIN_DISABLE_AUTH=true),
|
|
35
|
-
* returns a default anonymous user
|
|
63
|
+
* returns a default anonymous user.
|
|
36
64
|
*
|
|
37
|
-
* @param headers - HTTP request headers
|
|
38
|
-
* @returns Parsed user authentication data, or null if
|
|
65
|
+
* @param headers - HTTP request headers
|
|
66
|
+
* @returns Parsed user authentication data, or null if not authenticated
|
|
39
67
|
*
|
|
40
68
|
* @example
|
|
41
69
|
* ```typescript
|
|
@@ -46,48 +74,35 @@ export declare class ApisixAuthParser {
|
|
|
46
74
|
*
|
|
47
75
|
* const authUser = ApisixAuthParser.parseAuthHeaders(headers)
|
|
48
76
|
* // Returns: { id: '6e06a527...', roles: ['default-roles-master', 'offline_access'] }
|
|
49
|
-
*
|
|
50
|
-
* // With DIGITALTWIN_DISABLE_AUTH=true
|
|
51
|
-
* const authUser = ApisixAuthParser.parseAuthHeaders({})
|
|
52
|
-
* // Returns: { id: 'anonymous', roles: ['anonymous'] }
|
|
53
77
|
* ```
|
|
54
78
|
*/
|
|
55
|
-
static parseAuthHeaders(headers:
|
|
79
|
+
static parseAuthHeaders(headers: HeadersLike): AuthenticatedUser | null;
|
|
56
80
|
/**
|
|
57
|
-
* Checks if a request has valid authentication
|
|
81
|
+
* Checks if a request has valid authentication.
|
|
58
82
|
*
|
|
59
83
|
* Performs a quick validation to determine if the request contains
|
|
60
|
-
*
|
|
61
|
-
* Use this for early authentication checks before parsing.
|
|
84
|
+
* valid authentication credentials (gateway headers or JWT token).
|
|
62
85
|
*
|
|
63
|
-
* When authentication is disabled
|
|
64
|
-
* this always returns true to allow all requests through.
|
|
86
|
+
* When authentication is disabled, this always returns true.
|
|
65
87
|
*
|
|
66
88
|
* @param headers - HTTP request headers
|
|
67
|
-
* @returns true if
|
|
89
|
+
* @returns true if authentication is valid or disabled, false otherwise
|
|
68
90
|
*
|
|
69
91
|
* @example
|
|
70
92
|
* ```typescript
|
|
71
|
-
* // Early authentication check in handler
|
|
72
93
|
* if (!ApisixAuthParser.hasValidAuth(req.headers)) {
|
|
73
94
|
* return { status: 401, content: 'Authentication required' }
|
|
74
95
|
* }
|
|
75
|
-
*
|
|
76
|
-
* // Now safe to proceed with parsing
|
|
77
|
-
* const authUser = ApisixAuthParser.parseAuthHeaders(req.headers)
|
|
78
96
|
* ```
|
|
79
97
|
*/
|
|
80
|
-
static hasValidAuth(headers:
|
|
98
|
+
static hasValidAuth(headers: HeadersLike): boolean;
|
|
81
99
|
/**
|
|
82
100
|
* Extracts just the user ID from headers.
|
|
83
101
|
*
|
|
84
|
-
* Convenience method for cases where you only need the user ID
|
|
85
|
-
* without parsing the full authentication context.
|
|
86
|
-
*
|
|
87
|
-
* When authentication is disabled, returns the configured anonymous user ID.
|
|
102
|
+
* Convenience method for cases where you only need the user ID.
|
|
88
103
|
*
|
|
89
104
|
* @param headers - HTTP request headers
|
|
90
|
-
* @returns
|
|
105
|
+
* @returns User ID, or null if not authenticated
|
|
91
106
|
*
|
|
92
107
|
* @example
|
|
93
108
|
* ```typescript
|
|
@@ -97,17 +112,12 @@ export declare class ApisixAuthParser {
|
|
|
97
112
|
* }
|
|
98
113
|
* ```
|
|
99
114
|
*/
|
|
100
|
-
static getUserId(headers:
|
|
115
|
+
static getUserId(headers: HeadersLike): string | null;
|
|
101
116
|
/**
|
|
102
117
|
* Extracts just the user roles from headers.
|
|
103
118
|
*
|
|
104
|
-
* Convenience method for cases where you only need the user roles
|
|
105
|
-
* without parsing the full authentication context.
|
|
106
|
-
*
|
|
107
|
-
* When authentication is disabled, returns the anonymous user roles.
|
|
108
|
-
*
|
|
109
119
|
* @param headers - HTTP request headers
|
|
110
|
-
* @returns Array of role names,
|
|
120
|
+
* @returns Array of role names, empty array if not authenticated
|
|
111
121
|
*
|
|
112
122
|
* @example
|
|
113
123
|
* ```typescript
|
|
@@ -117,30 +127,20 @@ export declare class ApisixAuthParser {
|
|
|
117
127
|
* }
|
|
118
128
|
* ```
|
|
119
129
|
*/
|
|
120
|
-
static getUserRoles(headers:
|
|
130
|
+
static getUserRoles(headers: HeadersLike): string[];
|
|
121
131
|
/**
|
|
122
132
|
* Checks if a user has the admin role.
|
|
123
133
|
*
|
|
124
|
-
* Determines if the authenticated user has administrative privileges by checking
|
|
125
|
-
* if their roles include the configured admin role name (default: "admin").
|
|
126
|
-
*
|
|
127
|
-
* The admin role name can be configured via DIGITALTWIN_ADMIN_ROLE_NAME environment variable.
|
|
128
|
-
*
|
|
129
134
|
* @param headers - HTTP request headers
|
|
130
135
|
* @returns true if user has admin role, false otherwise
|
|
131
136
|
*
|
|
132
137
|
* @example
|
|
133
138
|
* ```typescript
|
|
134
139
|
* if (ApisixAuthParser.isAdmin(req.headers)) {
|
|
135
|
-
* //
|
|
136
|
-
* // Can view all assets including private assets owned by others
|
|
137
|
-
* console.log('Admin user detected')
|
|
140
|
+
* // Admin-only logic
|
|
138
141
|
* }
|
|
139
|
-
*
|
|
140
|
-
* // With custom admin role name (DIGITALTWIN_ADMIN_ROLE_NAME=administrator)
|
|
141
|
-
* const isAdmin = ApisixAuthParser.isAdmin(req.headers)
|
|
142
142
|
* ```
|
|
143
143
|
*/
|
|
144
|
-
static isAdmin(headers:
|
|
144
|
+
static isAdmin(headers: HeadersLike): boolean;
|
|
145
145
|
}
|
|
146
146
|
//# sourceMappingURL=apisix_parser.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apisix_parser.d.ts","sourceRoot":"","sources":["../../src/auth/apisix_parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"apisix_parser.d.ts","sourceRoot":"","sources":["../../src/auth/apisix_parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AACnD,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,oBAAoB,CAAA;AAGnE;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAA;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,gBAAgB;IACzB,OAAO,CAAC,MAAM,CAAC,SAAS,CAA4B;IAEpD;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,WAAW;IAO1B;;;OAGG;IACH,MAAM,CAAC,cAAc,IAAI,IAAI;IAI7B;;;OAGG;IACH,MAAM,CAAC,YAAY,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;IAIjD;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,aAAa;IAY5B;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,WAAW,GAAG,iBAAiB,GAAG,IAAI;IAIvE;;;;;;;;;;;;;;;;;OAiBG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO;IAIlD;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,GAAG,IAAI;IAIrD;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,EAAE;IAInD;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO;CAGhD"}
|