Class: EventStoreClient::Mapper::Encrypted
- Inherits:
-
Object
- Object
- EventStoreClient::Mapper::Encrypted
- 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
-
#deserialize(event_or_raw_event, skip_decryption: false) ⇒ Object
Decrypts the given event’s subset of data.
-
#initialize(key_repository, config:, serializer: Serializer::Json) ⇒ Encrypted
constructor
A new instance of Encrypted.
- #serialize(event) ⇒ Hash
Constructor Details
#initialize(key_repository, config:, serializer: Serializer::Json) ⇒ Encrypted
Returns a new instance of Encrypted.
23 24 25 26 27 |
# File 'lib/event_store_client/mapper/encrypted.rb', line 23 def initialize(key_repository, config:, serializer: Serializer::Json) @key_repository = key_repository @config = config @serializer = serializer end |
Instance Method Details
#deserialize(event_or_raw_event, skip_decryption: false) ⇒ Object
Decrypts the given event’s subset of data.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/event_store_client/mapper/encrypted.rb', line 56 def deserialize(event_or_raw_event, skip_decryption: false) if skip_decryption return Default.new(serializer: serializer, config: config).deserialize(event_or_raw_event) end event = if event_or_raw_event.is_a?(EventStoreClient::DeserializedEvent) event_or_raw_event else Serializer::EventDeserializer.call( event_or_raw_event, config: config, serializer: serializer ) end decrypted_data = EventStoreClient::DataDecryptor.new( data: event.data, schema: event.['encryption'], repository: key_repository ).call event.class.new(**event.to_h.merge(data: decrypted_data, skip_validation: true)) end |
#serialize(event) ⇒ Hash
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/event_store_client/mapper/encrypted.rb', line 31 def serialize(event) # Links don't need to be encrypted return Default.new(serializer: serializer, config: config).serialize(event) if event.link? serialized = Serializer::EventSerializer.call(event, serializer: serializer, config: config) encryption_schema = if event.class.respond_to?(:encryption_schema) event.class.encryption_schema end encryptor = EventStoreClient::DataEncryptor.new( data: serialized.data, schema: encryption_schema, repository: key_repository ) encryptor.call serialized.data = encryptor.encrypted_data serialized.['encryption'] = encryptor. serialized end |