active_record_encryption 0.2.1 → 1.0.0.alpha

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 585c87bfd8ce7dc5dd9aa303a89f23b5f7f7060e53401538465214e717f4cf4e
4
- data.tar.gz: adf27af387edb8ff6cac34ea735c8189e26889a59c22b8f0eb19fe190cc8f27f
3
+ metadata.gz: 14bd2384751a5ab2042b14864f2f873e666d78f5a01c8bdce9d8c2190284f514
4
+ data.tar.gz: 7a086e0bdccd0e140a444fd92688302c60a36355a93c73eef2e869f1a4962dc5
5
5
  SHA512:
6
- metadata.gz: '09cf19da814c601fafc4e5e05c64ac2eb6926f540e2116e2536c0eef1ef72529f1d2865403f2a7475ddd53b8de0ef5f1d42cfa8408b42fc48b0552ef74e8a569'
7
- data.tar.gz: 9a6602b13e6ced26450cee8ed43feab631ecd1a2cb009ed26d7a0f0df9a3a482f34b2cc7f63dc52316574c7274680b8296be1c749df83c4387d8287da7e5f178
6
+ metadata.gz: 20a722cb97287bb1f792f0f887ce8b1bcf1a229ecac56d141e205cd1cae3d4930ebbace743c279ff19cb2c2d8fd55605430a86a43ec13c2833942dacde7caab3
7
+ data.tar.gz: 0df865d7401f019eef15ecad0e78094e11b624eacced1bbe318f357e016d04ea8570f179a7c1a8d1af1eb54f96d731850702a0bfa4925d8423ec5c0a48d02545
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 1.0.0.alpha
2
+
3
+ - EOL Ruby and Rails no longer supported #29
4
+ - Requires Ruby 3.0.0 or newer.
5
+ - Requires Rails 6.1.0 or newer.
6
+ - Support rails 7.0.0 #25
7
+ - Support rails 7.1.0 #31
8
+ - Migrate CI from travis.ci to GitHub Action. #27
9
+ - In Rails 7.0, `ActiveRecordEncryption` do not change encoding for backward compatibility.
10
+
11
+ ## 0.3.0
12
+
13
+ - Support rails 6.1.0 #22
14
+ - Ensure casting only when Rails less than 6.0 #21
15
+ - Fix Ruby 2.7 keyword arguments warnings #20
16
+
1
17
  ## 0.2.1
2
18
 
3
19
  - Fixes #13 Add `:cipher` option to `active_support` encryptor to change cipher.
data/README.md CHANGED
@@ -49,7 +49,7 @@ class PointLog < ActiveRecord::Base
49
49
  encrypted_attribute(:serialized_address, :string)
50
50
 
51
51
  # Change encryptor
52
- encrypted_attribute(:name, :field, encryption: { encryptor: :active_support, key: ENV['ENCRYPTION_KEY'], salt: ['ENCRYPTION_SALT'] })
52
+ encrypted_attribute(:name, :field, encryption: { encryptor: :active_support, key: ENV['ENCRYPTION_KEY'], salt: ENV['ENCRYPTION_SALT'] })
53
53
  end
54
54
  ```
55
55
 
@@ -118,9 +118,8 @@ end
118
118
 
119
119
  ```bash
120
120
  bundle exec appraisal install
121
- bundle exec appraisal 5.0-stable rspec
122
- bundle exec appraisal 5.1-stable rspec
123
- bundle exec appraisal 5.2-stable rspec
121
+ bundle exec appraisal 6.1-stable rspec
122
+ bundle exec appraisal 7.0-stable rspec
124
123
  ```
125
124
 
126
125
  ## Contributing
@@ -6,7 +6,7 @@ module ActiveRecordEncryption
6
6
 
7
7
  module ClassMethods
8
8
  def encrypted_attribute(name, subtype, **options)
9
- attribute(name, :encryption, options.merge(subtype: subtype))
9
+ attribute(name, :encryption, **options, subtype: subtype)
10
10
  end
11
11
  end
12
12
  end
@@ -4,7 +4,7 @@ module ActiveRecordEncryption
4
4
  module Encryptor
5
5
  # Abstract interface of encryptor
6
6
  class Base
7
- def initialize(*); end
7
+ def initialize(*, **); end
8
8
 
9
9
  def encrypt(value)
10
10
  value
@@ -2,32 +2,15 @@
2
2
 
3
3
  module ActiveRecordEncryption
4
4
  module Encryptor
5
- class Registry
6
- def initialize
7
- @registrations = []
8
- end
9
-
10
- def register(encryptor_name, klass = nil, **options, &block)
11
- block ||= proc { |_, *args| klass.new(*args) }
12
- registrations << Registration.new(encryptor_name, block, **options)
13
- end
14
-
15
- def lookup(symbol, *args)
16
- registration = find_registration(symbol, *args)
17
-
18
- if registration
19
- registration.call(self, symbol, *args)
20
- else
21
- raise ArgumentError, "Unknown encryptor #{symbol.inspect}"
22
- end
23
- end
24
-
5
+ class Registry < ActiveModel::Type::Registry
25
6
  private
26
7
 
27
- attr_reader :registrations
8
+ def registration_klass
9
+ Registration
10
+ end
28
11
 
29
- def find_registration(symbol, *args)
30
- registrations.find { |registration| registration.matches?(symbol, *args) }
12
+ def find_registration(symbol, *args, **kwargs)
13
+ registrations.find { |r| r.matches?(symbol, *args, **kwargs) }
31
14
  end
32
15
  end
33
16
 
@@ -18,12 +18,27 @@ module ActiveRecordEncryption
18
18
  'f'
19
19
  end
20
20
 
21
+ if ActiveRecord::VERSION::MAJOR >= 7
22
+ # Cast value to string
23
+ def type_cast(value)
24
+ return value.to_s if value.is_a?(Numeric)
25
+ super(value)
26
+ end
27
+ end
28
+
29
+ # @return [Symbol]
30
+ def default_timezone
31
+ ActiveRecord::Base.connection.default_timezone
32
+ end
33
+
21
34
  private
22
35
 
23
- # Cast value to string
24
- def _type_cast(value)
25
- return value.to_s if value.is_a?(Numeric)
26
- super(value)
36
+ if ActiveRecord::VERSION::MAJOR < 7
37
+ # Cast value to string
38
+ def _type_cast(value)
39
+ return value.to_s if value.is_a?(Numeric)
40
+ super(value)
41
+ end
27
42
  end
28
43
  end
29
44
  end
@@ -0,0 +1,30 @@
1
+ module ActiveRecordEncryption
2
+ class Type
3
+ class Binary < ActiveModel::Type::Binary
4
+ def cast(value)
5
+ return super if ActiveRecord::VERSION::STRING < '7.0.0'
6
+
7
+ if value.is_a?(Data)
8
+ value.to_s
9
+ else
10
+ value = cast_value(value) unless value.nil?
11
+ # NOTE: Don't convert string to binary for backward compatibility.
12
+ # value = value.b if ::String === value && value.encoding != Encoding::BINARY
13
+ value
14
+ end
15
+ end
16
+
17
+ def serialize(value)
18
+ return if value.nil?
19
+ Data.new(value)
20
+ end
21
+
22
+ class Data < ActiveModel::Type::Binary::Data
23
+ # NOTE: Don't convert string to binary for backward compatibility.
24
+ def initialize(value)
25
+ @value = value.to_s
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,23 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_record_encryption/serializer_with_cast'
4
-
5
3
  module ActiveRecordEncryption
6
4
  class Type < ActiveRecord::Type::Value
7
- using(ActiveRecordEncryption::SerializerWithCast)
8
-
9
5
  delegate :type, :cast, to: :subtype
10
6
  delegate :user_input_in_time_zone, to: :subtype # for ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
11
7
 
12
8
  def initialize(
13
- subtype: default_value,
9
+ subtype: ActiveRecord::Type.default_value,
14
10
  encryption: ActiveRecordEncryption.default_encryption.clone,
15
11
  **options
16
12
  )
17
-
18
13
  # Lookup encryptor from options[:encryption]
19
14
  @encryptor = build_encryptor(encryption)
20
- @binary = ActiveRecord::Type.lookup(:binary)
15
+ @binary = ActiveRecord::Type.lookup(:encryption_binary)
21
16
 
22
17
  subtype = ActiveRecord::Type.lookup(subtype, **options) if subtype.is_a?(Symbol)
23
18
  @subtype = subtype
@@ -42,22 +37,13 @@ module ActiveRecordEncryption
42
37
 
43
38
  attr_reader :subtype, :binary, :encryptor
44
39
 
45
- # NOTE: `ActiveRecord::Type.default_value` is not defined in Rails 5.0
46
- def default_value
47
- if ActiveRecord.gem_version < Gem::Version.create('5.1.0')
48
- ActiveRecord::Type::Value.new
49
- else
50
- ActiveRecord::Type.default_value
51
- end
52
- end
53
-
54
40
  def build_encryptor(options)
55
41
  encryptor = options.delete(:encryptor)
56
42
 
57
43
  if encryptor.is_a?(Symbol)
58
44
  ActiveRecordEncryption::Encryptor.lookup(encryptor, **options)
59
45
  elsif encryptor.is_a?(Class)
60
- encryptor.new(options)
46
+ encryptor.new(**options)
61
47
  else
62
48
  encryptor
63
49
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordEncryption
4
- VERSION = '0.2.1'
4
+ VERSION = '1.0.0.alpha'
5
5
  end
@@ -13,6 +13,7 @@ end
13
13
 
14
14
  ActiveSupport.on_load(:active_record) do
15
15
  require 'active_record_encryption/type'
16
+ require 'active_record_encryption/type/binary'
16
17
  require 'active_record_encryption/encryptor'
17
18
  require 'active_record_encryption/encrypted_attribute'
18
19
  require 'active_record_encryption/binary'
@@ -20,6 +21,7 @@ ActiveSupport.on_load(:active_record) do
20
21
 
21
22
  # Register `:encryption` type
22
23
  ActiveRecord::Type.register(:encryption, ActiveRecordEncryption::Type)
24
+ ActiveRecord::Type.register(:encryption_binary, ActiveRecordEncryption::Type::Binary)
23
25
 
24
26
  # Define `.encrypted_attribute`
25
27
  ActiveRecord::Base.include(ActiveRecordEncryption::EncryptedAttribute)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_encryption
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0.alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - alpaca-tc
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-18 00:00:00.000000000 Z
11
+ date: 2023-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '6.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.0'
26
+ version: '6.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: appraisal
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.16'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.16'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: guard-rspec
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -98,16 +84,16 @@ dependencies:
98
84
  name: mysql2
99
85
  requirement: !ruby/object:Gem::Requirement
100
86
  requirements:
101
- - - "<"
87
+ - - ">="
102
88
  - !ruby/object:Gem::Version
103
- version: 0.5.0
89
+ version: '0'
104
90
  type: :development
105
91
  prerelease: false
106
92
  version_requirements: !ruby/object:Gem::Requirement
107
93
  requirements:
108
- - - "<"
94
+ - - ">="
109
95
  - !ruby/object:Gem::Version
110
- version: 0.5.0
96
+ version: '0'
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: pry
113
99
  requirement: !ruby/object:Gem::Requirement
@@ -201,14 +187,14 @@ files:
201
187
  - lib/active_record_encryption/encryptor/registry.rb
202
188
  - lib/active_record_encryption/exceptions.rb
203
189
  - lib/active_record_encryption/quoter.rb
204
- - lib/active_record_encryption/serializer_with_cast.rb
205
190
  - lib/active_record_encryption/type.rb
191
+ - lib/active_record_encryption/type/binary.rb
206
192
  - lib/active_record_encryption/version.rb
207
193
  homepage: https://github.com/alpaca-tc/active_record_encryption
208
194
  licenses:
209
195
  - MIT
210
196
  metadata: {}
211
- post_install_message:
197
+ post_install_message:
212
198
  rdoc_options: []
213
199
  require_paths:
214
200
  - lib
@@ -216,16 +202,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
216
202
  requirements:
217
203
  - - ">="
218
204
  - !ruby/object:Gem::Version
219
- version: '0'
205
+ version: 3.0.0
220
206
  required_rubygems_version: !ruby/object:Gem::Requirement
221
207
  requirements:
222
- - - ">="
208
+ - - ">"
223
209
  - !ruby/object:Gem::Version
224
- version: '0'
210
+ version: 1.3.1
225
211
  requirements: []
226
- rubyforge_project:
227
- rubygems_version: 2.7.3
228
- signing_key:
212
+ rubygems_version: 3.4.6
213
+ signing_key:
229
214
  specification_version: 4
230
215
  summary: Transparent ActiveRecord encryption
231
216
  test_files: []
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecordEncryption
4
- module SerializerWithCast
5
- # Unfortunately, current Rails doesn't serialize value from user input with `#cast`.
6
- # IMO, it is expected that serialized value from user input should be equal same result.
7
- #
8
- # Example:
9
- # # Current
10
- # User.attribute(:id, :boolean)
11
- # User.where(id: 'string').to_sql #=> SELECT `users`.* FROM `users` WHERE `users`.`id` = 'string'
12
- # User.where(id: true).to_sql #=> SELECT `users`.* FROM `users` WHERE `users`.`id` = TRUE
13
- #
14
- # # Expected
15
- # User.attribute(:id, :boolean)
16
- # User.where(id: 'string').to_sql #=> SELECT `users`.* FROM `users` WHERE `users`.`id` = TRUE
17
- # User.where(id: true).to_sql #=> SELECT `users`.* FROM `users` WHERE `users`.`id` = TRUE
18
- refine ActiveModel::Type::Decimal do
19
- def serialize(value)
20
- cast(value)
21
- end
22
- end
23
-
24
- refine ActiveModel::Type::Boolean do
25
- def serialize(value)
26
- cast(value)
27
- end
28
- end
29
-
30
- # Backport Rails5.2
31
- if ActiveRecord.gem_version < Gem::Version.create('5.2')
32
- refine ActiveModel::Type::DateTime do
33
- def serialize(value)
34
- super(cast(value))
35
- end
36
- end
37
- end
38
- end
39
- end