From 29f555531748f1016dd3cf6c9117d775468570a5 Mon Sep 17 00:00:00 2001 From: mihael Date: Sun, 5 May 2019 09:54:08 +0200 Subject: [PATCH] Allow configuring crypto_providers with Strings ~ to prevent autoloading SCrypt before config is applied --- lib/authlogic/acts_as_authentic/password.rb | 18 +++++++++------ lib/authlogic/crypto_providers.rb | 13 +++++++++++ test/acts_as_authentic_test/password_test.rb | 24 ++++++++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/authlogic/acts_as_authentic/password.rb b/lib/authlogic/acts_as_authentic/password.rb index 30bc2ee0..d0d5b1ba 100644 --- a/lib/authlogic/acts_as_authentic/password.rb +++ b/lib/authlogic/acts_as_authentic/password.rb @@ -113,7 +113,9 @@ def check_passwords_against_database(value = nil) # * Accepts: Class def crypto_provider(value = nil) CryptoProviders::Guidance.new(value).impart_wisdom - rw_config(:crypto_provider, value, CryptoProviders::SCrypt) + CryptoProviders.crypto_providers_from( + rw_config(:crypto_provider, value, "Authlogic::CryptoProviders::SCrypt") + ) end alias crypto_provider= crypto_provider @@ -135,10 +137,12 @@ def crypto_provider(value = nil) # * Default: nil # * Accepts: Class or Array def transition_from_crypto_providers(value = nil) - rw_config( - :transition_from_crypto_providers, - (!value.nil? && [value].flatten.compact) || value, - [] + CryptoProviders.crypto_providers_from( + rw_config( + :transition_from_crypto_providers, + (!value.nil? && [value].flatten.compact) || value, + [] + ) ) end alias transition_from_crypto_providers= transition_from_crypto_providers @@ -334,11 +338,11 @@ def password_salt_field end def crypto_provider - self.class.crypto_provider + CryptoProviders.crypto_providers_from(self.class.crypto_provider) end def transition_from_crypto_providers - self.class.transition_from_crypto_providers + CryptoProviders.crypto_providers_from(self.class.transition_from_crypto_providers) end end end diff --git a/lib/authlogic/crypto_providers.rb b/lib/authlogic/crypto_providers.rb index 3e4b4711..ae280139 100644 --- a/lib/authlogic/crypto_providers.rb +++ b/lib/authlogic/crypto_providers.rb @@ -83,5 +83,18 @@ def impart_wisdom end end end + + # Convert a string constant into a class. + def self.crypto_provider_from(value) + value.is_a?(String) ? value.constantize : value + end + + # Convert an array of string constants into an array of classes. + def self.crypto_providers_from(value) + if value.is_a?(Array) + return value.map { |provider| CryptoProviders.crypto_provider_from(provider) } + end + CryptoProviders.crypto_provider_from(value) + end end end diff --git a/test/acts_as_authentic_test/password_test.rb b/test/acts_as_authentic_test/password_test.rb index 1c60e4d1..fd97520c 100644 --- a/test/acts_as_authentic_test/password_test.rb +++ b/test/acts_as_authentic_test/password_test.rb @@ -67,6 +67,30 @@ def test_transition_from_crypto_providers_config assert_equal [], User.transition_from_crypto_providers end + def test_crypto_provider_config_with_string + assert_equal Authlogic::CryptoProviders::SCrypt, User.crypto_provider + silence_warnings do + User.crypto_provider = "Authlogic::CryptoProviders::BCrypt" + end + assert_equal Authlogic::CryptoProviders::BCrypt, User.crypto_provider + silence_warnings do + User.crypto_provider "Authlogic::CryptoProviders::Sha512" + end + assert_equal Authlogic::CryptoProviders::Sha512, User.crypto_provider + end + + def test_transition_from_crypto_providers_config_with_strings + assert_equal [Authlogic::CryptoProviders::Sha512], User.transition_from_crypto_providers + assert_equal [], Employee.transition_from_crypto_providers + + User.transition_from_crypto_providers = ["Authlogic::CryptoProviders::BCrypt", + "Authlogic::CryptoProviders::Sha512"] + assert_equal [Authlogic::CryptoProviders::BCrypt, Authlogic::CryptoProviders::Sha512], + User.transition_from_crypto_providers + User.transition_from_crypto_providers [] + assert_equal [], User.transition_from_crypto_providers + end + def test_password u = User.new old_password_salt = u.password_salt