Class: ActiveRecord::Encryption::MessageSerializer
Relationships & Source Files | |
Inherits: | Object |
Defined in: | activerecord/lib/active_record/encryption/message_serializer.rb |
Overview
A message serializer that serializes Messages
with JSON.
The generated structure is pretty simple:
{
p: <payload>,
h: {
header1: value1,
header2: value2,
#...
}
}
Both the payload and the header values are encoded with Base64 to prevent JSON parsing errors and encoding issues when storing the resulting serialized data.
Instance Attribute Summary
- #binary? ⇒ Boolean readonly
Instance Method Summary
- #dump(message)
- #load(serialized_content)
- #decode_if_needed(value) private
- #encode_if_needed(value) private
- #headers_to_json(headers) private
- #message_to_json(message) private
- #parse_message(data, level) private
- #parse_properties(headers, level) private
- #validate_message_data_format(data, level) private
Instance Attribute Details
#binary? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activerecord/lib/active_record/encryption/message_serializer.rb', line 36
def binary? false end
Instance Method Details
#decode_if_needed(value) (private)
[ GitHub ]#dump(message)
# File 'activerecord/lib/active_record/encryption/message_serializer.rb', line 31
def dump( ) raise ActiveRecord::Encryption::Errors::ForbiddenClass unless .is_a?(ActiveRecord::Encryption::Message) JSON.dump ( ) end
#encode_if_needed(value) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_serializer.rb', line 77
def encode_if_needed(value) if value.is_a?(String) ::Base64.strict_encode64 value else value end end
#headers_to_json(headers) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_serializer.rb', line 71
def headers_to_json(headers) headers.transform_values do |value| value.is_a?(ActiveRecord::Encryption::Message) ? (value) : encode_if_needed(value) end end
#load(serialized_content)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_serializer.rb', line 24
def load(serialized_content) data = JSON.parse(serialized_content) (data, 1) rescue JSON::ParserError raise ActiveRecord::Encryption::Errors::Encoding end
#message_to_json(message) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_serializer.rb', line 64
def ( ) { p: encode_if_needed( .payload), h: headers_to_json( .headers) } end
#parse_message(data, level) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_serializer.rb', line 41
def (data, level) (data, level) ActiveRecord::Encryption::Message.new(payload: decode_if_needed(data["p"]), headers: parse_properties(data["h"], level)) end
#parse_properties(headers, level) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_serializer.rb', line 56
def parse_properties(headers, level) ActiveRecord::Encryption::Properties.new.tap do |properties| headers&.each do |key, value| properties[key] = value.is_a?(Hash) ? (value, level + 1) : decode_if_needed(value) end end end
#validate_message_data_format(data, level) (private)
[ GitHub ]# File 'activerecord/lib/active_record/encryption/message_serializer.rb', line 46
def (data, level) if level > 2 raise ActiveRecord::Encryption::Errors::Decryption, "More than one level of hash nesting in headers is not supported" end unless data.is_a?(Hash) && data.has_key?("p") raise ActiveRecord::Encryption::Errors::Decryption, "Invalid data format: hash without payload" end end