@webpieces/openapi-generator 0.4.923 → 0.4.925
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpieces/openapi-generator",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.925",
|
|
4
4
|
"description": "Render an ApiDocModel to OpenAPI 3.1.0 — the canonical internal document and the customer-published one, JSON and YAML, from one generation pass. Ships the wp-openapi bin.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@webpieces/api-doc-model": "0.4.
|
|
27
|
-
"@webpieces/core-util": "0.4.
|
|
26
|
+
"@webpieces/api-doc-model": "0.4.925",
|
|
27
|
+
"@webpieces/core-util": "0.4.925",
|
|
28
28
|
"typescript": "5.9.3"
|
|
29
29
|
},
|
|
30
30
|
"bin": {
|
|
@@ -37,6 +37,7 @@ class SecurityDeriver {
|
|
|
37
37
|
if (methods.length === 1 && methods[0].kind === 'apikey')
|
|
38
38
|
return apiKeyRequirement;
|
|
39
39
|
const alternatives = [];
|
|
40
|
+
const builtins = new Set();
|
|
40
41
|
for (const method of methods) {
|
|
41
42
|
if (method.kind === 'apikey') {
|
|
42
43
|
if (!apiKeyRequirement)
|
|
@@ -49,6 +50,11 @@ class SecurityDeriver {
|
|
|
49
50
|
// authentication extension rather than inventing a header or publishing a subset.
|
|
50
51
|
if (!name)
|
|
51
52
|
return undefined;
|
|
53
|
+
// `oidcSameProject()` + `oidcSvcs([...])` are two caller scopes of ONE wire credential (the
|
|
54
|
+
// same bearer token), so the scheme is one alternative, listed once.
|
|
55
|
+
if (builtins.has(name))
|
|
56
|
+
continue;
|
|
57
|
+
builtins.add(name);
|
|
52
58
|
alternatives.push(new JsonObject_1.JsonObject().set(name, []));
|
|
53
59
|
}
|
|
54
60
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SecurityDeriver.js","sourceRoot":"","sources":["../../../../../../packages/docs/openapi-generator/src/generate/SecurityDeriver.ts"],"names":[],"mappings":";;;AACA,mDAA2D;AAC3D,sEAAmE;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAa,eAAe;IACxB,SAAS,CAAC,IAAgC,EAAE,iBAAmD;QAC3F,MAAM,OAAO,GAAG,IAAI,EAAE,OAAO,CAAC;QAC9B,IAAI,CAAC,OAAO,EAAE,MAAM;YAAE,OAAO,SAAS,CAAC;QACvC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO,iBAAiB,CAAC;QACnF,MAAM,YAAY,GAAgB,EAAE,CAAC;QACrC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC3B,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC3B,IAAI,CAAC,iBAAiB;oBAAE,MAAM,IAAI,KAAK,CAAC,qEAAqE,CAAC,CAAC;gBAC/G,YAAY,CAAC,IAAI,CAAC,GAAG,iBAAiB,CAAC,CAAC;YAC5C,CAAC;iBAAM,CAAC;gBACJ,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;gBAC3C,mFAAmF;gBACnF,kFAAkF;gBAClF,IAAI,CAAC,IAAI;oBAAE,OAAO,SAAS,CAAC;gBAC5B,YAAY,CAAC,IAAI,CAAC,IAAI,uBAAU,EAAE,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;YACtD,CAAC;QACL,CAAC;QACD,OAAO,YAAY,CAAC;IACxB,CAAC;IAED,iBAAiB,CAAC,OAAmB,EAAE,SAAwC;QAC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AClB,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC;aACrB,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC;aACnB,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,IAAI,CAAC;aAC5B,GAAG,CAAC,aAAa,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IACpD,CAAC;IAED,8FAA8F;IAC9F,WAAW,CAAC,WAA8B;QACtC,MAAM,KAAK,GAAG,IAAI,uBAAU,EAAE,CAAC;QAC/B,KAAK,MAAM,IAAI,IAAI,WAAW,EAAE,CAAC;YAC7B,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACxB,CAAC;QACD,OAAO,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;CACJ;AA5GD,0CA4GC","sourcesContent":["import { DocumentedAuth, DocumentedApiKey, DocumentedApiKeyCredential, DocumentedEndpoint } from '@webpieces/api-doc-model';\nimport { JsonObject, JsonValue } from '../json/JsonObject';\nimport { OpenApiGenerationError } from '../OpenApiGenerationError';\n\n/**\n * `components.securitySchemes` and the `security` requirement, DERIVED from the contract's own\n * `apiKey(regime, credentials)`. The manifest contributes the published KEYS and nothing else.\n *\n * ## Why the schemes are not in the manifest\n *\n * A `components.securitySchemes` block in a JSON file would be a second copy of header names the\n * running server never reads — and nothing could contradict it. Deriving them from the decorator\n * means the published document and the hook that enforces the credential are the same declaration,\n * so the document cannot say `x-api-key` while the server checks `x-partner-key`. The manifest keeps\n * the KEYS because a scheme key is partner-visible naming, which is a product decision.\n *\n * ## ONE requirement object holding every scheme, never a list of one-key objects\n *\n * OpenAPI's `security` is a list of ALTERNATIVES, and each alternative is an object whose keys must\n * ALL be satisfied. So:\n *\n * ```\n * [{ PartnerApiKey: [], PartnerOrg: [] }] // AND — present both. What a real regime means.\n * [{ PartnerApiKey: [] }, { PartnerOrg: [] }] // OR — \"either header alone suffices\". A lie.\n * ```\n *\n * The second form is the dangerous default because it is what \"a list of credentials\" reads like,\n * and NO BUILD COULD CONTRADICT IT: the document would be well-formed, the tooling green, and a\n * partner told they may skip the organization header. Hence one object, always.\n */\nexport class SecurityDeriver {\n operation(auth: DocumentedAuth | undefined, apiKeyRequirement: readonly JsonValue[] | undefined): readonly JsonValue[] | undefined {\n const methods = auth?.methods;\n if (!methods?.length) return undefined;\n if (methods.length === 1 && methods[0].kind === 'apikey') return apiKeyRequirement;\n const alternatives: JsonValue[] = [];\n for (const method of methods) {\n if (method.kind === 'apikey') {\n if (!apiKeyRequirement) throw new Error('Mixed credentials require an explicit API-key security requirement.');\n alternatives.push(...apiKeyRequirement);\n } else {\n const name = this.builtinName(method.kind);\n // Vendor signatures do not declare their wire headers. Keep the complete canonical\n // authentication extension rather than inventing a header or publishing a subset.\n if (!name) return undefined;\n alternatives.push(new JsonObject().set(name, []));\n }\n }\n return alternatives;\n }\n\n addBuiltinSchemes(schemes: JsonObject, endpoints: readonly DocumentedEndpoint[]): JsonObject {\n const added = new Set<string>();\n for (const endpoint of endpoints) for (const method of endpoint.auth?.methods ?? []) {\n const name = this.builtinName(method.kind);\n if (!name || added.has(name)) continue;\n if (schemes.has(name)) throw new Error(`securitySchemeNames reserves '${name}' for framework credentials; choose another API-key scheme name.`);\n added.add(name);\n schemes.set(name, new JsonObject().set('type', 'http').set('scheme', method.kind === 'shared-secret' ? 'Webpieces' : 'bearer'));\n }\n return schemes;\n }\n\n private builtinName(kind: string): string | undefined {\n switch (kind) {\n case 'jwt': return 'WebpiecesJwt';\n case 'oidc': return 'WebpiecesOidc';\n case 'shared-secret': return 'WebpiecesSharedSecret';\n default: return undefined;\n }\n }\n\n /** Every scheme, keyed by the manifest's published names, in declaration order. */\n schemes(\n apiKey: DocumentedApiKey,\n schemeNames: readonly string[],\n manifestPath: string,\n ): JsonObject {\n if (schemeNames.length !== apiKey.credentials.length) {\n throw new OpenApiGenerationError(\n `securitySchemeNames has ${schemeNames.length} name(s) but the '${apiKey.regime}' ` +\n `regime declares ${apiKey.credentials.length} credential(s)`,\n manifestPath,\n 'Give exactly one published scheme name per credential, in the order the ' +\n 'contract declares them. The names are partner-visible; the credentials are not ' +\n 'the manifest to choose.',\n );\n }\n const schemes = new JsonObject();\n for (let i = 0; i < schemeNames.length; i++) {\n schemes.set(schemeNames[i]!, this.scheme(apiKey.credentials[i]!, manifestPath));\n }\n return schemes;\n }\n\n /**\n * ONE credential -> ONE scheme. The two shapes are STRUCTURALLY different documents, which is\n * exactly why `ApiKeyCredential` is a union in the framework rather than one optional field.\n */\n private scheme(credential: DocumentedApiKeyCredential, manifestPath: string): JsonObject {\n if (credential.location === 'bearer') {\n return new JsonObject()\n .set('type', 'http')\n .set('scheme', 'bearer')\n .set('description', credential.description);\n }\n if (credential.location !== 'header') {\n throw new OpenApiGenerationError(\n `unknown api-key credential location '${credential.location}'`,\n manifestPath,\n \"A credential rides `in: 'header'` with its ContextKey, or `in: 'bearer'`.\",\n );\n }\n // The extractor reads the name off the credential's ContextKey and refuses, quoting the key,\n // when it cannot — so a model reaching here without one was built by hand, not extracted.\n if (credential.name === undefined) {\n throw new OpenApiGenerationError(\n 'a header api-key credential reached the generator with no resolved header name',\n manifestPath,\n \"Declare it as `{ in: 'header', key: MyKeys.API_KEY }` and let the extractor read the \" +\n \"header name off that ContextKey's httpHeader.\",\n );\n }\n return new JsonObject()\n .set('type', 'apiKey')\n .set('in', 'header')\n .set('name', credential.name)\n .set('description', credential.description);\n }\n\n /** The ONE AND-ed requirement — see the class doc for why it is one object and not a list. */\n requirement(schemeNames: readonly string[]): readonly JsonValue[] {\n const anded = new JsonObject();\n for (const name of schemeNames) {\n anded.set(name, []);\n }\n return [anded];\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"SecurityDeriver.js","sourceRoot":"","sources":["../../../../../../packages/docs/openapi-generator/src/generate/SecurityDeriver.ts"],"names":[],"mappings":";;;AACA,mDAA2D;AAC3D,sEAAmE;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAa,eAAe;IACxB,SA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AAG,CAAC,MAAM,EAAE,UAAU,CAAC,IAAI,CAAC;aAC5B,GAAG,CAAC,aAAa,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IACpD,CAAC;IAED,8FAA8F;IAC9F,WAAW,CAAC,WAA8B;QACtC,MAAM,KAAK,GAAG,IAAI,uBAAU,EAAE,CAAC;QAC/B,KAAK,MAAM,IAAI,IAAI,WAAW,EAAE,CAAC;YAC7B,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACxB,CAAC;QACD,OAAO,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;CACJ;AAjHD,0CAiHC","sourcesContent":["import { DocumentedAuth, DocumentedApiKey, DocumentedApiKeyCredential, DocumentedEndpoint } from '@webpieces/api-doc-model';\nimport { JsonObject, JsonValue } from '../json/JsonObject';\nimport { OpenApiGenerationError } from '../OpenApiGenerationError';\n\n/**\n * `components.securitySchemes` and the `security` requirement, DERIVED from the contract's own\n * `apiKey(regime, credentials)`. The manifest contributes the published KEYS and nothing else.\n *\n * ## Why the schemes are not in the manifest\n *\n * A `components.securitySchemes` block in a JSON file would be a second copy of header names the\n * running server never reads — and nothing could contradict it. Deriving them from the decorator\n * means the published document and the hook that enforces the credential are the same declaration,\n * so the document cannot say `x-api-key` while the server checks `x-partner-key`. The manifest keeps\n * the KEYS because a scheme key is partner-visible naming, which is a product decision.\n *\n * ## ONE requirement object holding every scheme, never a list of one-key objects\n *\n * OpenAPI's `security` is a list of ALTERNATIVES, and each alternative is an object whose keys must\n * ALL be satisfied. So:\n *\n * ```\n * [{ PartnerApiKey: [], PartnerOrg: [] }] // AND — present both. What a real regime means.\n * [{ PartnerApiKey: [] }, { PartnerOrg: [] }] // OR — \"either header alone suffices\". A lie.\n * ```\n *\n * The second form is the dangerous default because it is what \"a list of credentials\" reads like,\n * and NO BUILD COULD CONTRADICT IT: the document would be well-formed, the tooling green, and a\n * partner told they may skip the organization header. Hence one object, always.\n */\nexport class SecurityDeriver {\n operation(auth: DocumentedAuth | undefined, apiKeyRequirement: readonly JsonValue[] | undefined): readonly JsonValue[] | undefined {\n const methods = auth?.methods;\n if (!methods?.length) return undefined;\n if (methods.length === 1 && methods[0].kind === 'apikey') return apiKeyRequirement;\n const alternatives: JsonValue[] = [];\n const builtins = new Set<string>();\n for (const method of methods) {\n if (method.kind === 'apikey') {\n if (!apiKeyRequirement) throw new Error('Mixed credentials require an explicit API-key security requirement.');\n alternatives.push(...apiKeyRequirement);\n } else {\n const name = this.builtinName(method.kind);\n // Vendor signatures do not declare their wire headers. Keep the complete canonical\n // authentication extension rather than inventing a header or publishing a subset.\n if (!name) return undefined;\n // `oidcSameProject()` + `oidcSvcs([...])` are two caller scopes of ONE wire credential (the\n // same bearer token), so the scheme is one alternative, listed once.\n if (builtins.has(name)) continue;\n builtins.add(name);\n alternatives.push(new JsonObject().set(name, []));\n }\n }\n return alternatives;\n }\n\n addBuiltinSchemes(schemes: JsonObject, endpoints: readonly DocumentedEndpoint[]): JsonObject {\n const added = new Set<string>();\n for (const endpoint of endpoints) for (const method of endpoint.auth?.methods ?? []) {\n const name = this.builtinName(method.kind);\n if (!name || added.has(name)) continue;\n if (schemes.has(name)) throw new Error(`securitySchemeNames reserves '${name}' for framework credentials; choose another API-key scheme name.`);\n added.add(name);\n schemes.set(name, new JsonObject().set('type', 'http').set('scheme', method.kind === 'shared-secret' ? 'Webpieces' : 'bearer'));\n }\n return schemes;\n }\n\n private builtinName(kind: string): string | undefined {\n switch (kind) {\n case 'jwt': return 'WebpiecesJwt';\n case 'oidc': return 'WebpiecesOidc';\n case 'shared-secret': return 'WebpiecesSharedSecret';\n default: return undefined;\n }\n }\n\n /** Every scheme, keyed by the manifest's published names, in declaration order. */\n schemes(\n apiKey: DocumentedApiKey,\n schemeNames: readonly string[],\n manifestPath: string,\n ): JsonObject {\n if (schemeNames.length !== apiKey.credentials.length) {\n throw new OpenApiGenerationError(\n `securitySchemeNames has ${schemeNames.length} name(s) but the '${apiKey.regime}' ` +\n `regime declares ${apiKey.credentials.length} credential(s)`,\n manifestPath,\n 'Give exactly one published scheme name per credential, in the order the ' +\n 'contract declares them. The names are partner-visible; the credentials are not ' +\n 'the manifest to choose.',\n );\n }\n const schemes = new JsonObject();\n for (let i = 0; i < schemeNames.length; i++) {\n schemes.set(schemeNames[i]!, this.scheme(apiKey.credentials[i]!, manifestPath));\n }\n return schemes;\n }\n\n /**\n * ONE credential -> ONE scheme. The two shapes are STRUCTURALLY different documents, which is\n * exactly why `ApiKeyCredential` is a union in the framework rather than one optional field.\n */\n private scheme(credential: DocumentedApiKeyCredential, manifestPath: string): JsonObject {\n if (credential.location === 'bearer') {\n return new JsonObject()\n .set('type', 'http')\n .set('scheme', 'bearer')\n .set('description', credential.description);\n }\n if (credential.location !== 'header') {\n throw new OpenApiGenerationError(\n `unknown api-key credential location '${credential.location}'`,\n manifestPath,\n \"A credential rides `in: 'header'` with its ContextKey, or `in: 'bearer'`.\",\n );\n }\n // The extractor reads the name off the credential's ContextKey and refuses, quoting the key,\n // when it cannot — so a model reaching here without one was built by hand, not extracted.\n if (credential.name === undefined) {\n throw new OpenApiGenerationError(\n 'a header api-key credential reached the generator with no resolved header name',\n manifestPath,\n \"Declare it as `{ in: 'header', key: MyKeys.API_KEY }` and let the extractor read the \" +\n \"header name off that ContextKey's httpHeader.\",\n );\n }\n return new JsonObject()\n .set('type', 'apiKey')\n .set('in', 'header')\n .set('name', credential.name)\n .set('description', credential.description);\n }\n\n /** The ONE AND-ed requirement — see the class doc for why it is one object and not a list. */\n requirement(schemeNames: readonly string[]): readonly JsonValue[] {\n const anded = new JsonObject();\n for (const name of schemeNames) {\n anded.set(name, []);\n }\n return [anded];\n }\n}\n"]}
|