passwd 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +48 -10
- data/lib/passwd/base.rb +19 -0
- data/lib/passwd/configuration/config.rb +0 -2
- data/lib/passwd/configuration/policy.rb +46 -0
- data/lib/passwd/version.rb +1 -1
- data/spec/passwd/base_spec.rb +51 -0
- data/spec/passwd/configuration/policy_spec.rb +133 -0
- data/spec/spec_helper.rb +1 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f91e14eb692efe0d90d138bcc541788aec69c12
|
4
|
+
data.tar.gz: 67a298fe7246b5470272e55b11427262f44a245d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24d8d2da30dcd3cb480d114e0f637f594319095d21c7383071bb745217c374d14f0a7625f95c829d0389a1f62f20206f0028914a6f96d39e153636e24825cf9d
|
7
|
+
data.tar.gz: d11e78fc3350d5003e0fe96cde72fe13694ad4149997f41e6c73b0bd4105a9393114df219838e6977c32b91cfe98d6d7eb3ecff7fc70b30576146744b005da4d
|
data/README.md
CHANGED
@@ -49,8 +49,8 @@ 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 object.
|
53
|
-
Passwd.config(length: 10) => Change to the default length.
|
52
|
+
Passwd.config # => Get config object.
|
53
|
+
Passwd.config(length: 10) # => Change to the default length.
|
54
54
|
|
55
55
|
Passwd.configure do |c|
|
56
56
|
c.length = 10
|
@@ -67,6 +67,29 @@ Options that can be specified:
|
|
67
67
|
* :letters_upper => Define an array of upper case. default is ("A".."Z").to_a
|
68
68
|
* :letters_number => Define an array of numbers. default is ("0".."9").to_a
|
69
69
|
|
70
|
+
### Policy check
|
71
|
+
|
72
|
+
Default policy is 8 more characters and require lower case and require number.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
Passwd.policy_check("secret") # => true or false
|
76
|
+
```
|
77
|
+
|
78
|
+
### Policy settings
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
Passwd.policy_configure do |c|
|
82
|
+
c.min_length = 10
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
Options that can be specified:
|
87
|
+
|
88
|
+
* :min_length => Number of minimum characters. default is 8.
|
89
|
+
* :require_lower => Require lower case if set true. default is true.
|
90
|
+
* :require_upper => Require upper case if set true. default is false.
|
91
|
+
* :require_number => Require number if set true. default is true.
|
92
|
+
|
70
93
|
### Password object
|
71
94
|
|
72
95
|
Default password is randomly generated.
|
@@ -96,9 +119,12 @@ password == password.text # => true
|
|
96
119
|
password == "invalid!!" # => false
|
97
120
|
```
|
98
121
|
|
99
|
-
## For ActiveRecord
|
122
|
+
## For ActiveRecord
|
123
|
+
|
124
|
+
### User model
|
100
125
|
|
101
|
-
|
126
|
+
Include `Passwd::ActiveRecord` module and define id/salt/password column from `define_column` method.
|
127
|
+
`id` column is required uniqueness.
|
102
128
|
|
103
129
|
```ruby
|
104
130
|
class User < ActiveRecord::Base
|
@@ -110,7 +136,13 @@ class User < ActiveRecord::Base
|
|
110
136
|
end
|
111
137
|
```
|
112
138
|
|
113
|
-
|
139
|
+
Available following method by defining id/salt/password column.
|
140
|
+
|
141
|
+
### Authentication
|
142
|
+
|
143
|
+
`authenticate` method is available in both instance and class.
|
144
|
+
Return the user object if the authentication successful.
|
145
|
+
Return the nil if authentication fails or doesn't exists user.
|
114
146
|
|
115
147
|
```ruby
|
116
148
|
user = User.authenticate("foo@example.com", "secret") # => return user object or nil.
|
@@ -122,6 +154,8 @@ else
|
|
122
154
|
end
|
123
155
|
```
|
124
156
|
|
157
|
+
instance method is not required `id`.
|
158
|
+
|
125
159
|
```ruby
|
126
160
|
user = User.find(params[:id])
|
127
161
|
if user.authenticate("secret") # => return true or false
|
@@ -131,20 +165,24 @@ else
|
|
131
165
|
end
|
132
166
|
```
|
133
167
|
|
134
|
-
Change passowrd
|
168
|
+
### Change passowrd
|
169
|
+
|
170
|
+
`set_password` method will be set random password.
|
171
|
+
Return value is plain text password.
|
172
|
+
To specify the password as an argument if you want to specify a password.
|
173
|
+
`salt` also set if salt is nil.
|
135
174
|
|
136
175
|
```ruby
|
137
176
|
user = User.find(params[:id])
|
138
|
-
# set random password. (salt also set if salt is nil)
|
139
|
-
# return set password text.
|
140
|
-
# set specified password if specified argument.
|
141
|
-
# user.set_password("secret")
|
142
177
|
password_text = user.set_password
|
143
178
|
|
144
179
|
if user.save
|
145
180
|
NoticeMailer.change_mail(user, password_text).deliver
|
146
181
|
end
|
147
182
|
```
|
183
|
+
`update_password` method will be set new password if the authentication successful.
|
184
|
+
Return the nil if authentication fails.
|
185
|
+
But `update_password` method doesn't call `save` method.
|
148
186
|
|
149
187
|
```ruby
|
150
188
|
user.find(params[:id])
|
data/lib/passwd/base.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
+
require "singleton"
|
3
4
|
require "passwd/configuration/config"
|
4
5
|
require "passwd/configuration/tmp_config"
|
6
|
+
require "passwd/configuration/policy"
|
5
7
|
|
6
8
|
module Passwd
|
7
9
|
@config = Config.instance
|
10
|
+
@policy = Policy.instance
|
8
11
|
|
9
12
|
module Base
|
10
13
|
def create(options={})
|
@@ -38,9 +41,25 @@ module Passwd
|
|
38
41
|
end
|
39
42
|
alias :config :configure
|
40
43
|
|
44
|
+
def policy_configure(&block)
|
45
|
+
if block_given?
|
46
|
+
@policy.configure &block
|
47
|
+
else
|
48
|
+
@policy
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def policy_check(password)
|
53
|
+
@policy.valid?(password, @config)
|
54
|
+
end
|
55
|
+
|
41
56
|
def reset_config
|
42
57
|
@config.reset
|
43
58
|
end
|
59
|
+
|
60
|
+
def reset_policy
|
61
|
+
@policy.reset
|
62
|
+
end
|
44
63
|
end
|
45
64
|
|
46
65
|
extend Base
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module Passwd
|
4
|
+
class Policy
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
VALID_OPTIONS_KEYS = [
|
8
|
+
:min_length,
|
9
|
+
:require_lower,
|
10
|
+
:require_upper,
|
11
|
+
:require_number
|
12
|
+
].freeze
|
13
|
+
|
14
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
reset
|
18
|
+
end
|
19
|
+
|
20
|
+
def configure
|
21
|
+
yield self
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid?(password, config)
|
25
|
+
return false if self.min_length > password.size
|
26
|
+
return false if self.require_lower && !include_char?(config.letters_lower, password)
|
27
|
+
return false if self.require_upper && !include_char?(config.letters_upper, password)
|
28
|
+
return false if self.require_number && !include_char?(config.letters_number, password)
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
def include_char?(letters, strings)
|
33
|
+
strings.each_char do |c|
|
34
|
+
return true if letters.include? c
|
35
|
+
end
|
36
|
+
false
|
37
|
+
end
|
38
|
+
|
39
|
+
def reset
|
40
|
+
self.min_length = 8
|
41
|
+
self.require_lower = true
|
42
|
+
self.require_upper = false
|
43
|
+
self.require_number = true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/passwd/version.rb
CHANGED
data/spec/passwd/base_spec.rb
CHANGED
@@ -103,6 +103,57 @@ describe Passwd do
|
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
|
+
describe "#policy_configure" do
|
107
|
+
it "return policy object" do
|
108
|
+
expect(Passwd.policy_configure.is_a? Passwd::Policy).to be_true
|
109
|
+
end
|
110
|
+
|
111
|
+
it "set policy value from block" do
|
112
|
+
Passwd.policy_configure do |c|
|
113
|
+
c.min_length = 10
|
114
|
+
end
|
115
|
+
expect(Passwd.policy_configure.min_length).not_to eq(8)
|
116
|
+
expect(Passwd.policy_configure.min_length).to eq(10)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe "#policy_check" do
|
121
|
+
it "Policy#valid? should be called" do
|
122
|
+
Passwd::Policy.instance.should_receive(:valid?).with("secret1234" ,Passwd::Config.instance)
|
123
|
+
expect(Passwd.policy_check("secret1234")).not_to raise_error
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "#reset_policy" do
|
128
|
+
let(:policy) {Passwd::Policy.instance}
|
129
|
+
|
130
|
+
before {
|
131
|
+
policy.configure do |c|
|
132
|
+
c.min_length = 20
|
133
|
+
c.require_lower = false
|
134
|
+
c.require_upper = true
|
135
|
+
c.require_number = false
|
136
|
+
end
|
137
|
+
Passwd.reset_policy
|
138
|
+
}
|
139
|
+
|
140
|
+
it "min_length should be a default" do
|
141
|
+
expect(policy.min_length).to eq(8)
|
142
|
+
end
|
143
|
+
|
144
|
+
it "require_lower should be a default" do
|
145
|
+
expect(policy.require_lower).to be_true
|
146
|
+
end
|
147
|
+
|
148
|
+
it "upper should be a default" do
|
149
|
+
expect(policy.require_upper).to be_false
|
150
|
+
end
|
151
|
+
|
152
|
+
it "number should be a default" do
|
153
|
+
expect(policy.require_number).to be_true
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
106
157
|
describe "#reset_config" do
|
107
158
|
let(:config) {Passwd::Config.instance}
|
108
159
|
|
@@ -0,0 +1,133 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Passwd::Policy do
|
6
|
+
let(:policy) {Passwd::Policy.instance}
|
7
|
+
|
8
|
+
describe "defined accessors" do
|
9
|
+
it "defined min_length" do
|
10
|
+
expect(policy.respond_to? :min_length).to be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "defined require_lower" do
|
14
|
+
expect(policy.respond_to? :require_lower).to be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "defined require_upper" do
|
18
|
+
expect(policy.respond_to? :require_upper).to be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "defined require_number" do
|
22
|
+
expect(policy.respond_to? :require_number).to be_true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#initialize" do
|
27
|
+
it "min_length should be a default" do
|
28
|
+
expect(policy.min_length).to eq(8)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "require_lower should be a default" do
|
32
|
+
expect(policy.require_lower).to be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "require_upper should be a default" do
|
36
|
+
expect(policy.require_upper).to be_false
|
37
|
+
end
|
38
|
+
|
39
|
+
it "require_number should be a default" do
|
40
|
+
expect(policy.require_number).to be_true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#configure" do
|
45
|
+
before {
|
46
|
+
policy.configure do |c|
|
47
|
+
c.min_length = 20
|
48
|
+
c.require_lower = false
|
49
|
+
c.require_upper = true
|
50
|
+
c.require_number = false
|
51
|
+
end
|
52
|
+
}
|
53
|
+
|
54
|
+
it "set min_length from block" do
|
55
|
+
expect(policy.min_length).to eq(20)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "set require_lower from block" do
|
59
|
+
expect(policy.require_lower).to be_false
|
60
|
+
end
|
61
|
+
|
62
|
+
it "set require_upper from block" do
|
63
|
+
expect(policy.require_upper).to be_true
|
64
|
+
end
|
65
|
+
|
66
|
+
it "set require_number from block" do
|
67
|
+
expect(policy.require_number).to be_false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "#valid?" do
|
72
|
+
let(:config) {Passwd::Config.instance}
|
73
|
+
|
74
|
+
it "valid password should be valid" do
|
75
|
+
expect(policy.valid?("secret1234", config)).to be_true
|
76
|
+
end
|
77
|
+
|
78
|
+
it "short password should not valid" do
|
79
|
+
expect(policy.valid?("short1", config)).to be_false
|
80
|
+
end
|
81
|
+
|
82
|
+
it "password should not valid if not contain lower case" do
|
83
|
+
expect(policy.valid?("NOTLOWER12", config)).to be_false
|
84
|
+
end
|
85
|
+
|
86
|
+
it "password should not valid if not contain upper case" do
|
87
|
+
policy.configure {|c| c.require_upper = true}
|
88
|
+
expect(policy.valid?("notupper12", config)).to be_false
|
89
|
+
end
|
90
|
+
|
91
|
+
it "password should not valid if not contain number" do
|
92
|
+
expect(policy.valid?("notnumber", config)).to be_false
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#include_char?" do
|
97
|
+
it "should be return true if contains" do
|
98
|
+
expect(policy.include_char?(("a".."z").to_a, "contains")).to be_true
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should be return false if not contains" do
|
102
|
+
expect(policy.include_char?(("a".."z").to_a, "NOTCONTAINS")).to be_false
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#reset" do
|
107
|
+
before {
|
108
|
+
policy.configure do |c|
|
109
|
+
c.min_length = 20
|
110
|
+
c.require_lower = false
|
111
|
+
c.require_upper = true
|
112
|
+
c.require_number = false
|
113
|
+
end
|
114
|
+
policy.reset
|
115
|
+
}
|
116
|
+
|
117
|
+
it "min_length should be a default" do
|
118
|
+
expect(policy.min_length).to eq(8)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "require_lower should be a default" do
|
122
|
+
expect(policy.require_lower).to be_true
|
123
|
+
end
|
124
|
+
|
125
|
+
it "require_upper should be a default" do
|
126
|
+
expect(policy.require_upper).to be_false
|
127
|
+
end
|
128
|
+
|
129
|
+
it "require_number should be a default" do
|
130
|
+
expect(policy.require_number).to be_true
|
131
|
+
end
|
132
|
+
end
|
133
|
+
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/passwd/base.rb
|
72
72
|
- lib/passwd/configuration/abstract_config.rb
|
73
73
|
- lib/passwd/configuration/config.rb
|
74
|
+
- lib/passwd/configuration/policy.rb
|
74
75
|
- lib/passwd/configuration/tmp_config.rb
|
75
76
|
- lib/passwd/password.rb
|
76
77
|
- lib/passwd/version.rb
|
@@ -78,6 +79,7 @@ files:
|
|
78
79
|
- spec/passwd/active_record_spec.rb
|
79
80
|
- spec/passwd/base_spec.rb
|
80
81
|
- spec/passwd/configuration/config_spec.rb
|
82
|
+
- spec/passwd/configuration/policy_spec.rb
|
81
83
|
- spec/passwd/configuration/tmp_config_spec.rb
|
82
84
|
- spec/passwd/password_spec.rb
|
83
85
|
- spec/spec_helper.rb
|
@@ -109,6 +111,7 @@ test_files:
|
|
109
111
|
- spec/passwd/active_record_spec.rb
|
110
112
|
- spec/passwd/base_spec.rb
|
111
113
|
- spec/passwd/configuration/config_spec.rb
|
114
|
+
- spec/passwd/configuration/policy_spec.rb
|
112
115
|
- spec/passwd/configuration/tmp_config_spec.rb
|
113
116
|
- spec/passwd/password_spec.rb
|
114
117
|
- spec/spec_helper.rb
|