lockbox 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c64ea693f929a79e495419fe5203760f73a2e1047602031eb90dcfd6542448d7
4
- data.tar.gz: d5c43889eb01598b80ed47bb2d521fbd4970a94bf0e0880770f66f54d0ccfca9
3
+ metadata.gz: 05cf227cc78a31ce5ad67f4352196ee720d31e0f614db26089d1c5b0a17ae823
4
+ data.tar.gz: c47effd7829d2e7ffc7143d51fa6adb03ae6e11bd24f8b7048d39c2e559d5389
5
5
  SHA512:
6
- metadata.gz: 760e4d28aaa0e3c059541ee91367112036e34bb87b64232d6de3a5e0c016ed59d47c79ea8549d67bb30922277869eed1187d2c5d77349430b7de81d50f2bc7d3
7
- data.tar.gz: cee190766d994e26e05fce781c17404fd99fa74d1606c57f1103ed4e428abb0f2256779f005ace00123e07901fd3c4435083e6e950d8d01e92609625dfeca2da
6
+ metadata.gz: 79b18945ec5c492feedc8913669dde990b7849264b0ca3bc9e5a3be991656a59e5f953e414cf51108b747aa1d98687838715452824f0e38697cbe55fe0cae023
7
+ data.tar.gz: a747b3d2ebe4dd12cfafd91fb4970bd020515a8585a05d0bdd222ddbe30e6f402e596b4188c1d945bfaf6d2cb21914fddfd1199030f9ca73c61a5a02acf24b0f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.3.5 (2020-04-13)
2
+
3
+ - Added `array` type
4
+ - Fixed serialize error with `json` type
5
+ - Fixed empty hash with `hash` type
6
+
1
7
  ## 0.3.4 (2020-04-05)
2
8
 
3
9
  - Fixed `migrating: true` with `validate: false`
data/README.md CHANGED
@@ -103,6 +103,7 @@ class User < ApplicationRecord
103
103
  encrypts :video, type: :binary
104
104
  encrypts :properties, type: :json
105
105
  encrypts :settings, type: :hash
106
+ encrypts :messages, type: :array
106
107
  end
107
108
  ```
108
109
 
data/lib/lockbox/model.rb CHANGED
@@ -11,6 +11,8 @@ module Lockbox
11
11
  # options[:type] = :json
12
12
  # when Hash
13
13
  # options[:type] = :hash
14
+ # when Array
15
+ # options[:type] = :array
14
16
  # when String
15
17
  # options[:type] = :string
16
18
  # when Integer
@@ -20,7 +22,7 @@ module Lockbox
20
22
  # end
21
23
 
22
24
  custom_type = options[:type].respond_to?(:serialize) && options[:type].respond_to?(:deserialize)
23
- raise ArgumentError, "Unknown type: #{options[:type]}" unless custom_type || [nil, :string, :boolean, :date, :datetime, :time, :integer, :float, :binary, :json, :hash].include?(options[:type])
25
+ raise ArgumentError, "Unknown type: #{options[:type]}" unless custom_type || [nil, :string, :boolean, :date, :datetime, :time, :integer, :float, :binary, :json, :hash, :array].include?(options[:type])
24
26
 
25
27
  activerecord = defined?(ActiveRecord::Base) && self < ActiveRecord::Base
26
28
  raise ArgumentError, "Type not supported yet with Mongoid" if options[:type] && !activerecord
@@ -112,7 +114,7 @@ module Lockbox
112
114
  if options[:type]
113
115
  attribute_type =
114
116
  case options[:type]
115
- when :json, :hash
117
+ when :json, :hash, :array
116
118
  :string
117
119
  when :integer
118
120
  ActiveModel::Type::Integer.new(limit: 8)
@@ -124,6 +126,7 @@ module Lockbox
124
126
 
125
127
  serialize name, JSON if options[:type] == :json
126
128
  serialize name, Hash if options[:type] == :hash
129
+ serialize name, Array if options[:type] == :array
127
130
  elsif !attributes_to_define_after_schema_loads.key?(name.to_s)
128
131
  # when migrating it's best to specify the type directly
129
132
  # however, we can try to use the original type if its already defined
@@ -223,19 +226,23 @@ module Lockbox
223
226
  define_method(name) do
224
227
  message = super()
225
228
 
226
- unless message
229
+ # possibly keep track of decrypted attributes directly in the future
230
+ # Hash serializer returns {} when nil, Array serializer returns [] when nil
231
+ # check for this explicitly as a layer of safety
232
+ if message.nil? || ((message == {} || message == []) && activerecord && @attributes[name.to_s].value_before_type_cast.nil?)
227
233
  ciphertext = send(encrypted_attribute)
228
234
  message = self.class.send(decrypt_method_name, ciphertext, context: self)
229
235
 
230
236
  if activerecord
231
- # set previous attribute on first decrypt
232
- if @attributes[name.to_s]
233
- @attributes[name.to_s].instance_variable_set("@value_before_type_cast", message)
234
- end
237
+ # set previous attribute so changes populate correctly
238
+ # it's fine if this is set on future decryptions (as is the case when message is nil)
239
+ # as only the first value is loaded into changes
240
+ @attributes[name.to_s].instance_variable_set("@value_before_type_cast", message)
235
241
 
236
242
  # cache
237
- if respond_to?(:_write_attribute, true)
238
- _write_attribute(name, message) if !@attributes.frozen?
243
+ # decrypt method does type casting
244
+ if respond_to?(:write_attribute_without_type_cast, true)
245
+ write_attribute_without_type_cast(name, message) if !@attributes.frozen?
239
246
  else
240
247
  raw_write_attribute(name, message) if !@attributes.frozen?
241
248
  end
@@ -1,3 +1,3 @@
1
1
  module Lockbox
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lockbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-06 00:00:00.000000000 Z
11
+ date: 2020-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler