Module: Xml::Kit::Templatable

Included in:
Certificate, EncryptedKey, KeyInfo, KeyInfo::KeyValue
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

#asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM) ⇒ Object

This method is abstract.

Provides a default RSA asymmetric cipher. Can be overridden to provide custom ciphers.

Since:

  • 0.3.0



69
70
71
72
73
74
# File 'lib/xml/kit/templatable.rb', line 69

def asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM)
  @asymmetric_cipher ||= Crypto.cipher_for(
    algorithm,
    encryption_certificate.public_key
  )
end

#encrypt_data_for(xml:, key_info: nil) {|temp| ... } ⇒ Object

Parameters:

  • xml (Builder::XmlMarkup)

    the xml builder instance

  • key_info (Xml::Kit::KeyInfo) (defaults to: nil)

    the key info to render in the EncryptedData

Yields:

  • (temp)

Since:

  • 0.3.0



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xml/kit/templatable.rb', line 52

def encrypt_data_for(xml:, key_info: nil)
  return yield xml unless encrypt?

  temp = ::Builder::XmlMarkup.new
  yield temp
  ::Xml::Kit::EncryptedData.new(
    signatures.complete(temp.target!),
    symmetric_cipher: symmetric_cipher,
    asymmetric_cipher: asymmetric_cipher,
    key_info: key_info
  ).to_xml(xml: xml)
end

#encrypt_key_for(xml:, id:) ⇒ Object

Parameters:

  • xml (Builder::XmlMarkup)

    the xml builder instance

  • id (String)

    the id of EncryptedKey element

Since:

  • 0.3.0



31
32
33
34
35
36
37
# File 'lib/xml/kit/templatable.rb', line 31

def encrypt_key_for(xml:, id:)
  ::Xml::Kit::EncryptedKey.new(
    id: id,
    asymmetric_cipher: asymmetric_cipher,
    symmetric_cipher: symmetric_cipher
  ).to_xml(xml: xml)
end

#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.



106
107
108
109
# File 'lib/xml/kit/templatable.rb', line 106

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

#encryption_for(*args, &block) ⇒ Object

Deprecated.

Use #encrypt_data_for instead of this



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

def encryption_for(*args, &block)
  ::Xml::Kit.deprecate(
    'encryption_for is deprecated. Use encrypt_data_for instead.'
  )
  encrypt_data_for(*args, &block)
end

#render(model, options) ⇒ Object



84
85
86
# File 'lib/xml/kit/templatable.rb', line 84

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:



97
98
99
100
101
# File 'lib/xml/kit/templatable.rb', line 97

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



88
89
90
91
92
# File 'lib/xml/kit/templatable.rb', line 88

def signature_for(reference_id:, xml:)
  return unless sign?

  signatures.build(reference_id).to_xml(xml: xml)
end

#symmetric_cipherObject

This method is abstract.

Provides a default aes256-cbc symmetric cipher. Can be overridden to provide custom ciphers.

Since:

  • 0.3.0



80
81
82
# File 'lib/xml/kit/templatable.rb', line 80

def symmetric_cipher
  @symmetric_cipher ||= Crypto::SymmetricCipher.new
end

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

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



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

def to_xml(xml: ::Builder::XmlMarkup.new, pretty: false)
  result = signatures.complete(render(self, xml: xml))
  pretty ? Nokogiri::XML(result).to_xml(indent: 2) : result
end