mongoid-tokens 1.0.0
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 +15 -0
- data/.gitignore +4 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +144 -0
- data/README.md +99 -0
- data/Rakefile +5 -0
- data/lib/mongoid_tokens.rb +8 -0
- data/lib/tokens/generator.rb +12 -0
- data/lib/tokens/mongoid.rb +164 -0
- data/lib/tokens/railtie.rb +9 -0
- data/lib/tokens/token.rb +32 -0
- data/lib/tokens/version.rb +8 -0
- data/mongoid-tokens.gemspec +26 -0
- data/spec/schema.rb +25 -0
- data/spec/spec_helper.rb +41 -0
- data/spec/support/models.rb +15 -0
- data/spec/support/mongoid.yml +6 -0
- data/spec/tokens_spec.rb +187 -0
- data/templates/tokens.rb +17 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YTEzN2VhODk2ZmI0Yjg0YjZiYzBmZjk1YmMyMGU5NDAyYjJkZTg0Ng==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YTQwYWRjOTc1MzIzMWE0ZWQ1YmNkNDFkNjU2ODFhYjIzNDQ2MDM5Ng==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZWZiZDZiYTIwYTdjZTBkOTlkYTFkNDg4ZGViYThhOTI2NDljYWVmNDllMGM2
|
10
|
+
MGVjMzlmZGY0YmM3NjNhZTk2MWJhNGU1ZmQyMTVlM2UzOWRkNmZkMjZkODNm
|
11
|
+
MjMyNjY4YjVhODY1YjhhOTI3NDQyMmNhNzg0Mzc0YzM4YTQ5MDM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YjBiYWE3NTE2MDY2MzRkMGQ0ZjJmOTI5NGQ5YTdhODhiYjU1NWM3MzVlZThj
|
14
|
+
NDE4Nzg3NTFkYTRlYjMwN2VkM2ViMGVjMGE1M2VhYWZkZWM4NjI3NzI2ZjEz
|
15
|
+
MDgyODdjYjk4OTM0YzQ0NDQyZjg2MDI3MDM0ZTRmY2ZjMWRlODE=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mongoid-tokens (2.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionmailer (4.1.1)
|
10
|
+
actionpack (= 4.1.1)
|
11
|
+
actionview (= 4.1.1)
|
12
|
+
mail (~> 2.5.4)
|
13
|
+
actionpack (4.1.1)
|
14
|
+
actionview (= 4.1.1)
|
15
|
+
activesupport (= 4.1.1)
|
16
|
+
rack (~> 1.5.2)
|
17
|
+
rack-test (~> 0.6.2)
|
18
|
+
actionview (4.1.1)
|
19
|
+
activesupport (= 4.1.1)
|
20
|
+
builder (~> 3.1)
|
21
|
+
erubis (~> 2.7.0)
|
22
|
+
activemodel (4.1.1)
|
23
|
+
activesupport (= 4.1.1)
|
24
|
+
builder (~> 3.1)
|
25
|
+
activerecord (4.1.1)
|
26
|
+
activemodel (= 4.1.1)
|
27
|
+
activesupport (= 4.1.1)
|
28
|
+
arel (~> 5.0.0)
|
29
|
+
activesupport (4.1.1)
|
30
|
+
i18n (~> 0.6, >= 0.6.9)
|
31
|
+
json (~> 1.7, >= 1.7.7)
|
32
|
+
minitest (~> 5.1)
|
33
|
+
thread_safe (~> 0.1)
|
34
|
+
tzinfo (~> 1.1)
|
35
|
+
arel (5.0.1.20140414130214)
|
36
|
+
awesome_print (1.2.0)
|
37
|
+
bson (2.3.0)
|
38
|
+
builder (3.2.2)
|
39
|
+
coderay (1.1.0)
|
40
|
+
connection_pool (2.1.0)
|
41
|
+
database_cleaner (1.3.0)
|
42
|
+
diff-lcs (1.2.5)
|
43
|
+
erubis (2.7.0)
|
44
|
+
hike (1.2.3)
|
45
|
+
i18n (0.6.9)
|
46
|
+
json (1.8.1)
|
47
|
+
mail (2.5.4)
|
48
|
+
mime-types (~> 1.16)
|
49
|
+
treetop (~> 1.4.8)
|
50
|
+
method_source (0.8.2)
|
51
|
+
mime-types (1.25.1)
|
52
|
+
minitest (5.3.4)
|
53
|
+
mongoid (4.0.0)
|
54
|
+
activemodel (~> 4.0)
|
55
|
+
moped (~> 2.0.0)
|
56
|
+
origin (~> 2.1)
|
57
|
+
tzinfo (>= 0.3.37)
|
58
|
+
moped (2.0.3)
|
59
|
+
bson (~> 2.2)
|
60
|
+
connection_pool (~> 2.0)
|
61
|
+
optionable (~> 0.2.0)
|
62
|
+
multi_json (1.10.1)
|
63
|
+
optionable (0.2.0)
|
64
|
+
origin (2.1.1)
|
65
|
+
polyglot (0.3.5)
|
66
|
+
pry (0.9.12.6)
|
67
|
+
coderay (~> 1.0)
|
68
|
+
method_source (~> 0.8)
|
69
|
+
slop (~> 3.4)
|
70
|
+
pry-meta (0.0.7)
|
71
|
+
awesome_print
|
72
|
+
pry
|
73
|
+
pry-nav
|
74
|
+
pry-remote
|
75
|
+
pry-nav (0.2.3)
|
76
|
+
pry (~> 0.9.10)
|
77
|
+
pry-remote (0.1.8)
|
78
|
+
pry (~> 0.9)
|
79
|
+
slop (~> 3.0)
|
80
|
+
rack (1.5.2)
|
81
|
+
rack-test (0.6.2)
|
82
|
+
rack (>= 1.0)
|
83
|
+
rails (4.1.1)
|
84
|
+
actionmailer (= 4.1.1)
|
85
|
+
actionpack (= 4.1.1)
|
86
|
+
actionview (= 4.1.1)
|
87
|
+
activemodel (= 4.1.1)
|
88
|
+
activerecord (= 4.1.1)
|
89
|
+
activesupport (= 4.1.1)
|
90
|
+
bundler (>= 1.3.0, < 2.0)
|
91
|
+
railties (= 4.1.1)
|
92
|
+
sprockets-rails (~> 2.0)
|
93
|
+
railties (4.1.1)
|
94
|
+
actionpack (= 4.1.1)
|
95
|
+
activesupport (= 4.1.1)
|
96
|
+
rake (>= 0.8.7)
|
97
|
+
thor (>= 0.18.1, < 2.0)
|
98
|
+
rake (10.3.2)
|
99
|
+
rspec-core (3.0.0)
|
100
|
+
rspec-support (~> 3.0.0)
|
101
|
+
rspec-expectations (3.0.0)
|
102
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
103
|
+
rspec-support (~> 3.0.0)
|
104
|
+
rspec-mocks (3.0.0)
|
105
|
+
rspec-support (~> 3.0.0)
|
106
|
+
rspec-rails (3.0.1)
|
107
|
+
actionpack (>= 3.0)
|
108
|
+
activesupport (>= 3.0)
|
109
|
+
railties (>= 3.0)
|
110
|
+
rspec-core (~> 3.0.0)
|
111
|
+
rspec-expectations (~> 3.0.0)
|
112
|
+
rspec-mocks (~> 3.0.0)
|
113
|
+
rspec-support (~> 3.0.0)
|
114
|
+
rspec-support (3.0.0)
|
115
|
+
slop (3.5.0)
|
116
|
+
sprockets (2.12.1)
|
117
|
+
hike (~> 1.2)
|
118
|
+
multi_json (~> 1.0)
|
119
|
+
rack (~> 1.0)
|
120
|
+
tilt (~> 1.1, != 1.3.0)
|
121
|
+
sprockets-rails (2.1.3)
|
122
|
+
actionpack (>= 3.0)
|
123
|
+
activesupport (>= 3.0)
|
124
|
+
sprockets (~> 2.8)
|
125
|
+
thor (0.19.1)
|
126
|
+
thread_safe (0.3.4)
|
127
|
+
tilt (1.4.1)
|
128
|
+
treetop (1.4.15)
|
129
|
+
polyglot
|
130
|
+
polyglot (>= 0.3.1)
|
131
|
+
tzinfo (1.2.1)
|
132
|
+
thread_safe (~> 0.1)
|
133
|
+
|
134
|
+
PLATFORMS
|
135
|
+
ruby
|
136
|
+
|
137
|
+
DEPENDENCIES
|
138
|
+
database_cleaner
|
139
|
+
mongoid
|
140
|
+
mongoid-tokens!
|
141
|
+
pry-meta
|
142
|
+
rails
|
143
|
+
rake
|
144
|
+
rspec-rails
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# Mongoid Tokens
|
2
|
+
|
3
|
+
Porting for Mongoid of the great [tokens](https://github.com/fnando/tokens) gem by fnando.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
### Installation
|
8
|
+
|
9
|
+
```bash
|
10
|
+
gem install tokens
|
11
|
+
```
|
12
|
+
|
13
|
+
### Setting up
|
14
|
+
|
15
|
+
Finally, add the macro `tokenizable` to your model and be happy!
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
tokenizable
|
20
|
+
end
|
21
|
+
|
22
|
+
# create a new user; remember that the token requires an existing record
|
23
|
+
# because it depends on its id
|
24
|
+
user = User.create(username: "fnando")
|
25
|
+
|
26
|
+
# create token that never expires
|
27
|
+
user.add_token(:activate)
|
28
|
+
|
29
|
+
# uses custom expires_at
|
30
|
+
user.add_token(:activate, expires_at: 10.days.from_now)
|
31
|
+
|
32
|
+
# uses the default size (12 characters)
|
33
|
+
user.add_token(:activate)
|
34
|
+
|
35
|
+
# uses custom size (up to 122)
|
36
|
+
user.add_token(:activate, size: 20)
|
37
|
+
|
38
|
+
# uses custom token value
|
39
|
+
user.add_token(:activate, token: 'abc123')
|
40
|
+
|
41
|
+
# create token with arbitrary data.
|
42
|
+
user.add_token(:activate, data: {action: "do something"})
|
43
|
+
|
44
|
+
# find token by name
|
45
|
+
user.find_token_by_name(:reset_account)
|
46
|
+
|
47
|
+
# find valid token per user context.
|
48
|
+
user.find_valid_token(:reset_account, "ea2f14aeac40")
|
49
|
+
|
50
|
+
# find token by hash
|
51
|
+
user.find_token("ea2f14aeac40")
|
52
|
+
|
53
|
+
# check if a token has expired
|
54
|
+
user.tokens.first.expired?
|
55
|
+
|
56
|
+
# find user by token
|
57
|
+
User.find_by_token(:activate, "ea2f14aeac40")
|
58
|
+
|
59
|
+
# remove all expired tokens except those with NULL values
|
60
|
+
Token.clean
|
61
|
+
|
62
|
+
# generate a token as string, without saving it
|
63
|
+
User.generate_token
|
64
|
+
|
65
|
+
# remove a token by its name
|
66
|
+
user.remove_token(:activate)
|
67
|
+
|
68
|
+
# find user by valid token (same name, same hash, not expired)
|
69
|
+
User.find_by_valid_token(:activate, "ea2f14aeac40")
|
70
|
+
|
71
|
+
# find a token using class scope
|
72
|
+
User.find_token(:activate, "ea2f14aeac40")
|
73
|
+
|
74
|
+
# Token hash
|
75
|
+
token.to_s #=> ea2f14aeac40
|
76
|
+
```
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
Copyright (c) 2008-2013 Nando Vieira, released under the MIT license
|
81
|
+
|
82
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
83
|
+
a copy of this software and associated documentation files (the
|
84
|
+
"Software"), to deal in the Software without restriction, including
|
85
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
86
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
87
|
+
permit persons to whom the Software is furnished to do so, subject to
|
88
|
+
the following conditions:
|
89
|
+
|
90
|
+
The above copyright notice and this permission notice shall be
|
91
|
+
included in all copies or substantial portions of the Software.
|
92
|
+
|
93
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
94
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
95
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
96
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
97
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
98
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
99
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rails/generators/base"
|
2
|
+
|
3
|
+
module Tokens
|
4
|
+
class InstallGenerator < ::Rails::Generators::Base
|
5
|
+
source_root File.dirname(__FILE__) + "/../../templates"
|
6
|
+
|
7
|
+
def copy_migrations
|
8
|
+
stamp = proc {|time| time.utc.strftime("%Y%m%d%H%M%S")}
|
9
|
+
copy_file "tokens.rb", "db/migrate/#{stamp[Time.now]}_create_tokens.rb"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
module MongoidTokens
|
2
|
+
module Mongoid
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval { extend ClassMethods }
|
7
|
+
end
|
8
|
+
|
9
|
+
module Serializer
|
10
|
+
class << self
|
11
|
+
# Set the serializer adapter. Defaults to JSON.
|
12
|
+
attr_accessor :adapter
|
13
|
+
end
|
14
|
+
|
15
|
+
require "json"
|
16
|
+
self.adapter = ::JSON
|
17
|
+
|
18
|
+
def self.load(data)
|
19
|
+
data ? JSON.load(data) : {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.dump(data)
|
23
|
+
data ? JSON.dump(data) : nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
# Set up model for using tokens.
|
29
|
+
#
|
30
|
+
# class User < ActiveRecord::Base
|
31
|
+
# tokenizable
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
def tokenizable
|
35
|
+
has_many :tokens, as: "tokenizable", dependent: :destroy
|
36
|
+
include InstanceMethods
|
37
|
+
end
|
38
|
+
|
39
|
+
# Generate token with specified length.
|
40
|
+
#
|
41
|
+
# User.generate_token(10)
|
42
|
+
#
|
43
|
+
def generate_token(size)
|
44
|
+
validity = Proc.new {|token| Token.where(:token => token).first.nil?}
|
45
|
+
|
46
|
+
begin
|
47
|
+
token = SecureRandom.hex(size)[0, size]
|
48
|
+
token = token.encode("UTF-8")
|
49
|
+
end while validity[token] == false
|
50
|
+
|
51
|
+
token
|
52
|
+
end
|
53
|
+
|
54
|
+
# Find a token
|
55
|
+
#
|
56
|
+
# User.find_token(:activation, "abcdefg")
|
57
|
+
# User.find_token(name: activation, token: "abcdefg")
|
58
|
+
# User.find_token(name: activation, token: "abcdefg", tokenizable_id: 1)
|
59
|
+
#
|
60
|
+
def find_token(*args)
|
61
|
+
if args.first.kind_of?(Hash)
|
62
|
+
options = args.first
|
63
|
+
else
|
64
|
+
options = {
|
65
|
+
name: args.first,
|
66
|
+
token: args.last
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
options.merge!(name: options[:name].to_s, tokenizable_type: self.name)
|
71
|
+
Token.where(options).first
|
72
|
+
end
|
73
|
+
|
74
|
+
# Find object by token.
|
75
|
+
#
|
76
|
+
# User.find_by_token(:activation, "abcdefg")
|
77
|
+
#
|
78
|
+
def find_by_token(name, hash)
|
79
|
+
token = find_token(name: name.to_s, token: hash)
|
80
|
+
return unless token
|
81
|
+
token.tokenizable
|
82
|
+
end
|
83
|
+
|
84
|
+
# Find object by valid token (same name, same hash, not expired).
|
85
|
+
#
|
86
|
+
# User.find_by_valid_token(:activation, "abcdefg")
|
87
|
+
#
|
88
|
+
def find_by_valid_token(name, hash)
|
89
|
+
token = find_token(name: name.to_s, token: hash)
|
90
|
+
return if !token || token.expired?
|
91
|
+
token.tokenizable
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
module InstanceMethods
|
96
|
+
# Verify if given token is valid.
|
97
|
+
#
|
98
|
+
# @user.valid_token?(:active, "abcdefg")
|
99
|
+
#
|
100
|
+
def valid_token?(name, hash)
|
101
|
+
self.tokens.where(name: name.to_s, token: hash.to_s).first != nil
|
102
|
+
end
|
103
|
+
|
104
|
+
# Find a token.
|
105
|
+
#
|
106
|
+
# @user.find_token(:activation, "abcdefg")
|
107
|
+
#
|
108
|
+
def find_token(name, token)
|
109
|
+
self.class.find_token(
|
110
|
+
tokenizable_id: self.id,
|
111
|
+
name: name.to_s,
|
112
|
+
token: token
|
113
|
+
)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Find token by its name.
|
117
|
+
def find_token_by_name(name)
|
118
|
+
self.tokens.where(name: name.to_s).first
|
119
|
+
end
|
120
|
+
|
121
|
+
# Return <tt>Token</tt> instance when token is valid.
|
122
|
+
def find_valid_token(name, token)
|
123
|
+
token = find_token(name, token)
|
124
|
+
return unless token
|
125
|
+
!token.expired? && token
|
126
|
+
end
|
127
|
+
|
128
|
+
# Remove token.
|
129
|
+
#
|
130
|
+
# @user.remove_token(:activate)
|
131
|
+
#
|
132
|
+
def remove_token(name)
|
133
|
+
return if new_record?
|
134
|
+
token = find_token_by_name(name)
|
135
|
+
token && token.destroy
|
136
|
+
end
|
137
|
+
|
138
|
+
# Add a new token.
|
139
|
+
#
|
140
|
+
# @user.add_token(:api_key, token: 'abc123')
|
141
|
+
# @user.add_token(:api_key, expires_at: nil)
|
142
|
+
# @user.add_token(:api_key, size: 20)
|
143
|
+
# @user.add_token(:api_key, data: {when: Time.now})
|
144
|
+
#
|
145
|
+
def add_token(name, options={})
|
146
|
+
options.reverse_merge!({
|
147
|
+
expires_at: 2.days.from_now,
|
148
|
+
size: 12,
|
149
|
+
data: nil
|
150
|
+
})
|
151
|
+
|
152
|
+
remove_token(name)
|
153
|
+
attrs = {
|
154
|
+
name: name.to_s,
|
155
|
+
token: options[:token] || self.class.generate_token(options[:size]),
|
156
|
+
expires_at: options[:expires_at],
|
157
|
+
data: options.fetch(:data) || {}
|
158
|
+
}
|
159
|
+
|
160
|
+
self.tokens.create!(attrs)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
data/lib/tokens/token.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
class Token
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Timestamps
|
4
|
+
include Mongoid::Attributes::Dynamic
|
5
|
+
belongs_to :tokenizable, polymorphic: true
|
6
|
+
|
7
|
+
field :name, type: String
|
8
|
+
field :token, type: String
|
9
|
+
field :expires_at, type: DateTime
|
10
|
+
|
11
|
+
# index :tokens, [:tokenizable_type, :tokenizable_id]
|
12
|
+
# index :tokens, :token
|
13
|
+
# index :tokens, :expires_at
|
14
|
+
# index :tokens, [:tokenizable_id, :tokenizable_type, :name], unique: true
|
15
|
+
|
16
|
+
def self.clean
|
17
|
+
where(:expires_at.lt => Time.now, :expires_at.exists => true).delete
|
18
|
+
#where("expires_at < ? AND expires_at IS NOT NULL", Time.now).delete_all
|
19
|
+
end
|
20
|
+
|
21
|
+
def data
|
22
|
+
read_attribute(:data) || {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
token
|
27
|
+
end
|
28
|
+
|
29
|
+
def expired?
|
30
|
+
expires_at && expires_at < Time.now
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'tokens/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'mongoid-tokens'
|
7
|
+
s.version = MongoidTokens::Version::STRING
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Andrea Campolonghi']
|
10
|
+
s.email = ['acampolonghi@gild.com']
|
11
|
+
s.homepage = 'http://rubygems.org/gems/tokens'
|
12
|
+
s.summary = 'Porting of tokens of Nando Vieira http://rubygems.org/gems/tokens for Mongoid'
|
13
|
+
s.description = s.summary
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.add_development_dependency 'rails'
|
21
|
+
s.add_development_dependency 'rake'
|
22
|
+
s.add_development_dependency 'rspec-rails'
|
23
|
+
s.add_development_dependency 'mongoid'
|
24
|
+
s.add_development_dependency 'pry-meta'
|
25
|
+
s.add_development_dependency 'database_cleaner'
|
26
|
+
end
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :users do |t|
|
3
|
+
t.string :name
|
4
|
+
end
|
5
|
+
|
6
|
+
create_table :posts do |t|
|
7
|
+
t.string :title
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table :tokens do |t|
|
11
|
+
t.string :name, null: false
|
12
|
+
t.belongs_to :tokenizable, null: false, polymorphic: true
|
13
|
+
t.string :token, null: false
|
14
|
+
t.text :data, null: true
|
15
|
+
t.datetime :expires_at, null: true
|
16
|
+
t.datetime :created_at, null: false
|
17
|
+
end
|
18
|
+
|
19
|
+
add_index :tokens, :tokenizable_type
|
20
|
+
add_index :tokens, :tokenizable_id
|
21
|
+
add_index :tokens, [:tokenizable_type, :tokenizable_id]
|
22
|
+
add_index :tokens, :token
|
23
|
+
add_index :tokens, :expires_at
|
24
|
+
add_index :tokens, [:tokenizable_id, :tokenizable_type, :name], unique: true
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'rails/railtie'
|
5
|
+
require 'action_controller/railtie'
|
6
|
+
require 'rspec/rails'
|
7
|
+
require 'mongoid'
|
8
|
+
require 'mongoid_tokens'
|
9
|
+
require 'database_cleaner'
|
10
|
+
|
11
|
+
|
12
|
+
Mongoid.load!("#{File.dirname(__FILE__)}/support/mongoid.yml", ENV['RAILS_ENV'])
|
13
|
+
|
14
|
+
module Tokens
|
15
|
+
class Application < Rails::Application
|
16
|
+
config.root = File.dirname(__FILE__) + '/..'
|
17
|
+
config.active_support.deprecation = :log
|
18
|
+
config.eager_load = true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Tokens::Application.initialize!
|
23
|
+
|
24
|
+
require 'support/models'
|
25
|
+
|
26
|
+
RSpec.configure do |config|
|
27
|
+
config.mock_with :rspec
|
28
|
+
|
29
|
+
config.before(:suite) do
|
30
|
+
DatabaseCleaner[:mongoid].strategy = :truncation
|
31
|
+
DatabaseCleaner[:mongoid].clean_with(:truncation)
|
32
|
+
end
|
33
|
+
|
34
|
+
config.before(:each) do
|
35
|
+
DatabaseCleaner.start
|
36
|
+
end
|
37
|
+
|
38
|
+
config.after(:each) do
|
39
|
+
DatabaseCleaner.clean
|
40
|
+
end
|
41
|
+
end
|
data/spec/tokens_spec.rb
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Tokens do
|
4
|
+
before do
|
5
|
+
User.delete_all
|
6
|
+
Post.delete_all
|
7
|
+
|
8
|
+
@user = User.create(name: "Homer")
|
9
|
+
@another_user = User.create(name: "Bart")
|
10
|
+
@post = Post.create(title: "How to make donuts")
|
11
|
+
@expire = 3.days.from_now.to_i
|
12
|
+
end
|
13
|
+
|
14
|
+
it "has tokens association" do
|
15
|
+
expect { @user.tokens }.to_not raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it "removes all expired tokens" do
|
19
|
+
expect {
|
20
|
+
%w(uid activation_code reset_password_code).each do |name|
|
21
|
+
@user.add_token(name, :expires_at => 3.days.ago)
|
22
|
+
end
|
23
|
+
}.to change(Token, :count).by(3)
|
24
|
+
|
25
|
+
expect(Token.clean).to eql(3)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "generates token without saving it" do
|
29
|
+
expect {
|
30
|
+
User.generate_token(32)
|
31
|
+
}.to_not change(Token, :count)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "generates token with custom size" do
|
35
|
+
expect(User.generate_token(8).size).to eql(8)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "sets alias for token method" do
|
39
|
+
token = @user.add_token(:uid)
|
40
|
+
expect(token.to_s).to eql(token.token)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "finds user by token" do
|
44
|
+
token = @user.add_token(:uid)
|
45
|
+
expect(User.find_by_token(:uid, token.to_s)).to eql(@user)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns user by its valid token without expiration time" do
|
49
|
+
token = @user.add_token(:uid)
|
50
|
+
expect(User.find_by_valid_token(:uid, token.to_s)).to eql(@user)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns user by its valid token with expiration time" do
|
54
|
+
token = @user.add_token(:uid, :expires_at => @expire)
|
55
|
+
expect(User.find_by_valid_token(:uid, token.to_s)).to eql(@user)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "finds token using class method with one argument (hash only)" do
|
59
|
+
token = @user.add_token(:uid)
|
60
|
+
expect(User.find_token(:name => :uid, :token => token.to_s)).to eql(token)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "doesn't conflict with other models" do
|
64
|
+
user_token = @user.add_token(:uid)
|
65
|
+
post_token = @post.add_token(:uid)
|
66
|
+
|
67
|
+
expect(User.find_token(post_token.to_s)).to be_nil
|
68
|
+
User.find_token(name: :uid)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "to_s should return hash" do
|
72
|
+
token = @user.add_token(:uid)
|
73
|
+
expect(token.to_s).to eql(token.to_s)
|
74
|
+
end
|
75
|
+
|
76
|
+
describe Token do
|
77
|
+
it "is created" do
|
78
|
+
expect { @user.add_token(:uid) }.to change(Token, :count)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "is created for different users" do
|
82
|
+
expect(@user.add_token(:uid)).to be_valid
|
83
|
+
expect(@another_user.add_token(:uid)).to be_valid
|
84
|
+
end
|
85
|
+
|
86
|
+
it "is created with expiration date" do
|
87
|
+
expect(@user.add_token(:uid, expires_at: @expire).expires_at.to_i).to eql(@expire)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "serializes data" do
|
91
|
+
token = @user.add_token(:uid, data: {name: "John Doe"})
|
92
|
+
token.reload
|
93
|
+
|
94
|
+
expect(token.data).to include("name" => "John Doe")
|
95
|
+
end
|
96
|
+
|
97
|
+
it "returns empty hash as serialized data" do
|
98
|
+
expect(Token.new.data).to eql({})
|
99
|
+
end
|
100
|
+
|
101
|
+
it "is created with custom size" do
|
102
|
+
expect(@user.add_token(:uid, :size => 6).to_s.size).to eql(6)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "finds token by its name" do
|
106
|
+
token = @user.add_token(:uid)
|
107
|
+
expect(@user.find_token_by_name(:uid)).to eql(token)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "returns nil nil when no token is found" do
|
111
|
+
expect(@user.find_token(:uid, "abcdef")).to be_nil
|
112
|
+
expect(@user.find_token_by_name(:uid)).to be_nil
|
113
|
+
end
|
114
|
+
|
115
|
+
it "is a valid token" do
|
116
|
+
token = @user.add_token(:uid)
|
117
|
+
expect(@user.valid_token?(:uid, token.to_s)).to be_truthy
|
118
|
+
end
|
119
|
+
|
120
|
+
it "isn't a valid token" do
|
121
|
+
expect(@user.valid_token?(:uid, "invalid")).to be_falsy
|
122
|
+
end
|
123
|
+
|
124
|
+
it "finds token by its name and hash" do
|
125
|
+
token = @user.add_token(:uid)
|
126
|
+
expect(@user.find_token(:uid, token.to_s)).to eql(token)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "isn't expired when have no expiration date" do
|
130
|
+
expect(@user.add_token(:uid)).not_to be_expired
|
131
|
+
end
|
132
|
+
|
133
|
+
it "isn't expired when have a future expiration date" do
|
134
|
+
expect(@user.add_token(:uid, expires_at: 3.days.from_now)).not_to be_expired
|
135
|
+
end
|
136
|
+
|
137
|
+
it "is expired" do
|
138
|
+
expect(@user.add_token(:uid, :expires_at => 3.days.ago)).to be_expired
|
139
|
+
end
|
140
|
+
|
141
|
+
it "removes token" do
|
142
|
+
@user.add_token(:uid)
|
143
|
+
expect(@user.remove_token(:uid)).to be_truthy
|
144
|
+
end
|
145
|
+
|
146
|
+
it "doesn't remove other users tokens" do
|
147
|
+
@user.add_token(:uid)
|
148
|
+
@another_user.add_token(:uid)
|
149
|
+
|
150
|
+
@user.remove_token(:uid)
|
151
|
+
|
152
|
+
expect(@user.find_token_by_name(:uid)).to be_nil
|
153
|
+
expect(@another_user.find_token_by_name(:uid)).to be_an_instance_of(Token)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "isn't duplicated" do
|
157
|
+
@user.add_token(:uid)
|
158
|
+
@user.add_token(:uid)
|
159
|
+
|
160
|
+
expect(@user.tokens.where(name: "uid").count).to eql(1)
|
161
|
+
end
|
162
|
+
|
163
|
+
it "returns valid token" do
|
164
|
+
token = @user.add_token(:uid)
|
165
|
+
expect(@user.find_valid_token(:uid, token.to_s)).to eql(token)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "returns nothing for invalid token" do
|
169
|
+
token = @user.add_token(:uid)
|
170
|
+
expect(@user.find_valid_token(:uid, "invalid")).to be_nil
|
171
|
+
end
|
172
|
+
|
173
|
+
it "returns nothing for missing token" do
|
174
|
+
expect(@user.find_valid_token(:uid, "invalid")).to be_nil
|
175
|
+
end
|
176
|
+
|
177
|
+
it "returns nothing for expired token" do
|
178
|
+
token = @user.add_token(:uid, expires_at: 2.weeks.ago)
|
179
|
+
expect(@user.find_valid_token(:uid, "invalid")).to be_nil
|
180
|
+
end
|
181
|
+
|
182
|
+
it "creates token with provided value" do
|
183
|
+
token = @user.add_token(:uid, token: 'abc123')
|
184
|
+
expect(token.to_s).to eq("abc123")
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
data/templates/tokens.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateTokens < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :tokens do |t|
|
4
|
+
t.string :name, null: false
|
5
|
+
t.belongs_to :tokenizable, null: false, polymorphic: true
|
6
|
+
t.string :token, null: false
|
7
|
+
t.text :data, null: true
|
8
|
+
t.datetime :expires_at, null: true
|
9
|
+
t.datetime :created_at, null: false
|
10
|
+
end
|
11
|
+
|
12
|
+
add_index :tokens, [:tokenizable_type, :tokenizable_id]
|
13
|
+
add_index :tokens, :token
|
14
|
+
add_index :tokens, :expires_at
|
15
|
+
add_index :tokens, [:tokenizable_id, :tokenizable_type, :name], unique: true
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-tokens
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrea Campolonghi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mongoid
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-meta
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: database_cleaner
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Porting of tokens of Nando Vieira http://rubygems.org/gems/tokens for
|
98
|
+
Mongoid
|
99
|
+
email:
|
100
|
+
- acampolonghi@gild.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- .gitignore
|
106
|
+
- Gemfile
|
107
|
+
- Gemfile.lock
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/mongoid_tokens.rb
|
111
|
+
- lib/tokens/generator.rb
|
112
|
+
- lib/tokens/mongoid.rb
|
113
|
+
- lib/tokens/railtie.rb
|
114
|
+
- lib/tokens/token.rb
|
115
|
+
- lib/tokens/version.rb
|
116
|
+
- mongoid-tokens.gemspec
|
117
|
+
- spec/schema.rb
|
118
|
+
- spec/spec_helper.rb
|
119
|
+
- spec/support/models.rb
|
120
|
+
- spec/support/mongoid.yml
|
121
|
+
- spec/tokens_spec.rb
|
122
|
+
- templates/tokens.rb
|
123
|
+
homepage: http://rubygems.org/gems/tokens
|
124
|
+
licenses: []
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ! '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.4.4
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Porting of tokens of Nando Vieira http://rubygems.org/gems/tokens for Mongoid
|
146
|
+
test_files:
|
147
|
+
- spec/schema.rb
|
148
|
+
- spec/spec_helper.rb
|
149
|
+
- spec/support/models.rb
|
150
|
+
- spec/support/mongoid.yml
|
151
|
+
- spec/tokens_spec.rb
|
152
|
+
has_rdoc:
|