scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.43k stars 228 forks source link

securityHandlers let http routes specify scopes, these should be displayed in swagger UI #310

Closed asarver closed 2 years ago

asarver commented 2 years ago

using 4.5.0

openapi express allows you to specify scopes for individual routes using securityHandlers for http routes (similar to oauth). These should be displayed on the UI so users know which scopes are needed for which routes

{
  openapi: '3.0.0',
  info: {
    title: 'Example'
    version: '1.0.0'
  },
  components: {
    schemas: {
      Version: {
        type: 'object',
        properties: {
          version: {
            type: 'string',
            description: 'Semantic version number for the application',
            example: '0.1.0'
          }
        }
      }
    },
    securitySchemes: {
      BearerSchema: {
        type: 'http',
        scheme: 'bearer'
      },
      HeaderSchema: {
        type: 'apikey',
        name: 'apikey',
        in: 'header'
      }
    }
  },
  paths: {
    '/': {
      get: {
        operationId: 'getVersion',
        tags: ['version'],
        summary: 'Check the running version of the API',
        security: [{
          BearerSchema: ['read'],
          HeaderSchema: []
        }],
        responses: {
          200: {
            description: 'success',
            content: {
              'application/json': {
                schema: {
                  $ref: '#/components/schemas/Version'
                }
              }
            }
          }
        }
      }
    }
  }
}

and how it's used in the app

// register open api routes
initialize({
  app,
  apiDoc,
  promiseMode: true,
  exposeApiDocs: false,
  operations: {
    getVersion
  },
  securityHandlers: {
    BearerSchema: (req, scopes, def) => {
      log.info({ scopes, def }, 'in handler')
      return true
    }
  }
})
Screen Shot 2022-09-01 at 3 23 37 PM
scottie1984 commented 2 years ago

I am guessing this should be raised with swagger ui directly? This module is just about simplifying the hosting and configuration of the UI