password_rehasher 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 409cef18ec9de50002c666881ae5cb056408d819
4
- data.tar.gz: 8fd6efa013ae5e7cacaf6dcbff3ec0513dacd884
3
+ metadata.gz: 5169827d96b12add64e015006c864e49377f80f0
4
+ data.tar.gz: 62a0b15caeab61c3984f3f21468943b76db0dc0c
5
5
  SHA512:
6
- metadata.gz: 01cd32f04dd356ed981600d1da983976e83937c75ab877e4f2ad13b85215cb40969e2f226fabbd7c5effbada3c7ac0942091098770f5d0898d768f938792ebce
7
- data.tar.gz: e059422957191020d44b05508408db860539c8ee7ad1f38735655a61f542b34f9729feee4da13a6e10325640322fb3d4cf5df2eda2448da1d6e23b77a67c1274
6
+ metadata.gz: d6bf1ec18d4001703ae8b261f0d3b25438a82973757f67bea675f228ccc7b80c85fc0a49a74d04de4e58d2464c2de5f05966c7981938984bb96ab83bb40f6227
7
+ data.tar.gz: e1e0a533d1e0a7f383771f61224754084e21dd64289ecb7673734406574ef06e58615479b099822cab09f3896f31f209964501e594d1da7e8c1ae87ca881a3ad
data/README.md CHANGED
@@ -7,11 +7,22 @@ Password Rehasher is a temporary gem to rehash the passwords in the RPM database
7
7
  ```ruby
8
8
  if (PasswordRehasher.password_valid?(plaintext_password, crypted_password)) {
9
9
  if (PasswordRehasher.rehash_needed?(crypted_password)) {
10
- user.crypted_password = PasswordRehasher.hash_password(password)
10
+ user.crypted_password = PasswordRehasher.hash_password(plaintext_password)
11
11
  user.save
12
12
  }
13
13
  # user is logged in
14
14
  } else {
15
15
  # user is not logged in
16
16
  }
17
+ ```
18
+
19
+ ## Alternatively, to do all of the above if user.update_attribute("crypted_password", crypted_password) is what you want to do
20
+
21
+ ```ruby
22
+ if (PasswordRehasher.validate_and_rehash?(user, plaintext_password, crypted_password)) {
23
+ # user is logged in and the password is rehashed (if necessary)
24
+ } else {
25
+ # user is not logged in
26
+ }
27
+
17
28
  ```
@@ -2,7 +2,7 @@ require "scrypt"
2
2
  require 'digest/sha1'
3
3
 
4
4
  class PasswordRehasher
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
 
7
7
  def self.password_valid?(plaintext_password, hashed_password)
8
8
  case hashed_password.length
@@ -28,4 +28,15 @@ class PasswordRehasher
28
28
  def self.hash_password(plaintext_password)
29
29
  SCrypt::Password.create(plaintext_password)
30
30
  end
31
+
32
+ def self.validate_and_rehash?(user, plaintext_password, hashed_password)
33
+ if (plaintext_password && password_valid?(plaintext_password, hashed_password))
34
+ if (rehash_needed?(hashed_password))
35
+ user.update_attribute("crypted_password", hash_password(plaintext_password))
36
+ end
37
+ return true
38
+ else
39
+ return false
40
+ end
41
+ end
31
42
  end
@@ -26,12 +26,12 @@ describe PasswordRehasher do
26
26
  it { is_expected.to be_truthy }
27
27
  end
28
28
 
29
- context 'with an scrypt hash' do
29
+ context 'with a scrypt hash' do
30
30
  let(:hashed_password) { scrypt_hashed_password }
31
31
  it { is_expected.to be_truthy }
32
32
  end
33
33
 
34
- context 'with an scrypt hash' do
34
+ context 'with an invalid scrypt hash' do
35
35
  let(:hashed_password) { invalid_hashed_password }
36
36
  it { is_expected.to be_falsey }
37
37
  end
@@ -50,7 +50,7 @@ describe PasswordRehasher do
50
50
  it { is_expected.to be_truthy }
51
51
  end
52
52
 
53
- context 'with an scrypt hash' do
53
+ context 'with a scrypt hash' do
54
54
  let(:hashed_password) { scrypt_hashed_password }
55
55
  it { is_expected.to be_falsey }
56
56
  end
@@ -67,4 +67,58 @@ describe PasswordRehasher do
67
67
  expect(password_object == incorrect_plaintext_password).to be_falsey
68
68
  end
69
69
  end
70
+
71
+ describe '.validate_and_rehash?' do
72
+ subject { PasswordRehasher.validate_and_rehash?(user, plaintext_password, hashed_password) }
73
+ let(:user) { MockUser.new(hashed_password) }
74
+
75
+ context 'with a SHA1 hash' do
76
+ let(:hashed_password) { sha1_hashed_password }
77
+ it { is_expected.to be_truthy }
78
+
79
+ it "updates the stored crypted_password value" do
80
+ expect { subject }.to change { user.crypted_password }.from(hashed_password)
81
+ end
82
+ end
83
+
84
+ context 'with a nested hash' do
85
+ let(:hashed_password) { nested_hashed_password }
86
+ it { is_expected.to be_truthy }
87
+
88
+ it "updates the stored crypted_password value" do
89
+ expect { subject }.to change { user.crypted_password }.from(hashed_password)
90
+ end
91
+ end
92
+
93
+ context 'with a scrypt hash' do
94
+ let(:hashed_password) { scrypt_hashed_password }
95
+ it { is_expected.to be_truthy }
96
+
97
+ it "does not update the stored crypted_password value" do
98
+ expect { subject }.to_not change { user.crypted_password }.from(hashed_password)
99
+ end
100
+ end
101
+
102
+ context 'with an invalid scrypt hash' do
103
+ let(:hashed_password) { invalid_hashed_password }
104
+ it { is_expected.to be_falsey }
105
+
106
+ it "does not update the stored crypted_password value" do
107
+ expect { subject }.to_not change { user.crypted_password }.from(hashed_password)
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ class MockUser
114
+ attr_accessor :crypted_password
115
+
116
+ def initialize(crypted_password)
117
+ @crypted_password = crypted_password
118
+ end
119
+
120
+ def update_attribute(attribute, value)
121
+ @crypted_password = value
122
+ # attribute is always "crypted_password" for now
123
+ end
70
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: password_rehasher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hyland
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-10-19 00:00:00.000000000 Z
12
+ date: 2015-10-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: scrypt