Skip to content

Commit

Permalink
Merge pull request #1520 from chriswiggins/feat/add-http-bearer-security
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos authored Jan 1, 2025
2 parents a7aa2fe + b3c23ba commit ce95c28
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/afraid-mangos-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': minor
---

Add support for HTTP Bearer Authentication Scheme
21 changes: 20 additions & 1 deletion packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,17 @@ const operationStatements = ({
}> = [];

for (const securitySchemeObject of operation.security) {
let supported = false;

if (securitySchemeObject.type === 'oauth2') {
if (securitySchemeObject.flows.password) {
security.push({
fn: 'accessToken',
in: 'header',
name: 'Authorization',
});

supported = true;
}
} else if (securitySchemeObject.type === 'apiKey') {
// TODO: parser - support cookies auth
Expand All @@ -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)}`,
);
Expand Down
13 changes: 13 additions & 0 deletions packages/openapi-ts/test/3.0.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 13 additions & 0 deletions packages/openapi-ts/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
export * from './sdk.gen';
Original file line number Diff line number Diff line change
@@ -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 = <ThrowOnError extends boolean = false>(options?: Options<GetFooData, ThrowOnError>) => {
return (options?.client ?? client).get<unknown, unknown, ThrowOnError>({
...options,
security: [
{
fn: 'accessToken',
in: 'header',
name: 'Authorization'
}
],
url: '/foo'
});
};
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
export * from './sdk.gen';
Original file line number Diff line number Diff line change
@@ -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 = <ThrowOnError extends boolean = false>(options?: Options<GetFooData, ThrowOnError>) => {
return (options?.client ?? client).get<unknown, unknown, ThrowOnError>({
...options,
security: [
{
fn: 'accessToken',
in: 'header',
name: 'Authorization'
}
],
url: '/foo'
});
};
Original file line number Diff line number Diff line change
@@ -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;
};
32 changes: 32 additions & 0 deletions packages/openapi-ts/test/spec/3.0.x/security-http-bearer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
32 changes: 32 additions & 0 deletions packages/openapi-ts/test/spec/3.1.x/security-http-bearer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}

0 comments on commit ce95c28

Please sign in to comment.