From b3c23ba99c361bdca3ab9c44017b6e5c044f40a7 Mon Sep 17 00:00:00 2001 From: Chris Wiggins Date: Wed, 1 Jan 2025 12:59:50 +1300 Subject: [PATCH] feat: Add support for HTTP Bearer Security Scheme --- .changeset/afraid-mangos-search.md | 5 +++ .../src/plugins/@hey-api/sdk/plugin.ts | 21 +++++++++++- packages/openapi-ts/test/3.0.x.test.ts | 13 ++++++++ packages/openapi-ts/test/3.1.x.test.ts | 13 ++++++++ .../3.0.x/security-http-bearer/index.ts | 3 ++ .../3.0.x/security-http-bearer/sdk.gen.ts | 20 ++++++++++++ .../3.0.x/security-http-bearer/types.gen.ts | 15 +++++++++ .../3.1.x/security-http-bearer/index.ts | 3 ++ .../3.1.x/security-http-bearer/sdk.gen.ts | 20 ++++++++++++ .../3.1.x/security-http-bearer/types.gen.ts | 15 +++++++++ .../test/spec/3.0.x/security-http-bearer.json | 32 +++++++++++++++++++ .../test/spec/3.1.x/security-http-bearer.json | 32 +++++++++++++++++++ 12 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 .changeset/afraid-mangos-search.md create mode 100644 packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/index.ts create mode 100644 packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/sdk.gen.ts create mode 100644 packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/types.gen.ts create mode 100644 packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/index.ts create mode 100644 packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/sdk.gen.ts create mode 100644 packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/types.gen.ts create mode 100644 packages/openapi-ts/test/spec/3.0.x/security-http-bearer.json create mode 100644 packages/openapi-ts/test/spec/3.1.x/security-http-bearer.json diff --git a/.changeset/afraid-mangos-search.md b/.changeset/afraid-mangos-search.md new file mode 100644 index 000000000..be1b0153e --- /dev/null +++ b/.changeset/afraid-mangos-search.md @@ -0,0 +1,5 @@ +--- +'@hey-api/openapi-ts': minor +--- + +Add support for HTTP Bearer Authentication Scheme diff --git a/packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts b/packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts index c993862ff..32001ecee 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts @@ -225,6 +225,8 @@ const operationStatements = ({ }> = []; for (const securitySchemeObject of operation.security) { + let supported = false; + if (securitySchemeObject.type === 'oauth2') { if (securitySchemeObject.flows.password) { security.push({ @@ -232,6 +234,8 @@ const operationStatements = ({ in: 'header', name: 'Authorization', }); + + supported = true; } } else if (securitySchemeObject.type === 'apiKey') { // TODO: parser - support cookies auth @@ -241,8 +245,23 @@ const operationStatements = ({ in: securitySchemeObject.in, name: securitySchemeObject.name, }); + + supported = true; + } + } else if (securitySchemeObject.type === 'http') { + if (securitySchemeObject.scheme === 'bearer') { + // Our accessToken function puts Bearer in front of the token, so it works for this scheme too + security.push({ + fn: 'accessToken', + in: 'header', + name: 'Authorization', + }); + + supported = true; } - } else { + } + + if (!supported) { console.warn( `❗️ SDK warning: security scheme isn't currently supported. Please open an issue if you'd like it added https://github.com/hey-api/openapi-ts/issues\n${JSON.stringify(securitySchemeObject, null, 2)}`, ); diff --git a/packages/openapi-ts/test/3.0.x.test.ts b/packages/openapi-ts/test/3.0.x.test.ts index f0b2397ec..ab61638ee 100644 --- a/packages/openapi-ts/test/3.0.x.test.ts +++ b/packages/openapi-ts/test/3.0.x.test.ts @@ -422,6 +422,19 @@ describe(`OpenAPI ${VERSION}`, () => { }), description: 'generates SDK functions with auth (api key)', }, + { + config: createConfig({ + input: 'security-http-bearer.json', + output: 'security-http-bearer', + plugins: [ + { + auth: true, + name: '@hey-api/sdk', + }, + ], + }), + description: 'generates SDK functions with auth (Bearer token)', + }, { config: createConfig({ input: 'security-oauth2.json', diff --git a/packages/openapi-ts/test/3.1.x.test.ts b/packages/openapi-ts/test/3.1.x.test.ts index 840e6e7f3..3c860f5fb 100644 --- a/packages/openapi-ts/test/3.1.x.test.ts +++ b/packages/openapi-ts/test/3.1.x.test.ts @@ -483,6 +483,19 @@ describe(`OpenAPI ${VERSION}`, () => { }), description: 'generates SDK functions with auth (api key)', }, + { + config: createConfig({ + input: 'security-http-bearer.json', + output: 'security-http-bearer', + plugins: [ + { + auth: true, + name: '@hey-api/sdk', + }, + ], + }), + description: 'generates SDK functions with auth (Bearer token)', + }, { config: createConfig({ input: 'security-oauth2.json', diff --git a/packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/index.ts b/packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/index.ts new file mode 100644 index 000000000..e64537d21 --- /dev/null +++ b/packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/index.ts @@ -0,0 +1,3 @@ +// This file is auto-generated by @hey-api/openapi-ts +export * from './types.gen'; +export * from './sdk.gen'; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/sdk.gen.ts b/packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/sdk.gen.ts new file mode 100644 index 000000000..a3425f992 --- /dev/null +++ b/packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/sdk.gen.ts @@ -0,0 +1,20 @@ +// This file is auto-generated by @hey-api/openapi-ts + +import { createClient, createConfig, type Options } from '@hey-api/client-fetch'; +import type { GetFooData } from './types.gen'; + +export const client = createClient(createConfig()); + +export const getFoo = (options?: Options) => { + return (options?.client ?? client).get({ + ...options, + security: [ + { + fn: 'accessToken', + in: 'header', + name: 'Authorization' + } + ], + url: '/foo' + }); +}; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/types.gen.ts b/packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/types.gen.ts new file mode 100644 index 000000000..915f766f3 --- /dev/null +++ b/packages/openapi-ts/test/__snapshots__/3.0.x/security-http-bearer/types.gen.ts @@ -0,0 +1,15 @@ +// This file is auto-generated by @hey-api/openapi-ts + +export type GetFooData = { + body?: never; + path?: never; + query?: never; + url: '/foo'; +}; + +export type GetFooResponses = { + /** + * OK + */ + 200: unknown; +}; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/index.ts b/packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/index.ts new file mode 100644 index 000000000..e64537d21 --- /dev/null +++ b/packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/index.ts @@ -0,0 +1,3 @@ +// This file is auto-generated by @hey-api/openapi-ts +export * from './types.gen'; +export * from './sdk.gen'; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/sdk.gen.ts b/packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/sdk.gen.ts new file mode 100644 index 000000000..a3425f992 --- /dev/null +++ b/packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/sdk.gen.ts @@ -0,0 +1,20 @@ +// This file is auto-generated by @hey-api/openapi-ts + +import { createClient, createConfig, type Options } from '@hey-api/client-fetch'; +import type { GetFooData } from './types.gen'; + +export const client = createClient(createConfig()); + +export const getFoo = (options?: Options) => { + return (options?.client ?? client).get({ + ...options, + security: [ + { + fn: 'accessToken', + in: 'header', + name: 'Authorization' + } + ], + url: '/foo' + }); +}; \ No newline at end of file diff --git a/packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/types.gen.ts b/packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/types.gen.ts new file mode 100644 index 000000000..915f766f3 --- /dev/null +++ b/packages/openapi-ts/test/__snapshots__/3.1.x/security-http-bearer/types.gen.ts @@ -0,0 +1,15 @@ +// This file is auto-generated by @hey-api/openapi-ts + +export type GetFooData = { + body?: never; + path?: never; + query?: never; + url: '/foo'; +}; + +export type GetFooResponses = { + /** + * OK + */ + 200: unknown; +}; \ No newline at end of file diff --git a/packages/openapi-ts/test/spec/3.0.x/security-http-bearer.json b/packages/openapi-ts/test/spec/3.0.x/security-http-bearer.json new file mode 100644 index 000000000..e79a7e647 --- /dev/null +++ b/packages/openapi-ts/test/spec/3.0.x/security-http-bearer.json @@ -0,0 +1,32 @@ +{ + "openapi": "3.0.4", + "info": { + "title": "OpenAPI 3.0.4 security bearer example", + "version": "1" + }, + "paths": { + "/foo": { + "get": { + "responses": { + "200": { + "description": "OK" + } + }, + "security": [ + { + "foo": [] + } + ] + } + } + }, + "components": { + "securitySchemes": { + "foo": { + "bearerFormat": "JWT", + "scheme": "bearer", + "type": "http" + } + } + } +} diff --git a/packages/openapi-ts/test/spec/3.1.x/security-http-bearer.json b/packages/openapi-ts/test/spec/3.1.x/security-http-bearer.json new file mode 100644 index 000000000..59719ec1d --- /dev/null +++ b/packages/openapi-ts/test/spec/3.1.x/security-http-bearer.json @@ -0,0 +1,32 @@ +{ + "openapi": "3.1.1", + "info": { + "title": "OpenAPI 3.1.1 security bearer example", + "version": "1" + }, + "paths": { + "/foo": { + "get": { + "responses": { + "200": { + "description": "OK" + } + }, + "security": [ + { + "foo": [] + } + ] + } + } + }, + "components": { + "securitySchemes": { + "foo": { + "bearerFormat": "JWT", + "scheme": "bearer", + "type": "http" + } + } + } +}