Skip to content

Commit

Permalink
feat(openapi-v3): add security enhancer extension
Browse files Browse the repository at this point in the history
add security enhancer to add default security scheme

impl. loopbackio#4386

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Feb 3, 2020
1 parent 75ba77d commit fe230f8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
// License text available at https://opensource.org/licenses/MIT

import {Application, createBindingFromClass} from '@loopback/core';
import {OASEnhancerService, OAS_ENHANCER_SERVICE} from '../../../..';
import {
OASEnhancerService,
OAS_ENHANCER_SERVICE,
SecuritySpecEnhancer,
} from '../../../..';
import {InfoSpecEnhancer} from './info.spec.extension';
import {SecuritySpecEnhancer} from './security.spec.extension';

export class SpecServiceApplication extends Application {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('spec-enhancer-extension-point', () => {
// the security scheme entry is added by the security enhancer
components: {
securitySchemes: {
bearerAuth: {
jwt: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,36 @@
import {bind} from '@loopback/core';
import debugModule from 'debug';
import {inspect} from 'util';
import {
mergeOpenAPISpec,
ReferenceObject,
SecuritySchemeObject,
} from '../../../..';
import {asSpecEnhancer, OASEnhancer} from '../../../../enhancers/types';
import {OpenApiSpec} from '../../../../types';
import {mergeOpenAPISpec, ReferenceObject, SecuritySchemeObject} from '../..';
import {OpenApiSpec} from '../../types';
import {asSpecEnhancer, OASEnhancer} from '../types';
const debug = debugModule('loopback:openapi:spec-enhancer');

export type SecuritySchemeObjects = {
[securityScheme: string]: SecuritySchemeObject | ReferenceObject;
};

export const SECURITY_SCHEME_SPEC: SecuritySchemeObjects = {
bearerAuth: {
jwt: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
};

/**
* A spec enhancer to add bearer token OpenAPI security entry to
* A spec enhancer to add jwt bearer token OpenAPI security entry to
* `spec.component.securitySchemes`
*/
@bind(asSpecEnhancer)
export class SecuritySpecEnhancer implements OASEnhancer {
name = 'security';

modifySpec(spec: OpenApiSpec): OpenApiSpec {
const patchSpec = {components: {securitySchemes: SECURITY_SCHEME_SPEC}};
if (spec?.components?.securitySchemes?.jwt) return spec;
const patchSpec = {
components: {securitySchemes: SECURITY_SCHEME_SPEC},
};
const mergedSpec = mergeOpenAPISpec(spec, patchSpec);
debug(`security spec extension, merged spec: ${inspect(mergedSpec)}`);
return mergedSpec;
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-v3/src/enhancers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './extensions/security.spec.extension';
export * from './keys';
export * from './spec-enhancer.service';
export * from './types';

0 comments on commit fe230f8

Please sign in to comment.