Commit f737798

mo <mo.khan@gmail.com>
2018-03-03 19:09:36
provide api to specify encryption public key.
1 parent 386c075
Changed files (2)
lib/xml/kit/templatable.rb
@@ -53,6 +53,14 @@ module Xml
         signatures.sign_with(key_pair)
       end
 
+      # 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.
+      def encrypt_with(certificate)
+        self.encrypt = true
+        self.encryption_certificate = certificate
+      end
+
       private
 
       def sign?
spec/xml/kit/templatable_spec.rb
@@ -49,4 +49,20 @@ RSpec.describe ::Xml::Kit::Templatable do
       expect(xml_hash['HelloWorld']).to be_present
     end
   end
+
+  describe "#encrypt_with" do
+    it 'returns an encrypted xml' do
+      key_pair = ::Xml::Kit::KeyPair.generate(use: :encryption)
+      subject.encrypt_with(key_pair.certificate)
+
+      result = subject.encryption_for(xml: ::Builder::XmlMarkup.new) do |xml|
+        xml.HelloWorld Time.now.iso8601
+      end
+
+      expect(result).to include('EncryptedData')
+      xml_hash = Hash.from_xml(result)
+      expect(xml_hash['EncryptedData']).to be_present
+      expect(xml_hash['EncryptedData']['EncryptionMethod']).to be_present
+    end
+  end
 end