2010-12-18 16:38:05 -05:00
|
|
|
module ActiveModel
|
|
|
|
module SecurePassword
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
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
|
|
|
#
|
2010-12-19 11:58:14 -05:00
|
|
|
# Validations for presence of password, confirmation of password (using
|
|
|
|
# a "password_confirmation" attribute) are automatically added.
|
2010-12-18 16:38:05 -05:00
|
|
|
# You can add more validations by hand if need be.
|
|
|
|
#
|
2011-09-03 01:27:07 -04:00
|
|
|
# You need to add bcrypt-ruby (~> 3.0.0) to Gemfile to use has_secure_password:
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
2010-12-19 11:58:14 -05:00
|
|
|
# user = User.new(:name => "david", :password => "", :password_confirmation => "nomatch")
|
|
|
|
# user.save # => false, password required
|
2010-12-19 04:39:54 -05:00
|
|
|
# user.password = "mUc3m00RsqyRe"
|
2010-12-18 16:38:05 -05:00
|
|
|
# user.save # => false, confirmation doesn't match
|
2010-12-19 04:39:54 -05:00
|
|
|
# user.password_confirmation = "mUc3m00RsqyRe"
|
2010-12-18 16:38:05 -05:00
|
|
|
# user.save # => true
|
|
|
|
# user.authenticate("notright") # => false
|
|
|
|
# user.authenticate("mUc3m00RsqyRe") # => user
|
|
|
|
# User.find_by_name("david").try(:authenticate, "notright") # => nil
|
|
|
|
# User.find_by_name("david").try(:authenticate, "mUc3m00RsqyRe") # => user
|
|
|
|
def has_secure_password
|
2011-10-17 09:44:29 -04:00
|
|
|
# Load bcrypt-ruby only when has_secure_password is used.
|
|
|
|
# This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library.
|
2011-09-02 00:54:17 -04:00
|
|
|
gem 'bcrypt-ruby', '~> 3.0.0'
|
|
|
|
require 'bcrypt'
|
|
|
|
|
2011-05-22 07:02:47 -04:00
|
|
|
attr_reader :password
|
2010-12-18 16:38:05 -05:00
|
|
|
|
|
|
|
validates_confirmation_of :password
|
|
|
|
validates_presence_of :password_digest
|
2011-04-11 00:18:12 -04:00
|
|
|
|
2010-12-29 13:18:14 -05:00
|
|
|
include InstanceMethodsOnActivation
|
2011-01-25 21:35:02 -05:00
|
|
|
|
|
|
|
if respond_to?(:attributes_protected_by_default)
|
|
|
|
def self.attributes_protected_by_default
|
|
|
|
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
|
|
|
|
# Returns self if the password is correct, otherwise false.
|
|
|
|
def authenticate(unencrypted_password)
|
|
|
|
if BCrypt::Password.new(password_digest) == unencrypted_password
|
|
|
|
self
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2010-12-18 16:38:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-29 13:18:14 -05:00
|
|
|
# Encrypts the password into the password_digest attribute.
|
|
|
|
def password=(unencrypted_password)
|
|
|
|
@password = unencrypted_password
|
2011-04-14 17:54:25 -04:00
|
|
|
unless unencrypted_password.blank?
|
|
|
|
self.password_digest = BCrypt::Password.create(unencrypted_password)
|
|
|
|
end
|
2010-12-29 13:18:14 -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
|