Skip to content

Commit

Permalink
Merge pull request #1648 from hey-api/feat/auth-open-id-connect
Browse files Browse the repository at this point in the history
fix: add support for openIdConnect auth flow
  • Loading branch information
mrlubos authored Jan 30, 2025
2 parents 3bdc656 + 66a9e45 commit b767e10
Show file tree
Hide file tree
Showing 32 changed files with 340 additions and 77 deletions.
1 change: 0 additions & 1 deletion .changeset/orange-bags-hope.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
'@hey-api/client-axios': patch
'@hey-api/client-fetch': patch
'@hey-api/client-next': patch
'@hey-api/client-nuxt': patch
---

Expand Down
1 change: 0 additions & 1 deletion .changeset/tiny-kings-reflect.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
'@hey-api/client-axios': patch
'@hey-api/client-fetch': patch
'@hey-api/client-next': patch
'@hey-api/client-nuxt': patch
---

Expand Down
5 changes: 5 additions & 0 deletions .changeset/unlucky-ligers-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: add support for openIdConnect auth flow
10 changes: 10 additions & 0 deletions packages/client-core/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
export type AuthToken = string | undefined;

export interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down
21 changes: 19 additions & 2 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ import {
import { serviceFunctionIdentifier } from './plugin-legacy';
import type { Config } from './types';

// type copied from client packages
interface Auth {
// copy-pasted from @hey-api/client-core
export interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down Expand Up @@ -129,6 +139,13 @@ const securitySchemeObjectToAuthObject = ({
}: {
securitySchemeObject: IR.SecurityObject;
}): Auth | undefined => {
if (securitySchemeObject.type === 'openIdConnect') {
return {
scheme: 'bearer',
type: 'http',
};
}

if (securitySchemeObject.type === 'oauth2') {
if (
securitySchemeObject.flows.password ||
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/test/2.0.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe(`OpenAPI ${version}`, () => {
},
{
config: createConfig({
input: 'security-oauth2.json',
input: 'security-oauth2.yaml',
output: 'security-oauth2',
plugins: [
'@hey-api/client-fetch',
Expand All @@ -261,7 +261,7 @@ describe(`OpenAPI ${version}`, () => {
},
{
config: createConfig({
input: 'security-oauth2.json',
input: 'security-oauth2.yaml',
output: 'security-false',
plugins: [
'@hey-api/client-fetch',
Expand Down
18 changes: 16 additions & 2 deletions packages/openapi-ts/test/3.0.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ describe(`OpenAPI ${version}`, () => {
},
{
config: createConfig({
input: 'security-oauth2.json',
input: 'security-oauth2.yaml',
output: 'security-oauth2',
plugins: [
'@hey-api/client-fetch',
Expand All @@ -462,7 +462,21 @@ describe(`OpenAPI ${version}`, () => {
},
{
config: createConfig({
input: 'security-oauth2.json',
input: 'security-open-id-connect.yaml',
output: 'security-open-id-connect',
plugins: [
'@hey-api/client-fetch',
{
auth: true,
name: '@hey-api/sdk',
},
],
}),
description: 'generates SDK functions with auth (OpenID Connect)',
},
{
config: createConfig({
input: 'security-oauth2.yaml',
output: 'security-false',
plugins: [
'@hey-api/client-fetch',
Expand Down
14 changes: 14 additions & 0 deletions packages/openapi-ts/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,20 @@ describe(`OpenAPI ${version}`, () => {
}),
description: 'generates SDK functions with auth (oauth2)',
},
{
config: createConfig({
input: 'security-open-id-connect.yaml',
output: 'security-open-id-connect',
plugins: [
'@hey-api/client-fetch',
{
auth: true,
name: '@hey-api/sdk',
},
],
}),
description: 'generates SDK functions with auth (OpenID Connect)',
},
{
config: createConfig({
input: 'security-oauth2.yaml',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file is auto-generated by @hey-api/openapi-ts

import { createClient, createConfig } from '@hey-api/client-fetch';

export const client = createClient(createConfig());
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,27 @@
// This file is auto-generated by @hey-api/openapi-ts

import type { Options as ClientOptions, TDataShape, Client } from '@hey-api/client-fetch';
import type { GetFooData } from './types.gen';
import { client as _heyApiClient } from './client.gen';

export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean> = ClientOptions<TData, ThrowOnError> & {
/**
* You can provide a client instance returned by `createClient()` instead of
* individual options. This might be also useful if you want to implement a
* custom client.
*/
client?: Client;
};

export const getFoo = <ThrowOnError extends boolean = false>(options?: Options<GetFooData, ThrowOnError>) => {
return (options?.client ?? _heyApiClient).get<unknown, unknown, ThrowOnError>({
security: [
{
scheme: 'bearer',
type: 'http'
}
],
url: '/foo',
...options
});
};
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
Expand Up @@ -2,7 +2,17 @@ import { CreateAxiosDefaults, AxiosStatic, AxiosResponse, AxiosError, AxiosInsta

type AuthToken = string | undefined;
interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ import { CreateAxiosDefaults, AxiosStatic, AxiosResponse, AxiosError, AxiosInsta

type AuthToken = string | undefined;
interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
type AuthToken = string | undefined;
interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
type AuthToken = string | undefined;
interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
type AuthToken = string | undefined;
interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
type AuthToken = string | undefined;
interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import { Ref } from 'vue';

type AuthToken = string | undefined;
interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import { Ref } from 'vue';

type AuthToken = string | undefined;
interface Auth {
/**
* Which part of the request do we use to send the auth?
*
* @default 'header'
*/
in?: 'header' | 'query';
/**
* Header or query parameter name.
*
* @default 'Authorization'
*/
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file is auto-generated by @hey-api/openapi-ts

import { createClient, createConfig } from '@hey-api/client-fetch';

export const client = createClient(createConfig());
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,27 @@
// This file is auto-generated by @hey-api/openapi-ts

import type { Options as ClientOptions, TDataShape, Client } from '@hey-api/client-fetch';
import type { GetFooData } from './types.gen';
import { client as _heyApiClient } from './client.gen';

export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean> = ClientOptions<TData, ThrowOnError> & {
/**
* You can provide a client instance returned by `createClient()` instead of
* individual options. This might be also useful if you want to implement a
* custom client.
*/
client?: Client;
};

export const getFoo = <ThrowOnError extends boolean = false>(options?: Options<GetFooData, ThrowOnError>) => {
return (options?.client ?? _heyApiClient).get<unknown, unknown, ThrowOnError>({
security: [
{
scheme: 'bearer',
type: 'http'
}
],
url: '/foo',
...options
});
};
Loading

0 comments on commit b767e10

Please sign in to comment.