Skip to content

Commit d4735eb

Browse files
committed
refactor: simplify basic auth implementation
- Inline before_action condition with a lambda instead of a separate private method - Add .to_s guard on credentials in secure_compare - Rename validate! to warn_if_basic_auth_misconfigured for clarity - Add enabled? alias for basic_auth_enabled? - Merge attr_accessor declarations into a single line - Fix long lines in controller specs
1 parent d2adcc7 commit d4735eb

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

‎app/controllers/letter_opener_web/application_controller.rb‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
module LetterOpenerWeb
44
class ApplicationController < ActionController::Base
5-
before_action :enforce_basic_auth, if: :basic_auth_configured?
5+
before_action :enforce_basic_auth, if: -> { LetterOpenerWeb.config.enabled? }
66

77
protect_from_forgery with: :exception, unless: -> { Rails.configuration.try(:api_only) }
88

99
private
1010

11-
def basic_auth_configured?
12-
LetterOpenerWeb.config.basic_auth_enabled?
13-
end
14-
1511
def enforce_basic_auth
1612
authenticate_or_request_with_http_basic('Letter Opener Web') do |username, password|
1713
config = LetterOpenerWeb.config
18-
ActiveSupport::SecurityUtils.secure_compare(username, config.username) &
19-
ActiveSupport::SecurityUtils.secure_compare(password, config.password)
14+
ActiveSupport::SecurityUtils.secure_compare(username, config.username.to_s) &
15+
ActiveSupport::SecurityUtils.secure_compare(password, config.password.to_s)
2016
end
2117
end
2218
end

‎lib/letter_opener_web.rb‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
module LetterOpenerWeb
88
class Config
9-
attr_accessor :letters_location
10-
attr_accessor :authentication_enabled
11-
attr_accessor :username, :password
9+
attr_accessor :letters_location, :authentication_enabled, :username, :password
1210

1311
def basic_auth_enabled?
1412
authentication_enabled && username.present? && password.present?
1513
end
14+
alias enabled? basic_auth_enabled?
1615

17-
def validate!
16+
def warn_if_basic_auth_misconfigured
1817
return unless authentication_enabled && (username.blank? || password.blank?)
1918

2019
Rails.logger.warn('[LetterOpenerWeb] authentication_enabled is true but username or password is blank. ' \
@@ -31,7 +30,7 @@ def self.config
3130

3231
def self.configure
3332
yield config if block_given?
34-
config.validate!
33+
config.warn_if_basic_auth_misconfigured
3534
end
3635

3736
def self.reset!

‎spec/controllers/letter_opener_web/letters_controller_spec.rb‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,24 @@
2525

2626
it 'returns 200 with correct credentials' do
2727
allow(LetterOpenerWeb::Letter).to receive(:search)
28-
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials('admin', 'secret')
28+
request.env['HTTP_AUTHORIZATION'] =
29+
ActionController::HttpAuthentication::Basic.encode_credentials('admin', 'secret')
2930
get :index
3031
expect(response.status).to eq(200)
3132
end
3233

3334
it 'returns 401 with wrong password' do
3435
allow(LetterOpenerWeb::Letter).to receive(:search)
35-
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials('admin', 'wrong')
36+
request.env['HTTP_AUTHORIZATION'] =
37+
ActionController::HttpAuthentication::Basic.encode_credentials('admin', 'wrong')
3638
get :index
3739
expect(response.status).to eq(401)
3840
end
3941

4042
it 'returns 401 with wrong username' do
4143
allow(LetterOpenerWeb::Letter).to receive(:search)
42-
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials('wrong', 'secret')
44+
request.env['HTTP_AUTHORIZATION'] =
45+
ActionController::HttpAuthentication::Basic.encode_credentials('wrong', 'secret')
4346
get :index
4447
expect(response.status).to eq(401)
4548
end

0 commit comments

Comments
 (0)