move order logic to xml_elements

This commit is contained in:
Benjamin Neff 2016-12-27 12:40:24 +01:00
parent 3e8534c5c4
commit a91e3c2c97
2 changed files with 18 additions and 6 deletions

View file

@ -146,19 +146,26 @@ module DiasporaFederation
Base64.strict_encode64(privkey.sign(DIGEST, signature_data))
end
# Sort all XML elements according to the order used for the signatures.
# It updates also the signatures with the keys of the author and the parent
# Update the signatures with the keys of the author and the parent
# if the signatures are not there yet and if the keys are available.
#
# @return [Hash] sorted xml elements with updated signatures
# @return [Hash] properties with updated signatures
def enriched_properties
data = super.merge(additional_xml_elements)
signature_order.map {|element| [element, data[element] || ""] }.to_h.tap do |hash|
super.merge(additional_xml_elements).tap do |hash|
hash[:author_signature] = author_signature || sign_with_author
hash[:parent_author_signature] = parent_author_signature || sign_with_parent_author_if_available.to_s
end
end
# Sort all XML elements according to the order used for the signatures.
#
# @return [Hash] sorted xml elements
def xml_elements
data = super
order = signature_order + %i(author_signature parent_author_signature)
order.map {|element| [element, data[element] || ""] }.to_h
end
# The order for signing
# @return [Array]
def signature_order

View file

@ -96,7 +96,7 @@ module DiasporaFederation
def to_xml
doc = Nokogiri::XML::DocumentFragment.new(Nokogiri::XML::Document.new)
Nokogiri::XML::Element.new(self.class.entity_name, doc).tap do |root_element|
enriched_properties.each do |name, value|
xml_elements.each do |name, value|
add_property_to_xml(doc, root_element, name, value)
end
end
@ -224,6 +224,11 @@ module DiasporaFederation
normalized_properties
end
# default: no special order
def xml_elements
enriched_properties
end
def add_property_to_xml(doc, root_element, name, value)
if value.is_a? String
root_element << simple_node(doc, name, value)