2010-12-18 16:38:05 -05:00
|
|
|
module ActiveModel
|
|
|
|
module SecurePassword
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2012-11-14 10:42:54 -05:00
|
|
|
class << self; attr_accessor :min_cost; end
|
2012-11-17 12:21:47 -05:00
|
|
|
self.min_cost = false
|
2012-11-14 10:42:54 -05:00
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
module ClassMethods
|
2010-12-18 22:09:07 -05:00
|
|
|
# Adds methods to set and authenticate against a BCrypt password.
|
|
|
|
# This mechanism requires you to have a password_digest attribute.
|
2010-12-19 04:39:54 -05:00
|
|
|
#
|
2012-07-29 13:04:43 -04:00
|
|
|
# Validations for presence of password on create, confirmation of password
|
|
|
|
# (using a +password_confirmation+ attribute) are automatically added. If
|
|
|
|
# you wish to turn off validations, pass <tt>validations: false</tt> as an
|
|
|
|
# argument. You can add more validations by hand if need be.
|
2010-12-18 16:38:05 -05:00
|
|
|
#
|
2012-11-17 12:21:47 -05:00
|
|
|
# If you don't need the confirmation validation, just don't set any
|
2013-06-25 06:25:03 -04:00
|
|
|
# value to the password_confirmation attribute and the validation
|
2012-11-13 18:31:46 -05:00
|
|
|
# will not be triggered.
|
|
|
|
#
|
2012-07-29 14:35:33 -04:00
|
|
|
# You need to add bcrypt-ruby (~> 3.0.0) to Gemfile to use #has_secure_password:
|
2011-09-03 01:27:07 -04:00
|
|
|
#
|
|
|
|
# gem 'bcrypt-ruby', '~> 3.0.0'
|
|
|
|
#
|
2010-12-18 16:38:05 -05:00
|
|
|
# Example using Active Record (which automatically includes ActiveModel::SecurePassword):
|
|
|
|
#
|
2010-12-18 22:09:07 -05:00
|
|
|
# # Schema: User(name:string, password_digest:string)
|
2010-12-18 16:38:05 -05:00
|
|
|
# class User < ActiveRecord::Base
|
|
|
|
# has_secure_password
|
|
|
|
# end
|
|
|
|
#
|
2012-07-29 13:04:43 -04:00
|
|
|
# user = User.new(name: 'david', password: '', password_confirmation: 'nomatch')
|
2013-04-02 15:09:15 -04:00
|
|
|
# user.save # => false, password required
|
2012-07-29 13:04:43 -04:00
|
|
|
# user.password = 'mUc3m00RsqyRe'
|
2013-04-02 15:09:15 -04:00
|
|
|
# user.save # => false, confirmation doesn't match
|
2012-07-29 13:04:43 -04:00
|
|
|
# user.password_confirmation = 'mUc3m00RsqyRe'
|
2013-04-02 15:09:15 -04:00
|
|
|
# user.save # => true
|
|
|
|
# user.authenticate('notright') # => false
|
|
|
|
# user.authenticate('mUc3m00RsqyRe') # => user
|
|
|
|
# User.find_by(name: 'david').try(:authenticate, 'notright') # => false
|
|
|
|
# User.find_by(name: 'david').try(:authenticate, 'mUc3m00RsqyRe') # => user
|
2012-05-08 18:54:25 -04:00
|
|
|
def has_secure_password(options = {})
|
2011-10-17 09:44:29 -04:00
|
|
|
# Load bcrypt-ruby only when has_secure_password is used.
|
2012-07-29 13:04:43 -04:00
|
|
|
# This is to avoid ActiveModel (and by extension the entire framework)
|
|
|
|
# being dependent on a binary library.
|
2013-03-21 23:51:08 -04:00
|
|
|
begin
|
|
|
|
gem 'bcrypt-ruby', '~> 3.0.0'
|
|
|
|
require 'bcrypt'
|
2013-03-22 13:00:06 -04:00
|
|
|
rescue LoadError
|
2013-03-21 23:51:08 -04:00
|
|
|
$stderr.puts "You don't have bcrypt-ruby installed in your application. Please add it to your Gemfile and run bundle install"
|
2013-03-22 13:00:06 -04:00
|
|
|
raise
|
2013-03-21 23:51:08 -04:00
|
|
|
end
|
2011-09-02 00:54:17 -04:00
|
|
|
|
2011-05-22 07:02:47 -04:00
|
|
|
attr_reader :password
|
2012-07-29 13:04:43 -04:00
|
|
|
|
2013-03-04 12:56:05 -05:00
|
|
|
include InstanceMethodsOnActivation
|
|
|
|
|
2012-05-08 18:54:25 -04:00
|
|
|
if options.fetch(:validations, true)
|
2013-05-20 15:13:21 -04:00
|
|
|
validates_confirmation_of :password, if: lambda { |m| m.password.present? }
|
2013-05-01 20:10:06 -04:00
|
|
|
validates_presence_of :password, on: :create
|
2013-05-20 15:13:21 -04:00
|
|
|
validates_presence_of :password_confirmation, if: lambda { |m| m.password.present? }
|
2012-10-18 02:22:55 -04:00
|
|
|
|
2012-07-31 16:16:21 -04:00
|
|
|
before_create { raise "Password digest missing on new record" if password_digest.blank? }
|
2012-05-08 18:54:25 -04:00
|
|
|
end
|
2011-04-11 00:18:12 -04:00
|
|
|
|
2011-01-25 21:35:02 -05:00
|
|
|
if respond_to?(:attributes_protected_by_default)
|
2012-07-29 13:04:43 -04:00
|
|
|
def self.attributes_protected_by_default #:nodoc:
|
2011-01-25 21:35:02 -05:00
|
|
|
super + ['password_digest']
|
|
|
|
end
|
|
|
|
end
|
2010-12-19 04:39:54 -05:00
|
|
|
end
|
2010-12-18 16:38:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-29 13:18:14 -05:00
|
|
|
module InstanceMethodsOnActivation
|
2012-07-29 13:04:43 -04:00
|
|
|
# Returns +self+ if the password is correct, otherwise +false+.
|
|
|
|
#
|
|
|
|
# class User < ActiveRecord::Base
|
|
|
|
# has_secure_password validations: false
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# user = User.new(name: 'david', password: 'mUc3m00RsqyRe')
|
|
|
|
# user.save
|
|
|
|
# user.authenticate('notright') # => false
|
2012-10-18 02:22:55 -04:00
|
|
|
# user.authenticate('mUc3m00RsqyRe') # => user
|
2010-12-29 13:18:14 -05:00
|
|
|
def authenticate(unencrypted_password)
|
2012-04-24 13:03:59 -04:00
|
|
|
BCrypt::Password.new(password_digest) == unencrypted_password && self
|
2010-12-18 16:38:05 -05:00
|
|
|
end
|
|
|
|
|
2012-07-29 13:04:43 -04:00
|
|
|
# Encrypts the password into the +password_digest+ attribute, only if the
|
2012-04-24 13:08:57 -04:00
|
|
|
# new password is not blank.
|
2012-07-29 13:04:43 -04:00
|
|
|
#
|
|
|
|
# class User < ActiveRecord::Base
|
|
|
|
# has_secure_password validations: false
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# user = User.new
|
|
|
|
# user.password = nil
|
|
|
|
# user.password_digest # => nil
|
|
|
|
# user.password = 'mUc3m00RsqyRe'
|
2012-10-18 02:22:55 -04:00
|
|
|
# user.password_digest # => "$2a$10$4LEA7r4YmNHtvlAvHhsYAeZmk/xeUVtMTYqwIvYY76EW5GUqDiP4."
|
2010-12-29 13:18:14 -05:00
|
|
|
def password=(unencrypted_password)
|
2011-04-14 17:54:25 -04:00
|
|
|
unless unencrypted_password.blank?
|
2012-04-24 13:16:01 -04:00
|
|
|
@password = unencrypted_password
|
2012-11-14 10:42:54 -05:00
|
|
|
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine::DEFAULT_COST
|
|
|
|
self.password_digest = BCrypt::Password.create(unencrypted_password, cost: cost)
|
2011-04-14 17:54:25 -04:00
|
|
|
end
|
2010-12-29 13:18:14 -05:00
|
|
|
end
|
2013-03-04 12:56:05 -05:00
|
|
|
|
|
|
|
def password_confirmation=(unencrypted_password)
|
2013-05-20 15:13:21 -04:00
|
|
|
@password_confirmation = unencrypted_password
|
2013-03-04 12:56:05 -05:00
|
|
|
end
|
2010-12-19 03:30:46 -05:00
|
|
|
end
|
2010-12-18 16:38:05 -05:00
|
|
|
end
|
2010-12-19 11:58:14 -05:00
|
|
|
end
|