has_secure_token 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/has_secure_token.rb +39 -11
- data/lib/has_secure_token/version.rb +1 -1
- data/test/has_secure_password_test.rb +24 -0
- data/test/models/user.rb +4 -0
- data/test/models/visitor.rb +15 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1148bcff41e4dab40603ae600aae8129d4fd4dab
|
4
|
+
data.tar.gz: 1686f165bdc43dba2030e09a5d72c847763b718c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0f969596f567eefe82d69bac7a2d5258100018f904cef879c3c4fa65690d7eddc380e7cb1c6532e380da2c2f8d6972703eb12e85693798a8c9cb5bb34a80601
|
7
|
+
data.tar.gz: 7a414116ae3415f7121fe401256e23a0997d6ad7ad38b9d0b1ec6ac7e7b8af1c02ce4b2222cee86eae9f4b234cdbb74cea7da50b252aa761e1ce066226a5669f
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
|
10
10
|
Add this line to your application's Gemfile:
|
11
11
|
|
12
|
-
gem '
|
12
|
+
gem 'has_secure_token'
|
13
13
|
|
14
14
|
And then execute:
|
15
15
|
|
@@ -17,12 +17,12 @@ And then execute:
|
|
17
17
|
|
18
18
|
Or install it yourself as:
|
19
19
|
|
20
|
-
$ gem install
|
20
|
+
$ gem install has_secure_token
|
21
21
|
|
22
22
|
##Setting your Model
|
23
23
|
|
24
24
|
```ruby
|
25
|
-
class User
|
25
|
+
class User < ActiveRecord::Base
|
26
26
|
has_secure_token :token1, :token2
|
27
27
|
end
|
28
28
|
|
@@ -33,7 +33,7 @@ user.token2 => "226dd46af6be78953bde1641622497a8"
|
|
33
33
|
|
34
34
|
## Contributing
|
35
35
|
|
36
|
-
1. Fork it ( https://github.com/
|
36
|
+
1. Fork it ( https://github.com/robertomiranda/has_secure_password/fork )
|
37
37
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
38
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
39
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/has_secure_token.rb
CHANGED
@@ -4,22 +4,50 @@ require 'securerandom'
|
|
4
4
|
module HasSecureToken
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
module ClassMethods
|
7
|
-
|
7
|
+
# Example using Active Record (which automatically includes ActiveModel::SecurePassword):
|
8
|
+
#
|
9
|
+
# # Schema: User(auth_token:string, invitation_token:string)
|
10
|
+
# class User < ActiveRecord::Base
|
11
|
+
# has_secure_token :auth_token, :invitation_token
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# user = User.new
|
15
|
+
# user.save
|
16
|
+
# user.auth_token # => "44539a6a59835a4ee9d7b112"
|
17
|
+
# user.invitation_token # => "226dd46af6be78953bde1648"
|
18
|
+
# user.regenerate_auth_token # => true
|
19
|
+
# user.regenerate_invitation_token # => true
|
20
|
+
def has_secure_token(*args)
|
21
|
+
# Load securerandom only when has_secure_key is used.
|
22
|
+
require 'securerandom'
|
8
23
|
include InstanceMethodsOnActivation
|
9
|
-
cattr_accessor :token_columns
|
10
|
-
|
11
|
-
|
24
|
+
cattr_accessor :token_columns, :options
|
25
|
+
options = args.extract_options!
|
26
|
+
|
27
|
+
key_length = options.fetch(:key_length, 24)
|
28
|
+
bytes = (key_length / 2.0).ceil
|
29
|
+
|
30
|
+
args.each do |attribute|
|
31
|
+
define_method("regenerate_#{attribute}!") do
|
32
|
+
send(:generate_unique_secure_token, attribute, bytes, key_length)
|
33
|
+
save
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
before_create do
|
38
|
+
args.each do |attribute|
|
39
|
+
self.generate_unique_secure_token(attribute, bytes, key_length)
|
40
|
+
end
|
41
|
+
end
|
12
42
|
end
|
13
43
|
end
|
14
44
|
|
15
45
|
module InstanceMethodsOnActivation
|
16
|
-
def
|
17
|
-
self.
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end)
|
22
|
-
end
|
46
|
+
def generate_unique_secure_token(attribute, bytes, key_length)
|
47
|
+
self.send("#{attribute}=", loop do
|
48
|
+
random_token = SecureRandom.hex(bytes)[0..key_length]
|
49
|
+
break random_token unless self.class.exists?(attribute => random_token)
|
50
|
+
end)
|
23
51
|
end
|
24
52
|
end
|
25
53
|
end
|
@@ -4,10 +4,34 @@ class HasSecureTokenTest < MiniTest::Unit::TestCase
|
|
4
4
|
def setup
|
5
5
|
@user = User.new
|
6
6
|
@user.run_callbacks :create
|
7
|
+
@visitor = Visitor.new
|
8
|
+
@visitor.run_callbacks :create
|
7
9
|
end
|
8
10
|
|
9
11
|
def test_assing_token_values
|
10
12
|
assert_not_nil @user.auth_token
|
11
13
|
assert_not_nil @user.invitation_token
|
12
14
|
end
|
15
|
+
|
16
|
+
def test_default_length_of_secure_token_is_set_to_24
|
17
|
+
assert_equal 24, @user.auth_token.length
|
18
|
+
assert_equal 24, @user.invitation_token.length
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_create_record_with_customised_length_of_secure_token
|
22
|
+
assert_equal 30, @visitor.auth_token.length
|
23
|
+
assert_equal 30, @visitor.invitation_token.length
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_regenerate_the_secure_key_for_the_attribute
|
27
|
+
old_auth_token = @user.auth_token
|
28
|
+
old_invitation_token = @user.invitation_token
|
29
|
+
@user.regenerate_auth_token!
|
30
|
+
@user.regenerate_invitation_token!
|
31
|
+
|
32
|
+
assert @user.auth_token != old_auth_token
|
33
|
+
assert @user.invitation_token != old_invitation_token
|
34
|
+
assert_equal 24, @user.auth_token.length
|
35
|
+
assert_equal 24, @user.invitation_token.length
|
36
|
+
end
|
13
37
|
end
|
data/test/models/user.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
class Visitor
|
2
|
+
extend ActiveModel::Callbacks
|
3
|
+
include ActiveModel::SecurePassword
|
4
|
+
include HasSecureToken
|
5
|
+
|
6
|
+
define_model_callbacks :create
|
7
|
+
|
8
|
+
has_secure_token :auth_token, :invitation_token, key_length: 30
|
9
|
+
|
10
|
+
attr_accessor :auth_token, :invitation_token
|
11
|
+
|
12
|
+
def self.exists?(attrs)
|
13
|
+
false
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_secure_token
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberto Miranda Altamar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- lib/has_secure_token/version.rb
|
85
85
|
- test/has_secure_password_test.rb
|
86
86
|
- test/models/user.rb
|
87
|
+
- test/models/visitor.rb
|
87
88
|
- test/test_helper.rb
|
88
89
|
homepage: https://github.com/robertomiranda/has_secure_token
|
89
90
|
licenses:
|
@@ -112,4 +113,5 @@ summary: Create uniques random tokens for any model in ruby on rails.
|
|
112
113
|
test_files:
|
113
114
|
- test/has_secure_password_test.rb
|
114
115
|
- test/models/user.rb
|
116
|
+
- test/models/visitor.rb
|
115
117
|
- test/test_helper.rb
|