passwd 0.1.4 → 0.1.5

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: b9ba7557a87c9ef3d0b947b32bae95c8693ce96c
4
- data.tar.gz: 07de417852343a3cdb13f6ecf27f36b95190c307
3
+ metadata.gz: a606e91758a61195dc7a2f4a82a16ce1d845c68e
4
+ data.tar.gz: 70f0ece850a255a83972a3ac47c4eb3a6a994a1d
5
5
  SHA512:
6
- metadata.gz: b1072b0d5f670f2698c0f6b5982c457a587e3a3afcd7638586233a3a2425c5ec98a4ae8a50c9b91adeacd4a10c8eb8f59f95a8db2d7eb5b855c7bd8aef6f4b32
7
- data.tar.gz: 4367e04477af7a420763b8681a9a455263a3094d2190a49c6c0c1b75bbd8b5ae9398574771410d5bc926e9b1bb0c5ea9c24ab947b53a55e5aa3aa6eadcb1acb6
6
+ metadata.gz: 0cd8cd9771d2252d9c0123da43ac2324fcd7d33cb194accb5bff78c4732d7713d61d7b8556513a6b4a747aeb14e35473d873d3731f0ad9e4cfad56347bd98fb2
7
+ data.tar.gz: 9a78a83972cbf53c3a8ded6caa0ee7e84b58d0598c3a8b368d710ead163e63b1e114d4ecb47bd2d91b29f151845c4cee4ea391fb0b86aa7b2961390cae92100b
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## 0.1.5
2
+
3
+ Features:
4
+
5
+ - Can be specified algorithm of hashing
6
+ - Change default hashing algorithm to SHA512 from SHA1
data/README.md CHANGED
@@ -53,12 +53,14 @@ Passwd.config # => Get config object.
53
53
  Passwd.config(length: 10) # => Change to the default length.
54
54
 
55
55
  Passwd.configure do |c|
56
+ c.algorithm = :sha512
56
57
  c.length = 10
57
58
  end
58
59
  ```
59
60
 
60
61
  Options that can be specified:
61
62
 
63
+ * :algorithm => Hashing algorithm. default is :sha512.
62
64
  * :length => Number of characters. default is 8.
63
65
  * :lower => Skip lower case if set false. default is true.
64
66
  * :upper => Skip upper case if set false. default is true.
@@ -145,23 +147,28 @@ Return the user object if the authentication successful.
145
147
  Return the nil if authentication fails or doesn't exists user.
146
148
 
147
149
  ```ruby
148
- user = User.authenticate("foo@example.com", "secret") # => return user object or nil.
150
+ user = User.authenticate(params[:email], params[:password]) # => return user object or nil.
149
151
 
150
152
  if user
151
- puts "Hello #{user.name}!"
153
+ session[:user] = user.id
154
+ redirect_to bar_path, notice: "Hello #{user.name}!"
152
155
  else
153
- puts "Authentication failed"
156
+ flash.now[:alert] = "Authentication failed"
157
+ render action: :new
154
158
  end
155
159
  ```
156
160
 
157
161
  instance method is not required `id`.
158
162
 
159
163
  ```ruby
160
- user = User.find(params[:id])
161
- if user.authenticate("secret") # => return true or false
162
- puts "Authentication is successful!"
164
+ current_user = User.find(session[:user])
165
+
166
+ if current_user.authenticate(params[:password]) # => return true or false
167
+ # some process
168
+ redirect_to bar_path, notice: "Some process is successfully"
163
169
  else
164
- puts "Authentication failed!"
170
+ flash.now[:alert] = "Authentication failed"
171
+ render action: :edit
165
172
  end
166
173
  ```
167
174
 
@@ -173,11 +180,13 @@ To specify the password as an argument if you want to specify a password.
173
180
  `salt` also set if salt is nil.
174
181
 
175
182
  ```ruby
176
- user = User.find(params[:id])
177
- password_text = user.set_password
183
+ current_user = User.find(session[:user])
184
+ password_text = current_user.set_password
178
185
 
179
- if user.save
180
- NoticeMailer.change_mail(user, password_text).deliver
186
+ if current_user.save
187
+ redirect_to bar_path, notice: "Password update successfully"
188
+ else
189
+ render action: :edit
181
190
  end
182
191
  ```
183
192
 
@@ -185,23 +194,28 @@ end
185
194
  But `update_password` method doesn't call `save` method.
186
195
 
187
196
  ```ruby
188
- @user = User.find(params[:id])
197
+ current_user = User.find(session[:user])
189
198
 
190
199
  begin
191
- confirm_check(new_pass, confirm)
192
- @user.update_password(old_pass, new_pass, true)
193
- @user.save!
200
+ Passwd.confirm_check(params[:password], params[:password_confirmation])
201
+ # update_password(OLD_PASSWORD, NEW_PASSWORD[, POLICY_CHECK=false])
202
+ current_user.update_password(old_pass, new_pass, true)
203
+ current_user.save!
194
204
  redirect_to bar_path, notice: "Password updated successfully"
195
- rescue PasswordNotMatch
205
+ rescue Passwd::PasswordNotMatch
206
+ # PASSWORD != PASSWORD_CONFIRMATION from Passwd.#confirm_check
196
207
  flash.now[:alert] = "Password not match"
197
208
  render action: :edit
198
- rescue AuthError
209
+ rescue Passwd::AuthError
210
+ # Authentication failed from #update_password
199
211
  flash.now[:alert] = "Password is incorrect"
200
212
  render action: :edit
201
- rescue PolicyNotMatch
213
+ rescue Passwd::PolicyNotMatch
214
+ # Policy not match from #update_password
202
215
  flash.now[:alert] = "Policy not match"
203
216
  render action: :edit
204
217
  rescue
218
+ # Other errors
205
219
  flash.now[:alert] = "Password update failed"
206
220
  render action: :edit
207
221
  end
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
- require 'rspec/core/rake_task'
2
+ require "rspec/core/rake_task"
3
3
 
4
4
  desc "Run all specs"
5
5
  RSpec::Core::RakeTask.new(:spec) do |t|
data/lib/passwd.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require "digest/sha1"
4
+ require "digest/sha2"
4
5
 
5
6
  require "passwd/version"
6
7
  require "passwd/errors"
data/lib/passwd/base.rb CHANGED
@@ -24,8 +24,12 @@ module Passwd
24
24
  password_hash == enc_pass
25
25
  end
26
26
 
27
- def hashing(plain)
28
- Digest::SHA1.hexdigest plain
27
+ def hashing(plain, algorithm=nil)
28
+ if algorithm.nil?
29
+ eval "Digest::#{@config.algorithm.to_s.upcase}.hexdigest \"#{plain}\""
30
+ else
31
+ eval "Digest::#{algorithm.to_s.upcase}.hexdigest \"#{plain}\""
32
+ end
29
33
  end
30
34
 
31
35
  def confirm_check(password, confirm, with_policy=false)
@@ -3,6 +3,7 @@
3
3
  module Passwd
4
4
  class AbstractConfig
5
5
  VALID_OPTIONS_KEYS = [
6
+ :algorithm,
6
7
  :length,
7
8
  :lower,
8
9
  :upper,
@@ -11,6 +11,7 @@ module Passwd
11
11
  end
12
12
 
13
13
  def reset
14
+ self.algorithm = :sha512
14
15
  self.length = 8
15
16
  self.lower = true
16
17
  self.upper = true
@@ -1,3 +1,3 @@
1
1
  module Passwd
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -8,9 +8,9 @@ describe Passwd::ActiveRecord do
8
8
  define_column
9
9
  end
10
10
 
11
- let(:salt) {Digest::SHA1.hexdigest("salt")}
11
+ let(:salt) {Digest::SHA512.hexdigest("salt")}
12
12
  let(:password_text) {"secret"}
13
- let(:password_hash) {Digest::SHA1.hexdigest("#{salt}#{password_text}")}
13
+ let(:password_hash) {Digest::SHA512.hexdigest("#{salt}#{password_text}")}
14
14
 
15
15
  describe ".#included" do
16
16
  it "define singleton methods" do
@@ -69,9 +69,19 @@ describe Passwd do
69
69
  end
70
70
 
71
71
  describe "#hashing" do
72
+ it "should call SHA512.#hexdigest" do
73
+ Digest::SHA512.should_receive(:hexdigest)
74
+ Passwd.hashing("secret")
75
+ end
76
+
72
77
  it "return hashed password" do
73
- Digest::SHA1.should_receive(:hexdigest).with("secret").and_return("hash")
74
- expect(Passwd.hashing("secret")).to eq("hash")
78
+ hashed = Digest::SHA512.hexdigest "secret"
79
+ expect(Passwd.hashing("secret")).to eq(hashed)
80
+ end
81
+
82
+ it "return hashed password specified algorithm" do
83
+ hashed = Digest::SHA256.hexdigest "secret"
84
+ expect(Passwd.hashing("secret", :sha256)).to eq(hashed)
75
85
  end
76
86
 
77
87
  it "should create exception if not specified argument" do
@@ -6,6 +6,10 @@ describe Passwd::Config do
6
6
  let(:config) {Passwd::Config.instance}
7
7
 
8
8
  describe "defined accessors" do
9
+ it "defined algorithm" do
10
+ expect(config.respond_to? :algorithm).to be_true
11
+ end
12
+
9
13
  it "defined length" do
10
14
  expect(config.respond_to? :length).to be_true
11
15
  end
@@ -36,6 +40,10 @@ describe Passwd::Config do
36
40
  end
37
41
 
38
42
  describe "#initialize" do
43
+ it "algorithm should be a default" do
44
+ expect(config.algorithm).to eq(:sha512)
45
+ end
46
+
39
47
  it "length should be a default" do
40
48
  expect(config.length).to eq(8)
41
49
  end
@@ -10,6 +10,10 @@ describe Passwd::TmpConfig do
10
10
  end
11
11
 
12
12
  describe "defined accessors" do
13
+ it "defined algorithm" do
14
+ expect(config.respond_to? :algorithm).to be_true
15
+ end
16
+
13
17
  it "defined length" do
14
18
  expect(tmp_config.respond_to? :length).to be_true
15
19
  end
@@ -41,6 +45,10 @@ describe Passwd::TmpConfig do
41
45
 
42
46
  describe "#initialize" do
43
47
  context "with empty options" do
48
+ it "algorithm should be a default" do
49
+ expect(config.algorithm).to eq(:sha512)
50
+ end
51
+
44
52
  it "length should be a default" do
45
53
  expect(tmp_config.length).to eq(8)
46
54
  end
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.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - i2bskn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-03 00:00:00.000000000 Z
11
+ date: 2013-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -62,6 +62,7 @@ files:
62
62
  - .coveralls.yml
63
63
  - .gitignore
64
64
  - .travis.yml
65
+ - CHANGELOG.md
65
66
  - Gemfile
66
67
  - LICENSE.txt
67
68
  - README.md
@@ -77,7 +78,6 @@ files:
77
78
  - lib/passwd/password.rb
78
79
  - lib/passwd/version.rb
79
80
  - passwd.gemspec
80
- - samples/activerecord/user.rake
81
81
  - spec/passwd/active_record_spec.rb
82
82
  - spec/passwd/base_spec.rb
83
83
  - spec/passwd/configuration/config_spec.rb
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  requirements: []
107
107
  rubyforge_project:
108
- rubygems_version: 2.0.0
108
+ rubygems_version: 2.0.3
109
109
  signing_key:
110
110
  specification_version: 4
111
111
  summary: Password utility
@@ -1,28 +0,0 @@
1
- namespace :user do
2
- # E.G. rake name="Taro Suzuki" email=taro@example.com password=secret user:create
3
- desc "create user (specify env of name and email and password)"
4
- task :create => :environment do
5
- raise ArgumentError, "email is required" if ENV["email"].nil?
6
- raise ArgumentError, "name is required" if ENV["name"].nil?
7
-
8
- raise "must be a unique email" if User.exists?(email: ENV["email"])
9
-
10
- user = User.new(name: ENV["name"], email: ENV["email"])
11
-
12
- password = user.set_password(ENV["password"])
13
- user.save!
14
- puts "Creating a user was successful.\nPassword: #{password}"
15
- end
16
-
17
- # E.G. rake email=taro@example.com password=secret user:password_update
18
- desc "update password (specify env of email and password)"
19
- task :password_update => :environment do
20
- raise ArgumentError, "email is required" if ENV["email"].nil?
21
- raise "must specify the email that exists" unless User.exists?(email: ENV["email"])
22
-
23
- user = User.find_by(email: ENV["email"])
24
- password = user.set_password(ENV["password"])
25
- user.save!
26
- puts "Password update was successful.\nPassword: #{password}"
27
- end
28
- end