active_model_secure_token 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6735c5897ecc1dc3567ae74a81f82ed0bc022cf
4
+ data.tar.gz: d3ac27f6aa5f67b8d4df64f920b5c9f9df9893b8
5
+ SHA512:
6
+ metadata.gz: 1222fbec657f3ce0aba6b9a2f75b03f9626dd7e0da7be63a895df6f29f3a5f18ef93524cb6e866c1878f460fd504b5369917eedb78c7d91ff9ce31f087258f76
7
+ data.tar.gz: bbe427cc5aaf58b91e0175d4b144743ecc5eb0c4f481844d6c7239958f1986e60d1ee5bd65354dce73d45ad963199fa0f3fe211abc3dc044fb90f5a2c2a85f32
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1
6
+ - 2.2
7
+ gemfile:
8
+ - gemfiles/rails3.gemfile
9
+ - gemfiles/rails4_0.gemfile
10
+ - gemfiles/rails4_1.gemfile
11
+ - gemfiles/rails4_2.gemfile
12
+ notifications:
13
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 robertomiranda
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,91 @@
1
+ # active_model_secure_token
2
+
3
+ Provides an easy way to generate uniques random tokens for any ActiveModel. **SecureRandom::base58** is used to generate the 24-character unique tokens, so collisions are highly unlikely.
4
+
5
+ **Note** If you're worried about possible collissions, there's a way to generate a race condition in the database in the same way that [validates_uniqueness_of](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html) can. You're encouraged to add an unique index in the database to deal
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'active_model_secure_token'
12
+
13
+ And then run:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_model_secure_token
20
+
21
+ This gem does not depend on ActiveRecord, only on ActiveModel. It works fine with ActiveRecord and Mongoid. You will need to `include ActiveModel::SecureToken` in your model/document before you can use it.
22
+
23
+ ## Setting your ActiveRecord model
24
+
25
+ The first step is to generate a migration in order to add the token key field.
26
+
27
+ ```ruby
28
+ rails g migration AddTokenToUsers token:string
29
+ =>
30
+ invoke active_record
31
+ create db/migrate/20150424010931_add_token_to_users.rb
32
+ ```
33
+
34
+ Then run `rake db:migrate` in order to update users table in the database. The next step is to add `has_secure_token`
35
+ to the model:
36
+ ```ruby
37
+ # Schema: User(token:string, auth_token:string)
38
+ class User < ActiveRecord::Base
39
+ include ActiveModel::SecureToken
40
+ has_secure_token
41
+ end
42
+
43
+ user = User.new
44
+ user.save
45
+ user.token # => "pX27zsMN2ViQKta1bGfLmVJE"
46
+ user.regenerate_token # => true
47
+ ```
48
+
49
+ To use a custom column to store the token key field you can specify the column_name option. See example above (e.g: auth_token):
50
+
51
+ ```ruby
52
+ # Schema: User(token:string, auth_token:string)
53
+ class User < ActiveRecord::Base
54
+ include ActiveModel::SecureToken
55
+ has_secure_token :auth_token
56
+ end
57
+
58
+ user = User.new
59
+ user.save
60
+ user.auth_token # => "pX27zsMN2ViQKta1bGfLmVJE"
61
+ user.regenerate_auth_token # => true
62
+ ```
63
+
64
+ ## Setting your Mongoid document
65
+
66
+ Add the field, include this gem and enable it like so:
67
+
68
+ ```ruby
69
+ class User
70
+ include Mongoid::Document
71
+ include ActiveModel::SecureToken
72
+ field :token, type: String
73
+ has_secure_token
74
+ end
75
+ ```
76
+
77
+ The Mongoid document has all the same methods as the ActiveRecord model.
78
+
79
+ ## Running tests
80
+
81
+ Running
82
+
83
+ ```shell
84
+ $ rake test
85
+ ```
86
+
87
+ Should return
88
+
89
+ ```shell
90
+ 8 runs, 14 assertions, 0 failures, 0 errors, 0 skips
91
+ ```
@@ -0,0 +1,22 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ task :help do
6
+ puts "Available rake tasks: "
7
+ puts "rake console - Run a IRB console with all enviroment loaded"
8
+ puts "rake test - Run tests"
9
+ end
10
+
11
+ task :test do
12
+ Dir.chdir('test')
13
+ end
14
+
15
+ Rake::TestTask.new(:test) do |t|
16
+ t.libs << '../lib'
17
+ t.libs << '../test'
18
+ t.test_files = FileList['*_test.rb']
19
+ t.verbose = false
20
+ end
21
+
22
+ task :default => :test
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_model_secure_token/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_model_secure_token"
8
+ spec.version = HasSecureToken::VERSION
9
+ spec.authors = ["Roberto Miranda Altamar", "Joost Baaij"]
10
+ spec.email = ["rjmaltamar@gmail.com", "joost@spacebabies.nl"]
11
+ spec.summary = %q{Create uniques random tokens for any model in ruby on rails. Backport of ActiveRecord::SecureToken 5 to AR 3.x and 4.x}
12
+ spec.description = %q{HasSecureToken provides you an easily way to geneatre uniques random tokens for any model in ruby on rails. **SecureRandom::base58** is used to generate the 24-character unique token, so collisions are highly unlikely.}
13
+ spec.homepage = "https://github.com/sign2pay/active_model_secure_token"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activemodel"
22
+ spec.add_development_dependency "mongoid"
23
+ spec.add_development_dependency "activerecord", [">= 3.0", "< 5.0"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "minitest"
28
+ spec.add_development_dependency 'sqlite3'
29
+ end
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => '../'
3
+
4
+ group :development, :test do
5
+ gem 'activerecord', '~> 3.2.0'
6
+ end
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => '../'
3
+
4
+ group :development, :test do
5
+ gem 'activerecord', '~> 4.0.0'
6
+ gem 'minitest', '~> 4.2'
7
+ end
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => '../'
3
+
4
+ group :development, :test do
5
+ gem 'activerecord', '~> 4.1.0'
6
+ end
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => '../'
3
+
4
+ group :development, :test do
5
+ gem 'activerecord', '~> 4.2.0'
6
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_support'
2
+ module ActiveModel
3
+ module SecureToken
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ # Example using has_secure_token
8
+ #
9
+ # # Schema: User(token:string, auth_token:string)
10
+ # class User < ActiveRecord::Base
11
+ # has_secure_token
12
+ # has_secure_token :auth_token
13
+ # end
14
+ #
15
+ # user = User.new
16
+ # user.save
17
+ # user.token # => "pX27zsMN2ViQKta1bGfLmVJE"
18
+ # user.auth_token # => "77TMHrHJFvFDwodq8w7Ev2m7"
19
+ # user.regenerate_token # => true
20
+ # user.regenerate_auth_token # => true
21
+ #
22
+ # SecureRandom::base58 is used to generate the 24-character unique token, so collisions are highly unlikely.
23
+ #
24
+ # Note that it's still possible to generate a race condition in the database in the same way that
25
+ # <tt>validates_uniqueness_of</tt> can. You're encouraged to add a unique index in the database to deal
26
+ # with this even more unlikely scenario.
27
+ def has_secure_token(attribute = :token)
28
+ # Load securerandom only when has_secure_token is used.
29
+ require 'active_support/core_ext/securerandom'
30
+ define_method("regenerate_#{attribute}") { update_attributes attribute => self.class.generate_unique_secure_token }
31
+ before_create { self.send("#{attribute}=", self.class.generate_unique_secure_token) unless self.send("#{attribute}?")}
32
+ end
33
+
34
+ def generate_unique_secure_token
35
+ SecureRandom.base58(24)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module HasSecureToken
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ require 'securerandom'
2
+
3
+ module SecureRandom
4
+ BASE58_ALPHABET = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a - ['0', 'O', 'I', 'l']
5
+ # SecureRandom.base58 generates a random base58 string.
6
+ #
7
+ # The argument _n_ specifies the length, of the random string to be generated.
8
+ #
9
+ # If _n_ is not specified or is nil, 16 is assumed. It may be larger in the future.
10
+ #
11
+ # The result may contain alphanumeric characters except 0, O, I and l
12
+ #
13
+ # p SecureRandom.base58 #=> "4kUgL2pdQMSCQtjE"
14
+ # p SecureRandom.base58(24) #=> "77TMHrHJFvFDwodq8w7Ev2m7"
15
+ #
16
+ def self.base58(n = 16)
17
+ SecureRandom.random_bytes(n).unpack("C*").map do |byte|
18
+ idx = byte % 64
19
+ idx = SecureRandom.random_number(58) if idx >= 58
20
+ BASE58_ALPHABET[idx]
21
+ end.join
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ require "test_helper"
2
+
3
+ class ActiveRecordSecureTokenTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @user = User.new
6
+ end
7
+
8
+ def test_token_values_are_generated_for_specified_attributes_and_persisted_on_save
9
+ @user.save
10
+ refute_nil @user.token
11
+ refute_nil @user.auth_token
12
+ end
13
+
14
+ def test_regenerating_the_secure_token
15
+ @user.save
16
+ old_token = @user.token
17
+ old_auth_token = @user.auth_token
18
+ @user.regenerate_token
19
+ @user.regenerate_auth_token
20
+
21
+ refute_equal @user.token, old_token
22
+ refute_equal @user.auth_token, old_auth_token
23
+ end
24
+
25
+ def test_token_value_not_overwritten_when_present
26
+ @user.token = "custom-secure-token"
27
+ @user.save
28
+
29
+ assert_equal @user.token, "custom-secure-token"
30
+ end
31
+ end
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: active_model_secure_token
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,6 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+ create_table :users do |t|
3
+ t.string :token
4
+ t.string :auth_token
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ class Post
2
+ include Mongoid::Document
3
+ include ActiveModel::SecureToken
4
+
5
+ field :token, type: String
6
+ field :auth_token, type: String
7
+
8
+ has_secure_token
9
+ has_secure_token :auth_token
10
+ end
@@ -0,0 +1,5 @@
1
+ class User < ActiveRecord::Base
2
+ include ActiveModel::SecureToken
3
+ has_secure_token
4
+ has_secure_token :auth_token
5
+ end
@@ -0,0 +1,35 @@
1
+ require "test_helper"
2
+
3
+ class MongoidSecureTokenTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @post = Post.new
6
+ end
7
+
8
+ def teardown
9
+ Mongoid.purge!
10
+ end
11
+
12
+ def test_token_values_are_generated_for_specified_attributes_and_persisted_on_save
13
+ @post.save
14
+ refute_nil @post.token
15
+ refute_nil @post.auth_token
16
+ end
17
+
18
+ def test_regenerating_the_secure_token
19
+ @post.save
20
+ old_token = @post.token
21
+ old_auth_token = @post.auth_token
22
+ @post.regenerate_token
23
+ @post.regenerate_auth_token
24
+
25
+ refute_equal @post.token, old_token
26
+ refute_equal @post.auth_token, old_auth_token
27
+ end
28
+
29
+ def test_token_value_not_overwritten_when_present
30
+ @post.token = "custom-secure-token"
31
+ @post.save
32
+
33
+ assert_equal @post.token, "custom-secure-token"
34
+ end
35
+ end
@@ -0,0 +1,19 @@
1
+ require "test_helper"
2
+
3
+ class SecureRandomTest < MiniTest::Unit::TestCase
4
+ def test_base58
5
+ s1 = SecureRandom.base58
6
+ s2 = SecureRandom.base58
7
+
8
+ refute_equal s1, s2
9
+ assert_equal 16, s1.length
10
+ end
11
+
12
+ def test_base58_with_length
13
+ s1 = SecureRandom.base58(24)
14
+ s2 = SecureRandom.base58(24)
15
+
16
+ refute_equal s1, s2
17
+ assert_equal 24, s1.length
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ testdir = File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift testdir unless $LOAD_PATH.include?(testdir)
3
+
4
+ libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
5
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
6
+
7
+ require "rubygems"
8
+ require "active_model_secure_token"
9
+ require "active_record"
10
+ require "mongoid"
11
+ require "minitest/autorun"
12
+ require "minitest/unit"
13
+
14
+ ENV['MONGOID_ENV'] = 'test'
15
+ Mongoid.load!('./config/mongoid.yml')
16
+
17
+ Dir["models/*.rb"].each {|file| require file }
18
+
19
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ":memory:"
20
+
21
+ load './config/schema.rb'
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model_secure_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Miranda Altamar
8
+ - Joost Baaij
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-06-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: mongoid
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: activerecord
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '5.0'
52
+ type: :development
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '3.0'
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ - !ruby/object:Gem::Dependency
77
+ name: rake
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ - !ruby/object:Gem::Dependency
91
+ name: minitest
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ - !ruby/object:Gem::Dependency
105
+ name: sqlite3
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ description: HasSecureToken provides you an easily way to geneatre uniques random
119
+ tokens for any model in ruby on rails. **SecureRandom::base58** is used to generate
120
+ the 24-character unique token, so collisions are highly unlikely.
121
+ email:
122
+ - rjmaltamar@gmail.com
123
+ - joost@spacebabies.nl
124
+ executables: []
125
+ extensions: []
126
+ extra_rdoc_files: []
127
+ files:
128
+ - ".gitignore"
129
+ - ".travis.yml"
130
+ - Gemfile
131
+ - LICENSE.txt
132
+ - README.md
133
+ - Rakefile
134
+ - active_model_secure_token.gemspec
135
+ - gemfiles/rails3.gemfile
136
+ - gemfiles/rails4_0.gemfile
137
+ - gemfiles/rails4_1.gemfile
138
+ - gemfiles/rails4_2.gemfile
139
+ - lib/active_model_secure_token.rb
140
+ - lib/active_model_secure_token/version.rb
141
+ - lib/active_support/core_ext/securerandom.rb
142
+ - test/active_record_test.rb
143
+ - test/config/mongoid.yml
144
+ - test/config/schema.rb
145
+ - test/models/post.rb
146
+ - test/models/user.rb
147
+ - test/mongoid_test.rb
148
+ - test/securerandom_test.rb
149
+ - test/test_helper.rb
150
+ homepage: https://github.com/sign2pay/active_model_secure_token
151
+ licenses:
152
+ - MIT
153
+ metadata: {}
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 2.4.5
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Create uniques random tokens for any model in ruby on rails. Backport of
174
+ ActiveRecord::SecureToken 5 to AR 3.x and 4.x
175
+ test_files:
176
+ - test/active_record_test.rb
177
+ - test/config/mongoid.yml
178
+ - test/config/schema.rb
179
+ - test/models/post.rb
180
+ - test/models/user.rb
181
+ - test/mongoid_test.rb
182
+ - test/securerandom_test.rb
183
+ - test/test_helper.rb
184
+ has_rdoc: