passwd 0.1.3 → 0.1.4

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: fcb00a834c359109c28cbe26c845b8baa2ec93b7
4
- data.tar.gz: 07cff7e7538f4f59889fecad96ab0715edd9ee05
3
+ metadata.gz: b9ba7557a87c9ef3d0b947b32bae95c8693ce96c
4
+ data.tar.gz: 07de417852343a3cdb13f6ecf27f36b95190c307
5
5
  SHA512:
6
- metadata.gz: cc8ac70bace4de95e9ae1d9d6f05b16843b1c27d3c2af09b748c38100db9f79105a991da929a387b9bf76733b57a7885d98238fccc27f9b7b9b5ca5bd5af804b
7
- data.tar.gz: b53e2c20abb856f932de3612a63f0cde50d6d7498ec233b7bc2a3652899c6420d63801ee5e1901c1adf7b32b0a9c6a5040f462d4ce3ab7ac21cce4dc0f20d1f9
6
+ metadata.gz: b1072b0d5f670f2698c0f6b5982c457a587e3a3afcd7638586233a3a2425c5ec98a4ae8a50c9b91adeacd4a10c8eb8f59f95a8db2d7eb5b855c7bd8aef6f4b32
7
+ data.tar.gz: 4367e04477af7a420763b8681a9a455263a3094d2190a49c6c0c1b75bbd8b5ae9398574771410d5bc926e9b1bb0c5ea9c24ab947b53a55e5aa3aa6eadcb1acb6
data/README.md CHANGED
@@ -180,21 +180,30 @@ if user.save
180
180
  NoticeMailer.change_mail(user, password_text).deliver
181
181
  end
182
182
  ```
183
+
183
184
  `update_password` method will be set new password if the authentication successful.
184
- Return the nil if authentication fails.
185
185
  But `update_password` method doesn't call `save` method.
186
186
 
187
187
  ```ruby
188
188
  @user = User.find(params[:id])
189
189
 
190
- if Passwd.confirm_check(params[:new_pass], params[:new_pass_confirm])
191
- if @user.update_password(old_pass, new_pass) && @user.save # => return new password(text) or false
192
- NoticeMailer.change_mail(user, password_text).deliver
193
- else
194
- puts "Authentication failed!"
195
- end
196
- else
197
- puts "Password don't match!"
190
+ begin
191
+ confirm_check(new_pass, confirm)
192
+ @user.update_password(old_pass, new_pass, true)
193
+ @user.save!
194
+ redirect_to bar_path, notice: "Password updated successfully"
195
+ rescue PasswordNotMatch
196
+ flash.now[:alert] = "Password not match"
197
+ render action: :edit
198
+ rescue AuthError
199
+ flash.now[:alert] = "Password is incorrect"
200
+ render action: :edit
201
+ rescue PolicyNotMatch
202
+ flash.now[:alert] = "Policy not match"
203
+ render action: :edit
204
+ rescue
205
+ flash.now[:alert] = "Password update failed"
206
+ render action: :edit
198
207
  end
199
208
  ```
200
209
 
@@ -3,6 +3,7 @@
3
3
  require "digest/sha1"
4
4
 
5
5
  require "passwd/version"
6
+ require "passwd/errors"
6
7
  require "passwd/base"
7
8
  require "passwd/password"
8
9
  require "passwd/active_record"
@@ -39,11 +39,15 @@ module Passwd
39
39
  end
40
40
 
41
41
  def define_update_password(salt_name, password_name)
42
- define_method :update_password do |old_pass, new_pass|
42
+ define_method :update_password do |old_pass, new_pass, policy_check=false|
43
43
  if Passwd.auth(old_pass, self.send(salt_name), self.send(password_name))
44
+ if policy_check
45
+ raise Passwd::PolicyNotMatch, "Policy not match" unless Passwd.policy_check(new_pass)
46
+ end
47
+
44
48
  set_password(new_pass)
45
49
  else
46
- false
50
+ raise Passwd::AuthError
47
51
  end
48
52
  end
49
53
  end
@@ -29,7 +29,7 @@ module Passwd
29
29
  end
30
30
 
31
31
  def confirm_check(password, confirm, with_policy=false)
32
- return false if password != confirm
32
+ raise PasswordNotMatch, "Password not match" if password != confirm
33
33
  return true unless with_policy
34
34
  Passwd.policy_check(password)
35
35
  end
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+
3
+ module Passwd
4
+ class PasswdError < StandardError
5
+ end
6
+
7
+ class AuthError < PasswdError
8
+ end
9
+
10
+ class PasswordNotMatch < PasswdError
11
+ end
12
+
13
+ class PolicyNotMatch < PasswdError
14
+ end
15
+ end
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  module Passwd
4
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
5
3
  end
@@ -12,14 +12,14 @@ describe Passwd::ActiveRecord do
12
12
  let(:password_text) {"secret"}
13
13
  let(:password_hash) {Digest::SHA1.hexdigest("#{salt}#{password_text}")}
14
14
 
15
- describe ".included" do
15
+ describe ".#included" do
16
16
  it "define singleton methods" do
17
17
  expect(User.respond_to? :define_column).to be_true
18
18
  end
19
19
  end
20
20
 
21
21
  describe "extend methods" do
22
- describe ".define_column" do
22
+ describe ".#define_column" do
23
23
  let(:user) {User.new}
24
24
 
25
25
  it "define singleton methods" do
@@ -41,7 +41,7 @@ describe Passwd::ActiveRecord do
41
41
  end
42
42
 
43
43
  describe "defined methods from define_column" do
44
- describe ".authenticate" do
44
+ describe ".#authenticate" do
45
45
  let!(:record) {
46
46
  record = double("record mock")
47
47
  record.stub(:salt).and_return(salt)
@@ -126,16 +126,37 @@ describe Passwd::ActiveRecord do
126
126
  user
127
127
  }
128
128
 
129
- it "should return update password" do
130
- pass = "new_password"
131
- user.should_receive(:set_password).with(pass).and_return(pass)
132
- expect(user.update_password(password_text, pass)).to eq(pass)
129
+ context "without policy check" do
130
+ it "should return update password" do
131
+ pass = "new_password"
132
+ user.should_receive(:set_password).with(pass).and_return(pass)
133
+ expect(user.update_password(password_text, pass)).to eq(pass)
134
+ end
135
+
136
+ it "should generate exception if authentication failed" do
137
+ Passwd.should_receive(:auth).and_return(false)
138
+ user.should_not_receive(:set_password)
139
+ expect {
140
+ user.update_password("invalid_password", "new_password")
141
+ }.to raise_error(Passwd::AuthError)
142
+ end
133
143
  end
134
144
 
135
- it "should return false if authentication failed" do
136
- Passwd.should_receive(:auth).and_return(false)
137
- user.should_not_receive(:set_password)
138
- user.update_password("invalid_password", "new_password")
145
+ context "with policy check" do
146
+ it "should return update password" do
147
+ pass = "new_password"
148
+ Passwd.should_receive(:policy_check).and_return(true)
149
+ user.should_receive(:set_password).with(pass).and_return(pass)
150
+ expect(user.update_password(password_text, pass, true)).to eq(pass)
151
+ end
152
+
153
+ it "should generate exception if policy not match" do
154
+ pass = "new_password"
155
+ Passwd.should_receive(:policy_check).and_return(false)
156
+ expect {
157
+ user.update_password(password_text, pass, true)
158
+ }.to raise_error(Passwd::PolicyNotMatch)
159
+ end
139
160
  end
140
161
  end
141
162
  end
@@ -81,8 +81,10 @@ describe Passwd do
81
81
 
82
82
  describe "#confirm_check" do
83
83
  context "with out policy check" do
84
- it "return false if password don't match" do
85
- expect(Passwd.confirm_check("secret", "invalid")).to be_false
84
+ it "should generate exception if password don't match" do
85
+ expect{
86
+ Passwd.confirm_check("secret", "invalid")
87
+ }.to raise_error(Passwd::PasswordNotMatch)
86
88
  end
87
89
 
88
90
  it "return true if password matches" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passwd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - i2bskn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-15 00:00:00.000000000 Z
11
+ date: 2013-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,7 @@ files:
73
73
  - lib/passwd/configuration/config.rb
74
74
  - lib/passwd/configuration/policy.rb
75
75
  - lib/passwd/configuration/tmp_config.rb
76
+ - lib/passwd/errors.rb
76
77
  - lib/passwd/password.rb
77
78
  - lib/passwd/version.rb
78
79
  - passwd.gemspec