Skip to content

Instantly share code, notes, and snippets.

@ponelat
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponelat/7ee105d25a1cd7f02fc6 to your computer and use it in GitHub Desktop.
Save ponelat/7ee105d25a1cd7f02fc6 to your computer and use it in GitHub Desktop.
Setup Auth in Swagger-UI
//...
window.swaggerUi = new SwaggerUi({
url: url,
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
onComplete: function(swaggerApi, swaggerUi){
if(typeof initOAuth == "function") {
initOAuth({
clientId: "your-client-id",
realm: "your-realms",
appName: "your-app-name"
});
}
$('pre code').each(function(i, e) {
hljs.highlightBlock(e)
});
addApiKeyAuthorization(); // --------------------------------------------< make sure this is here
},
// ...........
function addApiKeyAuthorization(){
// You can use the 'key', which comes from the input box in the -UI... only if you want.
var key = encodeURIComponent($('#input_apiKey')[0].value);
if(key && key.trim() != "") {
// Lets add some Auth!
window.swaggerUi.api.clientAuthorizations.add("api_key", new SwaggerClient.ApiKeyAuthorization('some_header_key', 'value', 'header'););
window.swaggerUi.api.clientAuthorizations.add("another_auth", new SwaggerClient.ApiKeyAuthorization('some_query_key', 'value', 'query'););
window.swaggerUi.api.clientAuthorizations.add("and_another_auth", new SwaggerClient.PasswordAuthorization('', 'username', 'password'););
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment