shixian-obfuscate_id 0.0.4 → 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 +4 -4
- data/README.md +10 -10
- data/lib/obfuscate_id/version.rb +1 -1
- data/lib/obfuscate_id.rb +20 -9
- metadata +33 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15717dfa4ebf50eb21ef397773901bcbd9c5e74fabe86310c91228608b4b488e
|
4
|
+
data.tar.gz: 5f63523142b284c2526e649fbe38d5188f02042657711d33d44d0ad4ee05597a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bb8d1b5f753e780314d858d6c44e4d94563929d98af8b81066e322a790f49cd3871a00fc452ef68b490e34d27cbcdd3e74c34b9908f8bda46e173e4ee0e597e
|
7
|
+
data.tar.gz: 3323ef77de6b948c028317e2d9c8c0e6e5a20452444ee0aac086e16224c9d3d9ee9ff7cf71765f1ff974da50f6fce5d59f81a1b84911e64a8e5169092b97f31f
|
data/README.md
CHANGED
@@ -13,7 +13,7 @@ obfuscate_id turns a URL like this:
|
|
13
13
|
into something like:
|
14
14
|
|
15
15
|
http://example.com/users/2356513904
|
16
|
-
|
16
|
+
|
17
17
|
Sequential ActiveRecord ids become non-sequential, random looking, numeric ids.
|
18
18
|
|
19
19
|
# post 7000
|
@@ -22,7 +22,7 @@ Sequential ActiveRecord ids become non-sequential, random looking, numeric ids.
|
|
22
22
|
http://example.com/posts/7107163820
|
23
23
|
# post 7002
|
24
24
|
http://example.com/posts/3296163828
|
25
|
-
|
25
|
+
|
26
26
|
## Why would you want this?
|
27
27
|
|
28
28
|
If your site is scaling well, you might not want to leak that you are getting 50 new posts a minute.
|
@@ -33,7 +33,7 @@ Every website has a third user, but that third user doesn't have to know he is t
|
|
33
33
|
|
34
34
|
## Features
|
35
35
|
|
36
|
-
*
|
36
|
+
* Extremely simple. A single line of code in the model turns it on.
|
37
37
|
* Transforms normal seqential ids into random-looking ten digit numerical strings.
|
38
38
|
* Gently masks resource ids while retaining a cleaner look than using an encrypted hash.
|
39
39
|
* No database changes or migrations are needed. The record is still stored in the database with its original id.
|
@@ -52,7 +52,7 @@ Run bundler.
|
|
52
52
|
|
53
53
|
## Usage
|
54
54
|
|
55
|
-
In your model, add a single line.
|
55
|
+
In your model, add a single line.
|
56
56
|
|
57
57
|
class Post < ActiveRecord::Base
|
58
58
|
obfuscate_id
|
@@ -81,6 +81,12 @@ ActiveRecord reverses this obfuscated id back to the plain id before building th
|
|
81
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
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
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
|
+
|
84
90
|
## Development
|
85
91
|
|
86
92
|
To run the tests, first clone the repo and run bundler:
|
@@ -89,12 +95,6 @@ To run the tests, first clone the repo and run bundler:
|
|
89
95
|
cd obfuscate_id
|
90
96
|
bundle install
|
91
97
|
|
92
|
-
Change to the dummy rails app and load the test database
|
93
|
-
|
94
|
-
cd spec/dummy
|
95
|
-
bundle exec rake db:test:load
|
96
|
-
cd -
|
97
|
-
|
98
98
|
Run the tests
|
99
99
|
|
100
100
|
bundle exec rspec spec
|
data/lib/obfuscate_id/version.rb
CHANGED
data/lib/obfuscate_id.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
module ObfuscateId
|
2
|
-
|
3
2
|
def obfuscate_id(options = {})
|
4
3
|
require 'scatter_swap'
|
5
4
|
|
6
|
-
extend ClassMethods
|
5
|
+
extend ClassMethods
|
7
6
|
include InstanceMethods
|
8
7
|
cattr_accessor :obfuscate_id_spin
|
9
8
|
self.obfuscate_id_spin = (options[:spin] || obfuscate_id_default_spin)
|
@@ -17,7 +16,6 @@ module ObfuscateId
|
|
17
16
|
ScatterSwap.reverse_hash(id, spin)
|
18
17
|
end
|
19
18
|
|
20
|
-
|
21
19
|
module ClassMethods
|
22
20
|
def find(*args)
|
23
21
|
scope = args.slice!(0)
|
@@ -44,13 +42,13 @@ module ObfuscateId
|
|
44
42
|
# This makes it easy to drop obfuscate_id onto any model
|
45
43
|
# and produce different obfuscated ids for different models
|
46
44
|
def obfuscate_id_default_spin
|
47
|
-
alphabet = Array("a".."z")
|
45
|
+
alphabet = Array("a".."z")
|
48
46
|
number = name.split("").collect do |char|
|
49
47
|
alphabet.index(char)
|
50
48
|
end
|
49
|
+
|
51
50
|
number.shift(12).join.to_i
|
52
51
|
end
|
53
|
-
|
54
52
|
end
|
55
53
|
|
56
54
|
module InstanceMethods
|
@@ -58,11 +56,24 @@ module ObfuscateId
|
|
58
56
|
ObfuscateId.hide(self.id, self.class.obfuscate_id_spin)
|
59
57
|
end
|
60
58
|
|
61
|
-
#
|
62
|
-
#
|
59
|
+
# Override ActiveRecord::Persistence#reload
|
60
|
+
# passing in an options flag with { no_obfuscated_id: true }
|
63
61
|
def reload(options = nil)
|
64
|
-
options = (options || {}).merge(:
|
65
|
-
|
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
|
66
77
|
end
|
67
78
|
|
68
79
|
def deobfuscate_id(obfuscated_id)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shixian-obfuscate_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Amick
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 4.
|
33
|
+
version: 4.2.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 4.
|
40
|
+
version: 4.2.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sqlite3
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,6 +122,34 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
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'
|
125
153
|
description: Make your ActiveRecord IDs non-obvious
|
126
154
|
email:
|
127
155
|
- github@nathanamick.com
|