url_encrypt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/Manifest +12 -0
- data/README +35 -0
- data/Rakefile +35 -0
- data/init.rb +4 -0
- data/install.rb +1 -0
- data/lib/url_encrypt.rb +111 -0
- data/tasks/url_encrypt_tasks.rake +4 -0
- data/test/test_helper.rb +7 -0
- data/test/url_encrypt_test.rb +58 -0
- data/uninstall.rb +1 -0
- data/url_encrypt.gemspec +31 -0
- metadata +74 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
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/Manifest
ADDED
data/README
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
UrlEncrypt
|
2
|
+
=================================================================================================================
|
3
|
+
|
4
|
+
This plugin provides the ability to encrypt a column of the database table which you can safely expose in the URL.
|
5
|
+
Provides handy method to make DB calls (Rails way of calling ya know !)
|
6
|
+
|
7
|
+
After Installation you can put the encryptor key in your environment.rb file:
|
8
|
+
|
9
|
+
UrlEncrypt.encryptors("abcdefghijklmnop", "mnbkjhkhkhkhkhkjhkjh") -> so that KEY and IV for Cipher encryption are different
|
10
|
+
|
11
|
+
OR
|
12
|
+
|
13
|
+
UrlEncrypt.encryptors("abcdefghijklmnop") -> so that KEY and IV for Cipher encryption are same
|
14
|
+
|
15
|
+
OR
|
16
|
+
|
17
|
+
NOTHING -> so that plugin takes care of hanving its own
|
18
|
+
|
19
|
+
|
20
|
+
Example
|
21
|
+
=======
|
22
|
+
class Book < ActiveRecord::Base
|
23
|
+
column :id, :integer
|
24
|
+
column :title, :string
|
25
|
+
|
26
|
+
encrypted :with => :title
|
27
|
+
end
|
28
|
+
|
29
|
+
Ypu have handy methods:
|
30
|
+
|
31
|
+
Book.find_by_encrypted_title('encrypted string')
|
32
|
+
|
33
|
+
Book.find_by_encrypted_title('encrypted string', :conditions => ["any other condition can go here"])
|
34
|
+
|
35
|
+
Copyright (c) 2008 (Amit Kumar), released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'echoe'
|
6
|
+
|
7
|
+
desc 'Default: run unit tests.'
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
desc 'Test the url_encrypt plugin.'
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate documentation for the url_encrypt plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'UrlEncrypt'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
Echoe.new('url_encrypt', '0.1.0') do |p|
|
27
|
+
p.description = "Encrypt your URLs"
|
28
|
+
p.url = "http://github.com/toamitkumar/url_encrypt"
|
29
|
+
p.author = "Amit Kumar"
|
30
|
+
p.email = "toamitkumar@gmail.com"
|
31
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
32
|
+
p.development_dependencies = []
|
33
|
+
end
|
34
|
+
|
35
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/url_encrypt.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'uri'
|
3
|
+
require 'base64'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'activerecord'
|
6
|
+
|
7
|
+
module UrlEncrypt
|
8
|
+
def self.cipher
|
9
|
+
@@cipher ||= OpenSSL::Cipher::Cipher.new('RC2')
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.encryptors key, iv=nil
|
13
|
+
@@key ||= key
|
14
|
+
@@iv ||= (iv || key)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.key
|
18
|
+
@@key ||= "abcdefghijklmnop"
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.iv
|
22
|
+
@@iv ||= @@key
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.included(base)
|
26
|
+
base.extend(ClassMethods)
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def self.extended(base)
|
31
|
+
class << base
|
32
|
+
self.instance_eval do
|
33
|
+
attr_accessor :encryptable_attribute
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def encrypted(options={})
|
39
|
+
self.encryptable_attribute = options[:with].to_s if options[:with]
|
40
|
+
include InstanceMethods
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module InstanceMethods
|
45
|
+
def self.included(base)
|
46
|
+
base.extend ClassMethods
|
47
|
+
end
|
48
|
+
|
49
|
+
def _encrypted_attribute
|
50
|
+
@_encrypted_attribute ||= self.encrypt(self.send(self.class.encryptable_attribute))
|
51
|
+
end
|
52
|
+
|
53
|
+
def _encrypted_attribute= (encrypted_attribute)
|
54
|
+
@_encrypted_attribute = encrypted_attribute
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_param
|
58
|
+
self.class.encryptable_attribute ? self._encrypted_attribute : id.to_s
|
59
|
+
end
|
60
|
+
|
61
|
+
def encrypt(attribute_val)
|
62
|
+
UrlEncrypt.cipher.encrypt
|
63
|
+
UrlEncrypt.cipher.key, UrlEncrypt.cipher.iv = UrlEncrypt.key, UrlEncrypt.iv
|
64
|
+
URI.encode((UrlEncrypt.cipher.update(attribute_val.to_s) + UrlEncrypt.cipher.final).unpack("H*").to_s)
|
65
|
+
end
|
66
|
+
|
67
|
+
module ClassMethods
|
68
|
+
def method_missing(method_id, *args)
|
69
|
+
if match = /^find_(all_by|by)_encrypted_([_a-zA-Z]\w*)$/.match(method_id.to_s)
|
70
|
+
finder = determine_finder(match)
|
71
|
+
attribute_names = extract_attribute_names_from_match(match)
|
72
|
+
super unless all_attributes_exists?(attribute_names)
|
73
|
+
|
74
|
+
self.class_eval %{
|
75
|
+
def self.#{method_id}(*args)
|
76
|
+
encrypted_str = args[0]
|
77
|
+
raise(ActiveRecord::RecordNotFound) if encrypted_str.nil?
|
78
|
+
decrypted_str = decrypt(encrypted_str)
|
79
|
+
|
80
|
+
args[0] = decrypted_str
|
81
|
+
|
82
|
+
options = args.extract_options!
|
83
|
+
attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args)
|
84
|
+
finder_options = { :conditions => attributes }
|
85
|
+
validate_find_options(options)
|
86
|
+
set_readonly_option!(options)
|
87
|
+
|
88
|
+
if options[:conditions]
|
89
|
+
with_scope(:find => finder_options) do
|
90
|
+
ActiveSupport::Deprecation.silence { self.send(:#{finder}, options) }
|
91
|
+
end
|
92
|
+
else
|
93
|
+
ActiveSupport::Deprecation.silence { self.send(:#{finder}, options.merge(finder_options)) }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
}, __FILE__, __LINE__
|
97
|
+
self.send(method_id, *args)
|
98
|
+
else
|
99
|
+
super
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
protected
|
104
|
+
def decrypt(encrypted_str)
|
105
|
+
UrlEncrypt.cipher.decrypt
|
106
|
+
UrlEncrypt.cipher.key, UrlEncrypt.cipher.iv = UrlEncrypt.key, UrlEncrypt.iv
|
107
|
+
UrlEncrypt.cipher.update(URI.decode(encrypted_str).to_a.pack("H*")) + UrlEncrypt.cipher.final
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/test_helper")
|
2
|
+
|
3
|
+
ActiveRecord::Base.class_eval do
|
4
|
+
alias_method :save, :valid?
|
5
|
+
|
6
|
+
def self.columns() @columns ||= []; end
|
7
|
+
|
8
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
9
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Article < ActiveRecord::Base
|
14
|
+
column :id, :integer
|
15
|
+
column :title, :string
|
16
|
+
column :body, :text
|
17
|
+
end
|
18
|
+
|
19
|
+
class Book < ActiveRecord::Base
|
20
|
+
column :id, :integer
|
21
|
+
column :title, :string
|
22
|
+
|
23
|
+
encrypted :with => :title
|
24
|
+
end
|
25
|
+
|
26
|
+
class UrlEncryptTest < Test::Unit::TestCase
|
27
|
+
|
28
|
+
def test_should_not_encrypt_when_not_declared
|
29
|
+
article = Article.new(:title => "some title")
|
30
|
+
article.id = '654765498479'
|
31
|
+
assert_equal article.to_param, "654765498479"
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_should_encrypt_column_when_declared
|
35
|
+
book = Book.new
|
36
|
+
book.title = "some title"
|
37
|
+
|
38
|
+
assert_equal book.to_param, "2ccc216a3fd804c52c152a1659f03ebd"
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_should_get_record_on_find_by_encrypted_column
|
42
|
+
book = Book.new(:title => "some title")
|
43
|
+
Book.expects(:find_initial).returns(book)
|
44
|
+
|
45
|
+
assert_equal Book.respond_to?(:find_by_encrypted_title), false
|
46
|
+
assert_equal Book.find_by_encrypted_title('2ccc216a3fd804c52c152a1659f03ebd'), book
|
47
|
+
assert_equal Book.respond_to?(:find_by_encrypted_title), true
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_should_get_all_records_on_find_all_by_encrypted_column
|
51
|
+
book = Book.new(:title => "some title")
|
52
|
+
Book.expects(:find_every).returns(book)
|
53
|
+
|
54
|
+
assert_equal Book.respond_to?(:find_all_by_encrypted_title), false
|
55
|
+
assert_equal Book.find_all_by_encrypted_title('2ccc216a3fd804c52c152a1659f03ebd'), book
|
56
|
+
assert_equal Book.respond_to?(:find_all_by_encrypted_title), true
|
57
|
+
end
|
58
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
data/url_encrypt.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{url_encrypt}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Amit Kumar"]
|
9
|
+
s.date = %q{2010-05-02}
|
10
|
+
s.description = %q{Encrypt your URLs}
|
11
|
+
s.email = %q{toamitkumar@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["README", "lib/url_encrypt.rb", "tasks/url_encrypt_tasks.rake"]
|
13
|
+
s.files = ["MIT-LICENSE", "README", "Rakefile", "init.rb", "install.rb", "lib/url_encrypt.rb", "tasks/url_encrypt_tasks.rake", "test/test_helper.rb", "test/url_encrypt_test.rb", "uninstall.rb", "Manifest", "url_encrypt.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/toamitkumar/url_encrypt}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Url_encrypt", "--main", "README"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{url_encrypt}
|
18
|
+
s.rubygems_version = %q{1.3.5}
|
19
|
+
s.summary = %q{Encrypt your URLs}
|
20
|
+
s.test_files = ["test/url_encrypt_test.rb", "test/test_helper.rb"]
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 3
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
else
|
28
|
+
end
|
29
|
+
else
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: url_encrypt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Amit Kumar
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-05-02 00:00:00 -03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Encrypt your URLs
|
17
|
+
email: toamitkumar@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- lib/url_encrypt.rb
|
25
|
+
- tasks/url_encrypt_tasks.rake
|
26
|
+
files:
|
27
|
+
- MIT-LICENSE
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- init.rb
|
31
|
+
- install.rb
|
32
|
+
- lib/url_encrypt.rb
|
33
|
+
- tasks/url_encrypt_tasks.rake
|
34
|
+
- test/test_helper.rb
|
35
|
+
- test/url_encrypt_test.rb
|
36
|
+
- uninstall.rb
|
37
|
+
- Manifest
|
38
|
+
- url_encrypt.gemspec
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/toamitkumar/url_encrypt
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --line-numbers
|
46
|
+
- --inline-source
|
47
|
+
- --title
|
48
|
+
- Url_encrypt
|
49
|
+
- --main
|
50
|
+
- README
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "1.2"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: url_encrypt
|
68
|
+
rubygems_version: 1.3.5
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Encrypt your URLs
|
72
|
+
test_files:
|
73
|
+
- test/url_encrypt_test.rb
|
74
|
+
- test/test_helper.rb
|