From 3f6102f24e2d2edf04dd6e04af6085076f355f74 Mon Sep 17 00:00:00 2001 From: Tieg Zaharia Date: Thu, 10 Apr 2014 14:07:47 -0400 Subject: [PATCH] Autoload CryptoProvider classes (fixes a bug where scrypt/bcrypt gems are required to run Authlogic -- let's just require them when necessary) --- lib/authlogic.rb | 8 +------- lib/authlogic/acts_as_authentic/password.rb | 6 +++++- .../acts_as_authentic/restful_authentication.rb | 14 +++++++------- lib/authlogic/crypto_providers.rb | 11 +++++++++++ 4 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 lib/authlogic/crypto_providers.rb diff --git a/lib/authlogic.rb b/lib/authlogic.rb index dd3e1e34..f0d97d95 100644 --- a/lib/authlogic.rb +++ b/lib/authlogic.rb @@ -9,13 +9,7 @@ "controller_adapters/abstract_adapter", - "crypto_providers/md5", - "crypto_providers/sha1", - "crypto_providers/sha256", - "crypto_providers/sha512", - "crypto_providers/bcrypt", - "crypto_providers/aes256", - "crypto_providers/scrypt", + "crypto_providers", "authenticates_many/base", "authenticates_many/association", diff --git a/lib/authlogic/acts_as_authentic/password.rb b/lib/authlogic/acts_as_authentic/password.rb index ce485255..0e6fcaba 100644 --- a/lib/authlogic/acts_as_authentic/password.rb +++ b/lib/authlogic/acts_as_authentic/password.rb @@ -149,7 +149,11 @@ def merge_validates_length_of_password_confirmation_field_options(options = {}) # * Default: CryptoProviders::SCrypt # * Accepts: Class def crypto_provider(value = nil) - rw_config(:crypto_provider, value, CryptoProviders::SCrypt) + if value.nil? and !acts_as_authentic_config.include?(:crypto_provider) + rw_config(:crypto_provider, CryptoProviders::SCrypt) + else + rw_config(:crypto_provider, value) + end end alias_method :crypto_provider=, :crypto_provider diff --git a/lib/authlogic/acts_as_authentic/restful_authentication.rb b/lib/authlogic/acts_as_authentic/restful_authentication.rb index 1e6d5a0a..31525c70 100644 --- a/lib/authlogic/acts_as_authentic/restful_authentication.rb +++ b/lib/authlogic/acts_as_authentic/restful_authentication.rb @@ -8,7 +8,7 @@ def self.included(klass) include InstanceMethods end end - + module Config # Switching an existing app to Authlogic from restful_authentication? No problem, just set this true and your users won't know # anything changed. From your database perspective nothing will change at all. Authlogic will continue to encrypt passwords @@ -23,18 +23,18 @@ def act_like_restful_authentication(value = nil) r end alias_method :act_like_restful_authentication=, :act_like_restful_authentication - + # This works just like act_like_restful_authentication except that it will start transitioning your users to the algorithm you # specify with the crypto provider option. The next time they log in it will resave their password with the new algorithm # and any new record will use the new algorithm as well. Make sure to update your users table if you are using the default - # migration since it will set crypted_password and salt columns to a maximum width of 40 characters which is not enough. + # migration since it will set crypted_password and salt columns to a maximum width of 40 characters which is not enough. def transition_from_restful_authentication(value = nil) r = rw_config(:transition_from_restful_authentication, value, false) set_restful_authentication_config if value r end alias_method :transition_from_restful_authentication=, :transition_from_restful_authentication - + private def set_restful_authentication_config crypto_provider_key = act_like_restful_authentication ? :crypto_provider : :transition_from_crypto_providers @@ -45,17 +45,17 @@ def set_restful_authentication_config end end end - + module InstanceMethods private def act_like_restful_authentication? self.class.act_like_restful_authentication == true end - + def transition_from_restful_authentication? self.class.transition_from_restful_authentication == true end end end end -end \ No newline at end of file +end diff --git a/lib/authlogic/crypto_providers.rb b/lib/authlogic/crypto_providers.rb new file mode 100644 index 00000000..7d7bad64 --- /dev/null +++ b/lib/authlogic/crypto_providers.rb @@ -0,0 +1,11 @@ +module Authlogic + module CryptoProviders + autoload :MD5, "authlogic/crypto_providers/md5" + autoload :Sha1, "authlogic/crypto_providers/sha1" + autoload :Sha256, "authlogic/crypto_providers/sha256" + autoload :Sha512, "authlogic/crypto_providers/sha512" + autoload :BCrypt, "authlogic/crypto_providers/bcrypt" + autoload :AES256, "authlogic/crypto_providers/aes256" + autoload :SCrypt, "authlogic/crypto_providers/scrypt" + end +end