Skip to main content

Encryption Factory Function

Overviewā€‹

To implement aggregate or read model encryption, you need to define an encryption factory function. This function receives data about the currently processed operation and returns an object that contains your implementation of encrypt and decrypt functions.

Aggregate Encryptionā€‹

An aggregate encryption function has the following structure:

// common/aggregates/encryption.js
const createEncryption = (aggregateId, context) => {
...
// Returns an object that contains 'encrypt' and 'decrypt' functions
return {
encrypt: (data) => ..., // A function that takes data and returns its encrypted version
decrypt: (blob) => ..., // A function that takes an encrypted blob and returns unencrypted data
}
}
export default createEncryption

Arguments

NameTypeDescription
aggregateIdstringThe aggregate ID associated with the current operation.
contextAn aggregate encryption context objectContains data and API related to the current operation.

Result

The returned value should be an object of the following structure:

{
encrypt: (data) => ..., // A function that takes data and returns its encrypted version
decrypt: (blob) => ..., // A function that takes an encrypted blob and returns unencrypted data
}

Read Model Encryptionā€‹

A read model encryption function has the following structure:

// common/read-models/encryption.js
const createEncryption = (event, context) => {
...
// Returns an object that contains 'encrypt' and 'decrypt' functions
return {
encrypt: (data) => ..., // A function that takes data and returns its encrypted version
decrypt: (blob) => ..., // A function that takes an encrypted blob and returns unencrypted data
}
}
export default createEncryption

Arguments

NameTypeDescription
eventeventThe currently processed event.
contextAn event handler encryption context objectContains data and API related to the current operation.

Result

The returned value should be an object of the following structure:

{
encrypt: (data) => ..., // A function that takes data and returns its encrypted version
decrypt: (blob) => ..., // A function that takes an encrypted blob and returns unencrypted data
}

Aggregate Encryption Contextā€‹

The aggregate encryption context object has the following fields:

NameTypeDescription
jwt?stringA JSON Web Token attached to the current request.
secretsManagerA secrets manager objectExposes API used to store cryptographic secrets in the event store.

Event Handler Encryption Contextā€‹

The event handler encryption context object has the following fields:

NameTypeDescription
secretsManagerA secrets manager objectExposes API used to store cryptographic secrets in the event store.

See Alsoā€‹