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
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AuthProviderFactory } from './auth_provider_factory.js';
|
|
2
2
|
/**
|
|
3
3
|
* Parses authentication information from Apache APISIX headers set after Keycloak authentication.
|
|
4
4
|
*
|
|
5
|
-
* This class
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* This class provides a static API for backward compatibility while internally using
|
|
6
|
+
* the AuthProvider system. It automatically handles:
|
|
7
|
+
* - Gateway mode (x-user-id, x-user-roles headers)
|
|
8
|
+
* - JWT mode (Authorization: Bearer token)
|
|
9
|
+
* - No-auth mode (DIGITALTWIN_DISABLE_AUTH=true)
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* For new code, consider using AuthProviderFactory directly:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const authProvider = AuthProviderFactory.fromEnv()
|
|
14
|
+
* const user = authProvider.parseRequest(req)
|
|
15
|
+
* ```
|
|
14
16
|
*
|
|
15
17
|
* @example
|
|
16
18
|
* ```typescript
|
|
@@ -24,18 +26,59 @@ import { AuthConfig } from './auth_config.js';
|
|
|
24
26
|
* ```
|
|
25
27
|
*/
|
|
26
28
|
export class ApisixAuthParser {
|
|
29
|
+
static { this._provider = null; }
|
|
30
|
+
/**
|
|
31
|
+
* Get the authentication provider instance.
|
|
32
|
+
* Creates it on first use based on environment configuration.
|
|
33
|
+
*/
|
|
34
|
+
static getProvider() {
|
|
35
|
+
if (!this._provider) {
|
|
36
|
+
this._provider = AuthProviderFactory.fromEnv();
|
|
37
|
+
}
|
|
38
|
+
return this._provider;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Reset the provider instance (useful for testing).
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
static _resetProvider() {
|
|
45
|
+
this._provider = null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Set a custom provider (useful for testing).
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
static _setProvider(provider) {
|
|
52
|
+
this._provider = provider;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create a request-like object from headers for the AuthProvider.
|
|
56
|
+
* Normalizes headers by taking only the first value for array headers.
|
|
57
|
+
*/
|
|
58
|
+
static toAuthRequest(headers) {
|
|
59
|
+
const normalizedHeaders = {};
|
|
60
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
61
|
+
if (typeof value === 'string') {
|
|
62
|
+
normalizedHeaders[key] = value;
|
|
63
|
+
}
|
|
64
|
+
else if (Array.isArray(value) && value.length > 0) {
|
|
65
|
+
normalizedHeaders[key] = value[0];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { headers: normalizedHeaders };
|
|
69
|
+
}
|
|
27
70
|
/**
|
|
28
|
-
* Extracts user information from
|
|
71
|
+
* Extracts user information from authentication headers.
|
|
29
72
|
*
|
|
30
|
-
* Parses the authentication headers
|
|
31
|
-
* - `x-user-id
|
|
32
|
-
* - `
|
|
73
|
+
* Parses the authentication headers (gateway mode) or JWT token (jwt mode):
|
|
74
|
+
* - Gateway: `x-user-id` and `x-user-roles` headers
|
|
75
|
+
* - JWT: `Authorization: Bearer <token>` header
|
|
33
76
|
*
|
|
34
77
|
* When authentication is disabled (DIGITALTWIN_DISABLE_AUTH=true),
|
|
35
|
-
* returns a default anonymous user
|
|
78
|
+
* returns a default anonymous user.
|
|
36
79
|
*
|
|
37
|
-
* @param headers - HTTP request headers
|
|
38
|
-
* @returns Parsed user authentication data, or null if
|
|
80
|
+
* @param headers - HTTP request headers
|
|
81
|
+
* @returns Parsed user authentication data, or null if not authenticated
|
|
39
82
|
*
|
|
40
83
|
* @example
|
|
41
84
|
* ```typescript
|
|
@@ -46,70 +89,39 @@ export class ApisixAuthParser {
|
|
|
46
89
|
*
|
|
47
90
|
* const authUser = ApisixAuthParser.parseAuthHeaders(headers)
|
|
48
91
|
* // 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
92
|
* ```
|
|
54
93
|
*/
|
|
55
94
|
static parseAuthHeaders(headers) {
|
|
56
|
-
|
|
57
|
-
if (AuthConfig.isAuthDisabled()) {
|
|
58
|
-
return AuthConfig.getAnonymousUser();
|
|
59
|
-
}
|
|
60
|
-
const userId = headers['x-user-id'];
|
|
61
|
-
if (!userId) {
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
// Parse roles from comma-separated string
|
|
65
|
-
const rolesString = headers['x-user-roles'] || '';
|
|
66
|
-
const roles = rolesString ? rolesString.split(',').map(role => role.trim()) : [];
|
|
67
|
-
return {
|
|
68
|
-
id: userId,
|
|
69
|
-
roles: roles
|
|
70
|
-
};
|
|
95
|
+
return this.getProvider().parseRequest(this.toAuthRequest(headers));
|
|
71
96
|
}
|
|
72
97
|
/**
|
|
73
|
-
* Checks if a request has valid authentication
|
|
98
|
+
* Checks if a request has valid authentication.
|
|
74
99
|
*
|
|
75
100
|
* Performs a quick validation to determine if the request contains
|
|
76
|
-
*
|
|
77
|
-
* Use this for early authentication checks before parsing.
|
|
101
|
+
* valid authentication credentials (gateway headers or JWT token).
|
|
78
102
|
*
|
|
79
|
-
* When authentication is disabled
|
|
80
|
-
* this always returns true to allow all requests through.
|
|
103
|
+
* When authentication is disabled, this always returns true.
|
|
81
104
|
*
|
|
82
105
|
* @param headers - HTTP request headers
|
|
83
|
-
* @returns true if
|
|
106
|
+
* @returns true if authentication is valid or disabled, false otherwise
|
|
84
107
|
*
|
|
85
108
|
* @example
|
|
86
109
|
* ```typescript
|
|
87
|
-
* // Early authentication check in handler
|
|
88
110
|
* if (!ApisixAuthParser.hasValidAuth(req.headers)) {
|
|
89
111
|
* return { status: 401, content: 'Authentication required' }
|
|
90
112
|
* }
|
|
91
|
-
*
|
|
92
|
-
* // Now safe to proceed with parsing
|
|
93
|
-
* const authUser = ApisixAuthParser.parseAuthHeaders(req.headers)
|
|
94
113
|
* ```
|
|
95
114
|
*/
|
|
96
115
|
static hasValidAuth(headers) {
|
|
97
|
-
|
|
98
|
-
if (AuthConfig.isAuthDisabled()) {
|
|
99
|
-
return true;
|
|
100
|
-
}
|
|
101
|
-
return !!headers['x-user-id'];
|
|
116
|
+
return this.getProvider().hasValidAuth(this.toAuthRequest(headers));
|
|
102
117
|
}
|
|
103
118
|
/**
|
|
104
119
|
* Extracts just the user ID from headers.
|
|
105
120
|
*
|
|
106
|
-
* Convenience method for cases where you only need the user ID
|
|
107
|
-
* without parsing the full authentication context.
|
|
108
|
-
*
|
|
109
|
-
* When authentication is disabled, returns the configured anonymous user ID.
|
|
121
|
+
* Convenience method for cases where you only need the user ID.
|
|
110
122
|
*
|
|
111
123
|
* @param headers - HTTP request headers
|
|
112
|
-
* @returns
|
|
124
|
+
* @returns User ID, or null if not authenticated
|
|
113
125
|
*
|
|
114
126
|
* @example
|
|
115
127
|
* ```typescript
|
|
@@ -120,22 +132,13 @@ export class ApisixAuthParser {
|
|
|
120
132
|
* ```
|
|
121
133
|
*/
|
|
122
134
|
static getUserId(headers) {
|
|
123
|
-
|
|
124
|
-
if (AuthConfig.isAuthDisabled()) {
|
|
125
|
-
return AuthConfig.getAnonymousUserId();
|
|
126
|
-
}
|
|
127
|
-
return headers['x-user-id'] || null;
|
|
135
|
+
return this.getProvider().getUserId(this.toAuthRequest(headers));
|
|
128
136
|
}
|
|
129
137
|
/**
|
|
130
138
|
* Extracts just the user roles from headers.
|
|
131
139
|
*
|
|
132
|
-
* Convenience method for cases where you only need the user roles
|
|
133
|
-
* without parsing the full authentication context.
|
|
134
|
-
*
|
|
135
|
-
* When authentication is disabled, returns the anonymous user roles.
|
|
136
|
-
*
|
|
137
140
|
* @param headers - HTTP request headers
|
|
138
|
-
* @returns Array of role names,
|
|
141
|
+
* @returns Array of role names, empty array if not authenticated
|
|
139
142
|
*
|
|
140
143
|
* @example
|
|
141
144
|
* ```typescript
|
|
@@ -146,40 +149,23 @@ export class ApisixAuthParser {
|
|
|
146
149
|
* ```
|
|
147
150
|
*/
|
|
148
151
|
static getUserRoles(headers) {
|
|
149
|
-
|
|
150
|
-
if (AuthConfig.isAuthDisabled()) {
|
|
151
|
-
return AuthConfig.getAnonymousUser().roles;
|
|
152
|
-
}
|
|
153
|
-
const rolesString = headers['x-user-roles'] || '';
|
|
154
|
-
return rolesString ? rolesString.split(',').map(role => role.trim()) : [];
|
|
152
|
+
return this.getProvider().getUserRoles(this.toAuthRequest(headers));
|
|
155
153
|
}
|
|
156
154
|
/**
|
|
157
155
|
* Checks if a user has the admin role.
|
|
158
156
|
*
|
|
159
|
-
* Determines if the authenticated user has administrative privileges by checking
|
|
160
|
-
* if their roles include the configured admin role name (default: "admin").
|
|
161
|
-
*
|
|
162
|
-
* The admin role name can be configured via DIGITALTWIN_ADMIN_ROLE_NAME environment variable.
|
|
163
|
-
*
|
|
164
157
|
* @param headers - HTTP request headers
|
|
165
158
|
* @returns true if user has admin role, false otherwise
|
|
166
159
|
*
|
|
167
160
|
* @example
|
|
168
161
|
* ```typescript
|
|
169
162
|
* if (ApisixAuthParser.isAdmin(req.headers)) {
|
|
170
|
-
* //
|
|
171
|
-
* // Can view all assets including private assets owned by others
|
|
172
|
-
* console.log('Admin user detected')
|
|
163
|
+
* // Admin-only logic
|
|
173
164
|
* }
|
|
174
|
-
*
|
|
175
|
-
* // With custom admin role name (DIGITALTWIN_ADMIN_ROLE_NAME=administrator)
|
|
176
|
-
* const isAdmin = ApisixAuthParser.isAdmin(req.headers)
|
|
177
165
|
* ```
|
|
178
166
|
*/
|
|
179
167
|
static isAdmin(headers) {
|
|
180
|
-
|
|
181
|
-
const adminRoleName = AuthConfig.getAdminRoleName();
|
|
182
|
-
return roles.includes(adminRoleName);
|
|
168
|
+
return this.getProvider().isAdmin(this.toAuthRequest(headers));
|
|
183
169
|
}
|
|
184
170
|
}
|
|
185
171
|
//# sourceMappingURL=apisix_parser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apisix_parser.js","sourceRoot":"","sources":["../../src/auth/apisix_parser.ts"],"names":[],"mappings":"
|
|
1
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ACH,MAAM,CAAC,gBAAgB,CAAC,OAAoB;QACxC,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAA;IACvE,CAAC;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,MAAM,CAAC,YAAY,CAAC,OAAoB;QACpC,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAA;IACvE,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,SAAS,CAAC,OAAoB;QACjC,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAA;IACpE,CAAC;IAED;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,YAAY,CAAC,OAAoB;QACpC,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAA;IACvE,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,OAAO,CAAC,OAAoB;QAC/B,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAA;IAClE,CAAC"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Authentication provider interface and types for the Digital Twin framework.
|
|
3
|
+
*
|
|
4
|
+
* This module defines the contract for authentication providers, allowing the framework
|
|
5
|
+
* to support multiple authentication mechanisms (gateway headers, JWT tokens, etc.).
|
|
6
|
+
*/
|
|
7
|
+
import type { AuthenticatedUser } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Authentication mode for the Digital Twin framework.
|
|
10
|
+
*
|
|
11
|
+
* - `gateway`: Parse authentication from gateway headers (x-user-id, x-user-roles)
|
|
12
|
+
* - `jwt`: Validate JWT tokens from Authorization header
|
|
13
|
+
* - `none`: Disable authentication (development/testing only)
|
|
14
|
+
*/
|
|
15
|
+
export type AuthMode = 'gateway' | 'jwt' | 'none';
|
|
16
|
+
/**
|
|
17
|
+
* JWT-specific configuration options.
|
|
18
|
+
*/
|
|
19
|
+
export interface JwtConfig {
|
|
20
|
+
/** Secret key for HMAC algorithms (HS256, HS384, HS512) */
|
|
21
|
+
secret?: string;
|
|
22
|
+
/** Public key for RSA/EC algorithms (RS256, RS384, RS512, ES256, ES384, ES512) */
|
|
23
|
+
publicKey?: string;
|
|
24
|
+
/** JWT algorithm (default: 'HS256') */
|
|
25
|
+
algorithm?: string;
|
|
26
|
+
/** Expected token issuer for validation */
|
|
27
|
+
issuer?: string;
|
|
28
|
+
/** Expected token audience for validation */
|
|
29
|
+
audience?: string;
|
|
30
|
+
/** Claim name for user ID (default: 'sub') */
|
|
31
|
+
userIdClaim?: string;
|
|
32
|
+
/** Claim name for roles (default: 'roles', supports nested paths like 'realm_access.roles') */
|
|
33
|
+
rolesClaim?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Authentication configuration for the Digital Twin framework.
|
|
37
|
+
*/
|
|
38
|
+
export interface AuthProviderConfig {
|
|
39
|
+
/** Authentication mode */
|
|
40
|
+
mode: AuthMode;
|
|
41
|
+
/** Name of the admin role (default: 'admin') */
|
|
42
|
+
adminRoleName?: string;
|
|
43
|
+
/** JWT-specific configuration (required when mode is 'jwt') */
|
|
44
|
+
jwt?: JwtConfig;
|
|
45
|
+
/** Anonymous user ID for 'none' mode (default: 'anonymous') */
|
|
46
|
+
anonymousUserId?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Request-like object for authentication parsing.
|
|
50
|
+
*
|
|
51
|
+
* This interface allows the auth provider to work with any request object
|
|
52
|
+
* that has headers, without requiring a full Express Request.
|
|
53
|
+
*/
|
|
54
|
+
export interface AuthRequest {
|
|
55
|
+
/** Request headers */
|
|
56
|
+
headers: Record<string, string | string[] | undefined>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Authentication provider interface.
|
|
60
|
+
*
|
|
61
|
+
* Implement this interface to create custom authentication mechanisms.
|
|
62
|
+
* The framework provides three built-in providers:
|
|
63
|
+
* - GatewayAuthProvider: For API gateway authentication (Apache APISIX, KrakenD)
|
|
64
|
+
* - JwtAuthProvider: For direct JWT token validation
|
|
65
|
+
* - NoAuthProvider: For development/testing without authentication
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* // Using the factory (recommended)
|
|
70
|
+
* const provider = AuthProviderFactory.fromEnv()
|
|
71
|
+
*
|
|
72
|
+
* // In a handler
|
|
73
|
+
* const user = provider.parseRequest(req)
|
|
74
|
+
* if (!user) {
|
|
75
|
+
* return { status: 401, content: 'Authentication required' }
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export interface AuthProvider {
|
|
80
|
+
/**
|
|
81
|
+
* Parse the request and return the authenticated user.
|
|
82
|
+
*
|
|
83
|
+
* @param req - Request object with headers
|
|
84
|
+
* @returns Authenticated user, or null if not authenticated or invalid
|
|
85
|
+
*/
|
|
86
|
+
parseRequest(req: AuthRequest): AuthenticatedUser | null;
|
|
87
|
+
/**
|
|
88
|
+
* Check if the request has valid authentication.
|
|
89
|
+
*
|
|
90
|
+
* This is a quick check that can be used before full parsing.
|
|
91
|
+
*
|
|
92
|
+
* @param req - Request object with headers
|
|
93
|
+
* @returns true if the request has valid authentication credentials
|
|
94
|
+
*/
|
|
95
|
+
hasValidAuth(req: AuthRequest): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Check if the authenticated user has admin privileges.
|
|
98
|
+
*
|
|
99
|
+
* @param req - Request object with headers
|
|
100
|
+
* @returns true if the user has the admin role
|
|
101
|
+
*/
|
|
102
|
+
isAdmin(req: AuthRequest): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Get the user ID from the request.
|
|
105
|
+
*
|
|
106
|
+
* @param req - Request object with headers
|
|
107
|
+
* @returns User ID, or null if not authenticated
|
|
108
|
+
*/
|
|
109
|
+
getUserId(req: AuthRequest): string | null;
|
|
110
|
+
/**
|
|
111
|
+
* Get the user roles from the request.
|
|
112
|
+
*
|
|
113
|
+
* @param req - Request object with headers
|
|
114
|
+
* @returns Array of role names, empty array if not authenticated
|
|
115
|
+
*/
|
|
116
|
+
getUserRoles(req: AuthRequest): string[];
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=auth_provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth_provider.d.ts","sourceRoot":"","sources":["../../src/auth/auth_provider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEnD;;;;;;GAMG;AACH,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,KAAK,GAAG,MAAM,CAAA;AAEjD;;GAEG;AACH,MAAM,WAAW,SAAS;IACtB,2DAA2D;IAC3D,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,kFAAkF;IAClF,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,uCAAuC;IACvC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,2CAA2C;IAC3C,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,6CAA6C;IAC7C,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,8CAA8C;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,+FAA+F;IAC/F,UAAU,CAAC,EAAE,MAAM,CAAA;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B,0BAA0B;IAC1B,IAAI,EAAE,QAAQ,CAAA;IACd,gDAAgD;IAChD,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,+DAA+D;IAC/D,GAAG,CAAC,EAAE,SAAS,CAAA;IACf,+DAA+D;IAC/D,eAAe,CAAC,EAAE,MAAM,CAAA;CAC3B;AAED;;;;;GAKG;AACH,MAAM,WAAW,WAAW;IACxB,sBAAsB;IACtB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAA;CACzD;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,YAAY;IACzB;;;;;OAKG;IACH,YAAY,CAAC,GAAG,EAAE,WAAW,GAAG,iBAAiB,GAAG,IAAI,CAAA;IAExD;;;;;;;OAOG;IACH,YAAY,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAA;IAEvC;;;;;OAKG;IACH,OAAO,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAA;IAElC;;;;;OAKG;IACH,SAAS,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,GAAG,IAAI,CAAA;IAE1C;;;;;OAKG;IACH,YAAY,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,EAAE,CAAA;CAC3C"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Authentication provider interface and types for the Digital Twin framework.
|
|
3
|
+
*
|
|
4
|
+
* This module defines the contract for authentication providers, allowing the framework
|
|
5
|
+
* to support multiple authentication mechanisms (gateway headers, JWT tokens, etc.).
|
|
6
|
+
*/
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=auth_provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth_provider.js","sourceRoot":"","sources":["../../src/auth/auth_provider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Factory for creating authentication providers.
|
|
3
|
+
*
|
|
4
|
+
* This module provides a factory for creating the appropriate authentication
|
|
5
|
+
* provider based on configuration or environment variables.
|
|
6
|
+
*/
|
|
7
|
+
import type { AuthProvider, AuthProviderConfig } from './auth_provider.js';
|
|
8
|
+
/**
|
|
9
|
+
* Factory for creating authentication providers.
|
|
10
|
+
*
|
|
11
|
+
* Use this factory to create the appropriate authentication provider based on
|
|
12
|
+
* configuration or environment variables.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Create from environment variables (recommended)
|
|
17
|
+
* const provider = AuthProviderFactory.fromEnv()
|
|
18
|
+
*
|
|
19
|
+
* // Create from explicit configuration
|
|
20
|
+
* const provider = AuthProviderFactory.create({
|
|
21
|
+
* mode: 'jwt',
|
|
22
|
+
* jwt: { secret: 'your-secret' }
|
|
23
|
+
* })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare class AuthProviderFactory {
|
|
27
|
+
/**
|
|
28
|
+
* Create an authentication provider from explicit configuration.
|
|
29
|
+
*
|
|
30
|
+
* @param config - Authentication configuration
|
|
31
|
+
* @returns Configured authentication provider
|
|
32
|
+
* @throws Error if configuration is invalid
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* // Gateway mode (default)
|
|
37
|
+
* const provider = AuthProviderFactory.create({ mode: 'gateway' })
|
|
38
|
+
*
|
|
39
|
+
* // JWT mode
|
|
40
|
+
* const provider = AuthProviderFactory.create({
|
|
41
|
+
* mode: 'jwt',
|
|
42
|
+
* jwt: { secret: 'your-secret', algorithm: 'HS256' }
|
|
43
|
+
* })
|
|
44
|
+
*
|
|
45
|
+
* // No auth mode (development only)
|
|
46
|
+
* const provider = AuthProviderFactory.create({ mode: 'none' })
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
static create(config: AuthProviderConfig): AuthProvider;
|
|
50
|
+
/**
|
|
51
|
+
* Create an authentication provider from environment variables.
|
|
52
|
+
*
|
|
53
|
+
* Environment variables:
|
|
54
|
+
* - `AUTH_MODE`: Authentication mode ('gateway', 'jwt', 'none'). Default: 'gateway'
|
|
55
|
+
* - `AUTH_ADMIN_ROLE`: Name of admin role. Default: 'admin'
|
|
56
|
+
*
|
|
57
|
+
* For JWT mode:
|
|
58
|
+
* - `JWT_SECRET`: Secret key for HMAC algorithms
|
|
59
|
+
* - `JWT_PUBLIC_KEY`: Public key content for RSA/EC algorithms
|
|
60
|
+
* - `JWT_PUBLIC_KEY_FILE`: Path to public key file
|
|
61
|
+
* - `JWT_ALGORITHM`: Algorithm (default: 'HS256')
|
|
62
|
+
* - `JWT_ISSUER`: Expected token issuer
|
|
63
|
+
* - `JWT_AUDIENCE`: Expected token audience
|
|
64
|
+
* - `JWT_USER_ID_CLAIM`: Claim for user ID (default: 'sub')
|
|
65
|
+
* - `JWT_ROLES_CLAIM`: Claim for roles (default: 'roles')
|
|
66
|
+
*
|
|
67
|
+
* For no-auth mode:
|
|
68
|
+
* - `DIGITALTWIN_DISABLE_AUTH`: Set to 'true' to disable auth
|
|
69
|
+
* - `DIGITALTWIN_ANONYMOUS_USER_ID`: Anonymous user ID (default: 'anonymous')
|
|
70
|
+
*
|
|
71
|
+
* @returns Configured authentication provider
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* // Gateway mode (default, no env vars needed)
|
|
76
|
+
* // AUTH_MODE=gateway or not set
|
|
77
|
+
* const provider = AuthProviderFactory.fromEnv()
|
|
78
|
+
*
|
|
79
|
+
* // JWT mode
|
|
80
|
+
* // AUTH_MODE=jwt
|
|
81
|
+
* // JWT_SECRET=your-secret
|
|
82
|
+
* const provider = AuthProviderFactory.fromEnv()
|
|
83
|
+
*
|
|
84
|
+
* // Disable auth for development
|
|
85
|
+
* // DIGITALTWIN_DISABLE_AUTH=true
|
|
86
|
+
* const provider = AuthProviderFactory.fromEnv()
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
static fromEnv(): AuthProvider;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=auth_provider_factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth_provider_factory.d.ts","sourceRoot":"","sources":["../../src/auth/auth_provider_factory.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAY,MAAM,oBAAoB,CAAA;AAKpF;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,mBAAmB;IAC5B;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,kBAAkB,GAAG,YAAY;IAgBvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAsCG;IACH,MAAM,CAAC,OAAO,IAAI,YAAY;CAkDjC"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Factory for creating authentication providers.
|
|
3
|
+
*
|
|
4
|
+
* This module provides a factory for creating the appropriate authentication
|
|
5
|
+
* provider based on configuration or environment variables.
|
|
6
|
+
*/
|
|
7
|
+
import * as fs from 'fs';
|
|
8
|
+
import { GatewayAuthProvider } from './providers/gateway_auth_provider.js';
|
|
9
|
+
import { JwtAuthProvider } from './providers/jwt_auth_provider.js';
|
|
10
|
+
import { NoAuthProvider } from './providers/no_auth_provider.js';
|
|
11
|
+
/**
|
|
12
|
+
* Factory for creating authentication providers.
|
|
13
|
+
*
|
|
14
|
+
* Use this factory to create the appropriate authentication provider based on
|
|
15
|
+
* configuration or environment variables.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // Create from environment variables (recommended)
|
|
20
|
+
* const provider = AuthProviderFactory.fromEnv()
|
|
21
|
+
*
|
|
22
|
+
* // Create from explicit configuration
|
|
23
|
+
* const provider = AuthProviderFactory.create({
|
|
24
|
+
* mode: 'jwt',
|
|
25
|
+
* jwt: { secret: 'your-secret' }
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export class AuthProviderFactory {
|
|
30
|
+
/**
|
|
31
|
+
* Create an authentication provider from explicit configuration.
|
|
32
|
+
*
|
|
33
|
+
* @param config - Authentication configuration
|
|
34
|
+
* @returns Configured authentication provider
|
|
35
|
+
* @throws Error if configuration is invalid
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* // Gateway mode (default)
|
|
40
|
+
* const provider = AuthProviderFactory.create({ mode: 'gateway' })
|
|
41
|
+
*
|
|
42
|
+
* // JWT mode
|
|
43
|
+
* const provider = AuthProviderFactory.create({
|
|
44
|
+
* mode: 'jwt',
|
|
45
|
+
* jwt: { secret: 'your-secret', algorithm: 'HS256' }
|
|
46
|
+
* })
|
|
47
|
+
*
|
|
48
|
+
* // No auth mode (development only)
|
|
49
|
+
* const provider = AuthProviderFactory.create({ mode: 'none' })
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
static create(config) {
|
|
53
|
+
switch (config.mode) {
|
|
54
|
+
case 'gateway':
|
|
55
|
+
return new GatewayAuthProvider(config.adminRoleName);
|
|
56
|
+
case 'jwt':
|
|
57
|
+
return new JwtAuthProvider(config);
|
|
58
|
+
case 'none':
|
|
59
|
+
return new NoAuthProvider(config.anonymousUserId);
|
|
60
|
+
default:
|
|
61
|
+
throw new Error(`Unknown auth mode: ${config.mode}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Create an authentication provider from environment variables.
|
|
66
|
+
*
|
|
67
|
+
* Environment variables:
|
|
68
|
+
* - `AUTH_MODE`: Authentication mode ('gateway', 'jwt', 'none'). Default: 'gateway'
|
|
69
|
+
* - `AUTH_ADMIN_ROLE`: Name of admin role. Default: 'admin'
|
|
70
|
+
*
|
|
71
|
+
* For JWT mode:
|
|
72
|
+
* - `JWT_SECRET`: Secret key for HMAC algorithms
|
|
73
|
+
* - `JWT_PUBLIC_KEY`: Public key content for RSA/EC algorithms
|
|
74
|
+
* - `JWT_PUBLIC_KEY_FILE`: Path to public key file
|
|
75
|
+
* - `JWT_ALGORITHM`: Algorithm (default: 'HS256')
|
|
76
|
+
* - `JWT_ISSUER`: Expected token issuer
|
|
77
|
+
* - `JWT_AUDIENCE`: Expected token audience
|
|
78
|
+
* - `JWT_USER_ID_CLAIM`: Claim for user ID (default: 'sub')
|
|
79
|
+
* - `JWT_ROLES_CLAIM`: Claim for roles (default: 'roles')
|
|
80
|
+
*
|
|
81
|
+
* For no-auth mode:
|
|
82
|
+
* - `DIGITALTWIN_DISABLE_AUTH`: Set to 'true' to disable auth
|
|
83
|
+
* - `DIGITALTWIN_ANONYMOUS_USER_ID`: Anonymous user ID (default: 'anonymous')
|
|
84
|
+
*
|
|
85
|
+
* @returns Configured authentication provider
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* // Gateway mode (default, no env vars needed)
|
|
90
|
+
* // AUTH_MODE=gateway or not set
|
|
91
|
+
* const provider = AuthProviderFactory.fromEnv()
|
|
92
|
+
*
|
|
93
|
+
* // JWT mode
|
|
94
|
+
* // AUTH_MODE=jwt
|
|
95
|
+
* // JWT_SECRET=your-secret
|
|
96
|
+
* const provider = AuthProviderFactory.fromEnv()
|
|
97
|
+
*
|
|
98
|
+
* // Disable auth for development
|
|
99
|
+
* // DIGITALTWIN_DISABLE_AUTH=true
|
|
100
|
+
* const provider = AuthProviderFactory.fromEnv()
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
static fromEnv() {
|
|
104
|
+
const adminRoleName = process.env.AUTH_ADMIN_ROLE || process.env.DIGITALTWIN_ADMIN_ROLE_NAME || 'admin';
|
|
105
|
+
// Check if auth is disabled (legacy env var)
|
|
106
|
+
if (process.env.DIGITALTWIN_DISABLE_AUTH === 'true') {
|
|
107
|
+
return new NoAuthProvider(process.env.DIGITALTWIN_ANONYMOUS_USER_ID || 'anonymous');
|
|
108
|
+
}
|
|
109
|
+
const mode = (process.env.AUTH_MODE || 'gateway');
|
|
110
|
+
if (mode === 'none') {
|
|
111
|
+
return new NoAuthProvider(process.env.DIGITALTWIN_ANONYMOUS_USER_ID || 'anonymous');
|
|
112
|
+
}
|
|
113
|
+
if (mode === 'gateway') {
|
|
114
|
+
return new GatewayAuthProvider(adminRoleName);
|
|
115
|
+
}
|
|
116
|
+
if (mode === 'jwt') {
|
|
117
|
+
// Load public key from file if specified
|
|
118
|
+
let publicKey;
|
|
119
|
+
if (process.env.JWT_PUBLIC_KEY_FILE) {
|
|
120
|
+
publicKey = fs.readFileSync(process.env.JWT_PUBLIC_KEY_FILE, 'utf-8');
|
|
121
|
+
}
|
|
122
|
+
else if (process.env.JWT_PUBLIC_KEY) {
|
|
123
|
+
publicKey = process.env.JWT_PUBLIC_KEY;
|
|
124
|
+
}
|
|
125
|
+
const secret = process.env.JWT_SECRET;
|
|
126
|
+
if (!secret && !publicKey) {
|
|
127
|
+
throw new Error('JWT mode requires either JWT_SECRET or JWT_PUBLIC_KEY/JWT_PUBLIC_KEY_FILE');
|
|
128
|
+
}
|
|
129
|
+
return new JwtAuthProvider({
|
|
130
|
+
mode: 'jwt',
|
|
131
|
+
adminRoleName,
|
|
132
|
+
jwt: {
|
|
133
|
+
secret,
|
|
134
|
+
publicKey,
|
|
135
|
+
algorithm: process.env.JWT_ALGORITHM || 'HS256',
|
|
136
|
+
issuer: process.env.JWT_ISSUER,
|
|
137
|
+
audience: process.env.JWT_AUDIENCE,
|
|
138
|
+
userIdClaim: process.env.JWT_USER_ID_CLAIM || 'sub',
|
|
139
|
+
rolesClaim: process.env.JWT_ROLES_CLAIM || 'roles'
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
throw new Error(`Unknown AUTH_MODE: ${mode}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=auth_provider_factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth_provider_factory.js","sourceRoot":"","sources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AK,CAAC,sBAAsB,IAAI,EAAE,CAAC,CAAA;IACjD,CAAC;CACJ"}
|
package/dist/auth/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export type { AuthProvider, AuthRequest, AuthMode, AuthProviderConfig, JwtConfig } from './auth_provider.js';
|
|
2
|
+
export { AuthProviderFactory } from './auth_provider_factory.js';
|
|
3
|
+
export { GatewayAuthProvider, JwtAuthProvider, NoAuthProvider } from './providers/index.js';
|
|
4
|
+
export { ApisixAuthParser, type HeadersLike } from './apisix_parser.js';
|
|
2
5
|
export { UserService } from './user_service.js';
|
|
3
6
|
export { AuthConfig } from './auth_config.js';
|
|
4
7
|
export type { AuthenticatedUser, UserRecord, AuthContext, AuthenticatedRequest } from './types.js';
|
package/dist/auth/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC5G,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AAChE,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAG3F,OAAO,EAAE,gBAAgB,EAAE,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAG7C,YAAY,EAAE,iBAAiB,EAAE,UAAU,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA"}
|
package/dist/auth/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export { AuthProviderFactory } from './auth_provider_factory.js';
|
|
2
|
+
export { GatewayAuthProvider, JwtAuthProvider, NoAuthProvider } from './providers/index.js';
|
|
3
|
+
// Backward-compatible API
|
|
1
4
|
export { ApisixAuthParser } from './apisix_parser.js';
|
|
2
5
|
export { UserService } from './user_service.js';
|
|
3
6
|
export { AuthConfig } from './auth_config.js';
|