Skip to content

Commit

Permalink
Fixed header-based authorization.
Browse files Browse the repository at this point in the history
Previous to this update, when I tried to do API-key authorization using
headers rather than the query string, Swagger-UI would not send my
authorization headers. This update basically makes the authorization
options get set AFTER Swagger-UI has finished initializing, which seems
to make the system much happier. Changes based on a conversation on a
mostly-unrelated pull request,
ruby-grape#25
  • Loading branch information
David Brewer committed Mar 8, 2016
1 parent 59372ab commit 28eba93
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions app/views/grape_swagger_rails/application/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
console.log(swaggerUi);
}
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
addApiKeyAuthorization();
},
onFailure: function(data) {
if('console' in window) {
Expand All @@ -40,20 +41,20 @@
apisSorter: "alpha"
});

$('#input_apiKey').change(function() {
function addApiKeyAuthorization() {
var key = $('#input_apiKey')[0].value;

if(key && key.trim() != "") {
if (key && key.trim() != "") {
if (options.api_auth == 'basic') {
key = "Basic " + Base64.encode(key);
} else if (options.api_auth == 'bearer') {
key = "Bearer " + key
}
window.swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization(options.api_key_name, key, options.api_key_type));
} else {
window.swaggerUi.api.clientAuthorizations.add("key", null);
}
});
}
}

$('#input_apiKey').change(addApiKeyAuthorization);

window.swaggerUi.load();

Expand Down

0 comments on commit 28eba93

Please sign in to comment.