class ActiveRecord::Encryption::KeyProvider

A KeyProvider serves keys:

Public Class Methods

new(keys) click to toggle source
# File lib/active_record/encryption/key_provider.rb, line 11
def initialize(keys)
  @keys = Array(keys)
end

Public Instance Methods

decryption_keys(encrypted_message) click to toggle source

Returns the list of decryption keys

When the message holds a reference to its encryption key, it will return an array with that key. If not, it will return the list of keys.

# File lib/active_record/encryption/key_provider.rb, line 32
def decryption_keys(encrypted_message)
  if encrypted_message.headers.encrypted_data_key_id
    keys_grouped_by_id[encrypted_message.headers.encrypted_data_key_id]
  else
    @keys
  end
end
encryption_key() click to toggle source

Returns the first key in the list as the active key to perform encryptions

When ActiveRecord::Encryption.config.store_key_references is true, the key will include a public tag referencing the key itself. That key will be stored in the public headers of the encrypted message

# File lib/active_record/encryption/key_provider.rb, line 20
def encryption_key
  @encryption_key ||= @keys.last.tap do |key|
    key.public_tags.encrypted_data_key_id = key.id if ActiveRecord::Encryption.config.store_key_references
  end

  @encryption_key
end

Private Instance Methods

keys_grouped_by_id() click to toggle source
# File lib/active_record/encryption/key_provider.rb, line 41
def keys_grouped_by_id
  @keys_grouped_by_id ||= @keys.group_by(&:id)
end