Module: Xml::Kit::Templatable

Defined in:
lib/xml/kit/templatable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#embed_signatureObject

Can be used to disable embeding a signature. By default a signature will be embedded if a signing certificate is available.



9
10
11
# File 'lib/xml/kit/templatable.rb', line 9

def embed_signature
  @embed_signature
end

#encryptObject

Used to enable/disable encrypting the document.



12
13
14
# File 'lib/xml/kit/templatable.rb', line 12

def encrypt
  @encrypt
end

#encryption_certificateObject

The [Xml::Kit::Certificate] that contains the public key to use for encrypting the document.



18
19
20
# File 'lib/xml/kit/templatable.rb', line 18

def encryption_certificate
  @encryption_certificate
end

#signing_key_pairObject

The [Xml::Kit::KeyPair] to use for generating a signature.



15
16
17
# File 'lib/xml/kit/templatable.rb', line 15

def signing_key_pair
  @signing_key_pair
end

Instance Method Details

#encrypt_with(certificate) ⇒ Object

Allows you to specify which public key to use for generating an XML encrypted element.

Parameters:

  • certificate (Xml::Kit::Certificate)

    the certificate containing the public key to use for encryption.



59
60
61
62
# File 'lib/xml/kit/templatable.rb', line 59

def encrypt_with(certificate)
  self.encrypt = true
  self.encryption_certificate = certificate
end

#encryption_for(xml:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/xml/kit/templatable.rb', line 25

def encryption_for(xml:)
  if encrypt?
    temp = ::Builder::XmlMarkup.new
    yield temp
    ::Xml::Kit::Encryption.new(
      signatures.complete(temp.target!),
      encryption_certificate.public_key
    ).to_xml(xml: xml)
  else
    yield xml
  end
end

#render(model, options) ⇒ Object



38
39
40
# File 'lib/xml/kit/templatable.rb', line 38

def render(model, options)
  ::Xml::Kit::Template.new(model).to_xml(options)
end

#sign_with(key_pair) ⇒ Object

Allows you to specify which key pair to use for generating an XML digital signature.

Parameters:



50
51
52
53
54
# File 'lib/xml/kit/templatable.rb', line 50

def sign_with(key_pair)
  self.signing_key_pair = key_pair
  self.embed_signature = true
  signatures.sign_with(key_pair)
end

#signature_for(reference_id:, xml:) ⇒ Object



42
43
44
45
# File 'lib/xml/kit/templatable.rb', line 42

def signature_for(reference_id:, xml:)
  return unless sign?
  signatures.build(reference_id).to_xml(xml: xml)
end

#to_xml(xml: ::Builder::XmlMarkup.new) ⇒ Object

Returns the generated XML document with an XML Digital Signature and XML Encryption.



21
22
23
# File 'lib/xml/kit/templatable.rb', line 21

def to_xml(xml: ::Builder::XmlMarkup.new)
  signatures.complete(render(self, xml: xml))
end