couch_potato-extensions 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
@@ -2,49 +2,45 @@ require 'bcrypt'
|
|
2
2
|
|
3
3
|
module CouchPotato
|
4
4
|
module Extensions
|
5
|
-
class EncryptedProperty
|
6
|
-
|
7
|
-
|
8
|
-
def initialize(owner, name, options = {})
|
9
|
-
@type = owner
|
10
|
-
@name = name
|
5
|
+
class EncryptedProperty < CouchPotato::Persistence::SimpleProperty
|
6
|
+
def initialize(owner_clazz, name, options = {})
|
7
|
+
super
|
11
8
|
@options = options
|
12
|
-
|
13
|
-
define_accessors(name)
|
14
9
|
end
|
15
10
|
|
16
|
-
def define_accessors(name)
|
17
|
-
type.class_eval do
|
18
|
-
define_method(name) do
|
19
|
-
instance_variable_get("@#{name}")
|
20
|
-
end
|
21
|
-
|
22
|
-
define_method("#{name}=") do |value|
|
23
|
-
instance_variable_set("@#{name}", value)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
11
|
def build(object, json)
|
29
12
|
value = json[name.to_s].nil? ? json[name.to_sym] : json[name.to_s]
|
30
13
|
object.send "#{name}=", decrypt(value)
|
14
|
+
object.instance_variable_set("@encrypted_#{name}", value)
|
31
15
|
end
|
32
16
|
|
33
17
|
def dirty?(object)
|
34
|
-
|
18
|
+
object.send("#{name}_changed?")
|
35
19
|
end
|
36
20
|
|
37
21
|
def serialize(json, object)
|
38
|
-
|
22
|
+
if dirty?(object) or encrypted_value(object).nil?
|
23
|
+
json[name] = encrypt(object.send(name))
|
24
|
+
else
|
25
|
+
json[name] = encrypted_value(object)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def value(result, object)
|
30
|
+
result[name] = object.send(name)
|
39
31
|
end
|
40
32
|
|
41
33
|
def encrypt(value)
|
42
|
-
Base64.encode64(EzCrypto::Key.encrypt_with_password(@options[:password], @options[:salt], value))
|
34
|
+
Base64.encode64(EzCrypto::Key.encrypt_with_password(@options[:password], @options[:salt], value))
|
43
35
|
end
|
44
36
|
|
45
37
|
def decrypt(value)
|
46
38
|
EzCrypto::Key.decrypt_with_password(@options[:password], @options[:salt], Base64.decode64(value))
|
47
39
|
end
|
40
|
+
|
41
|
+
def encrypted_value(object)
|
42
|
+
object.instance_variable_get("@encrypted_#{name}")
|
43
|
+
end
|
48
44
|
end
|
49
45
|
end
|
50
46
|
end
|
@@ -23,6 +23,11 @@ describe CouchPotato::Extensions::EncryptedProperty do
|
|
23
23
|
document = SecureDocument.new(:body => "very important")
|
24
24
|
document.body.should == 'very important'
|
25
25
|
end
|
26
|
+
|
27
|
+
it "should allow access through the attributes hash" do
|
28
|
+
document = SecureDocument.new(:body => "very important")
|
29
|
+
document.attributes[:body].should == "very important"
|
30
|
+
end
|
26
31
|
end
|
27
32
|
|
28
33
|
describe "saving documents" do
|
@@ -34,15 +39,31 @@ describe CouchPotato::Extensions::EncryptedProperty do
|
|
34
39
|
body.should_not == 'very important'
|
35
40
|
body.should == Base64.encode64(EzCrypto::Key.encrypt_with_password("coffee! more coffee!", "Va7JYeT7t08vMweYU6F6dO", "very important"))
|
36
41
|
end
|
42
|
+
|
43
|
+
it "should not encrypt an attribute that hasn't changed" do
|
44
|
+
@document = document
|
45
|
+
CouchPotato.database.save_document document
|
46
|
+
@document = CouchPotato.database.load_document(document.id)
|
47
|
+
@document.is_dirty
|
48
|
+
EzCrypto::Key.should_not_receive(:encrypt_with_password)
|
49
|
+
CouchPotato.database.save_document @document
|
50
|
+
end
|
37
51
|
end
|
38
52
|
|
39
53
|
describe "loading documents" do
|
40
|
-
let(:document)
|
54
|
+
let(:document) do
|
55
|
+
document = SecureDocument.new(:body => "very important")
|
56
|
+
CouchPotato.database.save_document document
|
57
|
+
CouchPotato.database.load_document(document.id)
|
58
|
+
end
|
41
59
|
|
42
60
|
it "should decrypt the attributes" do
|
43
|
-
|
44
|
-
|
45
|
-
|
61
|
+
document.body.should == "very important"
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should not encrypt the data when it was changed" do
|
65
|
+
document.instance_variable_get(:@encrypted_body).should_not == nil
|
46
66
|
end
|
47
67
|
end
|
68
|
+
|
48
69
|
end
|
metadata
CHANGED
@@ -1,12 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couch_potato-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 4
|
9
|
-
version: 0.0.4
|
4
|
+
version: 0.0.5
|
10
5
|
platform: ruby
|
11
6
|
authors:
|
12
7
|
- Mathias Meyer
|
@@ -14,47 +9,39 @@ autorequire:
|
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
11
|
|
17
|
-
date: 2010-04-
|
12
|
+
date: 2010-04-19 00:00:00 +02:00
|
18
13
|
default_executable:
|
19
14
|
dependencies:
|
20
15
|
- !ruby/object:Gem::Dependency
|
21
16
|
name: couch_potato
|
22
|
-
|
23
|
-
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
20
|
requirements:
|
25
21
|
- - ">="
|
26
22
|
- !ruby/object:Gem::Version
|
27
|
-
segments:
|
28
|
-
- 0
|
29
23
|
version: "0"
|
30
|
-
|
31
|
-
version_requirements: *id001
|
24
|
+
version:
|
32
25
|
- !ruby/object:Gem::Dependency
|
33
26
|
name: ezcrypto
|
34
|
-
|
35
|
-
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
30
|
requirements:
|
37
31
|
- - ">="
|
38
32
|
- !ruby/object:Gem::Version
|
39
|
-
segments:
|
40
|
-
- 0
|
41
33
|
version: "0"
|
42
|
-
|
43
|
-
version_requirements: *id002
|
34
|
+
version:
|
44
35
|
- !ruby/object:Gem::Dependency
|
45
36
|
name: rspec
|
46
|
-
|
47
|
-
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
48
40
|
requirements:
|
49
41
|
- - ~>
|
50
42
|
- !ruby/object:Gem::Version
|
51
|
-
segments:
|
52
|
-
- 1
|
53
|
-
- 2
|
54
|
-
- 9
|
55
43
|
version: 1.2.9
|
56
|
-
|
57
|
-
version_requirements: *id003
|
44
|
+
version:
|
58
45
|
description: See summary, it says it all, trust me.
|
59
46
|
email: meyer@paperplanes.de
|
60
47
|
executables: []
|
@@ -87,20 +74,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
74
|
requirements:
|
88
75
|
- - ">="
|
89
76
|
- !ruby/object:Gem::Version
|
90
|
-
segments:
|
91
|
-
- 0
|
92
77
|
version: "0"
|
78
|
+
version:
|
93
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
80
|
requirements:
|
95
81
|
- - ">="
|
96
82
|
- !ruby/object:Gem::Version
|
97
|
-
segments:
|
98
|
-
- 0
|
99
83
|
version: "0"
|
84
|
+
version:
|
100
85
|
requirements: []
|
101
86
|
|
102
87
|
rubyforge_project:
|
103
|
-
rubygems_version: 1.3.
|
88
|
+
rubygems_version: 1.3.5
|
104
89
|
signing_key:
|
105
90
|
specification_version: 3
|
106
91
|
summary: Some extensions for CouchPotato, because more awesome is always a good thing.
|