From 19e985a828011b2b961bc5a9c5e32f2cff51f85e Mon Sep 17 00:00:00 2001 From: Kabiru Mwenja Date: Mon, 6 Jan 2020 09:50:30 +0300 Subject: [PATCH] chore(rswag-ui): Organise basic auth files --- rswag-ui/lib/rswag/ui/basic_auth.rb | 25 +++++++++++++++++++++++++ rswag-ui/lib/rswag/ui/engine.rb | 25 +------------------------ 2 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 rswag-ui/lib/rswag/ui/basic_auth.rb diff --git a/rswag-ui/lib/rswag/ui/basic_auth.rb b/rswag-ui/lib/rswag/ui/basic_auth.rb new file mode 100644 index 0000000..957ddfb --- /dev/null +++ b/rswag-ui/lib/rswag/ui/basic_auth.rb @@ -0,0 +1,25 @@ +require 'rack/auth/basic' + +class BasicAuth < ::Rack::Auth::Basic + def call(env) + return @app.call(env) unless env_matching_path + + super(env) + end + + private + + def env_matching_path + swagger_endpoints = Rswag::Ui.config.swagger_endpoints[:urls] + swagger_endpoints.find do |endpoint| + base_path = base_path(endpoint[:url]) + env_base_path = base_path(env['PATH_INFO']) + + base_path == env_base_path + end + end + + def base_path(url) + url.downcase.split('/')[1] + end +end diff --git a/rswag-ui/lib/rswag/ui/engine.rb b/rswag-ui/lib/rswag/ui/engine.rb index 962dc9f..c4cffae 100644 --- a/rswag-ui/lib/rswag/ui/engine.rb +++ b/rswag-ui/lib/rswag/ui/engine.rb @@ -1,28 +1,5 @@ require 'rswag/ui/middleware' - -class UiBasicAuth < ::Rack::Auth::Basic - def call(env) - return @app.call(env) unless env_matching_path - - super(env) - end - - private - - def env_matching_path - swagger_endpoints = Rswag::Ui.config.swagger_endpoints[:urls] - swagger_endpoints.find do |endpoint| - base_path = base_path(endpoint[:url]) - env_base_path = base_path(env['PATH_INFO']) - - base_path == env_base_path - end - end - - def base_path(url) - url.downcase.split('/')[1] - end -end +require 'rswag/ui/basic_auth' module Rswag module Ui