Class: Moxml::Signature::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/signature/serializer.rb

Overview

Translates a Model::Signature into a Moxml::Document.

All XML construction goes through moxml primitives (create_element, []=, add_child, create_text). No string templates, no manual attribute escaping — moxml handles entity encoding correctly per the XML spec.

Constant Summary collapse

DS_PREFIX =
"ds"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:) ⇒ Serializer

Returns a new instance of Serializer.



18
19
20
# File 'lib/moxml/signature/serializer.rb', line 18

def initialize(context:)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



16
17
18
# File 'lib/moxml/signature/serializer.rb', line 16

def context
  @context
end

Instance Method Details

#serialize(signature) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/moxml/signature/serializer.rb', line 22

def serialize(signature)
  doc = context.create_document
  root = create_namespaced_element("Signature", doc)
  root["Id"] = signature.id if signature.id
  doc.root = root

  if signature.signed_info
    root.add_child(build_signed_info(signature.signed_info, doc))
  end
  if signature.signature_value
    root.add_child(build_signature_value(signature.signature_value, doc))
  end
  if signature.key_info
    root.add_child(build_key_info(signature.key_info, doc))
  end
  signature.objects.each do |obj|
    root.add_child(build_object(obj, doc))
  end

  doc
end

#serialize_signed_info(signed_info) ⇒ Object

Produces a standalone ds:SignedInfo document. Used by the Signer when canonicalizing SignedInfo in isolation.



46
47
48
49
50
51
52
53
# File 'lib/moxml/signature/serializer.rb', line 46

def serialize_signed_info(signed_info)
  doc = context.create_document
  root = create_namespaced_element("SignedInfo", doc)
  root["Id"] = signed_info.id if signed_info.id
  doc.root = root
  populate_signed_info(root, signed_info, doc)
  doc
end