Commit df52195

mo <mo@mokhan.ca>
2017-11-29 02:42:51
move logout request builder to separate file.
1 parent 6ab0459
lib/saml/kit/builders/logout_request.rb
@@ -0,0 +1,55 @@
+module Saml
+  module Kit
+    class LogoutRequest < Document
+      class Builder
+        attr_accessor :id, :destination, :issuer, :name_id_format, :now
+        attr_accessor :sign, :version
+        attr_reader :user
+
+        def initialize(user, configuration: Saml::Kit.configuration, sign: true)
+          @user = user
+          @id = SecureRandom.uuid
+          @issuer = configuration.issuer
+          @name_id_format = Saml::Kit::Namespaces::PERSISTENT
+          @now = Time.now.utc
+          @version = "2.0"
+          @sign = sign
+        end
+
+        def to_xml
+          Signature.sign(sign: sign) do |xml, signature|
+            xml.instruct!
+            xml.LogoutRequest logout_request_options do
+              xml.Issuer({ xmlns: Namespaces::ASSERTION }, issuer)
+              signature.template(id)
+              xml.NameID name_id_options, user.name_id_for(name_id_format)
+            end
+          end
+        end
+
+        def build
+          Saml::Kit::LogoutRequest.new(to_xml)
+        end
+
+        private
+
+        def logout_request_options
+          {
+            ID: "_#{id}",
+            Version: version,
+            IssueInstant: now.utc.iso8601,
+            Destination: destination,
+            xmlns: Namespaces::PROTOCOL,
+          }
+        end
+
+        def name_id_options
+          {
+            Format: name_id_format,
+            xmlns: Namespaces::ASSERTION,
+          }
+        end
+      end
+    end
+  end
+end
lib/saml/kit/builders.rb
@@ -1,2 +1,3 @@
 require 'saml/kit/builders/authentication_request'
 require 'saml/kit/builders/identity_provider_metadata'
+require 'saml/kit/builders/logout_request'
lib/saml/kit/logout_request.rb
@@ -21,58 +21,6 @@ module Saml
       def response_for(user)
         LogoutResponse::Builder.new(user, self)
       end
-
-      private
-
-      class Builder
-        attr_accessor :id, :destination, :issuer, :name_id_format, :now
-        attr_accessor :sign, :version
-        attr_reader :user
-
-        def initialize(user, configuration: Saml::Kit.configuration, sign: true)
-          @user = user
-          @id = SecureRandom.uuid
-          @issuer = configuration.issuer
-          @name_id_format = Saml::Kit::Namespaces::PERSISTENT
-          @now = Time.now.utc
-          @version = "2.0"
-          @sign = sign
-        end
-
-        def to_xml
-          Signature.sign(sign: sign) do |xml, signature|
-            xml.instruct!
-            xml.LogoutRequest logout_request_options do
-              xml.Issuer({ xmlns: Namespaces::ASSERTION }, issuer)
-              signature.template(id)
-              xml.NameID name_id_options, user.name_id_for(name_id_format)
-            end
-          end
-        end
-
-        def build
-          Saml::Kit::LogoutRequest.new(to_xml)
-        end
-
-        private
-
-        def logout_request_options
-          {
-            ID: "_#{id}",
-            Version: version,
-            IssueInstant: now.utc.iso8601,
-            Destination: destination,
-            xmlns: Namespaces::PROTOCOL,
-          }
-        end
-
-        def name_id_options
-          {
-            Format: name_id_format,
-            xmlns: Namespaces::ASSERTION,
-          }
-        end
-      end
     end
   end
 end
spec/saml/builders/identity_provider_metadata_spec.rb
@@ -4,7 +4,7 @@ RSpec.describe Saml::Kit::IdentityProviderMetadata::Builder do
   subject { described_class.new }
   let(:email) { FFaker::Internet.email }
   let(:org_name) { FFaker::Movie.title }
-  let(:url) { "https://#{FFaker::Internet.domain_name}" }
+  let(:url) { FFaker::Internet.uri("https") }
   let(:entity_id) { FFaker::Movie.title }
 
   it 'builds a proper metadata' do
spec/saml/builders/logout_response_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/logout_request_spec.rb
@@ -114,50 +114,6 @@ RSpec.describe Saml::Kit::LogoutRequest do
     end
   end
 
-  describe described_class::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
-
   describe "#response_for" do
     it 'returns a logout response for a particular user' do
       user = double(:user)