mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-24 14:56:41 +00:00
feat(auth): Allow Basic auth to be enabled
This commit is contained in:
parent
a50bf616b9
commit
b0712418a3
@ -7,4 +7,8 @@ Rswag::Ui.configure do |c|
|
|||||||
# then the list below should correspond to the relative paths for those endpoints
|
# then the list below should correspond to the relative paths for those endpoints
|
||||||
|
|
||||||
c.swagger_endpoint '/api-docs/v1/swagger.json', 'API V1 Docs'
|
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
|
end
|
||||||
|
|||||||
@ -4,6 +4,7 @@ module Rswag
|
|||||||
module Ui
|
module Ui
|
||||||
class Configuration
|
class Configuration
|
||||||
attr_reader :template_locations
|
attr_reader :template_locations
|
||||||
|
attr_accessor :basic_auth_enabled
|
||||||
attr_accessor :config_object
|
attr_accessor :config_object
|
||||||
attr_accessor :oauth_config_object
|
attr_accessor :oauth_config_object
|
||||||
attr_reader :assets_root
|
attr_reader :assets_root
|
||||||
@ -20,6 +21,7 @@ module Rswag
|
|||||||
@assets_root = File.expand_path('../../../../node_modules/swagger-ui-dist', __FILE__)
|
@assets_root = File.expand_path('../../../../node_modules/swagger-ui-dist', __FILE__)
|
||||||
@config_object = {}
|
@config_object = {}
|
||||||
@oauth_config_object = {}
|
@oauth_config_object = {}
|
||||||
|
@basic_auth_enabled = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def swagger_endpoint(url, name)
|
def swagger_endpoint(url, name)
|
||||||
@ -27,9 +29,15 @@ module Rswag
|
|||||||
@config_object[:urls] << { url: url, name: name }
|
@config_object[:urls] << { url: url, name: name }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def basic_auth_credentials(username, password)
|
||||||
|
@config_object[:basic_auth] = { username: username, password: password }
|
||||||
|
end
|
||||||
|
|
||||||
|
# rubocop:disable Naming/AccessorMethodName
|
||||||
def get_binding
|
def get_binding
|
||||||
binding
|
binding
|
||||||
end
|
end
|
||||||
|
# rubocop:enable Naming/AccessorMethodName
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -7,6 +7,13 @@ module Rswag
|
|||||||
|
|
||||||
initializer 'rswag-ui.initialize' do |app|
|
initializer 'rswag-ui.initialize' do |app|
|
||||||
middleware.use Rswag::Ui::Middleware, Rswag::Ui.config
|
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
|
end
|
||||||
|
|
||||||
rake_tasks do
|
rake_tasks do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user