From b2fec926e9033c1cce943d6c0cf4bb0f39a153c4 Mon Sep 17 00:00:00 2001 From: Vasya Shevchenko Date: Fri, 25 Oct 2019 13:00:02 +0300 Subject: [PATCH] add HTTP Basic Authentication --- README.md | 8 ++++++++ .../grape_swagger_rails/application_controller.rb | 7 +++++++ lib/grape-swagger-rails.rb | 6 +++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c2ab05d..eb53c45 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,14 @@ GrapeSwaggerRails.options.validator_url = nil Using the `headers` option above, you could hard-code Basic Authentication credentials. Alternatively, you can configure Basic Authentication through the UI, as described below. +### HTTP Basic Authentication +If you want to use HTTP Basic Authentication add: + +```ruby +GrapeSwaggerRails.options.http_base_auth_name = 'admin' +GrapeSwaggerRails.options.http_base_auth_password = 'admin' +``` + ### Basic Authentication If your application uses Basic Authentication, you can setup Swagger to send the username and password to the server with each request to your API: diff --git a/app/controllers/grape_swagger_rails/application_controller.rb b/app/controllers/grape_swagger_rails/application_controller.rb index 6bcd837..c526d56 100644 --- a/app/controllers/grape_swagger_rails/application_controller.rb +++ b/app/controllers/grape_swagger_rails/application_controller.rb @@ -1,5 +1,12 @@ module GrapeSwaggerRails class ApplicationController < ActionController::Base + + # HTTP Base Auth + if GrapeSwaggerRails.options.http_base_auth_name.present? && GrapeSwaggerRails.options.http_base_auth_password.present? + http_basic_authenticate_with name: GrapeSwaggerRails.options.http_base_auth_name, + password: GrapeSwaggerRails.options.http_base_auth_password + end + if Rails::VERSION::MAJOR >= 4 before_action { run_before_action } else diff --git a/lib/grape-swagger-rails.rb b/lib/grape-swagger-rails.rb index f8e0294..5324921 100644 --- a/lib/grape-swagger-rails.rb +++ b/lib/grape-swagger-rails.rb @@ -38,6 +38,10 @@ def before_action(&block) before_action_proc: nil, # Proc used as a controller before action hide_url_input: false, - hide_api_key_input: false + hide_api_key_input: false, + + # HTTP Base Auth + http_base_auth_name: '', + http_base_auth_password: '' ) end