module EncryptableAttributes::Base
Public Instance Methods
[](key)
click to toggle source
Override ActiveRecord accessor
# File lib/encryptable_attributes/base.rb, line 14 def [](key) send key end
[]=(key, value)
click to toggle source
Override ActiveRecord accessor
# File lib/encryptable_attributes/base.rb, line 19 def []=(key, value) send "#{key}=", value end
secure_attrs(*attr_names)
click to toggle source
# File lib/encryptable_attributes/base.rb, line 28 def secure_attrs(*attr_names) attr_names.each do |attr_name| define_method :"#{attr_name}=" do |value| if value.present? write_attribute(attr_name, crypt.encrypt_and_sign(value)) else # Allow to 'clear' attributes with nil write_attribute(attr_name, value) end end define_method :"#{attr_name}" do possibly_encrypted_value = read_attribute(attr_name) if possibly_encrypted_value.present? crypt.decrypt_and_verify(possibly_encrypted_value) else # Don't try to decrypt blank fields (nil) possibly_encrypted_value end end end end
secure_key(key)
click to toggle source
# File lib/encryptable_attributes/base.rb, line 24 def secure_key(key) self._secure_key = key end
Protected Instance Methods
crypt()
click to toggle source
# File lib/encryptable_attributes/base.rb, line 65 def crypt @crypt ||= new_crypt end
new_crypt()
click to toggle source
# File lib/encryptable_attributes/base.rb, line 55 def new_crypt len = ActiveSupport::MessageEncryptor.key_len salt = SecureRandom.random_bytes(len) key = ActiveSupport::KeyGenerator.new(static_or_dynamic_secure_key).generate_key(salt, len) @crypt = ActiveSupport::MessageEncryptor.new(key) @crypt.rotate(key) # Ensure configuration is kept @crypt end
static_or_dynamic_secure_key()
click to toggle source
# File lib/encryptable_attributes/base.rb, line 69 def static_or_dynamic_secure_key if self._secure_key.is_a?(String) self._secure_key elsif self._secure_key.is_a?(Symbol) send self._secure_key else raise ArgumentError, "#{self._secure_key} bust be of type String or Symbol, but is of type #{self._secure_key.class}" end end