Commit b73ed03

mo <mo@mokhan.ca>
2017-11-07 21:03:33
parse login url from IDP metadata.
1 parent 5930d0d
lib/saml/kit/identity_provider_metadata.rb
@@ -12,6 +12,13 @@ module Saml
         end
       end
 
+      def single_sign_on_service_for(binding:)
+        binding = Saml::Kit::Namespaces.binding_for(binding)
+        single_sign_on_services.find do |item|
+          item[:binding] == binding
+        end
+      end
+
       def attributes
         find_all("/md:EntityDescriptor/md:#{name}/saml:Attribute").map do |item|
           {
lib/saml/kit/namespaces.rb
@@ -25,8 +25,10 @@ module Saml
       def self.binding_for(binding)
         if :post == binding
           Namespaces::POST
-        else
+        elsif :http_redirect == binding
           Namespaces::HTTP_REDIRECT
+        else
+          nil
         end
       end
     end
spec/saml/identity_provider_metadata_spec.rb
@@ -221,4 +221,25 @@ RSpec.describe Saml::Kit::IdentityProviderMetadata do
       expect(subject.errors[:base]).to include("invalid signature.")
     end
   end
+
+  describe "#single_sign_on_service_for" do
+    let(:url) { FFaker::Internet.http_url }
+
+    subject do
+      builder = Saml::Kit::IdentityProviderMetadata::Builder.new
+      builder.add_single_sign_on_service(FFaker::Internet.http_url, binding: :http_redirect)
+      builder.add_single_sign_on_service(url, binding: :post)
+      builder.build
+    end
+
+    it 'returns the binding that matches the requested' do
+      result = subject.single_sign_on_service_for(binding: :post)
+      expect(result[:binding]).to eql(Saml::Kit::Namespaces::POST)
+      expect(result[:location]).to eql(url)
+    end
+
+    it 'returns nil if the binding cannot be found' do
+      expect(subject.single_sign_on_service_for(binding: :soap)).to be_nil
+    end
+  end
 end