Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rdmccumbers opened this issue Jun 20, 2023 · 3 comments
Open

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

rdmccumbers opened this issue Jun 20, 2023 · 3 comments

Comments

@rdmccumbers
Copy link

rdmccumbers commented Jun 20, 2023

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
Copy link

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

@appsmatics
Copy link

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
Copy link

Luxiorawa commented Mar 18, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants