Commit 9e6196d

mo <mo.khan@gmail.com>
2017-11-12 21:58:58
add signature to logout request.
1 parent 7c97171
Changed files (2)
lib/saml/kit/logout_request.rb
@@ -3,6 +3,7 @@ module Saml
     class LogoutRequest
       class Builder
         attr_accessor :id, :destination, :issuer, :name_id_format, :now
+        attr_accessor :sign
         attr_reader :user
 
         def initialize(user, configuration: Saml::Kit.configuration)
@@ -11,23 +12,25 @@ module Saml
           @issuer = configuration.issuer
           @name_id_format = Saml::Kit::Namespaces::PERSISTENT
           @now = Time.now.utc
+          @sign = true
         end
 
         def to_xml
-          xml = ::Builder::XmlMarkup.new
-          xml.instruct!
-          xml.LogoutRequest logout_request_options do
-            xml.Issuer issuer
-            xml.NameID name_id_options, user.name_id_for(self)
+          Signature.sign(id, sign: sign) do |xml, signature|
+            xml.instruct!
+            xml.LogoutRequest logout_request_options do
+              xml.Issuer issuer
+              signature.template(xml)
+              xml.NameID name_id_options, user.name_id_for(self)
+            end
           end
-          xml.target!
         end
 
         private
 
         def logout_request_options
           {
-            ID: id,
+            ID: "_#{id}",
             Version: "2.0",
             IssueInstant: now.utc.iso8601,
             Destination: destination,
spec/saml/logout_request_spec.rb
@@ -16,7 +16,7 @@ RSpec.describe Saml::Kit::LogoutRequest do
       result = subject.to_xml
       xml_hash = Hash.from_xml(result)
 
-      expect(xml_hash['LogoutRequest']['ID']).to eql(subject.id)
+      expect(xml_hash['LogoutRequest']['ID']).to eql("_#{subject.id}")
       expect(xml_hash['LogoutRequest']['Version']).to eql("2.0")
       expect(xml_hash['LogoutRequest']['IssueInstant']).to eql(Time.now.utc.iso8601)
       expect(xml_hash['LogoutRequest']['Destination']).to eql(subject.destination)
@@ -25,5 +25,20 @@ RSpec.describe Saml::Kit::LogoutRequest do
       expect(xml_hash['LogoutRequest']['NameID']).to eql(name_id)
       expect(result).to have_xpath("//LogoutRequest//NameID[@Format=\"#{subject.name_id_format}\"]")
     end
+
+    it 'includes a signature by default' do
+      travel_to 1.second.from_now
+      xml_hash = Hash.from_xml(subject.to_xml)
+
+      expect(xml_hash['LogoutRequest']['Signature']).to be_present
+    end
+
+    it 'excludes a signature' do
+      travel_to 1.second.from_now
+      subject.sign = false
+      xml_hash = Hash.from_xml(subject.to_xml)
+
+      expect(xml_hash['LogoutRequest']['Signature']).to be_nil
+    end
   end
 end