Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring crypto_providers with Strings #668

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/authlogic/acts_as_authentic/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def check_passwords_against_database(value = nil)
# * <tt>Accepts:</tt> 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

Expand All @@ -135,10 +137,12 @@ def crypto_provider(value = nil)
# * <tt>Default:</tt> nil
# * <tt>Accepts:</tt> 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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/authlogic/crypto_providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions test/acts_as_authentic_test/password_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down