feat(auth): Allow Basic auth to be enabled

This commit is contained in:
Austin Kabiru 2018-11-30 12:33:13 +03:00
parent a50bf616b9
commit b0712418a3
No known key found for this signature in database
GPG Key ID: 6D6C5615C3E00288
3 changed files with 19 additions and 0 deletions

View File

@ -7,4 +7,8 @@ Rswag::Ui.configure do |c|
# then the list below should correspond to the relative paths for those endpoints
c.swagger_endpoint '/api-docs/v1/swagger.json', 'API V1 Docs'
# Add Basic Auth in case your API is private
# c.basic_auth_enabled = true
# c.basic_auth_credentials 'username', 'password'
end

View File

@ -4,6 +4,7 @@ module Rswag
module Ui
class Configuration
attr_reader :template_locations
attr_accessor :basic_auth_enabled
attr_accessor :config_object
attr_accessor :oauth_config_object
attr_reader :assets_root
@ -20,6 +21,7 @@ module Rswag
@assets_root = File.expand_path('../../../../node_modules/swagger-ui-dist', __FILE__)
@config_object = {}
@oauth_config_object = {}
@basic_auth_enabled = false
end
def swagger_endpoint(url, name)
@ -27,9 +29,15 @@ module Rswag
@config_object[:urls] << { url: url, name: name }
end
def basic_auth_credentials(username, password)
@config_object[:basic_auth] = { username: username, password: password }
end
# rubocop:disable Naming/AccessorMethodName
def get_binding
binding
end
# rubocop:enable Naming/AccessorMethodName
end
end
end

View File

@ -7,6 +7,13 @@ module Rswag
initializer 'rswag-ui.initialize' do |app|
middleware.use Rswag::Ui::Middleware, Rswag::Ui.config
if Rswag::Ui.config.basic_auth_enabled
c = Rswag::Ui.config
app.middleware.use ::Rack::Auth::Basic do |username, password|
c.config_object[:basic_auth].values == [username, password]
end
end
end
rake_tasks do