import { injectable } from '@loopback/core' ; import { asSpecEnhancer , mergeOpenAPISpec , OASEnhancer , OpenApiSpec , ReferenceObject , SecuritySchemeObject , } from '@loopback/rest' ; import debugModule from 'debug' ; import { inspect } from 'util' ; const debug = debugModule ( 'loopback:jwt-extension:spec-enhancer' ) ; export type SecuritySchemeObjects = { [ securityScheme ] : SecuritySchemeObject | ReferenceObject ; } ; export const OPERATION_SECURITY_SPEC = [ { jwt : [ ] , } , ] ; export const SECURITY_SCHEME_SPEC = { jwt : { type : 'http' , scheme : 'bearer' , bearerFormat : 'JWT' , } , } ; /**
 * A spec enhancer to add bearer token OpenAPI security entry to
 * `spec.component.securitySchemes`
 */ @ injectable ( asSpecEnhancer ) export class SecuritySpecEnhancer implements OASEnhancer { name = 'bearerAuth' ; modifySpec ( spec ) { const patchSpec = { components : { securitySchemes : SECURITY_SCHEME_SPEC , } , security : OPERATION_SECURITY_SPEC , } ; const mergedSpec = mergeOpenAPISpec ( spec , patchSpec ) ; debug ( `security spec extension, merged spec: ${ inspect ( mergedSpec ) }` ) ; return mergedSpec ; } }