encrypted_id 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p194@encrypted_id"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.15.8 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ if [[ $- == *i* ]] # check for interactive shells
29
+ then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
30
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
31
+ fi
32
+ else
33
+ # If the environment file has not yet been created, use the RVM CLI to select.
34
+ rvm --create use "$environment_id" || {
35
+ echo "Failed to create RVM environment '${environment_id}'."
36
+ return 1
37
+ }
38
+ fi
39
+
40
+ # If you use bundler, this might be useful to you:
41
+ # if [[ -s Gemfile ]] && {
42
+ # ! builtin command -v bundle >/dev/null ||
43
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
44
+ # }
45
+ # then
46
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
47
+ # gem install bundler
48
+ # fi
49
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
50
+ # then
51
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
52
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'http://rubygems.org'
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem 'activesupport', '>= 2.3.5'
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem 'rspec', '~> 2.8.0'
10
+ gem 'bundler', '~> 1.2.0'
11
+ gem 'jeweler', '~> 1.8.4'
12
+ gem 'sqlite3'
13
+ end
14
+
15
+ gem 'activerecord', '>= 3.0.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.8)
5
+ activesupport (= 3.2.8)
6
+ builder (~> 3.0.0)
7
+ activerecord (3.2.8)
8
+ activemodel (= 3.2.8)
9
+ activesupport (= 3.2.8)
10
+ arel (~> 3.0.2)
11
+ tzinfo (~> 0.3.29)
12
+ activesupport (3.2.8)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
15
+ arel (3.0.2)
16
+ builder (3.0.3)
17
+ diff-lcs (1.1.3)
18
+ git (1.2.5)
19
+ i18n (0.6.1)
20
+ jeweler (1.8.4)
21
+ bundler (~> 1.0)
22
+ git (>= 1.2.5)
23
+ rake
24
+ rdoc
25
+ json (1.7.5)
26
+ multi_json (1.3.6)
27
+ rake (0.9.2.2)
28
+ rdoc (3.12)
29
+ json (~> 1.4)
30
+ rspec (2.8.0)
31
+ rspec-core (~> 2.8.0)
32
+ rspec-expectations (~> 2.8.0)
33
+ rspec-mocks (~> 2.8.0)
34
+ rspec-core (2.8.0)
35
+ rspec-expectations (2.8.0)
36
+ diff-lcs (~> 1.1.2)
37
+ rspec-mocks (2.8.0)
38
+ sqlite3 (1.3.6)
39
+ tzinfo (0.3.33)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ activerecord (>= 3.0.0)
46
+ bundler (~> 1.2.0)
47
+ jeweler (~> 1.8.4)
48
+ rspec (~> 2.8.0)
49
+ sqlite3
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Nils Caspar
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,48 @@
1
+ # encrypted_id
2
+
3
+ Sometimes you don't want your users to see the actual ID of your databases entries. This gem allows you to hide the ID.
4
+
5
+ encrypted_id turns a URL like this:
6
+
7
+ http://www.example.com/users/15
8
+
9
+ into something like:
10
+
11
+ http://www.example.com/users/1e8644f924812bec506b116ff14368c8
12
+
13
+ encrypted_id uses AES-256 (CBC) with a configurable key (per model) to do so.
14
+
15
+ ## Installation
16
+
17
+ Add the gem to your Gemfile:
18
+
19
+ gem 'encrypted_id'
20
+
21
+ Run bundler:
22
+
23
+ bundle install
24
+
25
+ ## Usage
26
+
27
+ In your model, add the following line:
28
+
29
+ class User < ActiveRecord::Base
30
+ encrypted_id key: '5gA6lgr5g3GOg7EOQ1caYQ'
31
+ end
32
+
33
+ Replace the "key" with a random value (should be different for every model).
34
+
35
+ ## Contributing to encrypted_id
36
+
37
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
38
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
39
+ * Fork the project.
40
+ * Start a feature/bugfix branch.
41
+ * Commit and push until you are happy with your contribution.
42
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
43
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
44
+
45
+ ## Copyright
46
+
47
+ Copyright (c) 2012 Nils Caspar. See LICENSE.txt for
48
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "encrypted_id"
18
+ gem.homepage = "http://github.com/pencil/encrypted_id"
19
+ gem.license = "MIT"
20
+ gem.summary = "Allows you to encrypt the ID of your ActiveRecord model."
21
+ gem.description = "Sometimes you don't want your users to see the actual ID of your databases entries. This gem allows you to hide the ID."
22
+ gem.email = "ncaspar@me.com"
23
+ gem.authors = ["Nils Caspar"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,58 @@
1
+ require 'openssl'
2
+ require 'active_record'
3
+
4
+ module EncryptedId
5
+
6
+ CIPHER_NAME = 'aes-256-cbc'
7
+ CIPHER_IV = ['1e5673b2572af26a8364a50af84c7d2a'].pack('H*')
8
+
9
+ def encrypted_id(options = {})
10
+ extend ClassMethods
11
+ include InstanceMethods
12
+ cattr_accessor :encrypted_id_key
13
+ self.encrypted_id_key = Digest::SHA256.digest(options[:key] || encrypted_id_default_key)
14
+ end
15
+
16
+ def self.decrypt(key, id)
17
+ c = OpenSSL::Cipher::Cipher.new(CIPHER_NAME).decrypt
18
+ c.iv = CIPHER_IV
19
+ c.key = key
20
+ c.update([id].pack('H*')) + c.final
21
+ end
22
+
23
+ def self.encrypt(key, id)
24
+ c = OpenSSL::Cipher::Cipher.new(CIPHER_NAME).encrypt
25
+ c.iv = CIPHER_IV
26
+ c.key = key
27
+ (c.update("#{id}") + c.final).unpack('H*')[0]
28
+ end
29
+
30
+ module ClassMethods
31
+ def find(*args)
32
+ if has_encrypted_id?
33
+ begin
34
+ args[0] = EncryptedId.decrypt(encrypted_id_key, "#{args[0]}")
35
+ rescue OpenSSL::Cipher::CipherError
36
+ raise ActiveRecord::RecordNotFound.new("Could not decrypt ID #{args[0]}")
37
+ end
38
+ end
39
+ super(*args)
40
+ end
41
+
42
+ def has_encrypted_id?
43
+ true
44
+ end
45
+
46
+ def encrypted_id_default_key
47
+ name
48
+ end
49
+ end
50
+
51
+ module InstanceMethods
52
+ def to_param
53
+ EncryptedId.encrypt(self.class.encrypted_id_key, self.id)
54
+ end
55
+ end
56
+ end
57
+
58
+ ActiveRecord::Base.extend EncryptedId
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'EncryptedId' do
4
+ describe 'in the User model' do
5
+ before(:each) do
6
+ @user = User.new
7
+ end
8
+
9
+ it 'should give us the encrypted ID via to_param' do
10
+ @user.id = 15
11
+ @user.to_param.should == '1e8644f924812bec506b116ff14368c8'
12
+ end
13
+
14
+ it 'should be possible to find an entry by the encrypred id' do
15
+ @user.id = 8
16
+ @user.save!
17
+ User.find('da9f98cd7c3eb2b0f0e88cc8daeb222c').id.should == 8
18
+ end
19
+
20
+ it 'should throw an exception if we try to find an entry by the real ID' do
21
+ @user.id = 1
22
+ @user.save!
23
+ expect { User.find 1 }.to raise_error(ActiveRecord::RecordNotFound)
24
+ end
25
+ end
26
+
27
+ describe 'in the Animal model' do
28
+ before(:each) do
29
+ @animal = Animal.new
30
+ end
31
+
32
+ it 'should give us the encrypted ID via to_param' do
33
+ @animal.id = 15
34
+ @animal.to_param.should == '2f71ba22a8e6975db0f13b7e3db6d9cd'
35
+ end
36
+
37
+ it 'should be possible to find an entry by the encrypred id' do
38
+ @animal.id = 8
39
+ @animal.save!
40
+ Animal.find('6916a1adba452d3fffde6e444ae1ad3a').id.should == 8
41
+ end
42
+
43
+ it 'should throw an exception if we try to find an entry by the real ID' do
44
+ @animal.id = 1
45
+ @animal.save!
46
+ expect { Animal.find 1 }.to raise_error(ActiveRecord::RecordNotFound)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'encrypted_id'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+ c = ActiveRecord::Base.establish_connection(
12
+ :adapter => 'sqlite3',
13
+ :database => 'spec/test.sqlite3'
14
+ )
15
+ [:users, :animals].each do |table|
16
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS '#{table}'")
17
+ ActiveRecord::Base.connection.create_table(table) do |t|
18
+ t.timestamps
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ class Animal < ActiveRecord::Base
2
+ encrypted_id :key => 'lbI3iFyKO9zsK1U8ppG5at'
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ encrypted_id :key => '5gA6lgr5g3GOg7EOQ1caYQ'
3
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encrypted_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nils Caspar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.8.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ - !ruby/object:Gem::Dependency
79
+ name: sqlite3
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Sometimes you don't want your users to see the actual ID of your databases
95
+ entries. This gem allows you to hide the ID.
96
+ email: ncaspar@me.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.md
102
+ files:
103
+ - .document
104
+ - .rspec
105
+ - .rvmrc
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - VERSION
112
+ - lib/encrypted_id.rb
113
+ - spec/encrypted_id_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/models/animal.rb
116
+ - spec/support/models/user.rb
117
+ homepage: http://github.com/pencil/encrypted_id
118
+ licenses:
119
+ - MIT
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ segments:
131
+ - 0
132
+ hash: -4444389274485543368
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 1.8.24
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: Allows you to encrypt the ID of your ActiveRecord model.
145
+ test_files: []