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

Fix: Autoload CryptoProvider classes #406

Merged
merged 1 commit into from
Apr 28, 2014
Merged
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
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only semi-weird part. Loading SCrypt here would require the gem (even w/autoload). So I only set it as the default if the provider hasn't been set yet.

It works, but another alternative could be allowing procs as the rw_config value?

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