passwd 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -5
- data/README.md +1 -1
- data/lib/passwd.rb +1 -39
- data/lib/passwd/active_record.rb +3 -3
- data/lib/passwd/base.rb +47 -0
- data/lib/passwd/{configuration.rb → configuration/abstract_config.rb} +1 -15
- data/lib/passwd/configuration/config.rb +25 -0
- data/lib/passwd/configuration/tmp_config.rb +18 -0
- data/lib/passwd/password.rb +2 -2
- data/lib/passwd/version.rb +3 -1
- data/spec/passwd/base_spec.rb +151 -0
- data/spec/passwd/{configuration_spec.rb → configuration/config_spec.rb} +2 -2
- data/spec/passwd/configuration/tmp_config_spec.rb +257 -0
- data/spec/spec_helper.rb +3 -0
- metadata +12 -7
- data/spec/passwd_spec.rb +0 -137
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a92e0fc9b057332f70b4adde83c0d52d39c837ad
|
4
|
+
data.tar.gz: fd0514c01725a3312fcc8b64591aa881a2584371
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 040c613b3fe930cd550cacc68a47977ab7e6a56a02870135e897a4dacc76699aadf1ae8479b2efac63401c742e56719b560b0f2d613b6b3ce39aee1484e29a61
|
7
|
+
data.tar.gz: 3e475498013103b4d9bf6f5a0c35b9395ed92eb7d32df93ebbe151c4f605220dd336fc2098eb1574114723217d55444cc5a2756dedd71d3e1eeffe7ad1cb5da1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -49,7 +49,7 @@ Default config is stored in the class instance variable.
|
|
49
49
|
Changing the default configs are as follows:
|
50
50
|
|
51
51
|
```ruby
|
52
|
-
Passwd.config => Get config
|
52
|
+
Passwd.config => Get config object.
|
53
53
|
Passwd.config(length: 10) => Change to the default length.
|
54
54
|
|
55
55
|
Passwd.configure do |c|
|
data/lib/passwd.rb
CHANGED
@@ -3,44 +3,6 @@
|
|
3
3
|
require "digest/sha1"
|
4
4
|
|
5
5
|
require "passwd/version"
|
6
|
+
require "passwd/base"
|
6
7
|
require "passwd/password"
|
7
8
|
require "passwd/active_record"
|
8
|
-
require "passwd/configuration"
|
9
|
-
|
10
|
-
module Passwd
|
11
|
-
@config = Configuration.new
|
12
|
-
|
13
|
-
class << self
|
14
|
-
def create(options={})
|
15
|
-
config = @config.dup
|
16
|
-
config.merge options
|
17
|
-
Array.new(config.length){config.letters[rand(config.letters.size)]}.join
|
18
|
-
end
|
19
|
-
|
20
|
-
def auth(password_text, salt_hash, password_hash)
|
21
|
-
enc_pass = Passwd.hashing("#{salt_hash}#{password_text}")
|
22
|
-
password_hash == enc_pass
|
23
|
-
end
|
24
|
-
|
25
|
-
def hashing(plain)
|
26
|
-
Digest::SHA1.hexdigest plain
|
27
|
-
end
|
28
|
-
|
29
|
-
def configure(options={}, &block)
|
30
|
-
if block_given?
|
31
|
-
@config.configure &block
|
32
|
-
else
|
33
|
-
if options.empty?
|
34
|
-
@config
|
35
|
-
else
|
36
|
-
@config.merge options
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
alias :config :configure
|
41
|
-
|
42
|
-
def reset_config
|
43
|
-
@config.reset
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
data/lib/passwd/active_record.rb
CHANGED
@@ -4,9 +4,9 @@ module Passwd
|
|
4
4
|
module ActiveRecord
|
5
5
|
module ClassMethods
|
6
6
|
def define_column(options={})
|
7
|
-
id_name = options
|
8
|
-
salt_name = options
|
9
|
-
password_name = options
|
7
|
+
id_name = options.fetch(:id, :email)
|
8
|
+
salt_name = options.fetch(:salt, :salt)
|
9
|
+
password_name = options.fetch(:password, :password)
|
10
10
|
|
11
11
|
define_singleton_auth(id_name, salt_name, password_name)
|
12
12
|
define_instance_auth(id_name, salt_name, password_name)
|
data/lib/passwd/base.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "passwd/configuration/config"
|
4
|
+
require "passwd/configuration/tmp_config"
|
5
|
+
|
6
|
+
module Passwd
|
7
|
+
@config = Config.instance
|
8
|
+
|
9
|
+
module Base
|
10
|
+
def create(options={})
|
11
|
+
if options.empty?
|
12
|
+
config = @config
|
13
|
+
else
|
14
|
+
config = TmpConfig.new(@config, options)
|
15
|
+
end
|
16
|
+
Array.new(config.length){config.letters[rand(config.letters.size)]}.join
|
17
|
+
end
|
18
|
+
|
19
|
+
def auth(password_text, salt_hash, password_hash)
|
20
|
+
enc_pass = Passwd.hashing("#{salt_hash}#{password_text}")
|
21
|
+
password_hash == enc_pass
|
22
|
+
end
|
23
|
+
|
24
|
+
def hashing(plain)
|
25
|
+
Digest::SHA1.hexdigest plain
|
26
|
+
end
|
27
|
+
|
28
|
+
def configure(options={}, &block)
|
29
|
+
if block_given?
|
30
|
+
@config.configure &block
|
31
|
+
else
|
32
|
+
if options.empty?
|
33
|
+
@config
|
34
|
+
else
|
35
|
+
@config.merge options
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
alias :config :configure
|
40
|
+
|
41
|
+
def reset_config
|
42
|
+
@config.reset
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
extend Base
|
47
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
3
|
module Passwd
|
4
|
-
class
|
4
|
+
class AbstractConfig
|
5
5
|
VALID_OPTIONS_KEYS = [
|
6
6
|
:length,
|
7
7
|
:lower,
|
@@ -14,10 +14,6 @@ module Passwd
|
|
14
14
|
|
15
15
|
attr_accessor *VALID_OPTIONS_KEYS
|
16
16
|
|
17
|
-
def initialize
|
18
|
-
reset
|
19
|
-
end
|
20
|
-
|
21
17
|
def configure
|
22
18
|
yield self
|
23
19
|
end
|
@@ -36,15 +32,5 @@ module Passwd
|
|
36
32
|
raise "letters is empty" if chars.empty?
|
37
33
|
chars
|
38
34
|
end
|
39
|
-
|
40
|
-
def reset
|
41
|
-
self.length = 8
|
42
|
-
self.lower = true
|
43
|
-
self.upper = true
|
44
|
-
self.number = true
|
45
|
-
self.letters_lower = ("a".."z").to_a
|
46
|
-
self.letters_upper = ("A".."Z").to_a
|
47
|
-
self.letters_number = ("0".."9").to_a
|
48
|
-
end
|
49
35
|
end
|
50
36
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "singleton"
|
4
|
+
|
5
|
+
require "passwd/configuration/abstract_config"
|
6
|
+
|
7
|
+
module Passwd
|
8
|
+
class Config < AbstractConfig
|
9
|
+
include Singleton
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
reset
|
13
|
+
end
|
14
|
+
|
15
|
+
def reset
|
16
|
+
self.length = 8
|
17
|
+
self.lower = true
|
18
|
+
self.upper = true
|
19
|
+
self.number = true
|
20
|
+
self.letters_lower = ("a".."z").to_a
|
21
|
+
self.letters_upper = ("A".."Z").to_a
|
22
|
+
self.letters_number = ("0".."9").to_a
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "passwd/configuration/abstract_config"
|
4
|
+
|
5
|
+
module Passwd
|
6
|
+
class TmpConfig < AbstractConfig
|
7
|
+
def initialize(config, options)
|
8
|
+
config.instance_variables.each do |v|
|
9
|
+
key = v.to_s.sub(/^@/, "").to_sym
|
10
|
+
if options.has_key? key
|
11
|
+
instance_variable_set v, options[key]
|
12
|
+
else
|
13
|
+
instance_variable_set v, config.instance_variable_get(v)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/passwd/password.rb
CHANGED
@@ -5,8 +5,8 @@ module Passwd
|
|
5
5
|
attr_reader :text, :hash, :salt_text, :salt_hash
|
6
6
|
|
7
7
|
def initialize(options={})
|
8
|
-
@text = options
|
9
|
-
@salt_text = options
|
8
|
+
@text = options.fetch(:password, Passwd.create)
|
9
|
+
@salt_text = options.fetch(:salt_text, Time.now.to_s)
|
10
10
|
@salt_hash = Passwd.hashing(@salt_text)
|
11
11
|
@hash = Passwd.hashing("#{@salt_hash}#{@text}")
|
12
12
|
end
|
data/lib/passwd/version.rb
CHANGED
@@ -0,0 +1,151 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Passwd do
|
6
|
+
describe "extended Base" do
|
7
|
+
describe "#create" do
|
8
|
+
context "without arguments" do
|
9
|
+
let(:password) {Passwd.create}
|
10
|
+
|
11
|
+
it "TmpConfig should not be generated" do
|
12
|
+
Passwd::TmpConfig.should_not_receive(:new)
|
13
|
+
expect{password}.not_to raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it "created password should be String object" do
|
17
|
+
expect(password.is_a? String).to be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "created password length should be default length" do
|
21
|
+
expect(password.size).to eq(8)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "with arguments" do
|
26
|
+
it "TmpConfig should be generated" do
|
27
|
+
tmp_config = double("tmp_config mock", length: 8, letters: ["a", "b"])
|
28
|
+
Passwd::TmpConfig.should_receive(:new).and_return(tmp_config)
|
29
|
+
expect{Passwd.create(length: 10)}.not_to raise_error
|
30
|
+
end
|
31
|
+
|
32
|
+
it "password was created specified characters" do
|
33
|
+
expect(Passwd.create(length: 10).size).to eq(10)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "password create without lower case" do
|
37
|
+
expect(("a".."z").to_a.include? Passwd.create(lower: false)).to be_false
|
38
|
+
end
|
39
|
+
|
40
|
+
it "password create without upper case" do
|
41
|
+
expect(("A".."Z").to_a.include? Passwd.create(upper: false)).to be_false
|
42
|
+
end
|
43
|
+
|
44
|
+
it "password create without number" do
|
45
|
+
expect(("0".."9").to_a.include? Passwd.create(number: false)).to be_false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#auth" do
|
51
|
+
let!(:password) do
|
52
|
+
password = Passwd.create
|
53
|
+
salt_hash = Passwd.hashing(Time.now.to_s)
|
54
|
+
password_hash = Passwd.hashing("#{salt_hash}#{password}")
|
55
|
+
{text: password, salt: salt_hash, hash: password_hash}
|
56
|
+
end
|
57
|
+
|
58
|
+
it "return true with valid password" do
|
59
|
+
expect(Passwd.auth(password[:text], password[:salt], password[:hash])).to be_true
|
60
|
+
end
|
61
|
+
|
62
|
+
it "return false with invalid password" do
|
63
|
+
expect(Passwd.auth("invalid", password[:salt], password[:hash])).to be_false
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should create exception if not specified arguments" do
|
67
|
+
expect(proc{Passwd.auth}).to raise_error
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "#hashing" do
|
72
|
+
it "return hashed password" do
|
73
|
+
Digest::SHA1.should_receive(:hexdigest).with("secret").and_return("hash")
|
74
|
+
expect(Passwd.hashing("secret")).to eq("hash")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should create exception if not specified argument" do
|
78
|
+
expect(proc{Passwd.hashing}).to raise_error
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "#configure" do
|
83
|
+
it "return configuration object" do
|
84
|
+
expect(Passwd.configure.is_a? Passwd::Config).to be_true
|
85
|
+
end
|
86
|
+
|
87
|
+
it "set config value from block" do
|
88
|
+
Passwd.configure do |c|
|
89
|
+
c.length = 10
|
90
|
+
end
|
91
|
+
expect(Passwd.configure.length).not_to eq(8)
|
92
|
+
expect(Passwd.configure.length).to eq(10)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "set config value from hash" do
|
96
|
+
Passwd.configure length: 20
|
97
|
+
expect(Passwd.config.length).not_to eq(8)
|
98
|
+
expect(Passwd.config.length).to eq(20)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "alias of configure as config" do
|
102
|
+
expect(Passwd.configure.object_id).to eq(Passwd.config.object_id)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#reset_config" do
|
107
|
+
let(:config) {Passwd::Config.instance}
|
108
|
+
|
109
|
+
before {
|
110
|
+
config.configure do |c|
|
111
|
+
c.length = 20
|
112
|
+
c.lower = false
|
113
|
+
c.upper = false
|
114
|
+
c.number = false
|
115
|
+
c.letters_lower = ["a"]
|
116
|
+
c.letters_upper = ["A"]
|
117
|
+
c.letters_number = ["0"]
|
118
|
+
end
|
119
|
+
Passwd.reset_config
|
120
|
+
}
|
121
|
+
|
122
|
+
it "length should be a default" do
|
123
|
+
expect(config.length).to eq(8)
|
124
|
+
end
|
125
|
+
|
126
|
+
it "lower should be a default" do
|
127
|
+
expect(config.lower).to be_true
|
128
|
+
end
|
129
|
+
|
130
|
+
it "upper should be a default" do
|
131
|
+
expect(config.upper).to be_true
|
132
|
+
end
|
133
|
+
|
134
|
+
it "number should be a default" do
|
135
|
+
expect(config.number).to be_true
|
136
|
+
end
|
137
|
+
|
138
|
+
it "letters_lower should be a default" do
|
139
|
+
expect(config.letters_lower).to eq(("a".."z").to_a)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "letters_upper should be a default" do
|
143
|
+
expect(config.letters_upper).to eq(("A".."Z").to_a)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "letters_number should be a default" do
|
147
|
+
expect(config.letters_number).to eq(("0".."9").to_a)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,257 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Passwd::TmpConfig do
|
6
|
+
let(:config) {Passwd::Config.instance}
|
7
|
+
|
8
|
+
def tmp_config(options={})
|
9
|
+
Passwd::TmpConfig.new(Passwd::Config.instance, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "defined accessors" do
|
13
|
+
it "defined length" do
|
14
|
+
expect(tmp_config.respond_to? :length).to be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "defined lower" do
|
18
|
+
expect(tmp_config.respond_to? :lower).to be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "defined upper" do
|
22
|
+
expect(tmp_config.respond_to? :upper).to be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "defined number" do
|
26
|
+
expect(tmp_config.respond_to? :number).to be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "defined letters_lower" do
|
30
|
+
expect(tmp_config.respond_to? :letters_lower).to be_true
|
31
|
+
end
|
32
|
+
|
33
|
+
it "defined letters_upper" do
|
34
|
+
expect(tmp_config.respond_to? :letters_upper).to be_true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "defined letters_number" do
|
38
|
+
expect(tmp_config.respond_to? :letters_number).to be_true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#initialize" do
|
43
|
+
context "with empty options" do
|
44
|
+
it "length should be a default" do
|
45
|
+
expect(tmp_config.length).to eq(8)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "lower should be a default" do
|
49
|
+
expect(tmp_config.lower).to be_true
|
50
|
+
end
|
51
|
+
|
52
|
+
it "upper should be a default" do
|
53
|
+
expect(tmp_config.upper).to be_true
|
54
|
+
end
|
55
|
+
|
56
|
+
it "number should be a default" do
|
57
|
+
expect(tmp_config.number).to be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
it "letters_lower should be a default" do
|
61
|
+
expect(tmp_config.letters_lower).to eq(("a".."z").to_a)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "letters_upper should be a default" do
|
65
|
+
expect(tmp_config.letters_upper).to eq(("A".."Z").to_a)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "letters_number should be a default" do
|
69
|
+
expect(tmp_config.letters_number).to eq(("0".."9").to_a)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "with options" do
|
74
|
+
it "length should be a specified params" do
|
75
|
+
expect(tmp_config(length: 10).length).to eq(10)
|
76
|
+
expect(config.length).to eq(8)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "lower should be a specified params" do
|
80
|
+
expect(tmp_config(lower: false).lower).to be_false
|
81
|
+
expect(config.lower).to be_true
|
82
|
+
end
|
83
|
+
|
84
|
+
it "upper should be a specified params" do
|
85
|
+
expect(tmp_config(upper: false).upper).to be_false
|
86
|
+
expect(config.upper).to be_true
|
87
|
+
end
|
88
|
+
|
89
|
+
it "number should be a specified params" do
|
90
|
+
expect(tmp_config(number: false).number).to be_false
|
91
|
+
expect(config.number).to be_true
|
92
|
+
end
|
93
|
+
|
94
|
+
it "letters_lower should be a specified params" do
|
95
|
+
expect(tmp_config(letters_lower: ["a"]).letters_lower).to eq(["a"])
|
96
|
+
expect(config.letters_lower).to eq(("a".."z").to_a)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "letters_upper should be a specified params" do
|
100
|
+
expect(tmp_config(letters_upper: ["A"]).letters_upper).to eq(["A"])
|
101
|
+
expect(config.letters_upper).to eq(("A".."Z").to_a)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "letters_number should be a specified params" do
|
105
|
+
expect(tmp_config(letters_number: ["0"]).letters_number).to eq(["0"])
|
106
|
+
expect(config.letters_number).to eq(("0".."9").to_a)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "#configure" do
|
112
|
+
let(:changed_tmp_config) do
|
113
|
+
tconf = tmp_config
|
114
|
+
tconf.configure do |c|
|
115
|
+
c.length = 20
|
116
|
+
c.lower = false
|
117
|
+
c.upper = false
|
118
|
+
c.number = false
|
119
|
+
c.letters_lower = ["a"]
|
120
|
+
c.letters_upper = ["A"]
|
121
|
+
c.letters_number = ["0"]
|
122
|
+
end
|
123
|
+
tconf
|
124
|
+
end
|
125
|
+
|
126
|
+
it "set length from block" do
|
127
|
+
expect(changed_tmp_config.length).to eq(20)
|
128
|
+
expect(config.length).to eq(8)
|
129
|
+
end
|
130
|
+
|
131
|
+
it "set lower from block" do
|
132
|
+
expect(changed_tmp_config.lower).to be_false
|
133
|
+
expect(config.lower).to be_true
|
134
|
+
end
|
135
|
+
|
136
|
+
it "set upper from block" do
|
137
|
+
expect(changed_tmp_config.upper).to be_false
|
138
|
+
expect(config.upper).to be_true
|
139
|
+
end
|
140
|
+
|
141
|
+
it "set number from block" do
|
142
|
+
expect(changed_tmp_config.number).to be_false
|
143
|
+
expect(config.number).to be_true
|
144
|
+
end
|
145
|
+
|
146
|
+
it "set letters_lower from block" do
|
147
|
+
expect(changed_tmp_config.letters_lower).to eq(["a"])
|
148
|
+
expect(config.letters_lower).to eq(("a".."z").to_a)
|
149
|
+
end
|
150
|
+
|
151
|
+
it "set letters_upper from block" do
|
152
|
+
expect(changed_tmp_config.letters_upper).to eq(["A"])
|
153
|
+
expect(config.letters_upper).to eq(("A".."Z").to_a)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "set letters_number from block" do
|
157
|
+
expect(changed_tmp_config.letters_number).to eq(["0"])
|
158
|
+
expect(config.letters_number).to eq(("0".."9").to_a)
|
159
|
+
end
|
160
|
+
|
161
|
+
it "raise error unknown setting" do
|
162
|
+
expect {
|
163
|
+
tmp_config.configure do |c|
|
164
|
+
c.unknown = true
|
165
|
+
end
|
166
|
+
}.to raise_error
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "#merge" do
|
171
|
+
let(:default_tmp_config) {tmp_config}
|
172
|
+
|
173
|
+
it "set length from hash" do
|
174
|
+
default_tmp_config.merge(length: 10)
|
175
|
+
expect(default_tmp_config.length).to eq(10)
|
176
|
+
expect(config.length).to eq(8)
|
177
|
+
end
|
178
|
+
|
179
|
+
it "set lower from hash" do
|
180
|
+
default_tmp_config.merge(lower: false)
|
181
|
+
expect(default_tmp_config.lower).to be_false
|
182
|
+
expect(config.lower).to be_true
|
183
|
+
end
|
184
|
+
|
185
|
+
it "set upper from hash" do
|
186
|
+
default_tmp_config.merge(upper: false)
|
187
|
+
expect(default_tmp_config.upper).to be_false
|
188
|
+
expect(config.upper).to be_true
|
189
|
+
end
|
190
|
+
|
191
|
+
it "set number from hash" do
|
192
|
+
default_tmp_config.merge(number: false)
|
193
|
+
expect(default_tmp_config.number).to be_false
|
194
|
+
expect(config.number).to be_true
|
195
|
+
end
|
196
|
+
|
197
|
+
it "set letters_lower from hash" do
|
198
|
+
default_tmp_config.merge(letters_lower: ["a"])
|
199
|
+
expect(default_tmp_config.letters_lower).to eq(["a"])
|
200
|
+
expect(config.letters_lower).to eq(("a".."z").to_a)
|
201
|
+
end
|
202
|
+
|
203
|
+
it "set letters_upper from hash" do
|
204
|
+
default_tmp_config.merge(letters_upper: ["A"])
|
205
|
+
expect(default_tmp_config.letters_upper).to eq(["A"])
|
206
|
+
expect(config.letters_upper).to eq(("A".."Z").to_a)
|
207
|
+
end
|
208
|
+
|
209
|
+
it "set letters_number from hash" do
|
210
|
+
default_tmp_config.merge(letters_number: ["0"])
|
211
|
+
expect(default_tmp_config.letters_number).to eq(["0"])
|
212
|
+
expect(config.letters_number).to eq(("0".."9").to_a)
|
213
|
+
end
|
214
|
+
|
215
|
+
it "raise error unknown setting" do
|
216
|
+
expect {
|
217
|
+
tmp_config.merge(unknown: true)
|
218
|
+
}.to raise_error
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
describe "#letters" do
|
223
|
+
it "return Array object" do
|
224
|
+
expect(tmp_config.letters.is_a? Array).to be_true
|
225
|
+
end
|
226
|
+
|
227
|
+
it "all elements of the string" do
|
228
|
+
tmp_config.letters.each do |l|
|
229
|
+
expect(l.is_a? String).to be_true
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
it "return all letters" do
|
234
|
+
all_letters = ("a".."z").to_a.concat(("A".."Z").to_a).concat(("0".."9").to_a)
|
235
|
+
expect(tmp_config.letters).to eq(all_letters)
|
236
|
+
end
|
237
|
+
|
238
|
+
it "return except for the lower case" do
|
239
|
+
expect(tmp_config(lower: false).letters.include? "a").to be_false
|
240
|
+
end
|
241
|
+
|
242
|
+
it "return except for the upper case" do
|
243
|
+
expect(tmp_config(upper: false).letters.include? "A").to be_false
|
244
|
+
end
|
245
|
+
|
246
|
+
it "return except for the number case" do
|
247
|
+
expect(tmp_config(number: false).letters.include? "0").to be_false
|
248
|
+
end
|
249
|
+
|
250
|
+
it "raise error if letters is empty" do
|
251
|
+
tconf = tmp_config(lower: false, upper: false, number: false)
|
252
|
+
expect {
|
253
|
+
tconf.letters
|
254
|
+
}.to raise_error
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,14 +68,18 @@ files:
|
|
68
68
|
- Rakefile
|
69
69
|
- lib/passwd.rb
|
70
70
|
- lib/passwd/active_record.rb
|
71
|
-
- lib/passwd/
|
71
|
+
- lib/passwd/base.rb
|
72
|
+
- lib/passwd/configuration/abstract_config.rb
|
73
|
+
- lib/passwd/configuration/config.rb
|
74
|
+
- lib/passwd/configuration/tmp_config.rb
|
72
75
|
- lib/passwd/password.rb
|
73
76
|
- lib/passwd/version.rb
|
74
77
|
- passwd.gemspec
|
75
78
|
- spec/passwd/active_record_spec.rb
|
76
|
-
- spec/passwd/
|
79
|
+
- spec/passwd/base_spec.rb
|
80
|
+
- spec/passwd/configuration/config_spec.rb
|
81
|
+
- spec/passwd/configuration/tmp_config_spec.rb
|
77
82
|
- spec/passwd/password_spec.rb
|
78
|
-
- spec/passwd_spec.rb
|
79
83
|
- spec/spec_helper.rb
|
80
84
|
homepage: https://github.com/i2bskn/passwd
|
81
85
|
licenses:
|
@@ -103,7 +107,8 @@ specification_version: 4
|
|
103
107
|
summary: Password utility
|
104
108
|
test_files:
|
105
109
|
- spec/passwd/active_record_spec.rb
|
106
|
-
- spec/passwd/
|
110
|
+
- spec/passwd/base_spec.rb
|
111
|
+
- spec/passwd/configuration/config_spec.rb
|
112
|
+
- spec/passwd/configuration/tmp_config_spec.rb
|
107
113
|
- spec/passwd/password_spec.rb
|
108
|
-
- spec/passwd_spec.rb
|
109
114
|
- spec/spec_helper.rb
|
data/spec/passwd_spec.rb
DELETED
@@ -1,137 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe Passwd do
|
6
|
-
let(:default) {Passwd::Configuration.new}
|
7
|
-
|
8
|
-
describe ".create" do
|
9
|
-
it "create random password" do
|
10
|
-
password = Passwd.create
|
11
|
-
expect(password.is_a? String).to be_true
|
12
|
-
expect(password.size).to eq(default.length)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "password was created specified characters" do
|
16
|
-
expect(Passwd.create(length: 10).size).to eq(10)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "password create without lower case" do
|
20
|
-
password = Passwd.create lower: false
|
21
|
-
expect(default.letters_lower.include? password).to be_false
|
22
|
-
end
|
23
|
-
|
24
|
-
it "password create without upper case" do
|
25
|
-
password = Passwd.create upper: false
|
26
|
-
expect(default.letters_upper.include? password).to be_false
|
27
|
-
end
|
28
|
-
|
29
|
-
it "password create without number" do
|
30
|
-
password = Passwd.create(number: false)
|
31
|
-
expect(default.letters_number.include? password).to be_false
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe ".auth" do
|
36
|
-
let!(:password) do
|
37
|
-
password = Passwd.create
|
38
|
-
salt_hash = Passwd.hashing(Time.now.to_s)
|
39
|
-
password_hash = Passwd.hashing("#{salt_hash}#{password}")
|
40
|
-
{text: password, salt: salt_hash, hash: password_hash}
|
41
|
-
end
|
42
|
-
|
43
|
-
it "return true with valid password" do
|
44
|
-
expect(Passwd.auth(password[:text], password[:salt], password[:hash])).to be_true
|
45
|
-
end
|
46
|
-
|
47
|
-
it "return false with invalid password" do
|
48
|
-
expect(Passwd.auth("invalid", password[:salt], password[:hash])).to be_false
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should create exception if not specified arguments" do
|
52
|
-
expect(proc{Passwd.auth}).to raise_error
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe ".hashing" do
|
57
|
-
it "return hashed password" do
|
58
|
-
Digest::SHA1.should_receive(:hexdigest).with("secret").and_return("hash")
|
59
|
-
expect(Passwd.hashing("secret")).to eq("hash")
|
60
|
-
end
|
61
|
-
|
62
|
-
it "should create exception if not specified argument" do
|
63
|
-
expect(proc{Passwd.hashing}).to raise_error
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
describe ".configure" do
|
68
|
-
after {Passwd.reset_config}
|
69
|
-
|
70
|
-
it "return configuration object" do
|
71
|
-
expect(Passwd.configure.is_a? Passwd::Configuration).to be_true
|
72
|
-
end
|
73
|
-
|
74
|
-
it "set config value from block" do
|
75
|
-
Passwd.configure do |c|
|
76
|
-
c.length = 10
|
77
|
-
end
|
78
|
-
expect(Passwd.configure.length).not_to eq(default.length)
|
79
|
-
expect(Passwd.configure.length).to eq(10)
|
80
|
-
end
|
81
|
-
|
82
|
-
it "set config value from hash" do
|
83
|
-
Passwd.configure length: 20
|
84
|
-
expect(Passwd.config.length).not_to eq(default.length)
|
85
|
-
expect(Passwd.config.length).to eq(20)
|
86
|
-
end
|
87
|
-
|
88
|
-
it "alias of configure as config" do
|
89
|
-
expect(Passwd.configure.object_id).to eq(Passwd.config.object_id)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
describe ".reset_config" do
|
94
|
-
let(:config) {Passwd.config}
|
95
|
-
|
96
|
-
before {
|
97
|
-
config.configure do |c|
|
98
|
-
c.length = 20
|
99
|
-
c.lower = false
|
100
|
-
c.upper = false
|
101
|
-
c.number = false
|
102
|
-
c.letters_lower = ["a"]
|
103
|
-
c.letters_upper = ["A"]
|
104
|
-
c.letters_number = ["0"]
|
105
|
-
end
|
106
|
-
config.reset
|
107
|
-
}
|
108
|
-
|
109
|
-
it "length should be a default" do
|
110
|
-
expect(config.length).to eq(8)
|
111
|
-
end
|
112
|
-
|
113
|
-
it "lower should be a default" do
|
114
|
-
expect(config.lower).to be_true
|
115
|
-
end
|
116
|
-
|
117
|
-
it "upper should be a default" do
|
118
|
-
expect(config.upper).to be_true
|
119
|
-
end
|
120
|
-
|
121
|
-
it "number should be a default" do
|
122
|
-
expect(config.number).to be_true
|
123
|
-
end
|
124
|
-
|
125
|
-
it "letters_lower should be a default" do
|
126
|
-
expect(config.letters_lower).to eq(("a".."z").to_a)
|
127
|
-
end
|
128
|
-
|
129
|
-
it "letters_upper should be a default" do
|
130
|
-
expect(config.letters_upper).to eq(("A".."Z").to_a)
|
131
|
-
end
|
132
|
-
|
133
|
-
it "letters_number should be a default" do
|
134
|
-
expect(config.letters_number).to eq(("0".."9").to_a)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|