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.4k stars 225 forks source link

Apply bearer auth to global scope is not working. #351

Open lmatthews0221 opened 1 year ago

lmatthews0221 commented 1 year ago

I'm trying to apply bearer auth to all operations and here's the implementation.

const options = {
  swaggerDefinition: {
    openapi: '3.0.0',
    info: {
      title: 'Node.js REST API',
      version: '1.0.0',
      description: 'API documentation for the Node.js REST API',
    },
    servers: [
      {
        url: 'http://localhost:3000', // Replace with the actual server URL
        description: 'Local development server',
      },
    ],
    components: {
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
        }
      }
    },
  },
  apis: ['./src/swagger/*.yaml'], // Replace with the path to your API route files
};

However, it doesn't work. When I add security to an individual route operation, it works.

paths:
  /users:
    get:
      summary: Get all users
      tags:
        - User
      security:
        - bearerAuth: []
      responses:
        200:
          description: Return an array of users
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'

Anyone help me why global declaration is not working?

JimiHFord commented 7 months ago

I'm seeing this as well. Both version 4.x and 5.0 are behaving this way for me.

appsmatics commented 5 months ago

I suspect the issue is deeper down in the dependencies either swagger-jsdoc or @apidevtools/swagger-parser. Have to dig a bit deeper. For now am declaring the security: in each path spec

Luxiorawa commented 3 months ago

The first code you posted is creating a global bearerAuth, but it is not telling that all routes will use this auth. To do that, you need to add a security property (as you do on each route) but on a global level.

Exemple (code not tested, but it's something similar to the "Describing Bearer Authentication" part here) :

const options = {
  swaggerDefinition: {
    openapi: '3.0.0',
    info: {
      title: 'Node.js REST API',
      version: '1.0.0',
      description: 'API documentation for the Node.js REST API',
    },
    servers: [
      {
        url: 'http://localhost:3000', // Replace with the actual server URL
        description: 'Local development server',
      },
    ],
    components: {
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
        }
      }
    },
   security: [
    {
       bearerAuth: []
    }
   ],
  },
  apis: ['./src/swagger/*.yaml'], // Replace with the path to your API route files
};

This will require all routes to have a bearerAuth.