6

I am using swaggo (https://github.com/swaggo/swag) to auto-create a working swagger specification for my API.

The swagger spec allows me to run all of my API endpoints and receive responses.

I then added JWT authentication to all of my endpoints. Now I'm unable to use the swagger spec to run any endpoints, as it always fails auth.

What annotations do I need to add to each endpoint, that will configure the Swagger spec to allow a JWT to be passed?

I've read the README at https://github.com/swaggo/swag and Google searched, to no avail.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Swoop
  • 514
  • 5
  • 17

3 Answers3

8

You will need these in main.go (this will send token in format to your header: "YourTokenName: token") :

//@securityDefinitions.apikey <YourTypeOfKey>
//@in header
//@name <YourTokenName>

Then add this comment to every endpoint that need an auth token:

// @Security <YourTypeOfKey>
Vũ Nam Hà
  • 81
  • 1
  • 1
6

Seems that these comments added to each endpoint did the trick...

// @Security ApiKeyAuth
// @param Authorization header string true "Authorization"

This comment was also added to our main.go file

// @securityDefinitions.apikey ApiKeyAuth
Swoop
  • 514
  • 5
  • 17
  • isnt adding a //@param defeating the purpose of the annotation in main.go file? I mean adding it as a @param is like saying, hey I need this as a header for this endpoint explicitly – Rajat banerjee Jun 02 '20 at 19:30
1

// @Param Authorization header string true "Insert your access token" default(Bearer <Add access token here>)

Add this param to header for each endpoint in swagger v2.0

In V3.0, we have direct bearer token access.