has_secure_uuid 0.1.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 +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +80 -0
- data/Rakefile +16 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/has_secure_uuid.gemspec +27 -0
- data/lib/has_secure_uuid.rb +45 -0
- data/lib/has_secure_uuid/version.rb +3 -0
- data/test/has_secure_uuid_test.rb +31 -0
- data/test/models/user.rb +4 -0
- data/test/schema.rb +6 -0
- data/test/test_helper.rb +25 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bbf5ec6244c537e9bbc2fcf9354d60b6e3f8682c8925c4b58eced5082951c5dd
|
4
|
+
data.tar.gz: 7cc9b0b833ec9fdba4172054e3834ccd1a29ed42d10b90add8194bae13d89205
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9e4416d7f7f2eb7e5cb73bc54269deffba876475e307291082b3025dcddfdc10ee5b85fce1c182e6a27520f2e5718e80fdd8fc2abbfc9eb130be3f4b75e39723
|
7
|
+
data.tar.gz: 1be6c18712a3e3c4816f01112d4ad6a2fc371b90dfaddc4c1b24c153de92b1479da5bd672920bdb20d870107c10ec9faeda21acc76e3117ec43eaab415e686c0
|
data/.gitignore
ADDED
@@ -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
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Sander Tuin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# HasSecureUuid
|
2
|
+
|
3
|
+
HasSecureUuid provides an easy way to generate uniques random uuid's for any model in ruby on rails. **SecureRandom::uuid** is used to generate the unique id's, 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 with this even more unlikely scenario.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'has_secure_uuid'
|
12
|
+
|
13
|
+
And then run:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install has_secure_uuid
|
20
|
+
|
21
|
+
## Setting your Model
|
22
|
+
|
23
|
+
The first step is to generate a migration in order to add the token key field.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
rails g migration AddidentifierToUsers identifier:string
|
27
|
+
=>
|
28
|
+
invoke active_record
|
29
|
+
create db/migrate/20150424010931_add_identifier_to_users.rb
|
30
|
+
```
|
31
|
+
|
32
|
+
Then run `rake db:migrate` in order to update users table in the database. The next step is to add `has_secure_uuid`
|
33
|
+
to the model:
|
34
|
+
```ruby
|
35
|
+
# Schema: User(identifier:string, uuid:string)
|
36
|
+
class User < ActiveRecord::Base
|
37
|
+
has_secure_uuid
|
38
|
+
end
|
39
|
+
|
40
|
+
user = User.new
|
41
|
+
user.save
|
42
|
+
user.identifier # => "6c3d256c-aaa7-443a-a16b-75a99ecde277"
|
43
|
+
user.regenerate_identifier # => true
|
44
|
+
```
|
45
|
+
|
46
|
+
To use a custom column to store the uuid field you can specify the column_name option. See example above (e.g: uuid):
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
# Schema: User(identifier:string, uuid:string)
|
50
|
+
class User < ActiveRecord::Base
|
51
|
+
has_secure_uuid :uuid
|
52
|
+
end
|
53
|
+
|
54
|
+
user = User.new
|
55
|
+
user.save
|
56
|
+
user.uuid # => "6c3d256c-aaa7-443a-a16b-75a99ecde277"
|
57
|
+
user.regenerate_uuid # => true
|
58
|
+
```
|
59
|
+
|
60
|
+
## Running tests
|
61
|
+
|
62
|
+
Running
|
63
|
+
|
64
|
+
```shell
|
65
|
+
$ rake test
|
66
|
+
```
|
67
|
+
|
68
|
+
Should return
|
69
|
+
|
70
|
+
```shell
|
71
|
+
3 runs, 5 assertions, 0 failures, 0 errors, 0 skips
|
72
|
+
```
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it ( https://github.com/sndrgrdn/has_secure_uuid/fork )
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
task :test do
|
6
|
+
Dir.chdir('test')
|
7
|
+
end
|
8
|
+
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << '../lib'
|
11
|
+
t.libs << '../test'
|
12
|
+
t.test_files = FileList['*_test.rb']
|
13
|
+
t.verbose = false
|
14
|
+
end
|
15
|
+
|
16
|
+
task default: :test
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'has_secure_uuid'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'has_secure_uuid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'has_secure_uuid'
|
8
|
+
spec.version = HasSecureUuid::VERSION
|
9
|
+
spec.authors = ['Sander Tuin']
|
10
|
+
spec.email = ['sandergarden@gmail.com']
|
11
|
+
spec.summary = 'Create uniques random uuids for any model in ruby on rails.'
|
12
|
+
spec.description = 'HasSecureUuid provides you an easily way to generate random uuids for any model in ruby on rails. **SecureRandom::uuid** is used to generate a unique uuid, so collisions are highly unlikely.'
|
13
|
+
spec.homepage = 'https://github.com/sndrgrdn/has_secure_uuid'
|
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 'activerecord', '>= 3.0'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'minitest'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
spec.add_development_dependency 'sqlite3'
|
27
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module SecureUuid
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# Example using has_secure_uuid
|
11
|
+
#
|
12
|
+
# # Schema: User(identifier:string, uuid:string)
|
13
|
+
# class User < ActiveRecord::Base
|
14
|
+
# has_secure_uuid
|
15
|
+
# has_secure_uuid :uuid
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# user = User.new
|
19
|
+
# user.save
|
20
|
+
# user.identifier # => "93712506-fe9e-4ae9-a713-53c67cd9bccc"
|
21
|
+
# user.uuid # => "245f9ae1-9f8d-4a0e-817d-8ba3e4d663c1"
|
22
|
+
# user.regenerate_identifier # => true
|
23
|
+
# user.regenerate_uuid # => true
|
24
|
+
#
|
25
|
+
# Note that it's still possible to generate a race condition in the database in the same way that
|
26
|
+
# <tt>validates_uniqueness_of</tt> can. You're encouraged to add a unique index in the database to deal
|
27
|
+
# with this even more unlikely scenario.
|
28
|
+
def has_secure_uuid(attribute = :identifier)
|
29
|
+
# Load securerandom only when has_secure_token is used.
|
30
|
+
define_method("regenerate_#{attribute}") do
|
31
|
+
update_attributes attribute => self.class.generate_unique_uuid
|
32
|
+
end
|
33
|
+
before_create do
|
34
|
+
send("#{attribute}=", self.class.generate_unique_uuid) unless send("#{attribute}?")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def generate_unique_uuid
|
39
|
+
SecureRandom.uuid
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
ActiveRecord::Base.send(:include, ActiveRecord::SecureUuid)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SecureUuidTest < MiniTest::Test
|
4
|
+
def setup
|
5
|
+
@user = User.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_uuid_values_are_generated_for_specified_attributes_and_persisted_on_save
|
9
|
+
@user.save
|
10
|
+
refute_nil @user.identifier
|
11
|
+
refute_nil @user.uuid
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_regenerating_the_secure_uuid
|
15
|
+
@user.save
|
16
|
+
old_identifier = @user.identifier
|
17
|
+
old_uuid = @user.uuid
|
18
|
+
@user.regenerate_identifier
|
19
|
+
@user.regenerate_uuid
|
20
|
+
|
21
|
+
refute_equal @user.identifier, old_identifier
|
22
|
+
refute_equal @user.uuid, old_uuid
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_uuid_value_not_overwritten_when_present
|
26
|
+
@user.identifier = '6c3d256c-aaa7-443a-a16b-75a99ecde277'
|
27
|
+
@user.save
|
28
|
+
|
29
|
+
assert_equal @user.identifier, '6c3d256c-aaa7-443a-a16b-75a99ecde277'
|
30
|
+
end
|
31
|
+
end
|
data/test/models/user.rb
ADDED
data/test/schema.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
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 'has_secure_uuid'
|
9
|
+
require 'minitest/autorun'
|
10
|
+
require 'minitest/unit'
|
11
|
+
|
12
|
+
Dir['models/*.rb'].each { |file| require file }
|
13
|
+
|
14
|
+
def assert_not_nil(exp, msg = nil)
|
15
|
+
msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
|
16
|
+
assert(!exp.nil?, msg)
|
17
|
+
end
|
18
|
+
|
19
|
+
DB_FILE = 'tmp/test_db'.freeze
|
20
|
+
FileUtils.mkdir_p File.dirname(DB_FILE)
|
21
|
+
FileUtils.rm_f DB_FILE
|
22
|
+
|
23
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: DB_FILE
|
24
|
+
|
25
|
+
load 'schema.rb'
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_secure_uuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sander Tuin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
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: rake
|
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: sqlite3
|
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
|
+
description: HasSecureUuid provides you an easily way to generate random uuids for
|
84
|
+
any model in ruby on rails. **SecureRandom::uuid** is used to generate a unique
|
85
|
+
uuid, so collisions are highly unlikely.
|
86
|
+
email:
|
87
|
+
- sandergarden@gmail.com
|
88
|
+
executables:
|
89
|
+
- console
|
90
|
+
- setup
|
91
|
+
extensions: []
|
92
|
+
extra_rdoc_files: []
|
93
|
+
files:
|
94
|
+
- ".gitignore"
|
95
|
+
- ".travis.yml"
|
96
|
+
- Gemfile
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- has_secure_uuid.gemspec
|
103
|
+
- lib/has_secure_uuid.rb
|
104
|
+
- lib/has_secure_uuid/version.rb
|
105
|
+
- test/has_secure_uuid_test.rb
|
106
|
+
- test/models/user.rb
|
107
|
+
- test/schema.rb
|
108
|
+
- test/test_helper.rb
|
109
|
+
homepage: https://github.com/sndrgrdn/has_secure_uuid
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.7.3
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Create uniques random uuids for any model in ruby on rails.
|
133
|
+
test_files:
|
134
|
+
- test/has_secure_uuid_test.rb
|
135
|
+
- test/models/user.rb
|
136
|
+
- test/schema.rb
|
137
|
+
- test/test_helper.rb
|