Commit dcf64bf

mo <mo@mokhan.ca>
2017-11-16 16:09:34
extract binding class.
1 parent fc4c0f2
lib/saml/kit/binding.rb
@@ -0,0 +1,20 @@
+module Saml
+  module Kit
+    class Binding
+      attr_reader :binding, :location
+
+      def initialize(binding:, location:)
+        @binding = binding
+        @location = location
+      end
+
+      def binding?(other)
+        binding == other
+      end
+
+      def to_h
+        { binding: binding, location: location }
+      end
+    end
+  end
+end
lib/saml/kit/identity_provider_metadata.rb
@@ -15,19 +15,18 @@ module Saml
       def single_sign_on_services
         xpath = "/md:EntityDescriptor/md:#{name}/md:SingleSignOnService"
         find_all(xpath).map do |item|
-          {
+          Saml::Kit::Binding.new(
             binding: item.attribute("Binding").value,
             location: item.attribute("Location").value,
-          }
+          )
         end
       end
 
       def single_sign_on_service_for(binding:)
         binding = Saml::Kit::Namespaces.binding_for(binding)
-        result = single_sign_on_services.find do |item|
-          item[:binding] == binding
+        single_sign_on_services.find do |item|
+          item.binding?(binding)
         end
-        return result[:location] if result
       end
 
       def attributes
lib/saml/kit.rb
@@ -14,6 +14,7 @@ require "xmldsig"
 
 require "saml/kit/xsd_validatable"
 require "saml/kit/authentication_request"
+require "saml/kit/binding"
 require "saml/kit/configuration"
 require "saml/kit/content"
 require "saml/kit/default_registry"
spec/saml/identity_provider_metadata_spec.rb
@@ -64,7 +64,7 @@ RSpec.describe Saml::Kit::IdentityProviderMetadata do
       ])
     end
     it do
-      expect(subject.single_sign_on_services).to match_array([
+      expect(subject.single_sign_on_services.map(&:to_h)).to match_array([
         { binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST", location: "https://dev-989848.oktapreview.com/app/ciscodev843126_portal_1/exk8dx3jilpueVzpU0h7/sso/saml" },
         { binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect", location: "https://dev-989848.oktapreview.com/app/ciscodev843126_portal_1/exk8dx3jilpueVzpU0h7/sso/saml" },
       ])
@@ -101,7 +101,7 @@ RSpec.describe Saml::Kit::IdentityProviderMetadata do
       ])
     end
     it do
-      expect(subject.single_sign_on_services).to match_array([
+      expect(subject.single_sign_on_services.map(&:to_h)).to match_array([
         { binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect", location: "https://www.example.com/adfs/ls/" },
         { binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST", location: "https://www.example.com/adfs/ls/" },
       ])
@@ -153,7 +153,7 @@ RSpec.describe Saml::Kit::IdentityProviderMetadata do
       ])
     end
     it do
-      expect(subject.single_sign_on_services).to match_array([
+      expect(subject.single_sign_on_services.map(&:to_h)).to match_array([
         { location: "https://www.example.com/adfs/ls/", binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" },
         { location: "https://www.example.com/adfs/ls/", binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" },
       ])
@@ -232,9 +232,16 @@ RSpec.describe Saml::Kit::IdentityProviderMetadata do
       builder.build
     end
 
-    it 'returns the binding that matches the requested' do
-      expect(subject.single_sign_on_service_for(binding: :post)).to eql(post_url)
-      expect(subject.single_sign_on_service_for(binding: :http_redirect)).to eql(redirect_url)
+    it 'returns the POST binding' do
+      result = subject.single_sign_on_service_for(binding: :post)
+      expect(result.location).to eql(post_url)
+      expect(result.binding).to eql(Saml::Kit::Namespaces::POST)
+    end
+
+    it 'returns the HTTP_REDIRECT binding' do
+      result = subject.single_sign_on_service_for(binding: :http_redirect)
+      expect(result.location).to eql(redirect_url)
+      expect(result.binding).to eql(Saml::Kit::Namespaces::HTTP_REDIRECT)
     end
 
     it 'returns nil if the binding cannot be found' do