module Xml::Kit::Templatable

Attributes

digest_method[RW]

Allows you to specify the digest method algorithm. (Default: SHA256) A list of digest methods can be found in [Xml::Kit::Signature].

embed_signature[RW]

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

encrypt[RW]

Used to enable/disable encrypting the document.

encryption_certificate[RW]

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

signature_method[RW]

Allows you to specify the signature method algorithm. (Default: SHA256) A list of signature methods can be found in [Xml::Kit::Signature].

signing_key_pair[RW]

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

Public Instance Methods

asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM) click to toggle source

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

@abstract @since 0.3.0

# File lib/xml/kit/templatable.rb, line 78
def asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM)
  raise Xml::Kit::Error, 'encryption_certificate is not specified.' unless encryption_certificate

  @asymmetric_cipher ||= Crypto.cipher_for(
    algorithm,
    encryption_certificate.public_key
  )
end
encrypt_data_for(xml:, key_info: nil) { |xml| ... } click to toggle source

Generates an {#Xml::Kit::EncryptedData} section. www.w3.org/TR/xmlenc-core1/#sec-EncryptedData

@since 0.3.0 @param xml [Builder::XmlMarkup] the xml builder instance @param key_info [Xml::Kit::KeyInfo] the key info to render in the EncryptedData

# File lib/xml/kit/templatable.rb, line 61
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:, key_info: nil) click to toggle source

Generates an {#Xml::Kit::EncryptedKey} section. www.w3.org/TR/xmlenc-core1/#sec-EncryptedKey

@since 0.3.0 @param xml [Builder::XmlMarkup] the xml builder instance @param id [String] the id of EncryptedKey element

# File lib/xml/kit/templatable.rb, line 39
def encrypt_key_for(xml:, id:, key_info: nil)
  ::Xml::Kit::EncryptedKey.new(
    id: id,
    asymmetric_cipher: asymmetric_cipher,
    symmetric_cipher: symmetric_cipher,
    key_info: key_info
  ).to_xml(xml: xml)
end
encrypt_with(certificate) click to toggle source

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

@param certificate [Xml::Kit::Certificate] the certificate containing the public key to use for encryption.

# File lib/xml/kit/templatable.rb, line 119
def encrypt_with(certificate)
  self.encrypt = true
  self.encryption_certificate = certificate
end
encryption_for(*args, &block) click to toggle source

@deprecated Use {#encrypt_data_for} instead of this

# File lib/xml/kit/templatable.rb, line 49
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) click to toggle source
# File lib/xml/kit/templatable.rb, line 95
def render(model, options)
  ::Xml::Kit::Template.new(model).to_xml(options)
end
sign_with(key_pair, signature_method: :SHA256, digest_method: :SHA256) click to toggle source

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

@param key_pair [Xml::Kit::KeyPair] the key pair to use for signing.

# File lib/xml/kit/templatable.rb, line 108
def sign_with(key_pair, signature_method: :SHA256, digest_method: :SHA256)
  self.signing_key_pair = key_pair
  self.embed_signature = true
  self.signature_method = signature_method
  self.digest_method = digest_method
  signatures.sign_with(key_pair)
end
signature_for(reference_id:, xml:) click to toggle source
# File lib/xml/kit/templatable.rb, line 99
def signature_for(reference_id:, xml:)
  return unless sign?

  signatures.build(reference_id).to_xml(xml: xml)
end
symmetric_cipher() click to toggle source

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

@abstract @since 0.3.0

# File lib/xml/kit/templatable.rb, line 91
def symmetric_cipher
  @symmetric_cipher ||= Crypto::SymmetricCipher.new
end
to_xml(xml: ::Builder::XmlMarkup.new, pretty: false) click to toggle source

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

# File lib/xml/kit/templatable.rb, line 29
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

Private Instance Methods

encrypt?() click to toggle source

@!visibility private

# File lib/xml/kit/templatable.rb, line 140
def encrypt?
  encrypt && encryption_certificate
end
sign?() click to toggle source
# File lib/xml/kit/templatable.rb, line 126
def sign?
  embed_signature
end
signatures() click to toggle source

@!visibility private

# File lib/xml/kit/templatable.rb, line 131
def signatures
  @signatures ||= ::Xml::Kit::Signatures.new(
    key_pair: signing_key_pair,
    digest_method: digest_method || :SHA256,
    signature_method: signature_method || :SHA256
  )
end