forked from thoughtbot/clearance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcrypt.rb
41 lines (33 loc) · 839 Bytes
/
bcrypt.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Clearance
module PasswordStrategies
module BCrypt
require 'bcrypt'
extend ActiveSupport::Concern
def authenticated?(password)
if encrypted_password.present?
::BCrypt::Password.new(encrypted_password) == password
end
end
def password=(new_password)
@password = new_password
if new_password.present?
self.encrypted_password = encrypt(new_password)
end
end
private
def encrypt(password)
::BCrypt::Password.create(password, cost: cost)
end
def cost
if test_environment?
::BCrypt::Engine::MIN_COST
else
::BCrypt::Engine::DEFAULT_COST
end
end
def test_environment?
defined?(::Rails) && ::Rails.env.test?
end
end
end
end