fastify / fastify-swagger-ui

Serve Swagger-UI for Fastify
MIT License
132 stars 40 forks source link

Authorization headers not passed on request from UI #70

Closed nick-berilov closed 1 year ago

nick-berilov commented 1 year ago

Prerequisites

Fastify version

4.18.0

Plugin version

1.9.0

Node.js version

16

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

22.04

Description

Authorization headers not passed on request from UI:

image

Steps to Reproduce

import { fastify } from "fastify";
import fs from "@fastify/swagger";
import fsu from "@fastify/swagger-ui";

const app = fastify();

await app.register(fs, {
  openapi: {
    openapi: "3.1.0",
  },
});

await app.register(fsu);

app.get(
  "/",
  {
    schema: {
      headers: {
        type: "object",
        properties: {
          authorization: { type: "string" },
        },
        required: ["authorization"],
      },
    },
  },
  async (request, reply) => {
    return { hello: "world" };
  }
);

app
  .listen({ port: 3001 })
  .then(() => {
    console.log("listening");
  })
  .catch((err) => console.log(err));

Expected Behavior

curl request should include authorization header:

curl -X 'GET' \
  'http://localhost:3001/' \
  -H 'accept: */*' \
  -H 'test: t'
  -H 'authorization: a'
nick-berilov commented 1 year ago

Based on openapi spec it's required to use securitySchemes to allow authorization headers

      components: {
        securitySchemes: {
          bearerAuth: {
            type: 'http',
            scheme: 'bearer',
          },
        },
      },