Commit 758a602
Changed files (3)
lib
xml
kit
templates
spec
lib/xml/kit/templates/certificate.builder
@@ -1,4 +1,4 @@
-xml.KeyDescriptor use: use do
+xml.KeyDescriptor use ? { use: use } : {} do
xml.KeyInfo "xmlns": ::Xml::Kit::Namespaces::XMLDSIG do
xml.X509Data do
xml.X509Certificate stripped
lib/xml/kit/certificate.rb
@@ -106,6 +106,11 @@ module Xml
x509.not_before
end
+ def to_xml(pretty: false, xml: ::Builder::XmlMarkup.new)
+ xml = ::Xml::Kit::Template.new(self).to_xml(xml: xml)
+ pretty ? Nokogiri::XML(xml).to_xml(indent: 2) : xml
+ end
+
class << self
def to_x509(value)
return value if value.is_a?(OpenSSL::X509::Certificate)
spec/xml/certificate_spec.rb
@@ -139,4 +139,21 @@ RSpec.describe Xml::Kit::Certificate do
expect(subject.not_before).to eql(certificate.not_before)
end
end
+
+ describe "#to_xml" do
+ it 'generates the correct xml' do
+ result = Hash.from_xml(subject.to_xml)
+ expect(result['KeyDescriptor']).to be_present
+ expect(result['KeyDescriptor']['use']).to eql('signing')
+ expect(result['KeyDescriptor']['KeyInfo']['xmlns']).to eql(Xml::Kit::Namespaces::XMLDSIG)
+ expect(result['KeyDescriptor']['KeyInfo']['X509Data']['X509Certificate']).to eql(subject.stripped)
+ end
+
+ it 'omits the `use` when the cert can be used for both signing and encryption' do
+ subject = described_class.new(certificate, use: nil)
+ result = Hash.from_xml(subject.to_xml)
+ expect(result['KeyDescriptor']).to be_present
+ expect(result['KeyDescriptor']['use']).to be_nil
+ end
+ end
end