mongoid-encryptor 0.0.1 → 0.0.4

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.
data/CHANGELOG.md CHANGED
@@ -1,6 +1,16 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ 0.0.3
5
+ -----
6
+
7
+ * fixed unexpected encryption after validation.
8
+
9
+ 0.0.2
10
+ -----
11
+
12
+ * fixed validation error on encrypted field in embedded document.
13
+
4
14
  0.0.1
5
15
  -----
6
16
 
data/README.md CHANGED
@@ -9,7 +9,7 @@ Install
9
9
 
10
10
  Put this line in your Gemfile:
11
11
 
12
- gem 'mongoid-encryptor'
12
+ gem 'mongoid-encryptor', :require => 'mongoid/encryptor'
13
13
 
14
14
  Then bundle:
15
15
 
@@ -19,10 +19,6 @@ Then bundle:
19
19
  Quick Start
20
20
  -----------
21
21
 
22
- Add mongoid-encryptor to your Gemfile.
23
-
24
- gem 'mongoid-encryptor', :require => 'mongoid/encryptor'
25
-
26
22
  Set up SHA encrypted field in models like this:
27
23
 
28
24
  class Credential
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mongoid #:nodoc:
4
4
  module Encryptor
5
- VERSION = '0.0.1'
5
+ VERSION = '0.0.4'
6
6
  end
7
7
  end
@@ -19,7 +19,7 @@ module Mongoid #:nodoc:
19
19
  mode = options.delete(:mode) || :sha
20
20
  cipher_class = EncryptedStrings.const_get("#{mode.to_s.classify}Cipher")
21
21
 
22
- send(:before_validation) do |doc|
22
+ send(:after_validation) do |doc|
23
23
  doc.send(:write_encrypted_attribute, attr_name, cipher_class, options)
24
24
  true
25
25
  end
@@ -32,6 +32,15 @@ module Mongoid #:nodoc:
32
32
  end
33
33
 
34
34
  module InstanceMethods #:nodoc:
35
+ # Returns decrypted value for key.
36
+ #
37
+ # @param [String] key
38
+ # @return [Object]
39
+ def read_attribute_for_validation(key)
40
+ v = read_attribute(key)
41
+ v.try(:encrypted?) ? v.decrypt : v
42
+ end
43
+
35
44
  private
36
45
 
37
46
  # @param [String] attr_name
@@ -50,6 +59,7 @@ module Mongoid #:nodoc:
50
59
  # @param [String] attr_name
51
60
  # @param [Class] cipher_class
52
61
  # @param [Hash] options
62
+ # @return [String]
53
63
  def read_encrypted_attribute(attr_name, cipher_class, options)
54
64
  value = read_attribute(attr_name)
55
65
 
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe "Symmetric encryption on embedded document" do
5
+ let(:person) do
6
+ Person.new(:first_name => 'John',
7
+ :last_name => 'Smith',
8
+ :credit_card => CreditCard.new(:number => '0000111122224444'))
9
+ end
10
+
11
+ context "Has valid attributes" do
12
+ context "Before save" do
13
+ subject { person.credit_card }
14
+ its(:number) { should_not be_encrypted }
15
+ its(:number) { should eq('0000111122224444') }
16
+ its(:number) { should_not eq("0pOZeQMtxuPsAPaoR3fkjMEUIoAuSbFO\n") }
17
+ it { should be_valid }
18
+ end
19
+
20
+ context "after save" do
21
+ before { person.save!; puts person.credit_card.number }
22
+ subject { person.credit_card }
23
+ its(:number) { should be_encrypted }
24
+ its(:number) { should eq("0pOZeQMtxuPsAPaoR3fkjMEUIoAuSbFO\n") }
25
+ its(:number) { should eq('0000111122224444') }
26
+ end
27
+ end
28
+
29
+ it "doesn't encrypt value of field after validation" do
30
+ person.credit_card.valid?
31
+ person.should be_valid
32
+ end
33
+
34
+ end
@@ -3,9 +3,23 @@ require 'spec_helper'
3
3
 
4
4
  describe 'Encrypted attributes' do
5
5
  context "Not encryption" do
6
- let(:credential) { Credential.create(:email => 'john.smith@example.com') }
6
+ let(:credential) { Credential.new(:email => 'john.smith@example.com') }
7
7
  before { credential.password = 'this is not a secret' }
8
8
  subject { credential.password }
9
9
  it { should eq('this is not a secret') }
10
10
  end
11
+
12
+ context "Has valid attributes" do
13
+ let(:credential) { Credential.new(:email => 'john.smith@example.com') }
14
+ before { credential.password = 'this is not a secret' }
15
+ subject { credential }
16
+ it { should be_valid }
17
+ end
18
+
19
+ context "Has invalid attributes" do
20
+ let(:credential) { Credential.new(:email => 'invalid-address') }
21
+ before { credential.password = '' }
22
+ subject { credential }
23
+ it { should_not be_valid }
24
+ end
11
25
  end
@@ -6,4 +6,5 @@ class Credential
6
6
  field :password
7
7
 
8
8
  validates_presence_of :email, :password
9
+ validates_format_of :email, :with => /^\A[^@]+@[^@]+\z/
9
10
  end
@@ -0,0 +1,10 @@
1
+ class CreditCard
2
+ include Mongoid::Document
3
+ include Mongoid::Encryptor
4
+
5
+ field :number
6
+
7
+ validates_format_of :number, :with => /^\A\d{16}\z/
8
+
9
+ encrypts :number, :mode => :symmetric, :password => 'secret'
10
+ end
@@ -0,0 +1,10 @@
1
+ class Person
2
+ include Mongoid::Document
3
+
4
+ field :first_name
5
+ field :last_name
6
+
7
+ embeds_one :credit_card
8
+
9
+ validates_presence_of :first_name, :last_name
10
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongoid-encryptor
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Junya Ogura
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-22 00:00:00 +09:00
13
+ date: 2011-02-25 00:00:00 +09:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirements:
33
33
  - - ~>
34
34
  - !ruby/object:Gem::Version
35
- version: 0.3.3
35
+ version: "0.3"
36
36
  type: :runtime
37
37
  version_requirements: *id002
38
38
  - !ruby/object:Gem::Dependency
@@ -43,7 +43,7 @@ dependencies:
43
43
  requirements:
44
44
  - - ~>
45
45
  - !ruby/object:Gem::Version
46
- version: 1.2.0
46
+ version: "1.2"
47
47
  type: :development
48
48
  version_requirements: *id003
49
49
  - !ruby/object:Gem::Dependency
@@ -54,7 +54,7 @@ dependencies:
54
54
  requirements:
55
55
  - - ~>
56
56
  - !ruby/object:Gem::Version
57
- version: 0.6.0
57
+ version: "0.6"
58
58
  type: :development
59
59
  version_requirements: *id004
60
60
  - !ruby/object:Gem::Dependency
@@ -65,20 +65,9 @@ dependencies:
65
65
  requirements:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
- version: 2.4.0
68
+ version: "2.4"
69
69
  type: :development
70
70
  version_requirements: *id005
71
- - !ruby/object:Gem::Dependency
72
- name: ruby-debug19
73
- prerelease: false
74
- requirement: &id006 !ruby/object:Gem::Requirement
75
- none: false
76
- requirements:
77
- - - ~>
78
- - !ruby/object:Gem::Version
79
- version: 0.11.0
80
- type: :development
81
- version_requirements: *id006
82
71
  description: mongoid-encryptor encrypts and decrypts one or more fields in a Mongoid model.
83
72
  email:
84
73
  - junyaogura@gmail.com
@@ -95,11 +84,14 @@ files:
95
84
  - LICENSE
96
85
  - README.md
97
86
  - spec/asymmetric_encryption_spec.rb
87
+ - spec/embedded_document_spec.rb
98
88
  - spec/encrypted_attributes_spec.rb
99
89
  - spec/keys/private
100
90
  - spec/keys/public
101
91
  - spec/models/asymmetric_credential.rb
102
92
  - spec/models/credential.rb
93
+ - spec/models/credit_card.rb
94
+ - spec/models/person.rb
103
95
  - spec/models/sha_credential.rb
104
96
  - spec/models/sha_with_salt_credential.rb
105
97
  - spec/models/symmetric_credential.rb
@@ -137,11 +129,14 @@ specification_version: 3
137
129
  summary: Encrypt a Mongoid model fields
138
130
  test_files:
139
131
  - spec/asymmetric_encryption_spec.rb
132
+ - spec/embedded_document_spec.rb
140
133
  - spec/encrypted_attributes_spec.rb
141
134
  - spec/keys/private
142
135
  - spec/keys/public
143
136
  - spec/models/asymmetric_credential.rb
144
137
  - spec/models/credential.rb
138
+ - spec/models/credit_card.rb
139
+ - spec/models/person.rb
145
140
  - spec/models/sha_credential.rb
146
141
  - spec/models/sha_with_salt_credential.rb
147
142
  - spec/models/symmetric_credential.rb