Class: Katalyst::Basic::Auth::Config
- Inherits:
-
Object
- Object
- Katalyst::Basic::Auth::Config
- Extended by:
- Enumerable
- Defined in:
- lib/katalyst/basic/auth/config.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- DEFAULT_USERNAME =
"katalyst"- ROOT_PATH =
"/"
Instance Attribute Summary collapse
-
#ip_allowlist ⇒ Object
readonly
Returns the value of attribute ip_allowlist.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
- .add(path:, username: nil, password: nil, enabled: nil, ip_allowlist: nil) ⇒ Object
- .all ⇒ Object
- .default_ip_allowlist ⇒ Object
- .default_password(username) ⇒ Object
- .default_password_salt ⇒ Object
- .default_username ⇒ Object
- .description ⇒ Object
- .each ⇒ Object
- .enabled? ⇒ Boolean
- .enabled_rails_env? ⇒ Boolean
-
.for_path(path) ⇒ Config
The config for the given path.
-
.global ⇒ Config
The global configuration.
- .rails? ⇒ Boolean
- .reset! ⇒ Object
- .up(path = "/up") ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#ip_allowlist ⇒ Object (readonly)
Returns the value of attribute ip_allowlist.
119 120 121 |
# File 'lib/katalyst/basic/auth/config.rb', line 119 def ip_allowlist @ip_allowlist end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
119 120 121 |
# File 'lib/katalyst/basic/auth/config.rb', line 119 def password @password end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
119 120 121 |
# File 'lib/katalyst/basic/auth/config.rb', line 119 def path @path end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
119 120 121 |
# File 'lib/katalyst/basic/auth/config.rb', line 119 def username @username end |
Class Method Details
.add(path:, username: nil, password: nil, enabled: nil, ip_allowlist: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/katalyst/basic/auth/config.rb', line 36 def add(path:, username: nil, password: nil, enabled: nil, ip_allowlist: nil) config = new( path: path, username: username, password: password, enabled: enabled, ip_allowlist: ip_allowlist, ) all.delete(all.detect { |i| i.path == config.path }) all << config config end |
.all ⇒ Object
53 54 55 |
# File 'lib/katalyst/basic/auth/config.rb', line 53 def all @all ||= [new, up] end |
.default_ip_allowlist ⇒ Object
114 115 116 |
# File 'lib/katalyst/basic/auth/config.rb', line 114 def default_ip_allowlist ENV.fetch("KATALYST_BASIC_AUTH_IP_ALLOWLIST", "").split(/[\s,]+/) end |
.default_password(username) ⇒ Object
100 101 102 103 104 |
# File 'lib/katalyst/basic/auth/config.rb', line 100 def default_password(username) return ENV["KATALYST_BASIC_AUTH_PASS"] if ENV["KATALYST_BASIC_AUTH_PASS"] Digest::SHA256.hexdigest("#{default_password_salt}#{username}")[0..15] end |
.default_password_salt ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/katalyst/basic/auth/config.rb', line 106 def default_password_salt if rails? && Rails.application.respond_to?(:secret_key_base) Rails.application.secret_key_base else ENV.fetch("SECRET_KEY_BASE", nil) end end |
.default_username ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/katalyst/basic/auth/config.rb', line 89 def default_username return ENV["KATALYST_BASIC_AUTH_USER"] if ENV["KATALYST_BASIC_AUTH_USER"] return DEFAULT_USERNAME unless rails? if Rails::VERSION::MAJOR >= 6 Rails.application.class.module_parent_name.underscore else Rails.application.class.parent_name.underscore end end |
.description ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/katalyst/basic/auth/config.rb', line 65 def description output = ["Basic auth settings:", ""] all.each do |config| output << config.description output << "" end output.join("\n") end |
.each ⇒ Object
61 62 63 |
# File 'lib/katalyst/basic/auth/config.rb', line 61 def each(&) all.each(&) end |
.enabled? ⇒ Boolean
74 75 76 77 |
# File 'lib/katalyst/basic/auth/config.rb', line 74 def enabled? global_enabled = ENV.fetch("KATALYST_BASIC_AUTH_ENABLED", enabled_rails_env?) [true, "yes", "true"].include?(global_enabled) end |
.enabled_rails_env? ⇒ Boolean
79 80 81 82 83 |
# File 'lib/katalyst/basic/auth/config.rb', line 79 def enabled_rails_env? return false unless rails? %w[staging uat].include?(Rails.env) end |
.for_path(path) ⇒ Config
Returns The config for the given path.
19 20 21 22 23 24 |
# File 'lib/katalyst/basic/auth/config.rb', line 19 def for_path(path) path ||= ROOT_PATH all.sort_by(&:path) .reverse .detect { |i| path.match(/^#{i.path}/) } || global end |
.global ⇒ Config
Returns The global configuration.
27 28 29 |
# File 'lib/katalyst/basic/auth/config.rb', line 27 def global all[0] end |
.rails? ⇒ Boolean
85 86 87 |
# File 'lib/katalyst/basic/auth/config.rb', line 85 def rails? defined?(Rails) end |
.reset! ⇒ Object
57 58 59 |
# File 'lib/katalyst/basic/auth/config.rb', line 57 def reset! @all = [new, up] end |
.up(path = "/up") ⇒ Object
49 50 51 |
# File 'lib/katalyst/basic/auth/config.rb', line 49 def up(path = "/up") new(path: path, enabled: false) end |
Instance Method Details
#allow_ip?(env) ⇒ Boolean
129 130 131 132 133 134 135 |
# File 'lib/katalyst/basic/auth/config.rb', line 129 def allow_ip?(env) request = ::Rack::Request.new(env) return false unless request.ip remote_ip = IPAddr.new(request.ip) ip_allowlist.any? { |i| i.include?(remote_ip) } end |
#description ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/katalyst/basic/auth/config.rb', line 137 def description output = [] output << "path: #{root_path? ? '(global)' : path}" output << "enabled: #{enabled?}" output << "username: #{username}" output << "password: #{password}" output << "ip allowlist: #{ip_allowlist.inspect}" output.join("\n") end |
#enabled? ⇒ Boolean
121 122 123 |
# File 'lib/katalyst/basic/auth/config.rb', line 121 def enabled? @enabled end |
#root_path? ⇒ Boolean
125 126 127 |
# File 'lib/katalyst/basic/auth/config.rb', line 125 def root_path? path == ROOT_PATH end |