swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

swagger 2.0 apikey authorization

Open ksuvarna34 opened this issue 9 years ago • 8 comments

I am trying to set api key but the Swagger 2.0 UI does not reflect the header info in the curl.

window.onload = function() { // Build a system const ui = SwaggerUIBundle({ url: "http://petstore.swagger.io/v2/swagger.json", dom_id: '#swagger-ui', onComplete: function(swaggerApi, swaggerUi, initOAuth){ $("pre code").each(function(i, e) {hljs.highlightBlock(e)}); var authKey = new SwaggerClient.ApiKeyAuthorization('Authorization', 'Bearer d88fb9b5-8625-4731-85db-99d6f2647a2e', 'header'); window.swaggerUi.api.clientAuthorizations.add("api_key", authKey);
}, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" }) window.ui = ui }

ksuvarna34 avatar Jun 30 '17 03:06 ksuvarna34

I init my Swagger UI const ui = SwaggerUIBundle({ url: '', dom_id: jqDom_id, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout", validatorUrl: null, operationsSorter: "alpha" }) window.ui = ui

When I load the page with the swagger UI documentation, I preload my authorization value into an HTML input field and the I add the api_key authorization header in this way ui.authActions.authorize({api_key: {name: "api_key", schema: {type: "apiKey", in: "header", name: "X-My-Header-Name", description: ""}, value: myAuthorizationValue}});

In some cases I manage also the logout in this way ui.authActions.logout(["api_key"]);

zem83 avatar Jun 30 '17 09:06 zem83

Thanks for the reply. I tried using ui.authAction.authorize but it still doesn't take effect. Am I missing any js files? Or something else?

window.onload = function() { // Build a system const ui = SwaggerUIBundle({ url: "http://localhost:9080/preferences/api-docs", dom_id: '#swagger-ui', presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" }) window.ui = ui var authKey = "Bearer b78fb6b5-8625-4731-85db-99y6f2647a2e"; ui.authActions.authorize({api_key: {name: "api_key", schema: {type: "apiKey", in: "header", name: "Authorization", description: ""}, value: authKey}}); }

ksuvarna34 avatar Jun 30 '17 14:06 ksuvarna34

Maybe depends on the wrong name of your authorization spec? Are you sure that is 'api_key'? Try to have a look at the issue #2915 (the third comment of fernandocamargoti)

zem83 avatar Jun 30 '17 14:06 zem83

Thanks zem83. I couldn't get the Header Bearer token to work but the access_token with query works for now with ui.authActions.authorize(). I should be able to get the header working too. Something to do with my securityDefinitions I guess.

ksuvarna34 avatar Jun 30 '17 16:06 ksuvarna34

Good :)

zem83 avatar Jun 30 '17 16:06 zem83

Make sure api_key name is match what is defined in 'securityDefinitions'

huasambo avatar Jul 05 '17 18:07 huasambo

I've the same issue to solve. What is my problem? `window.onload = function () {

    // Build a system
    const ui = SwaggerUIBundle({
        urls: [
            {url: "http://localhost:8080/v2/api-docs?group=api", name: "api"},
            {url: "http://localhost:8080/v2/api-docs?group=admin-api", name: "admin api"}
        ],
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
            SwaggerUIBundle.presets.apis,
            SwaggerUIStandalonePreset
        ],
        plugins: [
            SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout",
        securityDefinitions: {
            JWT: {
                type: "apiKey",
                description: "",
                name: "Authorization",
                in: "header"
            }
        }
    });

    ui.authActions.authorize({
        JWT: {
            name: "JWT",
            schema: {type: "apiKey", in: "header", name: "Authorization", description: ""},
            value: "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbkBpY29uZWN0LmlvIiwiYXVkaWVuY2UiOiJ3ZWIiLCJjcmVhdGVkIjoxNTAwOTk1MTk4NDM2LCJleHAiOjE1MDE1OTk5OTh9.KwkUcQ1hYB3FWS_rlhyaKUo1x84ZRuXKbukZOkEDeSU1uUpgpNuvrAfKlJsgmAYLwxUUU_gIeOKsYekHEGm2iQ"
        }
    });
    window.ui = ui
}`

SwaggerUI says still: curl -X GET "http://localhost:8080/api/contact/" -H "accept: /" without any header information.

larmic avatar Jul 25 '17 15:07 larmic

Closing due to inactivity.

This is simply to keep our issue tracker clean - feel free to comment if there are any further thoughts or concerns, and we'll be happy to reopen this issue.

shockey avatar Nov 29 '17 08:11 shockey