Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add dedicated typing for oauth2 and oidc #2384

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,10 @@
"type": "oauth2",
"flows": {}
},
"openIdConnect": {
"type": "openIdConnect",
"openIdConnectUrl": "https://oauth.example.com/.well-known/openid-configuration"
},
"api_key": {
"type": "apiKey",
"in": "header",
Expand Down
1 change: 1 addition & 0 deletions e2e/express.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Express Swagger', () => {
.addBasicAuth()
.addBearerAuth()
.addOAuth2()
.addOpenIdConnect({ openIdConnectUrl: 'https://oauth.example.com/.well-known/openid-configuration' })
.addApiKey()
.addApiKey({ type: 'apiKey' }, 'key1')
.addApiKey({ type: 'apiKey' }, 'key2')
Expand Down
1 change: 1 addition & 0 deletions e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Fastify Swagger', () => {
.addBasicAuth()
.addBearerAuth()
.addOAuth2()
.addOpenIdConnect({ openIdConnectUrl: 'https://oauth.example.com/.well-known/openid-configuration' })
.addApiKey()
.addApiKey({ type: 'apiKey' }, 'key1')
.addApiKey({ type: 'apiKey' }, 'key2')
Expand Down
1 change: 1 addition & 0 deletions e2e/manual-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ async function bootstrap() {
.addBasicAuth()
.addBearerAuth()
.addOAuth2()
.addOpenIdConnect({ openIdConnectUrl: 'https://oauth.example.com/.well-known/openid-configuration' })
.addApiKey()
.addApiKey({ type: 'apiKey' }, 'key1')
.addApiKey({ type: 'apiKey' }, 'key2')
Expand Down
1 change: 1 addition & 0 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('Validate OpenAPI schema', () => {
.addBasicAuth()
.addBearerAuth()
.addOAuth2()
.addOpenIdConnect({ openIdConnectUrl: 'https://oauth.example.com/.well-known/openid-configuration' })
.addApiKey()
.addApiKey({ type: 'apiKey' }, 'key1')
.addApiKey({ type: 'apiKey' }, 'key2')
Expand Down
26 changes: 21 additions & 5 deletions lib/document-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { buildDocumentBase } from './fixtures/document.base';
import { OpenAPIObject } from './interfaces';
import {
ExternalDocumentationObject,
HttpSecuritySchemeObject,
OAuth2SecuritySchemeObject,
OpenIdSecurityConnectSchemeObject,
ParameterObject,
SecurityRequirementObject,
SecuritySchemeObject,
Expand Down Expand Up @@ -117,7 +120,7 @@ export class DocumentBuilder {
}

public addBearerAuth(
options: SecuritySchemeObject = {
options: HttpSecuritySchemeObject = {
type: 'http'
},
name = 'bearer'
Expand All @@ -131,7 +134,7 @@ export class DocumentBuilder {
}

public addOAuth2(
options: SecuritySchemeObject = {
options: OAuth2SecuritySchemeObject | OpenIdSecurityConnectSchemeObject = {
type: 'oauth2'
},
name = 'oauth2'
Expand All @@ -144,8 +147,21 @@ export class DocumentBuilder {
return this;
}

public addOpenIdConnect(
options: Partial<OpenIdSecurityConnectSchemeObject> = {
type: 'openIdConnect'
},
name = 'openIdConnect'
): this {
this.addSecurity(name, {
type: 'openIdConnect',
...options
});
return this;
}

public addApiKey(
options: SecuritySchemeObject = {
options: HttpSecuritySchemeObject = {
type: 'apiKey'
},
name = 'api_key'
Expand All @@ -160,7 +176,7 @@ export class DocumentBuilder {
}

public addBasicAuth(
options: SecuritySchemeObject = {
options: HttpSecuritySchemeObject = {
type: 'http'
},
name = 'basic'
Expand All @@ -175,7 +191,7 @@ export class DocumentBuilder {

public addCookieAuth(
cookieName = 'connect.sid',
options: SecuritySchemeObject = {
options: HttpSecuritySchemeObject = {
type: 'apiKey'
},
securityName = 'cookie'
Expand Down
15 changes: 13 additions & 2 deletions lib/interfaces/open-api-spec.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,26 @@ export interface XmlObject {

export type SecuritySchemeType = 'apiKey' | 'http' | 'oauth2' | 'openIdConnect';

export interface SecuritySchemeObject {
export type SecuritySchemeObject = HttpSecuritySchemeObject | OAuth2SecuritySchemeObject | OpenIdSecurityConnectSchemeObject;

interface CommonSecuritySchemeObject {
type: SecuritySchemeType;
description?: string;
name?: string;
}

export interface HttpSecuritySchemeObject extends CommonSecuritySchemeObject {
in?: string;
scheme?: string;
bearerFormat?: string;
}

export interface OAuth2SecuritySchemeObject extends CommonSecuritySchemeObject {
flows?: OAuthFlowsObject;
openIdConnectUrl?: string;
}

export interface OpenIdSecurityConnectSchemeObject extends CommonSecuritySchemeObject {
openIdConnectUrl: string;
}

export interface OAuthFlowsObject {
Expand Down