Skip to content

Commit

Permalink
Autoload CryptoProvider classes
Browse files Browse the repository at this point in the history
(fixes a bug where scrypt/bcrypt gems are required to run Authlogic --
let's just require them when necessary)
  • Loading branch information
Tieg Zaharia committed Apr 10, 2014
1 parent f19ff3a commit 3f6102f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
8 changes: 1 addition & 7 deletions lib/authlogic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion lib/authlogic/acts_as_authentic/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ def merge_validates_length_of_password_confirmation_field_options(options = {})
# * <tt>Default:</tt> CryptoProviders::SCrypt
# * <tt>Accepts:</tt> 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

Expand Down
14 changes: 7 additions & 7 deletions lib/authlogic/acts_as_authentic/restful_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
end
11 changes: 11 additions & 0 deletions lib/authlogic/crypto_providers.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3f6102f

Please sign in to comment.