Module: ActiveJob::Temporal::PayloadEncryption

Extended by:
PayloadEncryption
Included in:
PayloadEncryption
Defined in:
lib/activejob/temporal/payload_encryption.rb

Defined Under Namespace

Classes: KeyEntry

Constant Summary collapse

CIPHER =
"aes-256-gcm"
LEGACY_VERSION =
1
VERSION =
2
DEFAULT_KEY_ID =
"primary"
KEY_ID_PATTERN =
/\A[A-Za-z0-9_.:-]{1,128}\z/
V2_IV_BYTES =
12

Instance Method Summary collapse

Instance Method Details

#decode_key(value) ⇒ Object



45
46
47
48
49
# File 'lib/activejob/temporal/payload_encryption.rb', line 45

def decode_key(value)
  Base64.strict_decode64(value.to_s)
rescue ArgumentError
  nil
end

#decrypt(payload, config, context: nil) ⇒ Object

Raises:

  • (ActiveJob::SerializationError)


33
34
35
36
37
38
39
# File 'lib/activejob/temporal/payload_encryption.rb', line 33

def decrypt(payload, config, context: nil)
  version = payload[:encrypted_payload_version] || payload["encrypted_payload_version"]
  return decrypt_legacy(payload, config) if version == LEGACY_VERSION
  return decrypt_v2(payload, config, context) if version == VERSION

  raise ActiveJob::SerializationError, "Unsupported encrypted payload version: #{version.inspect}"
end

#encrypt(payload, config, context: nil) ⇒ Object



27
28
29
30
31
# File 'lib/activejob/temporal/payload_encryption.rb', line 27

def encrypt(payload, config, context: nil)
  return encrypt_legacy(payload, config) unless context

  encrypt_v2(payload, config, context)
end

#encrypted?(payload) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/activejob/temporal/payload_encryption.rb', line 23

def encrypted?(payload)
  payload[:encrypted_payload] == true || payload["encrypted_payload"] == true
end

#key_lengthObject



41
42
43
# File 'lib/activejob/temporal/payload_encryption.rb', line 41

def key_length
  ActiveSupport::MessageEncryptor.key_len(CIPHER)
end

#valid_key?(value) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/activejob/temporal/payload_encryption.rb', line 51

def valid_key?(value)
  !key_entry(value, fallback_id: DEFAULT_KEY_ID).nil?
end