@3sixty/api-tenant-processor 0.0.0-stage → 0.20.0-next.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/LICENSE +201 -0
- package/README.md +24 -2
- package/dist/es/entities/tenant.js +77 -0
- package/dist/es/entities/tenant.js.map +1 -0
- package/dist/es/entities/tenantV0.js +61 -0
- package/dist/es/entities/tenantV0.js.map +1 -0
- package/dist/es/entities/tenantV1.js +77 -0
- package/dist/es/entities/tenantV1.js.map +1 -0
- package/dist/es/index.js +30 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/ISingleTenantProcessorConfig.js +4 -0
- package/dist/es/models/ISingleTenantProcessorConfig.js.map +1 -0
- package/dist/es/models/ISingleTenantProcessorConstructorOptions.js +2 -0
- package/dist/es/models/ISingleTenantProcessorConstructorOptions.js.map +1 -0
- package/dist/es/models/ITenantAdminServiceConfig.js +4 -0
- package/dist/es/models/ITenantAdminServiceConfig.js.map +1 -0
- package/dist/es/models/ITenantAdminServiceConstructorOptions.js +2 -0
- package/dist/es/models/ITenantAdminServiceConstructorOptions.js.map +1 -0
- package/dist/es/models/ITenantProcessorConfig.js +4 -0
- package/dist/es/models/ITenantProcessorConfig.js.map +1 -0
- package/dist/es/models/ITenantProcessorConstructorOptions.js +2 -0
- package/dist/es/models/ITenantProcessorConstructorOptions.js.map +1 -0
- package/dist/es/models/api/ITenantCreateRequest.js +2 -0
- package/dist/es/models/api/ITenantCreateRequest.js.map +1 -0
- package/dist/es/models/api/ITenantGetByApiKeyRequest.js +4 -0
- package/dist/es/models/api/ITenantGetByApiKeyRequest.js.map +1 -0
- package/dist/es/models/api/ITenantGetByIdRequest.js +4 -0
- package/dist/es/models/api/ITenantGetByIdRequest.js.map +1 -0
- package/dist/es/models/api/ITenantGetByPublicOriginRequest.js +4 -0
- package/dist/es/models/api/ITenantGetByPublicOriginRequest.js.map +1 -0
- package/dist/es/models/api/ITenantGetResponse.js +2 -0
- package/dist/es/models/api/ITenantGetResponse.js.map +1 -0
- package/dist/es/models/api/ITenantListRequest.js +4 -0
- package/dist/es/models/api/ITenantListRequest.js.map +1 -0
- package/dist/es/models/api/ITenantListResponse.js +2 -0
- package/dist/es/models/api/ITenantListResponse.js.map +1 -0
- package/dist/es/models/api/ITenantRemoveRequest.js +4 -0
- package/dist/es/models/api/ITenantRemoveRequest.js.map +1 -0
- package/dist/es/models/api/ITenantUpdateRequest.js +2 -0
- package/dist/es/models/api/ITenantUpdateRequest.js.map +1 -0
- package/dist/es/restEntryPoints.js +13 -0
- package/dist/es/restEntryPoints.js.map +1 -0
- package/dist/es/schema.js +64 -0
- package/dist/es/schema.js.map +1 -0
- package/dist/es/singleTenantProcessor.js +81 -0
- package/dist/es/singleTenantProcessor.js.map +1 -0
- package/dist/es/tenantAdminService.js +510 -0
- package/dist/es/tenantAdminService.js.map +1 -0
- package/dist/es/tenantIdContextIdHandler.js +47 -0
- package/dist/es/tenantIdContextIdHandler.js.map +1 -0
- package/dist/es/tenantProcessor.js +164 -0
- package/dist/es/tenantProcessor.js.map +1 -0
- package/dist/es/tenantRoutes.js +417 -0
- package/dist/es/tenantRoutes.js.map +1 -0
- package/dist/es/utils/tenantIdHelper.js +23 -0
- package/dist/es/utils/tenantIdHelper.js.map +1 -0
- package/dist/types/entities/tenant.d.ts +37 -0
- package/dist/types/entities/tenantV0.d.ts +29 -0
- package/dist/types/entities/tenantV1.d.ts +37 -0
- package/dist/types/index.d.ts +26 -0
- package/dist/types/models/ISingleTenantProcessorConfig.d.ts +9 -0
- package/dist/types/models/ISingleTenantProcessorConstructorOptions.d.ts +10 -0
- package/dist/types/models/ITenantAdminServiceConfig.d.ts +24 -0
- package/dist/types/models/ITenantAdminServiceConstructorOptions.d.ts +20 -0
- package/dist/types/models/ITenantProcessorConfig.d.ts +18 -0
- package/dist/types/models/ITenantProcessorConstructorOptions.d.ts +15 -0
- package/dist/types/models/api/ITenantCreateRequest.d.ts +12 -0
- package/dist/types/models/api/ITenantGetByApiKeyRequest.d.ts +14 -0
- package/dist/types/models/api/ITenantGetByIdRequest.d.ts +14 -0
- package/dist/types/models/api/ITenantGetByPublicOriginRequest.d.ts +14 -0
- package/dist/types/models/api/ITenantGetResponse.d.ts +10 -0
- package/dist/types/models/api/ITenantListRequest.d.ts +27 -0
- package/dist/types/models/api/ITenantListResponse.d.ts +17 -0
- package/dist/types/models/api/ITenantRemoveRequest.d.ts +14 -0
- package/dist/types/models/api/ITenantUpdateRequest.d.ts +19 -0
- package/dist/types/restEntryPoints.d.ts +5 -0
- package/dist/types/schema.d.ts +5 -0
- package/dist/types/singleTenantProcessor.d.ts +41 -0
- package/dist/types/tenantAdminService.d.ts +109 -0
- package/dist/types/tenantIdContextIdHandler.d.ts +33 -0
- package/dist/types/tenantProcessor.d.ts +34 -0
- package/dist/types/tenantRoutes.d.ts +78 -0
- package/dist/types/utils/tenantIdHelper.d.ts +15 -0
- package/docs/changelog.md +17 -0
- package/docs/examples.md +81 -0
- package/docs/reference/classes/SingleTenantProcessor.md +130 -0
- package/docs/reference/classes/Tenant.md +77 -0
- package/docs/reference/classes/TenantAdminService.md +393 -0
- package/docs/reference/classes/TenantIdContextIdHandler.md +123 -0
- package/docs/reference/classes/TenantIdHelper.md +41 -0
- package/docs/reference/classes/TenantProcessor.md +103 -0
- package/docs/reference/classes/TenantV0.md +61 -0
- package/docs/reference/classes/TenantV1.md +77 -0
- package/docs/reference/functions/generateRestRoutesTenants.md +25 -0
- package/docs/reference/functions/initSchema.md +10 -0
- package/docs/reference/functions/tenantByApiKey.md +31 -0
- package/docs/reference/functions/tenantById.md +31 -0
- package/docs/reference/functions/tenantByPublicOrigin.md +31 -0
- package/docs/reference/functions/tenantCreate.md +37 -0
- package/docs/reference/functions/tenantList.md +31 -0
- package/docs/reference/functions/tenantRemove.md +31 -0
- package/docs/reference/functions/tenantUpdate.md +31 -0
- package/docs/reference/index.md +47 -0
- package/docs/reference/interfaces/ISingleTenantProcessorConfig.md +11 -0
- package/docs/reference/interfaces/ISingleTenantProcessorConstructorOptions.md +11 -0
- package/docs/reference/interfaces/ITenantAdminServiceConfig.md +49 -0
- package/docs/reference/interfaces/ITenantAdminServiceConstructorOptions.md +39 -0
- package/docs/reference/interfaces/ITenantCreateRequest.md +17 -0
- package/docs/reference/interfaces/ITenantGetByApiKeyRequest.md +17 -0
- package/docs/reference/interfaces/ITenantGetByIdRequest.md +17 -0
- package/docs/reference/interfaces/ITenantGetByPublicOriginRequest.md +17 -0
- package/docs/reference/interfaces/ITenantGetResponse.md +11 -0
- package/docs/reference/interfaces/ITenantListRequest.md +36 -0
- package/docs/reference/interfaces/ITenantListResponse.md +23 -0
- package/docs/reference/interfaces/ITenantProcessorConfig.md +33 -0
- package/docs/reference/interfaces/ITenantProcessorConstructorOptions.md +25 -0
- package/docs/reference/interfaces/ITenantRemoveRequest.md +17 -0
- package/docs/reference/interfaces/ITenantUpdateRequest.md +25 -0
- package/docs/reference/variables/restEntryPoints.md +5 -0
- package/docs/reference/variables/tagsTenants.md +5 -0
- package/locales/en.json +20 -0
- package/package.json +57 -5
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// Copyright 2025 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { HttpErrorHelper, HttpContextIdKeys } from "@3sixty/api-models";
|
|
4
|
+
import { ContextIdKeys } from "@3sixty/context";
|
|
5
|
+
import { BaseError, ComponentFactory, GuardError, Is, NotFoundError, UnauthorizedError } from "@3sixty/core";
|
|
6
|
+
import { HttpStatusCode } from "@3sixty/web";
|
|
7
|
+
/**
|
|
8
|
+
* Handles incoming api keys and maps them to tenant ids.
|
|
9
|
+
*/
|
|
10
|
+
export class TenantProcessor {
|
|
11
|
+
/**
|
|
12
|
+
* The default name for the api key header.
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
static DEFAULT_API_KEY_NAME = "x-api-key";
|
|
16
|
+
/**
|
|
17
|
+
* Runtime name for the class.
|
|
18
|
+
*/
|
|
19
|
+
static CLASS_NAME = "TenantProcessor";
|
|
20
|
+
/**
|
|
21
|
+
* The default endpoints to match for api key processing.
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
static _DEFAULT_API_KEY_ENDPOINTS = ["/login$"];
|
|
25
|
+
/**
|
|
26
|
+
* The component used to resolve tenants, which also provides the lookup caching.
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
_tenantAdminComponent;
|
|
30
|
+
/**
|
|
31
|
+
* The key in the header to look for the api key.
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
_apiKeyName;
|
|
35
|
+
/**
|
|
36
|
+
* The list of regexp patterns to match against the request URL to determine if the api key header should be processed.
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
_apiKeyEndpoints;
|
|
40
|
+
/**
|
|
41
|
+
* Include the stack with errors.
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
_includeErrorStack;
|
|
45
|
+
/**
|
|
46
|
+
* Create a new instance of TenantProcessor.
|
|
47
|
+
* @param options Options for the processor.
|
|
48
|
+
*/
|
|
49
|
+
constructor(options) {
|
|
50
|
+
this._tenantAdminComponent = ComponentFactory.get(options?.tenantAdminComponentType ?? "tenant-admin");
|
|
51
|
+
this._apiKeyName = options?.config?.apiKeyName ?? TenantProcessor.DEFAULT_API_KEY_NAME;
|
|
52
|
+
this._apiKeyEndpoints = (options?.config?.apiKeyEndpoints ?? TenantProcessor._DEFAULT_API_KEY_ENDPOINTS).map((ep) => new RegExp(ep));
|
|
53
|
+
this._includeErrorStack = options?.config?.includeErrorStack ?? false;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns the class name of the component.
|
|
57
|
+
* @returns The class name of the component.
|
|
58
|
+
*/
|
|
59
|
+
className() {
|
|
60
|
+
return TenantProcessor.CLASS_NAME;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Pre process the REST request for the specified route.
|
|
64
|
+
* @param request The incoming request.
|
|
65
|
+
* @param response The outgoing response.
|
|
66
|
+
* @param route The route to process.
|
|
67
|
+
* @param contextIds The context IDs of the request.
|
|
68
|
+
* @param processorState The state handed through the processors.
|
|
69
|
+
* @returns A promise that resolves when the tenant context has been resolved and injected, or an error response set.
|
|
70
|
+
*/
|
|
71
|
+
async pre(request, response, route, contextIds, processorState) {
|
|
72
|
+
if (!Is.empty(route) && !(route.skipTenant ?? false)) {
|
|
73
|
+
try {
|
|
74
|
+
let tenant;
|
|
75
|
+
const isSkipAuth = route.skipAuth ?? false;
|
|
76
|
+
const urlPath = new URL(request.url, "http://localhost").pathname;
|
|
77
|
+
const isApiKeyEndpoint = this._apiKeyEndpoints.some(re => re.test(urlPath));
|
|
78
|
+
if (isApiKeyEndpoint) {
|
|
79
|
+
const apiKey = request.headers?.[this._apiKeyName] ?? request.query?.[this._apiKeyName];
|
|
80
|
+
if (Is.stringValue(apiKey)) {
|
|
81
|
+
tenant = await this.resolveByApiKey(apiKey);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
throw new UnauthorizedError(TenantProcessor.CLASS_NAME, "missingApiKey", {
|
|
85
|
+
keyName: this._apiKeyName
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const organizationIdQueryParam = request.query?.[ContextIdKeys.Organization];
|
|
91
|
+
if (Is.stringValue(organizationIdQueryParam)) {
|
|
92
|
+
tenant = await this.resolveByOrganizationId(organizationIdQueryParam);
|
|
93
|
+
}
|
|
94
|
+
else if (isSkipAuth) {
|
|
95
|
+
throw new UnauthorizedError(TenantProcessor.CLASS_NAME, "missingOrganizationId", {
|
|
96
|
+
paramName: ContextIdKeys.Organization
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (!Is.empty(tenant)) {
|
|
101
|
+
contextIds[ContextIdKeys.Tenant] = tenant.id;
|
|
102
|
+
contextIds[ContextIdKeys.Organization] = tenant.organizationId;
|
|
103
|
+
// If the tenant has a custom public origin, we set it in the context for downstream processors to use.
|
|
104
|
+
if (Is.stringValue(tenant.publicOrigin)) {
|
|
105
|
+
contextIds[HttpContextIdKeys.PublicOrigin] = tenant.publicOrigin;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
HttpErrorHelper.buildResponse(response, BaseError.fromError(err), HttpStatusCode.unauthorized, this._includeErrorStack);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Resolve the tenant context from an api key.
|
|
116
|
+
* @param apiKey The api key sent by the caller.
|
|
117
|
+
* @returns The tenant associated with the api key.
|
|
118
|
+
* @throws UnauthorizedError if no tenant matches the api key.
|
|
119
|
+
* @internal
|
|
120
|
+
*/
|
|
121
|
+
async resolveByApiKey(apiKey) {
|
|
122
|
+
try {
|
|
123
|
+
return await this._tenantAdminComponent.getByApiKey(apiKey);
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
if (this.isLookupFailure(err)) {
|
|
127
|
+
throw new UnauthorizedError(TenantProcessor.CLASS_NAME, "apiKeyNotFound", { key: apiKey }, err);
|
|
128
|
+
}
|
|
129
|
+
throw err;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Resolve the tenant context from a plain organization query param.
|
|
134
|
+
* Matches against the organization id or one of the legacy organization ids.
|
|
135
|
+
* @param organizationId The organization id from the query param.
|
|
136
|
+
* @returns The tenant associated with the organization id.
|
|
137
|
+
* @throws UnauthorizedError if no tenant matches the organization id.
|
|
138
|
+
* @internal
|
|
139
|
+
*/
|
|
140
|
+
async resolveByOrganizationId(organizationId) {
|
|
141
|
+
try {
|
|
142
|
+
return await this._tenantAdminComponent.getTenantByOrganizationId(organizationId, true);
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
if (this.isLookupFailure(err)) {
|
|
146
|
+
throw new UnauthorizedError(TenantProcessor.CLASS_NAME, "organizationIdNotFound", { organizationId }, err);
|
|
147
|
+
}
|
|
148
|
+
throw err;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Determine whether a tenant lookup failure means the credential is unusable.
|
|
153
|
+
* A credential that matches no tenant, or is not even well formed, is unauthorized; anything
|
|
154
|
+
* else is a genuine fault and is passed through untouched.
|
|
155
|
+
* @param err The error raised by the tenant admin component.
|
|
156
|
+
* @returns True if the credential should be reported as unauthorized.
|
|
157
|
+
* @internal
|
|
158
|
+
*/
|
|
159
|
+
isLookupFailure(err) {
|
|
160
|
+
return (BaseError.isErrorName(err, NotFoundError.CLASS_NAME) ||
|
|
161
|
+
BaseError.isErrorName(err, GuardError.CLASS_NAME));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=tenantProcessor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenantProcessor.js","sources":["../../src/tenantProcessor.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,eAAe,EAOf,iBAAiB,EACjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,aAAa,EAAoB,MAAM,iBAAiB,CAAC;AAClE,OAAO,EACN,SAAS,EACT,gBAAgB,EAChB,UAAU,EACV,EAAE,EACF,aAAa,EACb,iBAAiB,EACjB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG7C;;GAEG;AACH,MAAM,OAAO,eAAe;IAC3B;;;OAGG;IACI,MAAM,CAAU,oBAAoB,GAAW,WAAW,CAAC;IAElE;;OAEG;IACI,MAAM,CAAU,UAAU,GAAW,iBAAiB,CAAC;IAE9D;;;OAGG;IACK,MAAM,CAAU,0BAA0B,GAAa,CAAC,SAAS,CAAC,CAAC;IAE3E;;;OAGG;IACc,qBA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;;;;;;;OAOG;IACK,eAAe,CAAC,GAAY;QACnC,OAAO,CACN,SAAS,CAAC,WAAW,CAAC,GAAG,EAAE,aAAa,CAAC,UAAU,CAAC;YACpD,SAAS,CAAC,WAAW,CAAC,GAAG,EAAE,UAAU,CAAC,UAAU,CAAC,CACjD,CAAC;IACH,CAAC;CACD","sourcesContent":["// Copyright 2025 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tHttpErrorHelper,\n\ttype IBaseRoute,\n\ttype IBaseRouteProcessor,\n\ttype IHttpResponse,\n\ttype IHttpServerRequest,\n\ttype ITenant,\n\ttype ITenantAdminComponent,\n\tHttpContextIdKeys\n} from \"@3sixty/api-models\";\nimport { ContextIdKeys, type IContextIds } from \"@3sixty/context\";\nimport {\n\tBaseError,\n\tComponentFactory,\n\tGuardError,\n\tIs,\n\tNotFoundError,\n\tUnauthorizedError\n} from \"@3sixty/core\";\n\nimport { HttpStatusCode } from \"@3sixty/web\";\nimport type { ITenantProcessorConstructorOptions } from \"./models/ITenantProcessorConstructorOptions.js\";\n\n/**\n * Handles incoming api keys and maps them to tenant ids.\n */\nexport class TenantProcessor implements IBaseRouteProcessor {\n\t/**\n\t * The default name for the api key header.\n\t * @internal\n\t */\n\tpublic static readonly DEFAULT_API_KEY_NAME: string = \"x-api-key\";\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = \"TenantProcessor\";\n\n\t/**\n\t * The default endpoints to match for api key processing.\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_API_KEY_ENDPOINTS: string[] = [\"/login$\"];\n\n\t/**\n\t * The component used to resolve tenants, which also provides the lookup caching.\n\t * @internal\n\t */\n\tprivate readonly _tenantAdminComponent: ITenantAdminComponent;\n\n\t/**\n\t * The key in the header to look for the api key.\n\t * @internal\n\t */\n\tprivate readonly _apiKeyName: string;\n\n\t/**\n\t * The list of regexp patterns to match against the request URL to determine if the api key header should be processed.\n\t * @internal\n\t */\n\tprivate readonly _apiKeyEndpoints: RegExp[];\n\n\t/**\n\t * Include the stack with errors.\n\t * @internal\n\t */\n\tprivate readonly _includeErrorStack: boolean;\n\n\t/**\n\t * Create a new instance of TenantProcessor.\n\t * @param options Options for the processor.\n\t */\n\tconstructor(options?: ITenantProcessorConstructorOptions) {\n\t\tthis._tenantAdminComponent = ComponentFactory.get<ITenantAdminComponent>(\n\t\t\toptions?.tenantAdminComponentType ?? \"tenant-admin\"\n\t\t);\n\t\tthis._apiKeyName = options?.config?.apiKeyName ?? TenantProcessor.DEFAULT_API_KEY_NAME;\n\t\tthis._apiKeyEndpoints = (\n\t\t\toptions?.config?.apiKeyEndpoints ?? TenantProcessor._DEFAULT_API_KEY_ENDPOINTS\n\t\t).map((ep: string) => new RegExp(ep));\n\t\tthis._includeErrorStack = options?.config?.includeErrorStack ?? false;\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn TenantProcessor.CLASS_NAME;\n\t}\n\n\t/**\n\t * Pre process the REST request for the specified route.\n\t * @param request The incoming request.\n\t * @param response The outgoing response.\n\t * @param route The route to process.\n\t * @param contextIds The context IDs of the request.\n\t * @param processorState The state handed through the processors.\n\t * @returns A promise that resolves when the tenant context has been resolved and injected, or an error response set.\n\t */\n\tpublic async pre(\n\t\trequest: IHttpServerRequest,\n\t\tresponse: IHttpResponse,\n\t\troute: IBaseRoute | undefined,\n\t\tcontextIds: IContextIds,\n\t\tprocessorState: { [id: string]: unknown }\n\t): Promise<void> {\n\t\tif (!Is.empty(route) && !(route.skipTenant ?? false)) {\n\t\t\ttry {\n\t\t\t\tlet tenant;\n\t\t\t\tconst isSkipAuth = route.skipAuth ?? false;\n\t\t\t\tconst urlPath = new URL(request.url, \"http://localhost\").pathname;\n\t\t\t\tconst isApiKeyEndpoint = this._apiKeyEndpoints.some(re => re.test(urlPath));\n\n\t\t\t\tif (isApiKeyEndpoint) {\n\t\t\t\t\tconst apiKey = request.headers?.[this._apiKeyName] ?? request.query?.[this._apiKeyName];\n\t\t\t\t\tif (Is.stringValue(apiKey)) {\n\t\t\t\t\t\ttenant = await this.resolveByApiKey(apiKey);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new UnauthorizedError(TenantProcessor.CLASS_NAME, \"missingApiKey\", {\n\t\t\t\t\t\t\tkeyName: this._apiKeyName\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tconst organizationIdQueryParam = request.query?.[ContextIdKeys.Organization];\n\t\t\t\t\tif (Is.stringValue(organizationIdQueryParam)) {\n\t\t\t\t\t\ttenant = await this.resolveByOrganizationId(organizationIdQueryParam);\n\t\t\t\t\t} else if (isSkipAuth) {\n\t\t\t\t\t\tthrow new UnauthorizedError(TenantProcessor.CLASS_NAME, \"missingOrganizationId\", {\n\t\t\t\t\t\t\tparamName: ContextIdKeys.Organization\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (!Is.empty(tenant)) {\n\t\t\t\t\tcontextIds[ContextIdKeys.Tenant] = tenant.id;\n\t\t\t\t\tcontextIds[ContextIdKeys.Organization] = tenant.organizationId;\n\n\t\t\t\t\t// If the tenant has a custom public origin, we set it in the context for downstream processors to use.\n\t\t\t\t\tif (Is.stringValue(tenant.publicOrigin)) {\n\t\t\t\t\t\tcontextIds[HttpContextIdKeys.PublicOrigin] = tenant.publicOrigin;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tHttpErrorHelper.buildResponse(\n\t\t\t\t\tresponse,\n\t\t\t\t\tBaseError.fromError(err),\n\t\t\t\t\tHttpStatusCode.unauthorized,\n\t\t\t\t\tthis._includeErrorStack\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Resolve the tenant context from an api key.\n\t * @param apiKey The api key sent by the caller.\n\t * @returns The tenant associated with the api key.\n\t * @throws UnauthorizedError if no tenant matches the api key.\n\t * @internal\n\t */\n\tprivate async resolveByApiKey(apiKey: string): Promise<ITenant> {\n\t\ttry {\n\t\t\treturn await this._tenantAdminComponent.getByApiKey(apiKey);\n\t\t} catch (err) {\n\t\t\tif (this.isLookupFailure(err)) {\n\t\t\t\tthrow new UnauthorizedError(\n\t\t\t\t\tTenantProcessor.CLASS_NAME,\n\t\t\t\t\t\"apiKeyNotFound\",\n\t\t\t\t\t{ key: apiKey },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow err;\n\t\t}\n\t}\n\n\t/**\n\t * Resolve the tenant context from a plain organization query param.\n\t * Matches against the organization id or one of the legacy organization ids.\n\t * @param organizationId The organization id from the query param.\n\t * @returns The tenant associated with the organization id.\n\t * @throws UnauthorizedError if no tenant matches the organization id.\n\t * @internal\n\t */\n\tprivate async resolveByOrganizationId(organizationId: string): Promise<ITenant> {\n\t\ttry {\n\t\t\treturn await this._tenantAdminComponent.getTenantByOrganizationId(organizationId, true);\n\t\t} catch (err) {\n\t\t\tif (this.isLookupFailure(err)) {\n\t\t\t\tthrow new UnauthorizedError(\n\t\t\t\t\tTenantProcessor.CLASS_NAME,\n\t\t\t\t\t\"organizationIdNotFound\",\n\t\t\t\t\t{ organizationId },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow err;\n\t\t}\n\t}\n\n\t/**\n\t * Determine whether a tenant lookup failure means the credential is unusable.\n\t * A credential that matches no tenant, or is not even well formed, is unauthorized; anything\n\t * else is a genuine fault and is passed through untouched.\n\t * @param err The error raised by the tenant admin component.\n\t * @returns True if the credential should be reported as unauthorized.\n\t * @internal\n\t */\n\tprivate isLookupFailure(err: unknown): boolean {\n\t\treturn (\n\t\t\tBaseError.isErrorName(err, NotFoundError.CLASS_NAME) ||\n\t\t\tBaseError.isErrorName(err, GuardError.CLASS_NAME)\n\t\t);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// Copyright 2026 3Sixty Trade Ltd.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
4
|
+
import { HttpContextIdKeys, HttpHeaderHelper, HttpParameterHelper, HttpUrlHelper } from "@3sixty/api-models";
|
|
5
|
+
import { ContextIdStore } from "@3sixty/context";
|
|
6
|
+
import { Coerce, ComponentFactory, Guards, Is } from "@3sixty/core";
|
|
7
|
+
import { HeaderTypes, HttpStatusCode } from "@3sixty/web";
|
|
8
|
+
/**
|
|
9
|
+
* The source used when communicating about these routes.
|
|
10
|
+
*/
|
|
11
|
+
const ROUTES_SOURCE = "tenantRoutes";
|
|
12
|
+
/**
|
|
13
|
+
* The default authorization for the routes, used to seed authorization rules.
|
|
14
|
+
*/
|
|
15
|
+
const DEFAULT_AUTHORIZATION_READER = {
|
|
16
|
+
permission: "tenant:read",
|
|
17
|
+
role: "tenant-admin"
|
|
18
|
+
};
|
|
19
|
+
const DEFAULT_AUTHORIZATION_WRITER = {
|
|
20
|
+
permission: "tenant:write",
|
|
21
|
+
role: "tenant-admin",
|
|
22
|
+
inherits: [DEFAULT_AUTHORIZATION_READER.permission]
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The tag to associate with the routes.
|
|
26
|
+
*/
|
|
27
|
+
export const tagsTenants = [
|
|
28
|
+
{
|
|
29
|
+
name: "Tenants",
|
|
30
|
+
description: "Tenants endpoints for the REST server."
|
|
31
|
+
}
|
|
32
|
+
];
|
|
33
|
+
/**
|
|
34
|
+
* The REST routes for tenant management.
|
|
35
|
+
* @param baseRouteName Prefix to prepend to the paths.
|
|
36
|
+
* @param componentName The name of the component to use in the routes stored in the ComponentFactory.
|
|
37
|
+
* @returns The generated routes.
|
|
38
|
+
*/
|
|
39
|
+
export function generateRestRoutesTenants(baseRouteName, componentName) {
|
|
40
|
+
const tenantListRoute = {
|
|
41
|
+
operationId: "tenantList",
|
|
42
|
+
summary: "Get the list of tenants",
|
|
43
|
+
tag: tagsTenants[0].name,
|
|
44
|
+
method: "GET",
|
|
45
|
+
path: `${baseRouteName}/`,
|
|
46
|
+
handler: async (httpRequestContext, request) => tenantList(httpRequestContext, componentName, request),
|
|
47
|
+
responseType: [
|
|
48
|
+
{
|
|
49
|
+
type: "ITenantListResponse",
|
|
50
|
+
examples: [
|
|
51
|
+
{
|
|
52
|
+
id: "tenantListResponse",
|
|
53
|
+
description: "The response for the list tenants request.",
|
|
54
|
+
response: {
|
|
55
|
+
headers: {
|
|
56
|
+
[HeaderTypes.Link]: '<https://api.3sixty.global/tenants/1>; rel="next"'
|
|
57
|
+
},
|
|
58
|
+
body: [
|
|
59
|
+
{
|
|
60
|
+
id: "2a39d6e62d98aa5372432f2e5d2208d4",
|
|
61
|
+
apiKey: "ad7a5b0b816ca314b69c813ae1368232",
|
|
62
|
+
label: "node",
|
|
63
|
+
dateCreated: "2026-01-19T03:59:35.742Z",
|
|
64
|
+
dateModified: "2026-01-19T03:59:35.742Z",
|
|
65
|
+
publicOrigin: "https://example.com:4321",
|
|
66
|
+
organizationId: "org-1"
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
defaultAuthorization: DEFAULT_AUTHORIZATION_READER
|
|
75
|
+
};
|
|
76
|
+
const tenantGetByIdRoute = {
|
|
77
|
+
operationId: "tenantGetById",
|
|
78
|
+
summary: "Get the tenant by id",
|
|
79
|
+
tag: tagsTenants[0].name,
|
|
80
|
+
method: "GET",
|
|
81
|
+
path: `${baseRouteName}/:id`,
|
|
82
|
+
handler: async (httpRequestContext, request) => tenantById(httpRequestContext, componentName, request),
|
|
83
|
+
requestType: {
|
|
84
|
+
type: "ITenantGetByIdRequest",
|
|
85
|
+
examples: [
|
|
86
|
+
{
|
|
87
|
+
id: "tenantGetByIdRequestExample",
|
|
88
|
+
request: {
|
|
89
|
+
pathParams: {
|
|
90
|
+
id: "2a39d6e62d98aa5372432f2e5d2208d4"
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
]
|
|
95
|
+
},
|
|
96
|
+
responseType: [
|
|
97
|
+
{
|
|
98
|
+
type: "ITenantGetResponse",
|
|
99
|
+
examples: [
|
|
100
|
+
{
|
|
101
|
+
id: "tenantGetByIdResponse",
|
|
102
|
+
description: "The response for the get tenant by id request.",
|
|
103
|
+
response: {
|
|
104
|
+
body: {
|
|
105
|
+
id: "2a39d6e62d98aa5372432f2e5d2208d4",
|
|
106
|
+
apiKey: "ad7a5b0b816ca314b69c813ae1368232",
|
|
107
|
+
label: "node",
|
|
108
|
+
dateCreated: "2026-01-19T03:59:35.742Z",
|
|
109
|
+
dateModified: "2026-01-19T03:59:35.742Z",
|
|
110
|
+
publicOrigin: "https://example.com:4321",
|
|
111
|
+
organizationId: "org-1"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
],
|
|
118
|
+
defaultAuthorization: DEFAULT_AUTHORIZATION_READER
|
|
119
|
+
};
|
|
120
|
+
const tenantGetByApiKeyRoute = {
|
|
121
|
+
operationId: "tenantGetByApiKey",
|
|
122
|
+
summary: "Get the tenant by api key",
|
|
123
|
+
tag: tagsTenants[0].name,
|
|
124
|
+
method: "GET",
|
|
125
|
+
path: `${baseRouteName}/api-key/:apiKey`,
|
|
126
|
+
handler: async (httpRequestContext, request) => tenantByApiKey(httpRequestContext, componentName, request),
|
|
127
|
+
responseType: [
|
|
128
|
+
{
|
|
129
|
+
type: "ITenantGetResponse",
|
|
130
|
+
examples: [
|
|
131
|
+
{
|
|
132
|
+
id: "tenantGetByApiKeyResponse",
|
|
133
|
+
description: "The response for the get tenant by api key request.",
|
|
134
|
+
response: {
|
|
135
|
+
body: {
|
|
136
|
+
id: "2a39d6e62d98aa5372432f2e5d2208d4",
|
|
137
|
+
apiKey: "ad7a5b0b816ca314b69c813ae1368232",
|
|
138
|
+
label: "node",
|
|
139
|
+
dateCreated: "2026-01-19T03:59:35.742Z",
|
|
140
|
+
dateModified: "2026-01-19T03:59:35.742Z",
|
|
141
|
+
publicOrigin: "https://example.com:4321",
|
|
142
|
+
organizationId: "org-1"
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
]
|
|
147
|
+
}
|
|
148
|
+
],
|
|
149
|
+
defaultAuthorization: DEFAULT_AUTHORIZATION_READER
|
|
150
|
+
};
|
|
151
|
+
const tenantGetByPublicOriginRoute = {
|
|
152
|
+
operationId: "tenantGetByPublicOrigin",
|
|
153
|
+
summary: "Get the tenant by public origin",
|
|
154
|
+
tag: tagsTenants[0].name,
|
|
155
|
+
method: "GET",
|
|
156
|
+
path: `${baseRouteName}/public-origin/:publicOrigin`,
|
|
157
|
+
handler: async (httpRequestContext, request) => tenantByPublicOrigin(httpRequestContext, componentName, request),
|
|
158
|
+
responseType: [
|
|
159
|
+
{
|
|
160
|
+
type: "ITenantGetResponse",
|
|
161
|
+
examples: [
|
|
162
|
+
{
|
|
163
|
+
id: "tenantGetByPublicOriginResponse",
|
|
164
|
+
description: "The response for the get tenant by public origin request.",
|
|
165
|
+
response: {
|
|
166
|
+
body: {
|
|
167
|
+
id: "2a39d6e62d98aa5372432f2e5d2208d4",
|
|
168
|
+
apiKey: "ad7a5b0b816ca314b69c813ae1368232",
|
|
169
|
+
label: "node",
|
|
170
|
+
dateCreated: "2026-01-19T03:59:35.742Z",
|
|
171
|
+
publicOrigin: "https://example.com:4321"
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
]
|
|
176
|
+
}
|
|
177
|
+
],
|
|
178
|
+
defaultAuthorization: DEFAULT_AUTHORIZATION_READER
|
|
179
|
+
};
|
|
180
|
+
const tenantRemoveRoute = {
|
|
181
|
+
operationId: "tenantRemove",
|
|
182
|
+
summary: "Remove the tenant by id",
|
|
183
|
+
tag: tagsTenants[0].name,
|
|
184
|
+
method: "DELETE",
|
|
185
|
+
path: `${baseRouteName}/:id`,
|
|
186
|
+
handler: async (httpRequestContext, request) => tenantRemove(httpRequestContext, componentName, request),
|
|
187
|
+
requestType: {
|
|
188
|
+
type: "ITenantRemoveRequest",
|
|
189
|
+
examples: [
|
|
190
|
+
{
|
|
191
|
+
id: "tenantRemoveRequestExample",
|
|
192
|
+
description: "The request for the remove tenant by id request.",
|
|
193
|
+
request: {
|
|
194
|
+
pathParams: {
|
|
195
|
+
id: "2a39d6e62d98aa5372432f2e5d2208d4"
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
]
|
|
200
|
+
},
|
|
201
|
+
responseType: [
|
|
202
|
+
{
|
|
203
|
+
type: "INoContentResponse"
|
|
204
|
+
}
|
|
205
|
+
],
|
|
206
|
+
defaultAuthorization: DEFAULT_AUTHORIZATION_WRITER
|
|
207
|
+
};
|
|
208
|
+
const tenantCreateRoute = {
|
|
209
|
+
operationId: "tenantCreate",
|
|
210
|
+
summary: "Create a new tenant",
|
|
211
|
+
tag: tagsTenants[0].name,
|
|
212
|
+
method: "POST",
|
|
213
|
+
path: `${baseRouteName}/`,
|
|
214
|
+
handler: async (httpRequestContext, request) => tenantCreate(httpRequestContext, componentName, request, baseRouteName),
|
|
215
|
+
requestType: {
|
|
216
|
+
type: "ITenantCreateRequest",
|
|
217
|
+
examples: [
|
|
218
|
+
{
|
|
219
|
+
id: "tenantCreateRequestExample",
|
|
220
|
+
description: "The request for the create tenant request.",
|
|
221
|
+
request: {
|
|
222
|
+
body: {
|
|
223
|
+
apiKey: "ad7a5b0b816ca314b69c813ae1368232",
|
|
224
|
+
label: "node",
|
|
225
|
+
publicOrigin: "https://example.com:4321",
|
|
226
|
+
organizationId: "org-1"
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
]
|
|
231
|
+
},
|
|
232
|
+
responseType: [
|
|
233
|
+
{
|
|
234
|
+
type: "ICreatedResponse"
|
|
235
|
+
}
|
|
236
|
+
],
|
|
237
|
+
defaultAuthorization: DEFAULT_AUTHORIZATION_WRITER
|
|
238
|
+
};
|
|
239
|
+
const tenantUpdateRoute = {
|
|
240
|
+
operationId: "tenantUpdate",
|
|
241
|
+
summary: "Update an existing tenant",
|
|
242
|
+
tag: tagsTenants[0].name,
|
|
243
|
+
method: "PUT",
|
|
244
|
+
path: `${baseRouteName}/:id`,
|
|
245
|
+
handler: async (httpRequestContext, request) => tenantUpdate(httpRequestContext, componentName, request),
|
|
246
|
+
requestType: {
|
|
247
|
+
type: "ITenantUpdateRequest",
|
|
248
|
+
examples: [
|
|
249
|
+
{
|
|
250
|
+
id: "tenantUpdateRequestExample",
|
|
251
|
+
description: "The request for the update tenant request.",
|
|
252
|
+
request: {
|
|
253
|
+
pathParams: {
|
|
254
|
+
id: "2a39d6e62d98aa5372432f2e5d2208d4"
|
|
255
|
+
},
|
|
256
|
+
body: {
|
|
257
|
+
apiKey: "ad7a5b0b816ca314b69c813ae1368232",
|
|
258
|
+
label: "node",
|
|
259
|
+
publicOrigin: "https://example.com:4321",
|
|
260
|
+
organizationId: "org-1"
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
},
|
|
266
|
+
responseType: [
|
|
267
|
+
{
|
|
268
|
+
type: "INoContentResponse"
|
|
269
|
+
}
|
|
270
|
+
],
|
|
271
|
+
defaultAuthorization: DEFAULT_AUTHORIZATION_WRITER
|
|
272
|
+
};
|
|
273
|
+
return [
|
|
274
|
+
tenantListRoute,
|
|
275
|
+
tenantCreateRoute,
|
|
276
|
+
tenantGetByIdRoute,
|
|
277
|
+
tenantGetByApiKeyRoute,
|
|
278
|
+
tenantGetByPublicOriginRoute,
|
|
279
|
+
tenantRemoveRoute,
|
|
280
|
+
tenantUpdateRoute
|
|
281
|
+
];
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Get the list of tenants.
|
|
285
|
+
* @param httpRequestContext The request context for the API.
|
|
286
|
+
* @param componentName The name of the component to use in the routes.
|
|
287
|
+
* @param request The request.
|
|
288
|
+
* @returns The response object with additional http response properties.
|
|
289
|
+
*/
|
|
290
|
+
export async function tenantList(httpRequestContext, componentName, request) {
|
|
291
|
+
const component = ComponentFactory.get(componentName);
|
|
292
|
+
const result = await component.query(HttpParameterHelper.objectFromString(request.query?.conditions), HttpParameterHelper.arrayFromString(request.query?.properties), request.query?.cursor, Coerce.integer(request.query?.limit));
|
|
293
|
+
const headers = {};
|
|
294
|
+
const contextIds = await ContextIdStore.getContextIds();
|
|
295
|
+
const publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];
|
|
296
|
+
HttpHeaderHelper.buildCursor(headers, httpRequestContext.serverRequest.url, publicOrigin, result.cursor);
|
|
297
|
+
return {
|
|
298
|
+
headers,
|
|
299
|
+
body: result.tenants.map(t => ({
|
|
300
|
+
...t,
|
|
301
|
+
publicOrigin: t.publicOrigin ?? publicOrigin
|
|
302
|
+
}))
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Get the tenant by id.
|
|
307
|
+
* @param httpRequestContext The request context for the API.
|
|
308
|
+
* @param componentName The name of the component to use in the routes.
|
|
309
|
+
* @param request The request.
|
|
310
|
+
* @returns The response object with additional http response properties.
|
|
311
|
+
*/
|
|
312
|
+
export async function tenantById(httpRequestContext, componentName, request) {
|
|
313
|
+
Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
|
|
314
|
+
const component = ComponentFactory.get(componentName);
|
|
315
|
+
const result = await component.get(request.pathParams.id);
|
|
316
|
+
if (!Is.stringValue(result.publicOrigin)) {
|
|
317
|
+
const contextIds = await ContextIdStore.getContextIds();
|
|
318
|
+
const publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];
|
|
319
|
+
result.publicOrigin ??= publicOrigin;
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
body: result
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Get the tenant by api key.
|
|
327
|
+
* @param httpRequestContext The request context for the API.
|
|
328
|
+
* @param componentName The name of the component to use in the routes.
|
|
329
|
+
* @param request The request.
|
|
330
|
+
* @returns The response object with additional http response properties.
|
|
331
|
+
*/
|
|
332
|
+
export async function tenantByApiKey(httpRequestContext, componentName, request) {
|
|
333
|
+
Guards.stringValue(ROUTES_SOURCE, "request.pathParams.apiKey", request.pathParams.apiKey);
|
|
334
|
+
const component = ComponentFactory.get(componentName);
|
|
335
|
+
const result = await component.getByApiKey(request.pathParams.apiKey);
|
|
336
|
+
if (!Is.stringValue(result.publicOrigin)) {
|
|
337
|
+
const contextIds = await ContextIdStore.getContextIds();
|
|
338
|
+
const publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];
|
|
339
|
+
result.publicOrigin ??= publicOrigin;
|
|
340
|
+
}
|
|
341
|
+
return {
|
|
342
|
+
body: result
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Get the tenant by public origin.
|
|
347
|
+
* @param httpRequestContext The request context for the API.
|
|
348
|
+
* @param componentName The name of the component to use in the routes.
|
|
349
|
+
* @param request The request.
|
|
350
|
+
* @returns The response object with additional http response properties.
|
|
351
|
+
*/
|
|
352
|
+
export async function tenantByPublicOrigin(httpRequestContext, componentName, request) {
|
|
353
|
+
Guards.stringValue(ROUTES_SOURCE, "request.pathParams.publicOrigin", request.pathParams.publicOrigin);
|
|
354
|
+
const component = ComponentFactory.get(componentName);
|
|
355
|
+
const result = await component.getByPublicOrigin(request.pathParams.publicOrigin);
|
|
356
|
+
if (!Is.stringValue(result.publicOrigin)) {
|
|
357
|
+
const contextIds = await ContextIdStore.getContextIds();
|
|
358
|
+
const publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];
|
|
359
|
+
result.publicOrigin ??= publicOrigin;
|
|
360
|
+
}
|
|
361
|
+
return {
|
|
362
|
+
body: result
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Remove the tenant by id.
|
|
367
|
+
* @param httpRequestContext The request context for the API.
|
|
368
|
+
* @param componentName The name of the component to use in the routes.
|
|
369
|
+
* @param request The request.
|
|
370
|
+
* @returns The response object with additional http response properties.
|
|
371
|
+
*/
|
|
372
|
+
export async function tenantRemove(httpRequestContext, componentName, request) {
|
|
373
|
+
Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
|
|
374
|
+
const component = ComponentFactory.get(componentName);
|
|
375
|
+
await component.remove(request.pathParams.id);
|
|
376
|
+
return {
|
|
377
|
+
statusCode: HttpStatusCode.noContent
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Create the tenant.
|
|
382
|
+
* @param httpRequestContext The request context for the API.
|
|
383
|
+
* @param componentName The name of the component to use in the routes.
|
|
384
|
+
* @param request The request.
|
|
385
|
+
* @param baseRouteName The base route name for the tenant.
|
|
386
|
+
* @returns The response object with additional http response properties.
|
|
387
|
+
*/
|
|
388
|
+
export async function tenantCreate(httpRequestContext, componentName, request, baseRouteName) {
|
|
389
|
+
Guards.stringValue(ROUTES_SOURCE, "request.body", request.body);
|
|
390
|
+
const component = ComponentFactory.get(componentName);
|
|
391
|
+
const createdId = await component.create(request.body);
|
|
392
|
+
const contextIds = await ContextIdStore.getContextIds();
|
|
393
|
+
const publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];
|
|
394
|
+
const headers = {};
|
|
395
|
+
HttpHeaderHelper.buildId(headers, createdId, HttpUrlHelper.combineOriginPath(publicOrigin, `${baseRouteName}/:id`));
|
|
396
|
+
return {
|
|
397
|
+
statusCode: HttpStatusCode.created,
|
|
398
|
+
headers
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Update the tenant.
|
|
403
|
+
* @param httpRequestContext The request context for the API.
|
|
404
|
+
* @param componentName The name of the component to use in the routes.
|
|
405
|
+
* @param request The request.
|
|
406
|
+
* @returns The response object with additional http response properties.
|
|
407
|
+
*/
|
|
408
|
+
export async function tenantUpdate(httpRequestContext, componentName, request) {
|
|
409
|
+
Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
|
|
410
|
+
Guards.stringValue(ROUTES_SOURCE, "request.body", request.body);
|
|
411
|
+
const component = ComponentFactory.get(componentName);
|
|
412
|
+
await component.update(request.body);
|
|
413
|
+
return {
|
|
414
|
+
statusCode: HttpStatusCode.noContent
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
//# sourceMappingURL=tenantRoutes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenantRoutes.js","sources":["../../src/tenantRoutes.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,mCAAmC;AACnC,uCAAuC;AACvC,OAAO,EACN,iBAAiB,EACjB,gBAAgB,EAChB,mBAAmB,EACnB,aAAa,EAQb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,cAAc,CAAC;AAEpE,OAAO,EAAE,WAAW,EAAE,cAAc,EAAqB,MAAM,aAAa,CAAC;AAW7E;;GAEG;AACH,MAAM,aAAa,GAAG,cAAc,CAAC;AAErC;;GAEG;AACH,MAAM,4BAA4B,GAAwB;IACzD,UAAU,EAAE,aAAa;IACzB,IAAI,EAAE,cAAc;CACpB,CAAC;AACF,MAAM,4BAA4B,GAAwB;IACzD,UAAU,EAAE,cAAc;IAC1B,IAAI,EAAE,cAAc;IACpB,QAAQ,EAAE,CAAC,4BAA4B,CAAC,UAAU,CAAC;CACnD,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAW;IAClC;QACC,IAAI,EAAE,SAAS;QACf,WAAW,EAAE,wCAAwC;KACrD;CACD,CAAC;AAEF;;;;;G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AAE,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAClF,MAAM,CAAC,WAAW,CAAC,aAAa,EAAE,cAAc,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAChE,MAAM,SAAS,GAAG,gBAAgB,CAAC,GAAG,CAAwB,aAAa,CAAC,CAAC;IAE7E,MAAM,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAErC,OAAO;QACN,UAAU,EAAE,cAAc,CAAC,SAAS;KACpC,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// Copyright 2026 3Sixty Trade Ltd.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tHttpContextIdKeys,\n\tHttpHeaderHelper,\n\tHttpParameterHelper,\n\tHttpUrlHelper,\n\ttype ICreatedResponse,\n\ttype IHttpRequestContext,\n\ttype INoContentResponse,\n\ttype IRestRoute,\n\ttype ITag,\n\ttype ITenantAdminComponent,\n\ttype IRouteAuthorization\n} from \"@3sixty/api-models\";\nimport { ContextIdStore } from \"@3sixty/context\";\nimport { Coerce, ComponentFactory, Guards, Is } from \"@3sixty/core\";\n\nimport { HeaderTypes, HttpStatusCode, type IHttpHeaders } from \"@3sixty/web\";\nimport type { ITenantCreateRequest } from \"./models/api/ITenantCreateRequest.js\";\nimport type { ITenantGetByApiKeyRequest } from \"./models/api/ITenantGetByApiKeyRequest.js\";\nimport type { ITenantGetByIdRequest } from \"./models/api/ITenantGetByIdRequest.js\";\nimport type { ITenantGetByPublicOriginRequest } from \"./models/api/ITenantGetByPublicOriginRequest.js\";\nimport type { ITenantGetResponse } from \"./models/api/ITenantGetResponse.js\";\nimport type { ITenantListRequest } from \"./models/api/ITenantListRequest.js\";\nimport type { ITenantListResponse } from \"./models/api/ITenantListResponse.js\";\nimport type { ITenantRemoveRequest } from \"./models/api/ITenantRemoveRequest.js\";\nimport type { ITenantUpdateRequest } from \"./models/api/ITenantUpdateRequest.js\";\n\n/**\n * The source used when communicating about these routes.\n */\nconst ROUTES_SOURCE = \"tenantRoutes\";\n\n/**\n * The default authorization for the routes, used to seed authorization rules.\n */\nconst DEFAULT_AUTHORIZATION_READER: IRouteAuthorization = {\n\tpermission: \"tenant:read\",\n\trole: \"tenant-admin\"\n};\nconst DEFAULT_AUTHORIZATION_WRITER: IRouteAuthorization = {\n\tpermission: \"tenant:write\",\n\trole: \"tenant-admin\",\n\tinherits: [DEFAULT_AUTHORIZATION_READER.permission]\n};\n\n/**\n * The tag to associate with the routes.\n */\nexport const tagsTenants: ITag[] = [\n\t{\n\t\tname: \"Tenants\",\n\t\tdescription: \"Tenants endpoints for the REST server.\"\n\t}\n];\n\n/**\n * The REST routes for tenant management.\n * @param baseRouteName Prefix to prepend to the paths.\n * @param componentName The name of the component to use in the routes stored in the ComponentFactory.\n * @returns The generated routes.\n */\nexport function generateRestRoutesTenants(\n\tbaseRouteName: string,\n\tcomponentName: string\n): IRestRoute[] {\n\tconst tenantListRoute: IRestRoute<ITenantListRequest, ITenantListResponse> = {\n\t\toperationId: \"tenantList\",\n\t\tsummary: \"Get the list of tenants\",\n\t\ttag: tagsTenants[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttenantList(httpRequestContext, componentName, request),\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: \"ITenantListResponse\",\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"tenantListResponse\",\n\t\t\t\t\t\tdescription: \"The response for the list tenants request.\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\t[HeaderTypes.Link]: '<https://api.3sixty.global/tenants/1>; rel=\"next\"'\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tbody: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tid: \"2a39d6e62d98aa5372432f2e5d2208d4\",\n\t\t\t\t\t\t\t\t\tapiKey: \"ad7a5b0b816ca314b69c813ae1368232\",\n\t\t\t\t\t\t\t\t\tlabel: \"node\",\n\t\t\t\t\t\t\t\t\tdateCreated: \"2026-01-19T03:59:35.742Z\",\n\t\t\t\t\t\t\t\t\tdateModified: \"2026-01-19T03:59:35.742Z\",\n\t\t\t\t\t\t\t\t\tpublicOrigin: \"https://example.com:4321\",\n\t\t\t\t\t\t\t\t\torganizationId: \"org-1\"\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t],\n\t\tdefaultAuthorization: DEFAULT_AUTHORIZATION_READER\n\t};\n\n\tconst tenantGetByIdRoute: IRestRoute<ITenantGetByIdRequest, ITenantGetResponse> = {\n\t\toperationId: \"tenantGetById\",\n\t\tsummary: \"Get the tenant by id\",\n\t\ttag: tagsTenants[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttenantById(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: \"ITenantGetByIdRequest\",\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"tenantGetByIdRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"2a39d6e62d98aa5372432f2e5d2208d4\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: \"ITenantGetResponse\",\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"tenantGetByIdResponse\",\n\t\t\t\t\t\tdescription: \"The response for the get tenant by id request.\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tid: \"2a39d6e62d98aa5372432f2e5d2208d4\",\n\t\t\t\t\t\t\t\tapiKey: \"ad7a5b0b816ca314b69c813ae1368232\",\n\t\t\t\t\t\t\t\tlabel: \"node\",\n\t\t\t\t\t\t\t\tdateCreated: \"2026-01-19T03:59:35.742Z\",\n\t\t\t\t\t\t\t\tdateModified: \"2026-01-19T03:59:35.742Z\",\n\t\t\t\t\t\t\t\tpublicOrigin: \"https://example.com:4321\",\n\t\t\t\t\t\t\t\torganizationId: \"org-1\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t],\n\t\tdefaultAuthorization: DEFAULT_AUTHORIZATION_READER\n\t};\n\n\tconst tenantGetByApiKeyRoute: IRestRoute<ITenantGetByApiKeyRequest, ITenantGetResponse> = {\n\t\toperationId: \"tenantGetByApiKey\",\n\t\tsummary: \"Get the tenant by api key\",\n\t\ttag: tagsTenants[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/api-key/:apiKey`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttenantByApiKey(httpRequestContext, componentName, request),\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: \"ITenantGetResponse\",\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"tenantGetByApiKeyResponse\",\n\t\t\t\t\t\tdescription: \"The response for the get tenant by api key request.\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tid: \"2a39d6e62d98aa5372432f2e5d2208d4\",\n\t\t\t\t\t\t\t\tapiKey: \"ad7a5b0b816ca314b69c813ae1368232\",\n\t\t\t\t\t\t\t\tlabel: \"node\",\n\t\t\t\t\t\t\t\tdateCreated: \"2026-01-19T03:59:35.742Z\",\n\t\t\t\t\t\t\t\tdateModified: \"2026-01-19T03:59:35.742Z\",\n\t\t\t\t\t\t\t\tpublicOrigin: \"https://example.com:4321\",\n\t\t\t\t\t\t\t\torganizationId: \"org-1\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t],\n\t\tdefaultAuthorization: DEFAULT_AUTHORIZATION_READER\n\t};\n\n\tconst tenantGetByPublicOriginRoute: IRestRoute = {\n\t\toperationId: \"tenantGetByPublicOrigin\",\n\t\tsummary: \"Get the tenant by public origin\",\n\t\ttag: tagsTenants[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/public-origin/:publicOrigin`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttenantByPublicOrigin(httpRequestContext, componentName, request),\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: \"ITenantGetResponse\",\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"tenantGetByPublicOriginResponse\",\n\t\t\t\t\t\tdescription: \"The response for the get tenant by public origin request.\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tid: \"2a39d6e62d98aa5372432f2e5d2208d4\",\n\t\t\t\t\t\t\t\tapiKey: \"ad7a5b0b816ca314b69c813ae1368232\",\n\t\t\t\t\t\t\t\tlabel: \"node\",\n\t\t\t\t\t\t\t\tdateCreated: \"2026-01-19T03:59:35.742Z\",\n\t\t\t\t\t\t\t\tpublicOrigin: \"https://example.com:4321\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t],\n\t\tdefaultAuthorization: DEFAULT_AUTHORIZATION_READER\n\t};\n\n\tconst tenantRemoveRoute: IRestRoute<ITenantRemoveRequest, INoContentResponse> = {\n\t\toperationId: \"tenantRemove\",\n\t\tsummary: \"Remove the tenant by id\",\n\t\ttag: tagsTenants[0].name,\n\t\tmethod: \"DELETE\",\n\t\tpath: `${baseRouteName}/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttenantRemove(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: \"ITenantRemoveRequest\",\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"tenantRemoveRequestExample\",\n\t\t\t\t\tdescription: \"The request for the remove tenant by id request.\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"2a39d6e62d98aa5372432f2e5d2208d4\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: \"INoContentResponse\"\n\t\t\t}\n\t\t],\n\t\tdefaultAuthorization: DEFAULT_AUTHORIZATION_WRITER\n\t};\n\n\tconst tenantCreateRoute: IRestRoute<ITenantCreateRequest, ICreatedResponse> = {\n\t\toperationId: \"tenantCreate\",\n\t\tsummary: \"Create a new tenant\",\n\t\ttag: tagsTenants[0].name,\n\t\tmethod: \"POST\",\n\t\tpath: `${baseRouteName}/`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttenantCreate(httpRequestContext, componentName, request, baseRouteName),\n\t\trequestType: {\n\t\t\ttype: \"ITenantCreateRequest\",\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"tenantCreateRequestExample\",\n\t\t\t\t\tdescription: \"The request for the create tenant request.\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tapiKey: \"ad7a5b0b816ca314b69c813ae1368232\",\n\t\t\t\t\t\t\tlabel: \"node\",\n\t\t\t\t\t\t\tpublicOrigin: \"https://example.com:4321\",\n\t\t\t\t\t\t\torganizationId: \"org-1\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: \"ICreatedResponse\"\n\t\t\t}\n\t\t],\n\t\tdefaultAuthorization: DEFAULT_AUTHORIZATION_WRITER\n\t};\n\n\tconst tenantUpdateRoute: IRestRoute<ITenantUpdateRequest, INoContentResponse> = {\n\t\toperationId: \"tenantUpdate\",\n\t\tsummary: \"Update an existing tenant\",\n\t\ttag: tagsTenants[0].name,\n\t\tmethod: \"PUT\",\n\t\tpath: `${baseRouteName}/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttenantUpdate(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: \"ITenantUpdateRequest\",\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"tenantUpdateRequestExample\",\n\t\t\t\t\tdescription: \"The request for the update tenant request.\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"2a39d6e62d98aa5372432f2e5d2208d4\"\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tapiKey: \"ad7a5b0b816ca314b69c813ae1368232\",\n\t\t\t\t\t\t\tlabel: \"node\",\n\t\t\t\t\t\t\tpublicOrigin: \"https://example.com:4321\",\n\t\t\t\t\t\t\torganizationId: \"org-1\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: \"INoContentResponse\"\n\t\t\t}\n\t\t],\n\t\tdefaultAuthorization: DEFAULT_AUTHORIZATION_WRITER\n\t};\n\n\treturn [\n\t\ttenantListRoute,\n\t\ttenantCreateRoute,\n\t\ttenantGetByIdRoute,\n\t\ttenantGetByApiKeyRoute,\n\t\ttenantGetByPublicOriginRoute,\n\t\ttenantRemoveRoute,\n\t\ttenantUpdateRoute\n\t];\n}\n\n/**\n * Get the list of tenants.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function tenantList(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITenantListRequest\n): Promise<ITenantListResponse> {\n\tconst component = ComponentFactory.get<ITenantAdminComponent>(componentName);\n\n\tconst result = await component.query(\n\t\tHttpParameterHelper.objectFromString(request.query?.conditions),\n\t\tHttpParameterHelper.arrayFromString(request.query?.properties),\n\t\trequest.query?.cursor,\n\t\tCoerce.integer(request.query?.limit)\n\t);\n\n\tconst headers: ITenantListResponse[\"headers\"] = {};\n\tconst contextIds = await ContextIdStore.getContextIds();\n\tconst publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];\n\n\tHttpHeaderHelper.buildCursor(\n\t\theaders,\n\t\thttpRequestContext.serverRequest.url,\n\t\tpublicOrigin,\n\t\tresult.cursor\n\t);\n\n\treturn {\n\t\theaders,\n\t\tbody: result.tenants.map(t => ({\n\t\t\t...t,\n\t\t\tpublicOrigin: t.publicOrigin ?? publicOrigin\n\t\t}))\n\t};\n}\n\n/**\n * Get the tenant by id.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function tenantById(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITenantGetByIdRequest\n): Promise<ITenantGetResponse> {\n\tGuards.stringValue(ROUTES_SOURCE, \"request.pathParams.id\", request.pathParams.id);\n\tconst component = ComponentFactory.get<ITenantAdminComponent>(componentName);\n\n\tconst result = await component.get(request.pathParams.id);\n\n\tif (!Is.stringValue(result.publicOrigin)) {\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];\n\n\t\tresult.publicOrigin ??= publicOrigin;\n\t}\n\n\treturn {\n\t\tbody: result\n\t};\n}\n\n/**\n * Get the tenant by api key.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function tenantByApiKey(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITenantGetByApiKeyRequest\n): Promise<ITenantGetResponse> {\n\tGuards.stringValue(ROUTES_SOURCE, \"request.pathParams.apiKey\", request.pathParams.apiKey);\n\tconst component = ComponentFactory.get<ITenantAdminComponent>(componentName);\n\n\tconst result = await component.getByApiKey(request.pathParams.apiKey);\n\n\tif (!Is.stringValue(result.publicOrigin)) {\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];\n\t\tresult.publicOrigin ??= publicOrigin;\n\t}\n\n\treturn {\n\t\tbody: result\n\t};\n}\n\n/**\n * Get the tenant by public origin.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function tenantByPublicOrigin(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITenantGetByPublicOriginRequest\n): Promise<ITenantGetResponse> {\n\tGuards.stringValue(\n\t\tROUTES_SOURCE,\n\t\t\"request.pathParams.publicOrigin\",\n\t\trequest.pathParams.publicOrigin\n\t);\n\tconst component = ComponentFactory.get<ITenantAdminComponent>(componentName);\n\n\tconst result = await component.getByPublicOrigin(request.pathParams.publicOrigin);\n\n\tif (!Is.stringValue(result.publicOrigin)) {\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];\n\t\tresult.publicOrigin ??= publicOrigin;\n\t}\n\n\treturn {\n\t\tbody: result\n\t};\n}\n\n/**\n * Remove the tenant by id.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function tenantRemove(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITenantRemoveRequest\n): Promise<INoContentResponse> {\n\tGuards.stringValue(ROUTES_SOURCE, \"request.pathParams.id\", request.pathParams.id);\n\tconst component = ComponentFactory.get<ITenantAdminComponent>(componentName);\n\n\tawait component.remove(request.pathParams.id);\n\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n\n/**\n * Create the tenant.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @param baseRouteName The base route name for the tenant.\n * @returns The response object with additional http response properties.\n */\nexport async function tenantCreate(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITenantCreateRequest,\n\tbaseRouteName: string\n): Promise<ICreatedResponse> {\n\tGuards.stringValue(ROUTES_SOURCE, \"request.body\", request.body);\n\tconst component = ComponentFactory.get<ITenantAdminComponent>(componentName);\n\n\tconst createdId = await component.create(request.body);\n\n\tconst contextIds = await ContextIdStore.getContextIds();\n\tconst publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];\n\n\tconst headers: IHttpHeaders = {};\n\tHttpHeaderHelper.buildId(\n\t\theaders,\n\t\tcreatedId,\n\t\tHttpUrlHelper.combineOriginPath(publicOrigin, `${baseRouteName}/:id`)\n\t);\n\n\treturn {\n\t\tstatusCode: HttpStatusCode.created,\n\t\theaders\n\t};\n}\n\n/**\n * Update the tenant.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function tenantUpdate(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITenantUpdateRequest\n): Promise<INoContentResponse> {\n\tGuards.stringValue(ROUTES_SOURCE, \"request.pathParams.id\", request.pathParams.id);\n\tGuards.stringValue(ROUTES_SOURCE, \"request.body\", request.body);\n\tconst component = ComponentFactory.get<ITenantAdminComponent>(componentName);\n\n\tawait component.update(request.body);\n\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n"]}
|