Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use JWT Bearer token in swagger Laravel

i am using L5-swagger from DarkaOnLine for my project. I want to use JWT Auth in my documentation. I have added this code :

/**
  @OAS\SecurityScheme(
      securityScheme="API Key Auth",
      type="apiKey",
      in="header",
      name="Authorization",
  )
 **/

In swagger UI the "Authorize" button is showed and there is a form to fill with token. But after i enter it, i still got "token_not_provided" error in function that need token to access it.

Thanks in advance.

like image 771
Alfaritsi Hamdani Avatar asked May 30 '18 23:05

Alfaritsi Hamdani


People also ask

How do I use bearer token in swagger?

Token-based Authentication To retrieve a token via our Swagger UI, send a POST request like the following to the /api-token-auth/ endpoint. Copy the token generated from the response, excluding the quotation marks. Click the Authorize button and enter "Bearer", followed by the token from step 2. Click Authorize.


2 Answers

@OAS annotations are for OpenAPI 3.0, where Bearer authentication is defined as type: http + scheme: bearer:

/**
  @OAS\SecurityScheme(
      securityScheme="bearerAuth",
      type="http",
      scheme="bearer"
  )
 **/

Make sure your operations use security with the same name as specified in securityScheme="<NAME>" above. For example:

/**
 * @OAS\Get(
 *   ...
 *   security={{"bearerAuth":{}}}
 *   ...

In Swagger UI's "Authorize" dialog, enter the token without the "Bearer" prefix.

like image 196
Helen Avatar answered Oct 26 '22 22:10

Helen


this link solved my problem. Bearer Authorization setup

I used this for Laravel Lumen, JWT authentication.


Add this somewhere in your middleware or any other place like base controller

/**
 * @OA\SecurityScheme(
 *     type="http",
 *     description="Login with email and password to get the authentication token",
 *     name="Token based Based",
 *     in="header",
 *     scheme="bearer",
 *     bearerFormat="JWT",
 *     securityScheme="apiAuth",
 * )
 */

And add this to your action/function.

/**
 * @OA\Get(
 *  path="/resources",
 *  summary="Get the list of resources",
 *  tags={"Resource"},
 *  @OA\Response(response=200, description="Return a list of resources"),
 *  security={{ "apiAuth": {} }}
 * )
 */
like image 34
Em.MF Avatar answered Oct 26 '22 22:10

Em.MF