-
-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added blowfish password encryption strategy.
- Loading branch information
Showing
4 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module Clearance | ||
module PasswordStrategies | ||
autoload :SHA1, 'clearance/password_strategies/sha1' | ||
autoload :Blowfish, 'clearance/password_strategies/blowfish' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'openssl' | ||
|
||
module Clearance | ||
module PasswordStrategies | ||
module Blowfish | ||
extend ActiveSupport::Concern | ||
|
||
# Am I authenticated with given password? | ||
# | ||
# @param [String] plain-text password | ||
# @return [true, false] | ||
# @example | ||
# user.authenticated?('password') | ||
def authenticated?(password) | ||
encrypted_password == encrypt(password) | ||
end | ||
|
||
protected | ||
|
||
def encrypt_password | ||
initialize_salt_if_necessary | ||
if password.present? | ||
self.encrypted_password = encrypt(password) | ||
end | ||
end | ||
|
||
def generate_hash(string) | ||
# TODO: 1.9 vs 1.8 testing | ||
cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').encrypt | ||
cipher.key = Digest::SHA256.digest(salt) | ||
cipher.update(string) << cipher.final | ||
end | ||
|
||
def encrypt(string) | ||
generate_hash("--#{salt}--#{string}--") | ||
end | ||
|
||
def initialize_salt_if_necessary | ||
if salt.blank? | ||
self.salt = generate_random_code | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'spec_helper' | ||
|
||
describe Clearance::PasswordStrategies::Blowfish do | ||
subject do | ||
Class.new do | ||
attr_accessor :salt, :password, :encrypted_password | ||
include Clearance::PasswordStrategies::Blowfish | ||
|
||
def generate_random_code; "code"; end | ||
end.new | ||
end | ||
|
||
describe "#encrypt_password" do | ||
context "when the password is set" do | ||
let(:salt) { "salt" } | ||
let(:password) { "password" } | ||
|
||
before do | ||
subject.salt = salt | ||
subject.password = password | ||
subject.send(:encrypt_password) | ||
end | ||
|
||
it "should encrypt the password using Blowfish into encrypted_password" do | ||
cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').encrypt | ||
cipher.key = Digest::SHA256.digest(salt) | ||
expected = cipher.update("--#{salt}--#{password}--") << cipher.final | ||
|
||
subject.encrypted_password.should == expected | ||
end | ||
end | ||
|
||
context "when the salt is not set" do | ||
before do | ||
subject.salt = nil | ||
|
||
subject.send(:encrypt_password) | ||
end | ||
|
||
it "should initialize the salt" do | ||
subject.salt.should_not be_nil | ||
end | ||
end | ||
end | ||
end |