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 encryptors other than authlogic_sha512 #295

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ Devise.setup do |config|
end
```

### Encryptable

**Warning:** Changing the encryptor will invalidate all existing passwords in your users table!

Spree Auth (Devise) by default uses the `authlogic_sha512` encryptor provided by the [Devise Encryptable](https://github.com/plataformatec/devise-encryptable) gem. New projects (or older projects migrating to Spree) may require the more the secure `bcrypt` (Devise's default) or some other encryptor provided by Devise Encryptable.

To change the encryptor:

* Add this line to an initializer in your Rails project (typically `config/initializers/spree.rb`):
```ruby
Spree::Auth::Config[:encryptor] = 'bcrypt'
```

* Add a Devise initializer to your Rails project (typically `config/initializers/devise.rb`):
```ruby
Devise.setup do |config|
# Set stretches to at least 10 (the default)
config.stretches = 10
end
```

## Using in an existing Rails application

If you are installing Spree inside of a host application in which you want your own permission setup, you can do this using spree_auth_devise's register_ability method.
Expand Down
1 change: 1 addition & 0 deletions app/models/spree/auth_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class AuthConfiguration < Preferences::Configuration
preference :registration_step, :boolean, :default => true
preference :signout_after_password_change, :boolean, :default => true
preference :confirmable, :boolean, :default => false
preference :encryptor, :string, :default => 'authlogic_sha512'
end
end
8 changes: 7 additions & 1 deletion app/models/spree/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ class User < ActiveRecord::Base
include UserPaymentSource

devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :encryptable, :encryptor => 'authlogic_sha512'
:rememberable, :trackable, :validatable
devise :confirmable if Spree::Auth::Config[:confirmable]

# Devise's default encryptor is bcrypt and does not require the encryptable
# module to be loaded.
if Spree::Auth::Config[:encryptor] != 'bcrypt'
devise :encryptable, :encryptor => Spree::Auth::Config[:encryptor]
end

acts_as_paranoid
after_destroy :scramble_email_and_password

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# ==> Configuration for :database_authenticatable
# For bcrypt, this is the cost for hashing the password and defaults to 10. If
# using other encryptors, it sets how many times you want the password re-encrypted.
config.stretches = 20
config.stretches = 10

# Setup a pepper to generate the encrypted password.
config.pepper = Rails.configuration.secret_token
Expand Down
32 changes: 32 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,36 @@
expect(Spree::User.devise_modules).to_not include(:confirmable)
end
end

describe 'encryptable' do
context 'when encryptor option is set to authlogic_sha512' do
around do |example|
with_config_option(:encryptor, 'authlogic_sha512') { example.run }
end

it 'includes encryptable module' do
expect(Spree::User.devise_modules).to include(:encryptable)
end

describe '.encryptor' do
it 'returns authlogic_sha512' do
expect(Spree::User.encryptor).to eq('authlogic_sha512')
end
end
end

context 'when encryptor option is set to bcrypt' do
around do |example|
with_config_option(:encryptor, 'bcrypt') { example.run }
end

it 'does not include encryptable module' do
expect(Spree::User.devise_modules).to_not include(:encryptable)
end

it 'does not respond to .encryptor' do
expect(Spree::User).to_not respond_to(:encryptor)
end
end
end
end
18 changes: 18 additions & 0 deletions spec/support/config_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module ConfigHelpers
def with_config_option(key, value)
option_was = Spree::Auth::Config[key]
set_config_option(key, value)
yield
set_config_option(key, option_was)
end

def set_config_option(key, value)
Spree::Auth::Config[key] = value
Spree.send(:remove_const, 'User')
load File.expand_path("../../../app/models/spree/user.rb", __FILE__)
end
end

RSpec.configure do |c|
c.include ConfigHelpers
end