Commit 849ec1b

mokha <mo@mokhan.ca>
2019-01-21 23:37:43
allow the injection of a custom key info
1 parent 669cb2b
Changed files (3)
lib/xml/kit/templates/encrypted_key.builder
@@ -1,5 +1,6 @@
 xml.EncryptedKey Id: id, xmlns: ::Xml::Kit::Namespaces::XMLENC do
   xml.EncryptionMethod Algorithm: algorithm
+  render(key_info, xml: xml) if key_info
   xml.CipherData do
     xml.CipherValue cipher_value
   end
lib/xml/kit/encrypted_key.rb
@@ -3,16 +3,19 @@ require 'xml/kit/templatable'
 module Xml
   module Kit
     class EncryptedKey
+      DEFAULT_ALGORITHM = ::Xml::Kit::Crypto::RsaCipher::ALGORITHM
       include ::Xml::Kit::Templatable
 
       attr_reader :id, :algorithm
       attr_reader :public_key, :key
+      attr_accessor :key_info
 
-      def initialize(id:, public_key:, key:, algorithm: ::Xml::Kit::Crypto::RsaCipher::ALGORITHM)
+      def initialize(id:, public_key:, key:, key_info: nil, algorithm: DEFAULT_ALGORITHM)
         @id = id
         @algorithm = algorithm
         @public_key = public_key
         @key = key
+        @key_info = key_info
       end
 
       def cipher_value
spec/xml/kit/encrypted_key_spec.rb
@@ -1,7 +1,8 @@
 RSpec.describe ::Xml::Kit::EncryptedKey do
   describe "#to_xml" do
-    subject { described_class.new(id: id, algorithm: algorithm, public_key: public_key, key: symmetric_key) }
+    subject { described_class.new(id: id, algorithm: algorithm, public_key: public_key, key: symmetric_key, key_info: key_info) }
     let(:algorithm) { ::Xml::Kit::Crypto::RsaCipher::ALGORITHM }
+    let(:key_info) { ::Xml::Kit::KeyInfo.new(algorithm: algorithm, cipher_value: '') }
     let(:id) { ::Xml::Kit::Id.generate }
     let(:private_key) { OpenSSL::PKey::RSA.new(2048) }
     let(:public_key) { private_key.public_key }
@@ -9,10 +10,6 @@ RSpec.describe ::Xml::Kit::EncryptedKey do
     let(:symmetric_key) { SecureRandom.hex(32) }
     let(:result) { Hash.from_xml(subject.to_xml) }
 
-    before do
-      puts subject.to_xml(pretty: true)
-    end
-
     specify { expect(result.key?('EncryptedKey')).to be_present }
     specify { expect(result['EncryptedKey']['Id']).to eql(id) }
     specify { expect(result['EncryptedKey']['xmlns']).to eql(::Xml::Kit::Namespaces::XMLENC) }
@@ -20,5 +17,6 @@ RSpec.describe ::Xml::Kit::EncryptedKey do
     specify { expect(result['EncryptedKey']['CipherData']['CipherValue']).to be_present }
     specify { expect(private_key.private_decrypt(Base64.decode64(result['EncryptedKey']['CipherData']['CipherValue']))).to eql(symmetric_key) }
     specify { expect(subject.to_xml).to match_xsd('xenc-schema') }
+    specify { expect(result['EncryptedKey'].key?('KeyInfo')).to be(true) }
   end
 end