From 924038c1c5aca26e849085e7f51178cbc9a3d3d1 Mon Sep 17 00:00:00 2001 From: David Brewer Date: Wed, 2 Mar 2016 17:45:22 -0800 Subject: [PATCH] Fixed header-based authorization. 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, https://github.com/ruby-grape/grape-swagger-rails/pull/25 --- CHANGELOG.md | 1 + .../grape_swagger_rails/application/index.html.erb | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 609ff6f..026a601 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 0.2.1 (Next) * Your contribution here. +* Fixed header-based authorization - [@davidbrewer](https://github.com/davidbrewer). * Support Swagger-UI validatorUrl option - [@davidbrewer](https://github.com/davidbrewer). ### 0.2.0 (February 23, 2016) diff --git a/app/views/grape_swagger_rails/application/index.html.erb b/app/views/grape_swagger_rails/application/index.html.erb index ac5d700..4c72933 100644 --- a/app/views/grape_swagger_rails/application/index.html.erb +++ b/app/views/grape_swagger_rails/application/index.html.erb @@ -28,6 +28,7 @@ console.log(swaggerUi); } $('pre code').each(function(i, e) {hljs.highlightBlock(e)}); + addApiKeyAuthorization(); }, onFailure: function(data) { if('console' in window) { @@ -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();