Skip to content

Commit 52ccd26

Browse files
committed
Fix Style/IfUnlessModifier offense
- Convert multi-line if/unless statements to single-line modifier syntax where appropriate - Split long string literals in spec helper methods for better readability - Standardize string quotes in OAuth specs (double to single quotes)
1 parent 97003b9 commit 52ccd26

File tree

18 files changed

+124
-79
lines changed

18 files changed

+124
-79
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,6 @@ Style/ExpandPathArguments:
443443
Style/FrozenStringLiteralComment:
444444
Enabled: false
445445

446-
# Offense count: 32
447-
# This cop supports safe autocorrection (--autocorrect).
448-
Style/IfUnlessModifier:
449-
Enabled: false
450-
451446
# Offense count: 3
452447
# Configuration parameters: AllowedMethods.
453448
# AllowedMethods: respond_to_missing?

lib/generators/sorcery/install_generator.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ def copy_migration_files
6161
# Copy core migration file in all cases except when you pass --only-submodules.
6262
return unless defined?(ActiveRecord)
6363

64-
migration_template 'migration/core.rb', 'db/migrate/sorcery_core.rb', migration_class_name: migration_class_name unless only_submodules?
64+
unless only_submodules?
65+
migration_template 'migration/core.rb', 'db/migrate/sorcery_core.rb', migration_class_name: migration_class_name
66+
end
6567

6668
return unless submodules
6769

lib/sorcery/adapters/active_record_adapter.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ def find_by_id(id)
8888

8989
def find_by_username(username)
9090
@klass.sorcery_config.username_attribute_names.each do |attribute|
91-
if @klass.sorcery_config.downcase_username_before_authenticating
92-
username = username.downcase
93-
end
91+
username = username.downcase if @klass.sorcery_config.downcase_username_before_authenticating
9492

9593
result = @klass.where(attribute => username).first
9694
return result if result

lib/sorcery/adapters/mongoid_adapter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def callback_name(time, event, options)
4040
end
4141

4242
def credential_regex(credential)
43-
return { :$regex => /^#{Regexp.escape(credential)}$/i } if @klass.sorcery_config.downcase_username_before_authenticating
43+
if @klass.sorcery_config.downcase_username_before_authenticating
44+
return { :$regex => /^#{Regexp.escape(credential)}$/i }
45+
end
4446

4547
credential
4648
end

lib/sorcery/controller.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ def logged_in?
8181
# attempts to auto-login from the sources defined (session, basic_auth, cookie, etc.)
8282
# returns the logged in user if found, nil if not
8383
def current_user
84-
unless defined?(@current_user)
85-
@current_user = login_from_session || login_from_other_sources || nil
86-
end
84+
@current_user = login_from_session || login_from_other_sources || nil unless defined?(@current_user)
8785
@current_user
8886
end
8987

@@ -146,9 +144,7 @@ def login_from_other_sources
146144
end
147145

148146
def login_from_session
149-
@current_user = if session[:user_id]
150-
user_class.sorcery_adapter.find_by_id(session[:user_id])
151-
end
147+
@current_user = (user_class.sorcery_adapter.find_by_id(session[:user_id]) if session[:user_id])
152148
end
153149

154150
def after_login!(user, credentials = [])

lib/sorcery/controller/submodules/http_basic_auth.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ module InstanceMethods
3737
# To overcome this, we set a session when requesting the password, which logout will
3838
# reset, and that's how we know if we need to request for HTTP auth again.
3939
def require_login_from_http_basic
40-
request_http_basic_authentication(realm_name_by_controller) && (session[:http_authentication_used] = true) && return if request.authorization.nil? || session[:http_authentication_used].nil?
40+
if request.authorization.nil? || session[:http_authentication_used].nil?
41+
request_http_basic_authentication(realm_name_by_controller) && (session[:http_authentication_used] = true) && return
42+
end
4143

4244
require_login
4345
session[:http_authentication_used] = nil unless logged_in?
@@ -46,7 +48,9 @@ def require_login_from_http_basic
4648
# given to main controller module as a login source callback
4749
def login_from_basic_auth
4850
authenticate_with_http_basic do |username, password|
49-
@current_user = (user_class.authenticate(username, password) if session[:http_authentication_used]) || false
51+
@current_user = (if session[:http_authentication_used]
52+
user_class.authenticate(username, password)
53+
end) || false
5054
auto_login(@current_user) if @current_user
5155
@current_user
5256
end

lib/sorcery/crypto_providers/aes256.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def decrypt(crypted)
4242
private
4343

4444
def aes
45-
raise ArgumentError, "#{name} expects a 32 bytes long key. Please use Sorcery::Model::Config.encryption_key to set it." if @key.nil? || @key == ''
45+
if @key.nil? || @key == ''
46+
raise ArgumentError, "#{name} expects a 32 bytes long key. Please use Sorcery::Model::Config.encryption_key to set it."
47+
end
4648

4749
@aes ||= OpenSSL::Cipher.new('AES-256-ECB')
4850
end

lib/sorcery/engine.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ class Engine < Rails::Engine
1616
# FIXME: on_load is needed to fix Rails 6 deprecations, but it breaks
1717
# applications due to undefined method errors.
1818
# ActiveSupport.on_load(:action_controller_api) do
19-
if defined?(ActionController::API)
20-
ActionController::API.include Sorcery::Controller
21-
end
19+
ActionController::API.include Sorcery::Controller if defined?(ActionController::API)
2220

2321
# FIXME: on_load is needed to fix Rails 6 deprecations, but it breaks
2422
# applications due to undefined method errors.

lib/sorcery/model.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,13 @@ def sorcery_config
8282
def authenticate(*credentials, &block)
8383
raise ArgumentError, 'at least 2 arguments required' if credentials.size < 2
8484

85-
if credentials[0].blank?
86-
return authentication_response(return_value: false, failure: :invalid_login, &block)
87-
end
85+
return authentication_response(return_value: false, failure: :invalid_login, &block) if credentials[0].blank?
8886

89-
if @sorcery_config.downcase_username_before_authenticating
90-
credentials[0].downcase!
91-
end
87+
credentials[0].downcase! if @sorcery_config.downcase_username_before_authenticating
9288

9389
user = sorcery_adapter.find_by_credentials(credentials)
9490

95-
unless user
96-
return authentication_response(failure: :invalid_login, &block)
97-
end
91+
return authentication_response(failure: :invalid_login, &block) unless user
9892

9993
set_encryption_attributes
10094

@@ -105,9 +99,7 @@ def authenticate(*credentials, &block)
10599
@sorcery_config.before_authenticate.each do |callback|
106100
success, reason = user.send(callback)
107101

108-
unless success
109-
return authentication_response(user: user, failure: reason, &block)
110-
end
102+
return authentication_response(user: user, failure: reason, &block) unless success
111103
end
112104

113105
unless user.valid_password?(credentials[1])
@@ -130,9 +122,15 @@ def encrypt(*tokens)
130122
# FIXME: This method of passing config to the hashing provider is
131123
# questionable, and has been refactored in Sorcery v1.
132124
def set_encryption_attributes
133-
@sorcery_config.encryption_provider.stretches = @sorcery_config.stretches if @sorcery_config.encryption_provider.respond_to?(:stretches) && @sorcery_config.stretches
134-
@sorcery_config.encryption_provider.join_token = @sorcery_config.salt_join_token if @sorcery_config.encryption_provider.respond_to?(:join_token) && @sorcery_config.salt_join_token
135-
@sorcery_config.encryption_provider.pepper = @sorcery_config.pepper if @sorcery_config.encryption_provider.respond_to?(:pepper) && @sorcery_config.pepper
125+
if @sorcery_config.encryption_provider.respond_to?(:stretches) && @sorcery_config.stretches
126+
@sorcery_config.encryption_provider.stretches = @sorcery_config.stretches
127+
end
128+
if @sorcery_config.encryption_provider.respond_to?(:join_token) && @sorcery_config.salt_join_token
129+
@sorcery_config.encryption_provider.join_token = @sorcery_config.salt_join_token
130+
end
131+
return unless @sorcery_config.encryption_provider.respond_to?(:pepper) && @sorcery_config.pepper
132+
133+
@sorcery_config.encryption_provider.pepper = @sorcery_config.pepper
136134
end
137135

138136
protected
@@ -189,7 +187,9 @@ def valid_password?(pass)
189187
# encrypts password with salt and saves it.
190188
def encrypt_password
191189
config = sorcery_config
192-
send(:"#{config.salt_attribute_name}=", new_salt = TemporaryToken.generate_random_token) unless config.salt_attribute_name.nil?
190+
unless config.salt_attribute_name.nil?
191+
send(:"#{config.salt_attribute_name}=", new_salt = TemporaryToken.generate_random_token)
192+
end
193193
send(:"#{config.crypted_password_attribute_name}=", self.class.encrypt(send(config.password_attribute_name), new_salt))
194194
end
195195

lib/sorcery/model/submodules/activity_logging.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ def online?
6363
# shows if user is logged in, but it not show if user is online - see online?
6464
def logged_in?
6565
return false if send(sorcery_config.last_login_at_attribute_name).nil?
66-
return true if send(sorcery_config.last_login_at_attribute_name).present? && send(sorcery_config.last_logout_at_attribute_name).nil?
66+
if send(sorcery_config.last_login_at_attribute_name).present? && send(sorcery_config.last_logout_at_attribute_name).nil?
67+
return true
68+
end
6769

6870
send(sorcery_config.last_login_at_attribute_name) > send(sorcery_config.last_logout_at_attribute_name)
6971
end

0 commit comments

Comments
 (0)