obfuscated 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/obfuscated.rb +28 -25
  2. metadata +30 -49
@@ -1,22 +1,38 @@
1
1
  require 'digest/sha1'
2
2
  require 'active_record'
3
3
 
4
- # ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new( STDOUT )
5
-
6
4
  module Obfuscated
7
5
  mattr_accessor :salt
8
6
 
9
7
  def self.append_features(base)
10
8
  super
11
9
  base.extend(ClassMethods)
10
+ base.extend(Finder)
12
11
  end
13
12
 
14
13
  def self.supported?
15
- @@mysql_support ||= ActiveRecord::Base.connection.class.to_s.downcase.include?('mysql') ? true : false
14
+ db = ActiveRecord::Base.connection.class.to_s.downcase
15
+ @@db_support ||= db.include?('mysql') || db.include?('postgresql') ? true : false
16
16
  end
17
17
 
18
- module ClassMethods
18
+ module Finder
19
+ def find( *primary_key )
20
+ # Sale.find( '7e2d2c4da1b0' )
21
+ if primary_key.is_a?(String) && primary_key.length == 12
22
+ find_by_hashed_id( primary_key )
23
+
24
+ # Sale.includes(:store).find( '7e2d2c4da1b0' )
25
+ elsif primary_key.is_a?(Array) && primary_key.length == 1 && primary_key[0].is_a?(String) && primary_key[0].length == 12
26
+ find_by_hashed_id( primary_key[0] )
27
+
28
+ # Other queries
29
+ else
30
+ super
31
+ end
32
+ end
33
+ end
19
34
 
35
+ module ClassMethods
20
36
  def has_obfuscated_id( options={} )
21
37
  class_eval do
22
38
 
@@ -31,14 +47,18 @@ module Obfuscated
31
47
  return find_by_id(hash, options) unless Obfuscated::supported?
32
48
 
33
49
  # Update the conditions to use the hash calculation
34
- options.update(:conditions => ["SUBSTRING(SHA1(CONCAT('---',#{self.table_name}.id,'-WICKED-#{self.table_name}-#{Obfuscated::salt}')),1,12) = ?", hash])
35
-
50
+ db = ActiveRecord::Base.connection.class.to_s.downcase
51
+ if db.include?('postgresql')
52
+ options.update(:conditions => ["substring(encode(digest(concat('---',id::text,'-WICKED-#{self.table_name}-#{Obfuscated::salt}'), 'sha1'), 'hex'), 1, 12) = ?", hash])
53
+ elsif db.include?('mysql')
54
+ options.update(:conditions => ["SUBSTRING(SHA1(CONCAT('---',#{self.table_name}.id,'-WICKED-#{self.table_name}-#{Obfuscated::salt}')),1,12) = ?", hash])
55
+ end
36
56
  # Find it!
37
57
  first(options) or raise ActiveRecord::RecordNotFound, "Couldn't find #{self.class.to_s} with Hashed ID=#{hash}"
38
58
  end
39
59
  end
40
60
  end
41
-
61
+ # select substring(encode(digest(concat('---',to_char(id),'-WICKED-clouds-abc123'), 'sha1'), 'hex'), 1, 12) from clouds
42
62
  end
43
63
 
44
64
  module InstanceMethods
@@ -50,9 +70,7 @@ module Obfuscated
50
70
  return id unless Obfuscated::supported?
51
71
 
52
72
  # Use SHA1 to generate a consistent hash based on the id and the table name
53
- @hashed_id ||= Digest::SHA1.hexdigest(
54
- "---#{id}-WICKED-#{self.class.table_name}-#{Obfuscated::salt}"
55
- )[0..11]
73
+ @hashed_id ||= Digest::SHA1.hexdigest("---#{id}-WICKED-#{self.class.table_name}-#{Obfuscated::salt}")[0..11]
56
74
  end
57
75
 
58
76
  def to_param
@@ -62,18 +80,3 @@ module Obfuscated
62
80
  end
63
81
 
64
82
  ActiveRecord::Base.class_eval { include Obfuscated }
65
-
66
- # This will work for both ActiveRecord::Base as well as ActiveRecord::Relation.
67
- module ActiveRecord
68
- module FinderMethods
69
- def find( *args )
70
- if block_given?
71
- to_a.find { |*block_args| yield(*block_args) }
72
- elsif args.length == 1 && args[0].is_a?(String) && args[0].length == 12
73
- @klass.find_by_hashed_id( args[0] )
74
- else
75
- find_with_ids( *args )
76
- end
77
- end
78
- end
79
- end
metadata CHANGED
@@ -1,80 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: obfuscated
3
- version: !ruby/object:Gem::Version
4
- hash: 11
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 8
10
- version: 0.1.8
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Jon Collier
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-05-11 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: activerecord
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 2
31
- - 3
32
- - 0
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
33
21
  version: 2.3.0
34
22
  type: :runtime
35
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.3.0
36
30
  description: Obfuscate your autoincrementing primary key ids.
37
31
  email: github@joncollier.com
38
32
  executables: []
39
-
40
33
  extensions: []
41
-
42
34
  extra_rdoc_files: []
43
-
44
- files:
35
+ files:
45
36
  - lib/obfuscated.rb
46
37
  homepage: https://github.com/imnotquitejack/obfuscated
47
38
  licenses: []
48
-
49
39
  post_install_message:
50
40
  rdoc_options: []
51
-
52
- require_paths:
41
+ require_paths:
53
42
  - lib
54
- required_ruby_version: !ruby/object:Gem::Requirement
43
+ required_ruby_version: !ruby/object:Gem::Requirement
55
44
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
63
- required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
50
  none: false
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- hash: 3
69
- segments:
70
- - 0
71
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
72
55
  requirements: []
73
-
74
56
  rubyforge_project:
75
57
  rubygems_version: 1.8.24
76
58
  signing_key:
77
59
  specification_version: 3
78
60
  summary: Primary Key Obfuscation
79
61
  test_files: []
80
-