Skip to content

Commit

Permalink
feat: add support for multiple authentication types
Browse files Browse the repository at this point in the history
  • Loading branch information
nean committed Feb 24, 2021
1 parent 9c5bdf1 commit 538231c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
46 changes: 46 additions & 0 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
"in": "header",
"name": "api_key"
},
"key1": {
"type": "apiKey",
"in": "header",
"name": "key1"
},
"key2": {
"type": "apiKey",
"in": "header",
"name": "key2"
},
"cookie": {
"type": "apiKey",
"in": "cookie",
Expand Down Expand Up @@ -229,6 +239,10 @@
"security": [
{
"bearer": []
},
{
"basic": [],
"cookie": []
}
],
"paths": {
Expand Down Expand Up @@ -281,6 +295,10 @@
"create cats"
],
"security": [
{
"key2": [],
"key1": []
},
{
"bearer": []
},
Expand Down Expand Up @@ -383,6 +401,10 @@
"cats"
],
"security": [
{
"key2": [],
"key1": []
},
{
"bearer": []
},
Expand Down Expand Up @@ -432,6 +454,10 @@
"cats"
],
"security": [
{
"key2": [],
"key1": []
},
{
"bearer": []
},
Expand Down Expand Up @@ -536,6 +562,10 @@
"cats"
],
"security": [
{
"key2": [],
"key1": []
},
{
"bearer": []
},
Expand Down Expand Up @@ -569,6 +599,10 @@
"cats"
],
"security": [
{
"key2": [],
"key1": []
},
{
"bearer": []
},
Expand Down Expand Up @@ -613,6 +647,10 @@
"cats"
],
"security": [
{
"key2": [],
"key1": []
},
{
"bearer": []
},
Expand Down Expand Up @@ -667,6 +705,10 @@
"cats"
],
"security": [
{
"key2": [],
"key1": []
},
{
"bearer": []
},
Expand Down Expand Up @@ -700,6 +742,10 @@
"cats"
],
"security": [
{
"key2": [],
"key1": []
},
{
"bearer": []
},
Expand Down
5 changes: 4 additions & 1 deletion e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ describe('Fastify Swagger', () => {
.addBearerAuth()
.addOAuth2()
.addApiKey()
.addApiKey({ type: 'apiKey' }, 'key1')
.addApiKey({ type: 'apiKey' }, 'key2')
.addCookieAuth()
.addSecurityRequirements('bearer');
.addSecurityRequirements('bearer')
.addSecurityRequirements({ basic: [], cookie: [] });
});

it('should produce a valid OpenAPI 3.0 schema', async () => {
Expand Down
1 change: 1 addition & 0 deletions e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { PaginationQuery } from './dto/pagination-query.dto';

@ApiSecurity('basic')
@ApiBearerAuth()
@ApiSecurity({ key2: [], key1: [] })
@ApiTags('cats')
@ApiHeader({
name: 'header',
Expand Down
3 changes: 3 additions & 0 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ describe('Validate OpenAPI schema', () => {
.addBearerAuth()
.addOAuth2()
.addApiKey()
.addApiKey({ type: 'apiKey' }, 'key1')
.addApiKey({ type: 'apiKey' }, 'key2')
.addCookieAuth()
.addSecurityRequirements('bearer')
.addSecurityRequirements({ basic: [], cookie: [] })
.build();

document = SwaggerModule.createDocument(app, options);
Expand Down
11 changes: 9 additions & 2 deletions lib/decorators/api-security.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { isString } from 'lodash';
import { DECORATORS } from '../constants';
import { SecurityRequirementObject } from '../interfaces/open-api-spec.interface';
import { extendMetadata } from '../utils/extend-metadata.util';

export function ApiSecurity(
name: string,
name: string | SecurityRequirementObject,
requirements: string[] = []
): ClassDecorator & MethodDecorator {
let metadata: SecurityRequirementObject[] = [{ [name]: requirements }];
let metadata: SecurityRequirementObject[];

if (isString(name)) {
metadata = [{ [name]: requirements }];
} else {
metadata = [name];
}

return (
target: object,
Expand Down
15 changes: 12 additions & 3 deletions lib/document-builder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Logger } from '@nestjs/common';
import { isUndefined, negate, pickBy } from 'lodash';
import { isString, isUndefined, negate, pickBy } from 'lodash';
import { buildDocumentBase } from './fixtures/document.base';
import { OpenAPIObject } from './interfaces';
import {
ExternalDocumentationObject,
SecurityRequirementObject,
SecuritySchemeObject,
ServerVariableObject,
TagObject
Expand Down Expand Up @@ -91,11 +92,19 @@ export class DocumentBuilder {
}

public addSecurityRequirements(
name: string,
name: string | SecurityRequirementObject,
requirements: string[] = []
): this {
let securityRequirement: SecurityRequirementObject;

if (isString(name)) {
securityRequirement = { [name]: requirements };
} else {
securityRequirement = name;
}

this.document.security = (this.document.security || []).concat({
[name]: requirements
...securityRequirement
});
return this;
}
Expand Down

0 comments on commit 538231c

Please sign in to comment.