Swagger-php: Basic Auth example request

Created on 7 May 2018  路  1Comment  路  Source: zircote/swagger-php

I use swagger-ui and swagger json generated with swagger-php. I m not able to do a basic auth to use my endpoint. I m able to get the swagger json file but not to use endpoint. I don't see what i m misunderstanding. If someone could show me a full example with basich auth?

I use swagger-ui-dist 3.14.1 that is compatible with swagger 2.0 (swagger-php is 2.0)

3.14.1 | 2018-05-04 | 2.0, 3.0 | tag v3.14.1
-- | -- | -- | --

I m using theses SWG comments in my controllers to use basicAuth, (server-side)

/**
 *  @SWG\SecurityScheme(
  *      securityDefinition="basicAuth",
  *      name="authorization",
  *      type="basic",
  *      scheme="http"
  *  )
  */

and this comments

/**
 * @SWG\Get(
 *     description="Get all catalog",
 *     path="/ott/catalogs",
 *     produces={"application/json"},
 *     security = {"basicAuth"},
 *     @SWG\Response(response="200", description="Get all catalogs"),
 *     @SWG\Response(response="401",description="You are not authorized")
 *     )
 * )
 */

Here is my client-side code:

 window.onload = function() {
         // Build a system
       const ui = SwaggerUIBundle({
         url: "http://ott/ott/tools/swagger",
         host: 'ott',
         basePath: 'ott/',
         schemes: 'http',
         enableCORS: true,
         dom_id: '#swagger-ui',
         deepLinking: true,
         validatorUrl:null,
         presets: [
           SwaggerUIBundle.presets.apis,
           SwaggerUIStandalonePreset
         ],
         plugins: [
           SwaggerUIBundle.plugins.DownloadUrl
         ],
         layout: "StandaloneLayout",
         requestInterceptor: (req) => {
             if (req.loadSpec) {
                 let hash = btoa("sma_user" + ":" + "sma_rules");
                 req.headers.Authorization = "Basic " + hash;
             }
             return req
         },
           onComplete : () => {
             ui.preauthorizeBasic("basicAuth","sma_user","sma_rules")
           }
       });
       window.ui = ui

When i click to the lock, i have the first error in my console and then when i try to get my catalogs i have a 401 - authorized because the Basic Authentication header is not sent

capture du 2018-05-07 16-44-38

Most helpful comment

I think the problem is with:
security = {"basicAuth"},
it should be
security = { { "basicAuth": {} } },

However, how to get auth working in swagger-ui is out of scope for this project.
swagger-php is only responsible for generating the json. If the generated json is not what you expected let us know.

Ps. If you're using basic auth, you could send a WWW-Authenticate header which would trigger a dialog for a username and password. This lets the browser handle the authentication instead of swagger-ui.

>All comments

I think the problem is with:
security = {"basicAuth"},
it should be
security = { { "basicAuth": {} } },

However, how to get auth working in swagger-ui is out of scope for this project.
swagger-php is only responsible for generating the json. If the generated json is not what you expected let us know.

Ps. If you're using basic auth, you could send a WWW-Authenticate header which would trigger a dialog for a username and password. This lets the browser handle the authentication instead of swagger-ui.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hpatoio picture hpatoio  路  3Comments

justicenode picture justicenode  路  4Comments

xywenke picture xywenke  路  4Comments

huksley picture huksley  路  4Comments

xiehan picture xiehan  路  5Comments