shixian-obfuscate_id 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 15717dfa4ebf50eb21ef397773901bcbd9c5e74fabe86310c91228608b4b488e
4
+ data.tar.gz: 5f63523142b284c2526e649fbe38d5188f02042657711d33d44d0ad4ee05597a
5
+ SHA512:
6
+ metadata.gz: 2bb8d1b5f753e780314d858d6c44e4d94563929d98af8b81066e322a790f49cd3871a00fc452ef68b490e34d27cbcdd3e74c34b9908f8bda46e173e4ee0e597e
7
+ data.tar.gz: 3323ef77de6b948c028317e2d9c8c0e6e5a20452444ee0aac086e16224c9d3d9ee9ff7cf71765f1ff974da50f6fce5d59f81a1b84911e64a8e5169092b97f31f
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Nathan Amick
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # obfuscate_id
2
+ [![Build Status](https://secure.travis-ci.org/namick/obfuscate_id.png)](http://travis-ci.org/namick/obfuscate_id) [![Dependency Status](https://gemnasium.com/namick/obfuscate_id.png)](https://gemnasium.com/namick/obfuscate_id) [![Code Climate](https://codeclimate.com/github/namick/obfuscate_id.png)](https://codeclimate.com/github/namick/obfuscate_id)
3
+
4
+ **Make your ActiveRecord ids non-obvious**
5
+
6
+ ![cat with sunglasses](http://i.imgur.com/kYOtUll.jpg)
7
+
8
+
9
+ obfuscate_id turns a URL like this:
10
+
11
+ http://example.com/users/3
12
+
13
+ into something like:
14
+
15
+ http://example.com/users/2356513904
16
+
17
+ Sequential ActiveRecord ids become non-sequential, random looking, numeric ids.
18
+
19
+ # post 7000
20
+ http://example.com/posts/5270192353
21
+ # post 7001
22
+ http://example.com/posts/7107163820
23
+ # post 7002
24
+ http://example.com/posts/3296163828
25
+
26
+ ## Why would you want this?
27
+
28
+ If your site is scaling well, you might not want to leak that you are getting 50 new posts a minute.
29
+
30
+ Or, for new websites, you may not want to give away how few people are signed up.
31
+
32
+ Every website has a third user, but that third user doesn't have to know he is the third user.
33
+
34
+ ## Features
35
+
36
+ * Extremely simple. A single line of code in the model turns it on.
37
+ * Transforms normal seqential ids into random-looking ten digit numerical strings.
38
+ * Gently masks resource ids while retaining a cleaner look than using an encrypted hash.
39
+ * No database changes or migrations are needed. The record is still stored in the database with its original id.
40
+ * Fast, no heavy calculation.
41
+
42
+
43
+ ## Installation
44
+
45
+ Add the gem to your Gemfile.
46
+
47
+ gem "obfuscate_id"
48
+
49
+ Run bundler.
50
+
51
+ bundle install
52
+
53
+ ## Usage
54
+
55
+ In your model, add a single line.
56
+
57
+ class Post < ActiveRecord::Base
58
+ obfuscate_id
59
+ end
60
+
61
+ ## Customization
62
+
63
+ If you want your obfuscated ids to be different than some other website using the same plugin, you can throw a random number (spin) at obfuscate_id to make it hash out unique ids for your app.
64
+
65
+ class Post < ActiveRecord::Base
66
+ obfuscate_id :spin => 89238723
67
+ end
68
+
69
+ ## How it works
70
+
71
+ obfuscate_id mixes up the ids in a simple, reversable hashing algorithm so that it can then automatically revert the hashed number back to the original id for record lookup without having to store a hash or tag in the database.
72
+
73
+ Each number from 0 to 9,999,999,999 is paired with one and only one number in that same range. That other number is paired back to the first. This is an example of a minimal perfect hash function. Within a set of ten billion numbers, it simply maps every number to a different 10 digit number, and back again.
74
+
75
+ Plain record ids are switched to the obfuscated id in the model's `to_param` method.
76
+
77
+ ActiveRecord reverses this obfuscated id back to the plain id before building the database query. This means no migrations or changes to the database. Yay!
78
+
79
+ ## Limitations
80
+
81
+ * This is not security. obfuscate_id was created to lightly mask record id numbers for the casual user. If you need to really secure your database ids (hint, you probably don't), you need to use real encryption like AES.
82
+ * To properly generate obfuscated urls, make sure you trigger the model's `to_param` method by passing in the whole object rather than just the id; do this: `post_path(@post)` not this: `post_path(@post.id)`.
83
+
84
+ ## Versions
85
+
86
+ This is tested with Rails 4.2.0. For other versions of Rails, please see [the releases](https://github.com/namick/obfuscate_id/releases).
87
+
88
+ If you are trying to get it to work with a different version of rails that is not tested, let me know in [the issues](https://github.com/namick/obfuscate_id/issues)
89
+
90
+ ## Development
91
+
92
+ To run the tests, first clone the repo and run bundler:
93
+
94
+ git clone git@github.com:namick/obfuscate_id.git
95
+ cd obfuscate_id
96
+ bundle install
97
+
98
+ Run the tests
99
+
100
+ bundle exec rspec spec
101
+
102
+ Or have Guard run them continuously
103
+
104
+ bundle exec guard
105
+
106
+ ## Contributing
107
+
108
+ 1. Fork it
109
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
110
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
111
+ 4. Push to the branch (`git push origin my-new-feature`)
112
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ObfuscateId'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
25
+ require 'rspec/core/rake_task'
26
+
27
+ RSpec::Core::RakeTask.new(:spec)
28
+
29
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module ObfuscateId
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,85 @@
1
+ module ObfuscateId
2
+ def obfuscate_id(options = {})
3
+ require 'scatter_swap'
4
+
5
+ extend ClassMethods
6
+ include InstanceMethods
7
+ cattr_accessor :obfuscate_id_spin
8
+ self.obfuscate_id_spin = (options[:spin] || obfuscate_id_default_spin)
9
+ end
10
+
11
+ def self.hide(id, spin)
12
+ ScatterSwap.hash(id, spin)
13
+ end
14
+
15
+ def self.show(id, spin)
16
+ ScatterSwap.reverse_hash(id, spin)
17
+ end
18
+
19
+ module ClassMethods
20
+ def find(*args)
21
+ scope = args.slice!(0)
22
+ options = args.slice!(0) || {}
23
+ if has_obfuscated_id? && !options[:no_obfuscated_id]
24
+ if scope.is_a?(Array)
25
+ scope.map! {|a| deobfuscate_id(a).to_i}
26
+ else
27
+ scope = deobfuscate_id(scope)
28
+ end
29
+ end
30
+ super(scope)
31
+ end
32
+
33
+ def has_obfuscated_id?
34
+ true
35
+ end
36
+
37
+ def deobfuscate_id(obfuscated_id)
38
+ ObfuscateId.show(obfuscated_id, self.obfuscate_id_spin)
39
+ end
40
+
41
+ # Generate a default spin from the Model name
42
+ # This makes it easy to drop obfuscate_id onto any model
43
+ # and produce different obfuscated ids for different models
44
+ def obfuscate_id_default_spin
45
+ alphabet = Array("a".."z")
46
+ number = name.split("").collect do |char|
47
+ alphabet.index(char)
48
+ end
49
+
50
+ number.shift(12).join.to_i
51
+ end
52
+ end
53
+
54
+ module InstanceMethods
55
+ def to_param
56
+ ObfuscateId.hide(self.id, self.class.obfuscate_id_spin)
57
+ end
58
+
59
+ # Override ActiveRecord::Persistence#reload
60
+ # passing in an options flag with { no_obfuscated_id: true }
61
+ def reload(options = nil)
62
+ options = (options || {}).merge(no_obfuscated_id: true)
63
+
64
+ clear_aggregation_cache
65
+ clear_association_cache
66
+
67
+ fresh_object =
68
+ if options && options[:lock]
69
+ self.class.unscoped { self.class.lock(options[:lock]).find(id, options) }
70
+ else
71
+ self.class.unscoped { self.class.find(id, options) }
72
+ end
73
+
74
+ @attributes = fresh_object.instance_variable_get('@attributes')
75
+ @new_record = false
76
+ self
77
+ end
78
+
79
+ def deobfuscate_id(obfuscated_id)
80
+ self.class.deobfuscate_id(obfuscated_id)
81
+ end
82
+ end
83
+ end
84
+
85
+ ActiveRecord::Base.extend ObfuscateId
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shixian-obfuscate_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Amick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: scatter_swap
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
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: rspec-rails
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: capybara
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: guard-rspec
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
+ - !ruby/object:Gem::Dependency
98
+ name: guard-spork
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rb-inotify
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: launchy
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Make your ActiveRecord IDs non-obvious
154
+ email:
155
+ - github@nathanamick.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - MIT-LICENSE
161
+ - README.md
162
+ - Rakefile
163
+ - lib/obfuscate_id.rb
164
+ - lib/obfuscate_id/version.rb
165
+ homepage: https://github.com/shixiancom/obfuscate_id
166
+ licenses:
167
+ - MIT
168
+ - GPL-2
169
+ metadata: {}
170
+ post_install_message:
171
+ rdoc_options: []
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ requirements: []
185
+ rubygems_version: 3.5.22
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: Mask ActiveRecord IDs
189
+ test_files: []