Class: EventStoreClient::Mapper::Encrypted

Inherits:
Object
  • Object
show all
Defined in:
lib/event_store_client/mapper/encrypted.rb

Overview

Transforms given event’s data and encrypts/decrypts selected subset of data based on encryption schema stored in the event itself.

Constant Summary collapse

MissingEncryptionKey =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#deserialize(event, skip_decryption: false) ⇒ Object

Decrypts the given event’s subset of data. General Event instance with encrypted data

  • #data - hash with encrypted values.

  • encryption_metadata - hash with information which data to encrypt and which key should be used as an identifier.

Returns: Specific event class with all data decrypted



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/event_store_client/mapper/encrypted.rb', line 52

def deserialize(event, skip_decryption: false)
   = serializer.deserialize(event.)
  encryption_schema = ['encryption']

  decrypted_data =
    if skip_decryption
      serializer.deserialize(event.data)
    else
      EventStoreClient::DataDecryptor.new(
        data: serializer.deserialize(event.data),
        schema: encryption_schema,
        repository: key_repository
      ).call
    end

  event_class =
    begin
      Object.const_get(event.type)
    rescue NameError
      EventStoreClient.config.default_event_class
    end

  event_class.new(
    skip_validation: true,
    id: event.id,
    type: event.type,
    title: event.title,
    data: decrypted_data,
    metadata: 
  )
end

#serialize(event) ⇒ Object

Encrypts the given event’s subset of data. Accepts specific event class instance with:

  • #data - hash with non-encrypted values.

  • encryption_schema - hash with information which data to encrypt and which key should be used as an identifier.

Returns: General Event instance with encrypted data



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/event_store_client/mapper/encrypted.rb', line 24

def serialize(event)
  encryption_schema = (
    event.class.respond_to?(:encryption_schema) &&
    event.class.encryption_schema
  )
  encryptor = EventStoreClient::DataEncryptor.new(
    data: event.data,
    schema: encryption_schema,
    repository: key_repository
  )
  encryptor.call
  EventStoreClient::Event.new(
    data: serializer.serialize(encryptor.encrypted_data),
    metadata: serializer.serialize(
      event..merge(encryption: encryptor.)
    ),
    type: event.class.to_s
  )
end