Commit e844b18
Changed files (5)
lib
xml
kit
spec
xml
kit
lib/xml/kit/templates/key_info.builder
@@ -1,4 +1,6 @@
xml.KeyInfo xmlns: ::Xml::Kit::Namespaces::XMLDSIG do
+ xml.KeyName key_name if key_name
+ render(key_value, xml: xml) if @key_value
xml.EncryptedKey xmlns: ::Xml::Kit::Namespaces::XMLENC do
xml.EncryptionMethod Algorithm: algorithm
xml.CipherData do
lib/xml/kit/templates/key_value.builder
@@ -0,0 +1,3 @@
+xml.KeyValue do
+ render(rsa, xml: xml) if @rsa
+end
lib/xml/kit/templates/rsa_key_value.builder
@@ -0,0 +1,4 @@
+xml.RSAKeyValue do
+ xml.Modulus modulus
+ xml.Exponent exponent
+end
lib/xml/kit/key_info.rb
@@ -2,16 +2,30 @@
module Xml
module Kit
+ class RSAKeyValue
+ attr_accessor :modulus, :exponent
+ end
+
+ class KeyValue
+ include Templatable
+
+ def rsa
+ @rsa ||= RSAKeyValue.new
+ end
+ end
+
class KeyInfo
+ include Templatable
attr_reader :algorithm, :cipher_value
+ attr_accessor :key_name
def initialize(algorithm:, cipher_value:)
@algorithm = algorithm
@cipher_value = cipher_value
end
- def to_xml(xml: ::Builder::XmlMarkup.new)
- ::Xml::Kit::Template.new(self).to_xml(xml: xml)
+ def key_value
+ @key_value ||= KeyValue.new
end
end
end
spec/xml/kit/key_info_spec.rb
@@ -1,12 +1,41 @@
RSpec.describe Xml::Kit::KeyInfo do
+ subject { described_class.new(algorithm: algorithm, cipher_value: cipher_value) }
+ let(:algorithm) { 'asymmetric_cipher' }
+ let(:cipher_value) { Base64.strict_encode64('asymmetric CIPHERTEXT') }
+
describe "#to_xml" do
- subject { described_class.new(algorithm: algorithm, cipher_value: cipher_value) }
- let(:algorithm) { 'asymmetric_cipher' }
- let(:cipher_value) { Base64.strict_encode64('asymmetric CIPHERTEXT') }
- let(:result) { Hash.from_xml(subject.to_xml) }
-
- specify { expect(result['KeyInfo']).to be_present }
- specify { expect(result['KeyInfo']['EncryptedKey']['EncryptionMethod']['Algorithm']).to eql(algorithm) }
- specify { expect(result['KeyInfo']['EncryptedKey']['CipherData']['CipherValue']).to eql(cipher_value) }
+ context "with encrypted key" do
+
+ let(:result) { Hash.from_xml(subject.to_xml) }
+
+ specify { expect(result['KeyInfo']).to be_present }
+ specify { expect(result['KeyInfo']['EncryptedKey']['EncryptionMethod']['Algorithm']).to eql(algorithm) }
+ specify { expect(result['KeyInfo']['EncryptedKey']['CipherData']['CipherValue']).to eql(cipher_value) }
+ end
+
+ context "with key name" do
+ let(:result) { Hash.from_xml(subject.to_xml) }
+
+ before do
+ subject.key_name = "samlkey"
+ puts subject.to_xml(pretty: true)
+ end
+
+ specify { expect(result['KeyInfo']['KeyName']).to eql('samlkey') }
+ end
+
+ context "with key value" do
+ let(:result) { Hash.from_xml(subject.to_xml) }
+ let(:modulus) { 'xA7SEU+e0yQH5rm9kbCDN9o3aPIo7HbP7tX6WOocLZAtNfyxSZDU16ksL6WjubafOqNEpcwR3RdFsT7bCqnXPBe5ELh5u4VEy19MzxkXRgrMvavzyBpVRgBUwUlV5foK5hhmbktQhyNdy/6LpQRhDUDsTvK+g9Ucj47es9AQJ3U=' }
+ let(:exponent) { 'AQAB' }
+
+ before do
+ subject.key_value.rsa.modulus = modulus
+ subject.key_value.rsa.exponent = exponent
+ end
+
+ specify { expect(result['KeyInfo']['KeyValue']['RSAKeyValue']['Modulus']).to eql(modulus) }
+ specify { expect(result['KeyInfo']['KeyValue']['RSAKeyValue']['Exponent']).to eql(exponent) }
+ end
end
end