class LoggableActivity::EncryptionKey

This class represents the encryption key used to unlock the data for one payload. When deleted, only the encryption_key field is deleted.

Public Class Methods

create_encryption_key(record_type, record_id, parent_key = nil) click to toggle source

Creates an encryption key for a record, optionally using a parent key.

@param record_type [String] The type of the record.
@param record_id [Integer] The ID of the record.
@param parent_key [LoggableActivity::EncryptionKey, nil] The parent encryption key, if any.
@return [LoggableActivity::EncryptionKey] The created encryption key.

Example:

LoggableActivity::EncryptionKey.create_encryption_key('User', 1)

Returns:

{
  :id => 39,
  :parent_key_id => 38,
  :key => "a8f4774e7f42eb253045a4db7de7b79e",
  :record_type => "User",
  :record_id => 1
}
# File lib/loggable_activity/encryption_key.rb, line 91
def self.create_encryption_key(record_type, record_id, parent_key = nil)
  if parent_key
    create(record_type:, record_id:, key: random_key, parent_key: )
  else
    create(record_type:, record_id:, key: random_key)
  end
end
for_record(record, parent_key = nil) click to toggle source

Returns an encryption key for a record, optionally using a parent key.

@param record [ActiveRecord::Base] The record for which to get the encryption key.
@param parent_key [LoggableActivity::EncryptionKey, nil] The parent encryption key, if any.
@return [LoggableActivity::EncryptionKey] The encryption key for the record.

Example:

user = User.find(1)
LoggableActivity::EncryptionKey.for_record(user)

Returns:

{
  :id => 39,
  :parent_key_id => 38,
  :key => "a8f4774e7f42eb253045a4db7de7b79e",
  :record_type => "User",
  :record_id => 1
}
# File lib/loggable_activity/encryption_key.rb, line 65
def self.for_record(record, parent_key = nil)
  encryption_key = find_by(record:)
  return encryption_key if encryption_key

  create_encryption_key(record.class.name, record.id, parent_key)
end
for_record_by_type_and_id(record_type, record_id, parent_key = nil) click to toggle source

Returns an encryption key for a record by its type and ID, optionally using a parent key.

@param record_type [String] The type of the record.
@param record_id [Integer] The ID of the record.
@param parent_key [LoggableActivity::EncryptionKey, nil] The parent encryption key, if any.
@return [LoggableActivity::EncryptionKey] The encryption key for the record.

Example:

LoggableActivity::EncryptionKey.for_record_by_type_and_id('User', 1)

Returns:

{
  :id => 39,
  :parent_key_id => 38,
  :key => "a8f4774e7f42eb253045a4db7de7b79e",
  :record_type => "User",
  :record_id => 1
}
# File lib/loggable_activity/encryption_key.rb, line 39
def self.for_record_by_type_and_id(record_type, record_id, parent_key = nil)
  encryption_key = find_by(record_type:, record_id:)
  return encryption_key if encryption_key

  create_encryption_key(record_type, record_id, parent_key)
end
random_key() click to toggle source

Generates a random encryption key.

@return [String] The generated encryption key.

Example:

LoggableActivity::EncryptionKey.random_key

Returns:

"a8f4774e7f42eb253045a4db7de7b79e"
# File lib/loggable_activity/encryption_key.rb, line 109
def self.random_key
  SecureRandom.hex(16)
end

Public Instance Methods

mark_as_deleted() click to toggle source

Marks the encryption key as deleted by updating the key to nil.

# File lib/loggable_activity/encryption_key.rb, line 15
def mark_as_deleted
  update(key: nil)
  parent_key.mark_as_deleted if parent_key.present?
end