Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swagger 2.0 apikey authorization #3318

Closed
ksuvarna34 opened this issue Jun 30, 2017 · 8 comments
Closed

swagger 2.0 apikey authorization #3318

ksuvarna34 opened this issue Jun 30, 2017 · 8 comments

Comments

@ksuvarna34
Copy link

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
}

@zem83
Copy link

zem83 commented Jun 30, 2017

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"]);

@ksuvarna34
Copy link
Author

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?

<script src="js/swagger-ui-bundle.js" type="text/javascript"></script> <script src="js/swagger-ui-standalone-preset.js" type="text/javascript"></script>

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}});
}

@zem83
Copy link

zem83 commented Jun 30, 2017

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)

@ksuvarna34
Copy link
Author

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.

@zem83
Copy link

zem83 commented Jun 30, 2017

Good :)

@huasambo
Copy link

huasambo commented Jul 5, 2017

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

@larmic
Copy link

larmic commented Jul 25, 2017

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.

@shockey
Copy link
Contributor

shockey commented Nov 29, 2017

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 shockey closed this as completed Nov 29, 2017
@lock lock bot locked and limited conversation to collaborators Jul 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants