Commit 4b61137

mo <mo.khan@gmail.com>
2017-11-19 18:07:31
extract method for looking up service bindings.
1 parent c44dca3
saml-kit/lib/saml/kit/identity_provider_metadata.rb
@@ -13,17 +13,11 @@ module Saml
       end
 
       def single_sign_on_services
-        xpath = "/md:EntityDescriptor/md:#{name}/md:SingleSignOnService"
-        find_all(xpath).map do |item|
-          binding = item.attribute("Binding").value
-          location = item.attribute("Location").value
-          binding_for(binding, location)
-        end
+        services('SingleSignOnService')
       end
 
       def single_sign_on_service_for(binding:)
-        binding = Saml::Kit::Namespaces.binding_for(binding)
-        single_sign_on_services.find { |x| x.binding?(binding) }
+        service_for(binding: binding, type: 'SingleSignOnService')
       end
 
       def attributes
saml-kit/lib/saml/kit/metadata.rb
@@ -53,17 +53,25 @@ module Saml
         certificates.find_all { |x| x[:use] == :signing }
       end
 
-      def single_logout_services
-        find_all("/md:EntityDescriptor/md:#{name}/md:SingleLogoutService").map do |item|
+      def services(type)
+        find_all("/md:EntityDescriptor/md:#{name}/md:#{type}").map do |item|
           binding = item.attribute("Binding").value
           location = item.attribute("Location").value
           binding_for(binding, location)
         end
       end
 
-      def single_logout_service_for(binding:)
+      def service_for(binding:, type:)
         binding = Saml::Kit::Namespaces.binding_for(binding)
-        single_logout_services.find { |x| x.binding?(binding) }
+        services(type).find { |x| x.binding?(binding) }
+      end
+
+      def single_logout_services
+        services('SingleLogoutService')
+      end
+
+      def single_logout_service_for(binding:)
+        service_for(binding: binding, type: 'SingleLogoutService')
       end
 
       def matches?(fingerprint, use: :signing)
saml-kit/lib/saml/kit/service_provider_metadata.rb
@@ -6,12 +6,11 @@ module Saml
       end
 
       def assertion_consumer_services
-        find_all("/md:EntityDescriptor/md:#{name}/md:AssertionConsumerService").map do |item|
-          {
-            binding: item.attribute("Binding").value,
-            location: item.attribute("Location").value,
-          }
-        end
+        services('AssertionConsumerService')
+      end
+
+      def assertion_consumer_services_for(binding:)
+        service_for(binding: binding, type: 'AssertionConsumerService')
       end
 
       def want_assertions_signed
saml-kit/spec/saml/service_provider_metadata_spec.rb
@@ -78,7 +78,7 @@ RSpec.describe Saml::Kit::ServiceProviderMetadata do
     end
 
     it 'returns each acs url and binding' do
-      expect(subject.assertion_consumer_services).to match_array([
+      expect(subject.assertion_consumer_services.map(&:to_h)).to match_array([
         { location: acs_post_url, binding: Saml::Kit::Namespaces::POST },
         { location: acs_redirect_url, binding: Saml::Kit::Namespaces::HTTP_REDIRECT },
       ])