Commit 94e63bb

mo <mo@mokhan.ca>
2017-11-29 02:46:02
move logout response to separate file.
1 parent df52195
lib/saml/kit/builders/logout_response.rb
@@ -0,0 +1,54 @@
+module Saml
+  module Kit
+    class LogoutResponse < Document
+      class Builder
+        attr_accessor :id, :issuer, :version, :status_code, :sign, :now, :destination
+        attr_reader :request
+
+        def initialize(user, request, configuration: Saml::Kit.configuration, sign: true)
+          @user = user
+          @now = Time.now.utc
+          @request = request
+          @id = SecureRandom.uuid
+          @version = "2.0"
+          @status_code = Namespaces::SUCCESS
+          @sign = sign
+          @issuer = configuration.issuer
+          provider = configuration.registry.metadata_for(@issuer)
+          if provider
+            @destination = provider.single_logout_service_for(binding: :http_post).try(:location)
+          end
+        end
+
+        def to_xml
+          Signature.sign(sign: sign) do |xml, signature|
+            xml.LogoutResponse logout_response_options do
+              xml.Issuer(issuer, xmlns: Namespaces::ASSERTION)
+              signature.template(id)
+              xml.Status do
+                xml.StatusCode Value: status_code
+              end
+            end
+          end
+        end
+
+        def build
+          LogoutResponse.new(to_xml, request_id: request.id)
+        end
+
+        private
+
+        def logout_response_options
+          {
+            xmlns: Namespaces::PROTOCOL,
+            ID: "_#{id}",
+            Version: version,
+            IssueInstant: now.utc.iso8601,
+            Destination: destination,
+            InResponseTo: request.id,
+          }
+        end
+      end
+    end
+  end
+end
lib/saml/kit/builders.rb
@@ -1,3 +1,4 @@
 require 'saml/kit/builders/authentication_request'
 require 'saml/kit/builders/identity_provider_metadata'
 require 'saml/kit/builders/logout_request'
+require 'saml/kit/builders/logout_response'
lib/saml/kit/logout_response.rb
@@ -7,57 +7,6 @@ module Saml
         @request_id = request_id
         super(xml, name: "LogoutResponse")
       end
-
-      private
-
-      class Builder
-        attr_accessor :id, :issuer, :version, :status_code, :sign, :now, :destination
-        attr_reader :request
-
-        def initialize(user, request, configuration: Saml::Kit.configuration, sign: true)
-          @user = user
-          @now = Time.now.utc
-          @request = request
-          @id = SecureRandom.uuid
-          @version = "2.0"
-          @status_code = Namespaces::SUCCESS
-          @sign = sign
-          @issuer = configuration.issuer
-          provider = configuration.registry.metadata_for(@issuer)
-          if provider
-            @destination = provider.single_logout_service_for(binding: :http_post).try(:location)
-          end
-        end
-
-        def to_xml
-          Signature.sign(sign: sign) do |xml, signature|
-            xml.LogoutResponse logout_response_options do
-              xml.Issuer(issuer, xmlns: Namespaces::ASSERTION)
-              signature.template(id)
-              xml.Status do
-                xml.StatusCode Value: status_code
-              end
-            end
-          end
-        end
-
-        def build
-          LogoutResponse.new(to_xml, request_id: request.id)
-        end
-
-        private
-
-        def logout_response_options
-          {
-            xmlns: Namespaces::PROTOCOL,
-            ID: "_#{id}",
-            Version: version,
-            IssueInstant: now.utc.iso8601,
-            Destination: destination,
-            InResponseTo: request.id,
-          }
-        end
-      end
     end
   end
 end
spec/saml/builders/logout_request_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+RSpec.describe Saml::Kit::LogoutRequest::Builder do
+  subject { described_class.new(user) }
+  let(:user) { double(:user, name_id_for: name_id) }
+  let(:name_id) { SecureRandom.uuid }
+
+  it 'produces the expected xml' do
+    travel_to 1.second.from_now
+    subject.id = SecureRandom.uuid
+    subject.destination = FFaker::Internet.http_url
+    subject.issuer = FFaker::Internet.http_url
+    subject.name_id_format = Saml::Kit::Namespaces::TRANSIENT
+
+    result = subject.to_xml
+    xml_hash = Hash.from_xml(result)
+
+    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)
+
+    expect(xml_hash['LogoutRequest']['Issuer']).to eql(subject.issuer)
+    expect(xml_hash['LogoutRequest']['NameID']).to eql(name_id)
+    expect(result).to have_xpath("//samlp:LogoutRequest//saml:NameID[@Format=\"#{subject.name_id_format}\"]")
+  end
+
+  it 'includes a signature by default' do
+    xml_hash = Hash.from_xml(subject.to_xml)
+    expect(xml_hash['LogoutRequest']['Signature']).to be_present
+  end
+
+  it 'excludes a signature' do
+    subject.sign = false
+    xml_hash = Hash.from_xml(subject.to_xml)
+    expect(xml_hash['LogoutRequest']['Signature']).to be_nil
+  end
+
+  it 'builds a LogoutRequest' do
+    travel_to 1.second.from_now
+    result = subject.build
+    expect(result).to be_instance_of(Saml::Kit::LogoutRequest)
+    expect(result.to_xml).to eql(subject.to_xml)
+  end
+end
spec/saml/builders/logout_response_spec.rb
@@ -1,45 +1,32 @@
 require 'spec_helper'
 
-RSpec.describe Saml::Kit::LogoutRequest::Builder do
-  subject { described_class.new(user) }
-  let(:user) { double(:user, name_id_for: name_id) }
-  let(:name_id) { SecureRandom.uuid }
-
-  it 'produces the expected xml' do
-    travel_to 1.second.from_now
-    subject.id = SecureRandom.uuid
-    subject.destination = FFaker::Internet.http_url
-    subject.issuer = FFaker::Internet.http_url
-    subject.name_id_format = Saml::Kit::Namespaces::TRANSIENT
-
-    result = subject.to_xml
-    xml_hash = Hash.from_xml(result)
-
-    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)
-
-    expect(xml_hash['LogoutRequest']['Issuer']).to eql(subject.issuer)
-    expect(xml_hash['LogoutRequest']['NameID']).to eql(name_id)
-    expect(result).to have_xpath("//samlp:LogoutRequest//saml:NameID[@Format=\"#{subject.name_id_format}\"]")
-  end
-
-  it 'includes a signature by default' do
-    xml_hash = Hash.from_xml(subject.to_xml)
-    expect(xml_hash['LogoutRequest']['Signature']).to be_present
-  end
-
-  it 'excludes a signature' do
-    subject.sign = false
-    xml_hash = Hash.from_xml(subject.to_xml)
-    expect(xml_hash['LogoutRequest']['Signature']).to be_nil
-  end
-
-  it 'builds a LogoutRequest' do
-    travel_to 1.second.from_now
-    result = subject.build
-    expect(result).to be_instance_of(Saml::Kit::LogoutRequest)
-    expect(result.to_xml).to eql(subject.to_xml)
+RSpec.describe Saml::Kit::LogoutResponse::Builder do
+  subject { described_class.new(user, request, configuration: configuration) }
+  let(:configuration) { double(issuer: issuer)  }
+  let(:user) { double(:user, name_id_for: SecureRandom.uuid) }
+  let(:request) { Saml::Kit::LogoutRequest::Builder.new(user).build }
+  let(:issuer) { FFaker::Internet.http_url }
+  let(:destination) { FFaker::Internet.http_url }
+  let(:registry) { double(:registry) }
+  let(:provider) { double(:provider) }
+  let(:binding) { double(:binding, location: destination) }
+
+  describe "#build" do
+    it 'builds a logout response' do
+      allow(configuration).to receive(:registry).and_return(registry)
+      allow(registry).to receive(:metadata_for).with(issuer).and_return(provider)
+      allow(provider).to receive(:single_logout_service_for).and_return(binding)
+
+      travel_to 1.second.from_now
+
+      result = subject.build
+      expect(result.id).to be_present
+      expect(result.issue_instant).to eql(Time.now.utc.iso8601)
+      expect(result.version).to eql("2.0")
+      expect(result.issuer).to eql(issuer)
+      expect(result.status_code).to eql(Saml::Kit::Namespaces::SUCCESS)
+      expect(result.in_response_to).to eql(request.id)
+      expect(result.destination).to eql(destination)
+    end
   end
 end
spec/saml/logout_response_spec.rb
@@ -1,34 +1,4 @@
 require 'spec_helper'
 
 RSpec.describe Saml::Kit::LogoutResponse do
-  describe described_class::Builder do
-    subject { described_class.new(user, request, configuration: configuration) }
-    let(:configuration) { double(issuer: issuer)  }
-    let(:user) { double(:user, name_id_for: SecureRandom.uuid) }
-    let(:request) { Saml::Kit::LogoutRequest::Builder.new(user).build }
-    let(:issuer) { FFaker::Internet.http_url }
-    let(:destination) { FFaker::Internet.http_url }
-    let(:registry) { double(:registry) }
-    let(:provider) { double(:provider) }
-    let(:binding) { double(:binding, location: destination) }
-
-    describe "#build" do
-      it 'builds a logout response' do
-        allow(configuration).to receive(:registry).and_return(registry)
-        allow(registry).to receive(:metadata_for).with(issuer).and_return(provider)
-        allow(provider).to receive(:single_logout_service_for).and_return(binding)
-
-        travel_to 1.second.from_now
-
-        result = subject.build
-        expect(result.id).to be_present
-        expect(result.issue_instant).to eql(Time.now.utc.iso8601)
-        expect(result.version).to eql("2.0")
-        expect(result.issuer).to eql(issuer)
-        expect(result.status_code).to eql(Saml::Kit::Namespaces::SUCCESS)
-        expect(result.in_response_to).to eql(request.id)
-        expect(result.destination).to eql(destination)
-      end
-    end
-  end
 end