Skip to content

Commit

Permalink
Merge pull request #94 from TruemarkDev/rswag-generator-minor-fixes
Browse files Browse the repository at this point in the history
RSwag Generator Minor Fixes
  • Loading branch information
abhaynikam authored Mar 26, 2024
2 parents 2db0ad8 + 175af03 commit bcdd4cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
25 changes: 10 additions & 15 deletions lib/generators/boring/rswag/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'json'

module Boring
module Rswag
class InstallGenerator < Rails::Generators::Base
Expand All @@ -21,8 +19,8 @@ class InstallGenerator < Rails::Generators::Base
desc: "Use this option with value 'true' if you don't want to add Authentication when making API calls via swagger docs.",
default: false
class_option :api_authentication_options,
type: :string,
desc: 'Use together with authentication_type. Required for "api_key" authentication which has dynamic set of options. See: https://swagger.io/docs/specification/authentication. Example: "{ "name": "api_key", "in": "header" }"'
type: :hash,
desc: 'Use together with authentication_type. Required for "api_key" authentication which has dynamic set of options. See: https://swagger.io/docs/specification/authentication. Example: "--api_authentication_options=name:api_key in:header"'
class_option :enable_swagger_ui_authentication,
type: :boolean,
desc: "Use this option with value 'true' for securing your API docs behind a basic authentication to block unauthorized access",
Expand Down Expand Up @@ -85,7 +83,7 @@ def add_authentication_scheme
if authentication_type == 'api_key'
validate_api_authentication_options

authentication_options = JSON.parse(options[:api_authentication_options])
authentication_options = options[:api_authentication_options]
end

authentication_content = case authentication_type
Expand Down Expand Up @@ -126,19 +124,17 @@ def add_authentication_scheme
end

def enable_swagger_ui_authentication
unless options[:enable_swagger_ui_authentication]
return
end
return unless options[:enable_swagger_ui_authentication]

say "\nAdding Basic Authentication to secure the UI", :green

say "❗️❗️\nusername will be used from `ENV.fetch('SWAGGER_UI_LOGIN_USERNAME', 'admin')` and password from `Rails.application.credentials.dig(:swagger_ui, :password)`. You can change these values if they don't match with your app.\n", :yellow

uncomment_lines 'config/initializers/rswag_ui.rb', /c.basic_auth_enabled/
uncomment_lines 'config/initializers/rswag_ui.rb', /c.basic_auth_credentials/
gsub_file "config/initializers/rswag_ui.rb",
"c.basic_auth_credentials 'username', 'password'",
'c.basic_auth_credentials ENV.fetch("SWAGGER_UI_LOGIN_USERNAME", "admin"), Rails.application.credentials.dig(:swagger_ui, :password)'
'c.basic_auth_credentials Rails.application.credentials.dig(:swagger_ui, :username), Rails.application.credentials.dig(:swagger_ui, :password)'

say "❗️❗️\nusername will be used from `Rails.application.credentials.dig(:swagger_ui, :username)` and password from `Rails.application.credentials.dig(:swagger_ui, :password)`. You can change these values if they don't match with your app.\n", :yellow
end

def show_readme
Expand All @@ -148,7 +144,7 @@ def show_readme
private

def validate_api_authentication_options
api_authentication_options = JSON.parse(options[:api_authentication_options])
api_authentication_options = options[:api_authentication_options]

if api_authentication_options.blank?
say "api_authentication_options args should be provided for api_key authentication", :red
Expand All @@ -157,11 +153,10 @@ def validate_api_authentication_options
end

missing_options = %w[name in] - api_authentication_options.keys
example_valid_options = { name: 'api_key', in: 'query' }

if missing_options.length.positive?
say "Option/s '#{missing_options.to_sentence}' should be present for api_key authentication!", :red
say "Below is the one example of valid api_authentication_options:\n\n#{example_valid_options}"
say "Option/s '#{missing_options.join(', ')}' should be present for api_key authentication!", :red
say 'Example of valid options: "--api_authentication_options=name:api_key in:query"', :yellow

abort
end
Expand Down
4 changes: 2 additions & 2 deletions test/generators/rswag_install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_should_add_api_key_authentication
Dir.chdir(app_path) do
install_rspec

quietly { run_generator [destination_root, "--authentication_type=api_key", '--api_authentication_options={ "name": "api_key", "in": "query" }'] }
quietly { run_generator [destination_root, "--authentication_type=api_key", "--api_authentication_options=name:api_key", "in:query"] }

assert_file "spec/swagger_helper.rb" do |content|
assert_match(/type: :apiKey/, content)
Expand All @@ -107,7 +107,7 @@ def test_should_configure_ui_authentication
refute_match(/# c.basic_auth_enabled/, content)
assert_match(/c.basic_auth_enabled = true/, content)
refute_match(/# c.basic_auth_credentials/, content)
assert_includes(content, 'c.basic_auth_credentials ENV.fetch("SWAGGER_UI_LOGIN_USERNAME", "admin"), Rails.application.credentials.dig(:swagger_ui, :password)')
assert_includes(content, 'c.basic_auth_credentials Rails.application.credentials.dig(:swagger_ui, :username), Rails.application.credentials.dig(:swagger_ui, :password)')
end
end
end
Expand Down

0 comments on commit bcdd4cf

Please sign in to comment.