tokens 0.2.0.beta.1 → 0.2.0.beta.3
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.
- data/Rakefile +6 -1
- data/lib/tokens.rb +29 -22
- data/spec/spec_helper.rb +4 -2
- data/spec/tokens_spec.rb +43 -43
- metadata +4 -20
data/Rakefile
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require "rspec/core/rake_task"
|
1
2
|
require "lib/tokens/version"
|
2
3
|
|
3
4
|
begin
|
@@ -8,7 +9,7 @@ begin
|
|
8
9
|
gem.email = "fnando.vieira@gmail.com"
|
9
10
|
gem.homepage = "http://github.com/fnando/has_tokens"
|
10
11
|
gem.authors = ["Nando Vieira"]
|
11
|
-
gem.version = SimplesIdeias::Tokens::Version::STRING + ".beta.
|
12
|
+
gem.version = SimplesIdeias::Tokens::Version::STRING + ".beta.3"
|
12
13
|
gem.summary = "Generate named tokens on your ActiveRecord models."
|
13
14
|
gem.files = FileList["README.rdoc", "init.rb", "{lib,spec,source}/**/*", "Rakefile"]
|
14
15
|
end
|
@@ -17,3 +18,7 @@ begin
|
|
17
18
|
rescue LoadError => e
|
18
19
|
puts "[JEWELER] You can't build a gem until you install jeweler with `gem install jeweler`"
|
19
20
|
end
|
21
|
+
|
22
|
+
RSpec::Core::RakeTask.new do |t|
|
23
|
+
t.ruby_opts = %[ -rubygems -Ilib -Ispec ]
|
24
|
+
end
|
data/lib/tokens.rb
CHANGED
@@ -18,15 +18,15 @@ module Tokens
|
|
18
18
|
include InstanceMethods
|
19
19
|
end
|
20
20
|
|
21
|
-
# Generate token
|
21
|
+
# Generate token with specified length.
|
22
22
|
#
|
23
|
-
# User.generate_token(
|
23
|
+
# User.generate_token(10)
|
24
24
|
#
|
25
|
-
def generate_token(
|
25
|
+
def generate_token(size)
|
26
26
|
validity = Proc.new {|token| Token.where(:token => token).first.nil?}
|
27
27
|
|
28
28
|
begin
|
29
|
-
seed =
|
29
|
+
seed = "--#{rand}--#{Time.now}--#{rand}--"
|
30
30
|
token = Digest::SHA1.hexdigest(seed)[0, size]
|
31
31
|
end while validity[token] == false
|
32
32
|
|
@@ -35,13 +35,13 @@ module Tokens
|
|
35
35
|
|
36
36
|
# Find a token
|
37
37
|
#
|
38
|
-
# User.find_token(:activation,
|
39
|
-
# User.find_token(:name => activation, :token =>
|
40
|
-
# User.find_token(:name => activation, :token =>
|
38
|
+
# User.find_token(:activation, "abcdefg")
|
39
|
+
# User.find_token(:name => activation, :token => "abcdefg")
|
40
|
+
# User.find_token(:name => activation, :token => "abcdefg", :tokenizable_id => 1)
|
41
41
|
#
|
42
42
|
def find_token(*args)
|
43
43
|
if args.first.kind_of?(Hash)
|
44
|
-
args.first
|
44
|
+
options = args.first
|
45
45
|
else
|
46
46
|
options = {
|
47
47
|
:name => args.first,
|
@@ -50,12 +50,12 @@ module Tokens
|
|
50
50
|
end
|
51
51
|
|
52
52
|
options.merge!(:name => options[:name].to_s, :tokenizable_type => self.name)
|
53
|
-
Token.where(options).
|
53
|
+
Token.where(options).includes(:tokenizable).first
|
54
54
|
end
|
55
55
|
|
56
56
|
# Find object by token.
|
57
57
|
#
|
58
|
-
# User.find_by_token(:activation,
|
58
|
+
# User.find_by_token(:activation, "abcdefg")
|
59
59
|
#
|
60
60
|
def find_by_token(name, hash)
|
61
61
|
token = find_token(:name => name.to_s, :token => hash)
|
@@ -65,19 +65,27 @@ module Tokens
|
|
65
65
|
|
66
66
|
# Find object by valid token (same name, same hash, not expired).
|
67
67
|
#
|
68
|
-
# User.find_by_valid_token(:activation,
|
68
|
+
# User.find_by_valid_token(:activation, "abcdefg")
|
69
69
|
#
|
70
70
|
def find_by_valid_token(name, hash)
|
71
71
|
token = find_token(:name => name.to_s, :token => hash)
|
72
|
-
return nil if !token ||
|
73
|
-
|
72
|
+
return nil if !token || token.expired?
|
73
|
+
token.tokenizable
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
77
|
module InstanceMethods
|
78
|
+
# Verify if given token is valid.
|
79
|
+
#
|
80
|
+
# @user.valid_token?(:active, "abcdefg")
|
81
|
+
#
|
82
|
+
def valid_token?(name, hash)
|
83
|
+
self.tokens.where(:name => name.to_s, :token => hash.to_s).first != nil
|
84
|
+
end
|
85
|
+
|
78
86
|
# Find a token.
|
79
87
|
#
|
80
|
-
# @user.find_token(:activation,
|
88
|
+
# @user.find_token(:activation, "abcdefg")
|
81
89
|
#
|
82
90
|
def find_token(name, token)
|
83
91
|
self.class.find_token(
|
@@ -115,15 +123,14 @@ module Tokens
|
|
115
123
|
})
|
116
124
|
|
117
125
|
remove_token(name)
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
self.tokens.create(
|
122
|
-
:name => name.to_s,
|
123
|
-
:token => self.class.generate_token(seed, options[:size]),
|
126
|
+
attrs = {
|
127
|
+
:name => name.to_s,
|
128
|
+
:token => self.class.generate_token(options[:size]),
|
124
129
|
:expires_at => options[:expires_at],
|
125
|
-
:data
|
126
|
-
|
130
|
+
:data => options[:data]
|
131
|
+
}
|
132
|
+
|
133
|
+
self.tokens.create!(attrs)
|
127
134
|
end
|
128
135
|
end
|
129
136
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
|
2
|
-
|
3
1
|
require "rspec"
|
4
2
|
require "active_record"
|
5
3
|
require "tokens"
|
@@ -7,3 +5,7 @@ require "tokens"
|
|
7
5
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
8
6
|
|
9
7
|
load("schema.rb")
|
8
|
+
|
9
|
+
RSpec.configure do |c|
|
10
|
+
c.color_enabled = true
|
11
|
+
end
|
data/spec/tokens_spec.rb
CHANGED
@@ -9,156 +9,156 @@ class Post < ActiveRecord::Base
|
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "has_tokens" do
|
12
|
-
before
|
12
|
+
before do
|
13
13
|
User.delete_all
|
14
14
|
Post.delete_all
|
15
|
-
|
15
|
+
|
16
16
|
@user = User.create(:name => "Homer")
|
17
17
|
@another_user = User.create(:name => "Bart")
|
18
18
|
@post = Post.create(:title => "How to make donuts")
|
19
19
|
@expire = 3.days.from_now
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
describe "- token" do
|
23
23
|
it "should be created" do
|
24
24
|
expect { @user.add_token(:uid) }.to change(Token, :count)
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
it "should be created for different users" do
|
28
28
|
@user.add_token(:uid).should be_valid
|
29
29
|
@another_user.add_token(:uid).should be_valid
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
it "should be created with expiration date" do
|
33
33
|
@user.add_token(:uid, :expires_at => @expire).expires_at.should == @expire
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
it "should be created with additional data" do
|
37
|
-
@user.add_token(:uid, :data =>
|
37
|
+
@user.add_token(:uid, :data => "some value").data.should == "some value"
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
it "should be created with custom size" do
|
41
41
|
@user.add_token(:uid, :size => 6).hash.size.should == 6
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
it "should find token by its name" do
|
45
45
|
token = @user.add_token(:uid)
|
46
46
|
@user.find_token_by_name(:uid).should == token
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
it "should be nil when no token is found" do
|
50
|
-
@user.find_token(:uid,
|
50
|
+
@user.find_token(:uid, "abcdef").should be_nil
|
51
51
|
@user.find_token_by_name(:uid).should be_nil
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
it "should be a valid token" do
|
55
55
|
token = @user.add_token(:uid)
|
56
56
|
@user.valid_token?(:uid, token.hash).should be_true
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
it "should not be a valid token" do
|
60
|
-
@user.valid_token?(:uid,
|
60
|
+
@user.valid_token?(:uid, "invalid").should be_false
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
it "should find token by its name and hash" do
|
64
64
|
token = @user.add_token(:uid)
|
65
65
|
@user.find_token(:uid, token.hash).should == token
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
it "should not be expired when have no expiration date" do
|
69
69
|
@user.add_token(:uid).should_not be_expired
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
it "should not be expired when have a future expiration date" do
|
73
73
|
@user.add_token(:uid, :expires_at => 3.days.from_now).should_not be_expired
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
it "should be expired" do
|
77
77
|
@user.add_token(:uid, :expires_at => 3.days.ago).should be_expired
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
it "should remove token" do
|
81
81
|
@user.add_token(:uid)
|
82
|
-
@user.remove_token(:uid).should
|
82
|
+
@user.remove_token(:uid).should be_true
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
85
|
it "should not remove other users tokens" do
|
86
86
|
@user.add_token(:uid)
|
87
87
|
@another_user.add_token(:uid)
|
88
|
-
|
88
|
+
|
89
89
|
@user.remove_token(:uid)
|
90
|
-
|
90
|
+
|
91
91
|
@user.find_token_by_name(:uid).should be_nil
|
92
92
|
@another_user.find_token_by_name(:uid).should be_an_instance_of(Token)
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
it "should not be duplicated" do
|
96
96
|
@user.add_token(:uid)
|
97
97
|
@user.add_token(:uid)
|
98
|
-
|
99
|
-
@user.tokens.find_all_by_name(
|
98
|
+
|
99
|
+
@user.tokens.find_all_by_name("uid").size.should == 1
|
100
100
|
end
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
it "should have tokens association" do
|
104
104
|
expect { @user.tokens }.to_not raise_error
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
it "should remove all expired tokens" do
|
108
108
|
expect {
|
109
109
|
%w(uid activation_code reset_password_code).each do |name|
|
110
110
|
@user.add_token(name, :expires_at => 3.days.ago)
|
111
111
|
end
|
112
112
|
}.to change(Token, :count).by(3)
|
113
|
-
|
114
|
-
Token.
|
113
|
+
|
114
|
+
Token.clean.should == 3
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
it "should generate token without saving it" do
|
118
118
|
expect {
|
119
|
-
User.generate_token(
|
119
|
+
User.generate_token(32)
|
120
120
|
}.to_not change(Token, :count)
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
it "should generate token with custom size" do
|
124
|
-
User.generate_token(
|
124
|
+
User.generate_token(8).size.should == 8
|
125
125
|
end
|
126
|
-
|
126
|
+
|
127
127
|
it "should alias token method" do
|
128
128
|
token = @user.add_token(:uid)
|
129
129
|
token.hash.should == token.token
|
130
130
|
end
|
131
|
-
|
131
|
+
|
132
132
|
it "should find user by token" do
|
133
133
|
token = @user.add_token(:uid)
|
134
134
|
User.find_by_token(:uid, token.hash).should == @user
|
135
135
|
end
|
136
|
-
|
136
|
+
|
137
137
|
it "should return user by its valid token without expiration time" do
|
138
138
|
token = @user.add_token(:uid)
|
139
139
|
User.find_by_valid_token(:uid, token.hash).should == @user
|
140
140
|
end
|
141
|
-
|
141
|
+
|
142
142
|
it "should return user by its valid token with expiration time" do
|
143
143
|
token = @user.add_token(:uid, :expires_at => @expire)
|
144
144
|
User.find_by_valid_token(:uid, token.hash).should == @user
|
145
145
|
end
|
146
|
-
|
146
|
+
|
147
147
|
it "should find token using class method with one argument (hash only)" do
|
148
148
|
token = @user.add_token(:uid)
|
149
149
|
User.find_token(:name => :uid, :token => token.hash).should == token
|
150
150
|
end
|
151
|
-
|
151
|
+
|
152
152
|
it "should not conflict with other models" do
|
153
153
|
user_token = @user.add_token(:uid)
|
154
154
|
post_token = @post.add_token(:uid)
|
155
|
-
|
155
|
+
|
156
156
|
User.find_token(post_token.to_s).should == nil
|
157
157
|
User.find_token(:name => :uid)
|
158
158
|
end
|
159
|
-
|
159
|
+
|
160
160
|
it "to_s should return hash" do
|
161
161
|
token = @user.add_token(:uid)
|
162
162
|
token.to_s.should == token.hash
|
163
163
|
end
|
164
|
-
end
|
164
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tokens
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease: true
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
- 0
|
10
|
-
- beta
|
11
|
-
- 1
|
12
|
-
version: 0.2.0.beta.1
|
4
|
+
version: 0.2.0.beta.3
|
13
5
|
platform: ruby
|
14
6
|
authors:
|
15
7
|
- Nando Vieira
|
@@ -48,29 +40,21 @@ rdoc_options:
|
|
48
40
|
require_paths:
|
49
41
|
- lib
|
50
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
43
|
requirements:
|
53
44
|
- - ">="
|
54
45
|
- !ruby/object:Gem::Version
|
55
|
-
hash: 3
|
56
|
-
segments:
|
57
|
-
- 0
|
58
46
|
version: "0"
|
47
|
+
version:
|
59
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
-
none: false
|
61
49
|
requirements:
|
62
50
|
- - ">"
|
63
51
|
- !ruby/object:Gem::Version
|
64
|
-
hash: 25
|
65
|
-
segments:
|
66
|
-
- 1
|
67
|
-
- 3
|
68
|
-
- 1
|
69
52
|
version: 1.3.1
|
53
|
+
version:
|
70
54
|
requirements: []
|
71
55
|
|
72
56
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.3.
|
57
|
+
rubygems_version: 1.3.5
|
74
58
|
signing_key:
|
75
59
|
specification_version: 3
|
76
60
|
summary: Generate named tokens on your ActiveRecord models.
|