lib/xml/kit/templatable.rb in xml-kit-0.2.0 vs lib/xml/kit/templatable.rb in xml-kit-0.3.0

- old
+ new

@@ -16,24 +16,70 @@ # The [Xml::Kit::Certificate] that contains the public key to use for encrypting the document. attr_accessor :encryption_certificate # Returns the generated XML document with an XML Digital Signature and XML Encryption. - def to_xml(xml: ::Builder::XmlMarkup.new) - signatures.complete(render(self, xml: xml)) + 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 - 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 + # Generates an {#Xml::Kit::EncryptedKey} section. https://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 + 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 + + # @deprecated Use {#encrypt_data_for} instead of this + def encryption_for(*args, &block) + ::Xml::Kit.deprecate( + 'encryption_for is deprecated. Use encrypt_data_for instead.' + ) + encrypt_data_for(*args, &block) + end + + # Generates an {#Xml::Kit::EncryptedData} section. https://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 + 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 + + # Provides a default RSA asymmetric cipher. Can be overridden to provide custom ciphers. + # + # @abstract + # @since 0.3.0 + def asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM) + @asymmetric_cipher ||= Crypto.cipher_for( + algorithm, + encryption_certificate.public_key + ) + end + + # Provides a default aes256-cbc symmetric cipher. Can be overridden to provide custom ciphers. + # + # @abstract + # @since 0.3.0 + def symmetric_cipher + @symmetric_cipher ||= Crypto::SymmetricCipher.new end def render(model, options) ::Xml::Kit::Template.new(model).to_xml(options) end