Commit 05cae90

mo <mo@mokhan.ca>
2017-12-01 23:00:08
allow idp to create request and request to serialize response.
1 parent cfcc423
lib/saml/kit/authentication_request.rb
@@ -15,8 +15,12 @@ module Saml
         to_h[name]['NameIDPolicy']['Format']
       end
 
-      def response_for(user)
-        Saml::Kit::Builders::Response.new(user, self)
+      def response_for(user, binding:, relay_state: nil)
+        response_binding = provider.assertion_consumer_service_for(binding: binding)
+        builder = Saml::Kit::Response.builder(user, self) do |x|
+          yield x if block_given?
+        end
+        response_binding.serialize(builder, relay_state: relay_state)
       end
 
       Builder = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Saml::Kit::AuthenticationRequest::Builder', 'Saml::Kit::Builders::AuthenticationRequest')
lib/saml/kit/buildable.rb
@@ -5,9 +5,15 @@ module Saml
 
       class_methods do
         def build(*args)
-          builder = builder_class.new(*args)
-          yield builder if block_given?
-          builder.build
+          builder(*args).tap do |x|
+            yield x if block_given?
+          end.build
+        end
+
+        def builder(*args)
+          builder_class.new(*args).tap do |builder|
+            yield builder if block_given?
+          end
         end
       end
     end
lib/saml/kit/identity_provider_metadata.rb
@@ -29,6 +29,22 @@ module Saml
         end
       end
 
+      def login_request_for(binding:, relay_state: nil)
+        builder = Saml::Kit::AuthenticationRequest.builder do |x|
+          yield x if block_given?
+        end
+        request_binding = single_sign_on_service_for(binding: binding)
+        request_binding.serialize(builder, relay_state: relay_state)
+      end
+
+      def logout_request_for(user, binding: :http_post, relay_state: nil)
+        builder = Saml::Kit::LogoutRequest.builder(user) do |x|
+          yield x if block_given?
+        end
+        request_binding = single_logout_service_for(binding: binding)
+        request_binding.serialize(builder, relay_state: relay_state)
+      end
+
       def self.builder_class
         Saml::Kit::Builders::IdentityProviderMetadata
       end
lib/saml/kit/logout_request.rb
@@ -18,8 +18,12 @@ module Saml
         urls.first
       end
 
-      def response_for(user)
-        Saml::Kit::Builders::LogoutResponse.new(user, self)
+      def response_for(user, binding:, relay_state: nil)
+        builder = Saml::Kit::LogoutResponse.builder(user, self) do |x|
+          yield x if block_given?
+        end
+        response_binding = provider.single_logout_service_for(binding: binding)
+        response_binding.serialize(builder, relay_state: relay_state)
       end
 
       Builder = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Saml::Kit::LogoutRequest::Builder', 'Saml::Kit::Builders::LogoutRequest')
spec/saml/logout_request_spec.rb
@@ -121,9 +121,20 @@ RSpec.describe Saml::Kit::LogoutRequest do
   end
 
   describe "#response_for" do
-    it 'returns a logout response for a particular user' do
-      user = double(:user)
-      expect(subject.response_for(user)).to be_instance_of(Saml::Kit::Builders::LogoutResponse)
+    let(:user) { double(:user, name_id_for: SecureRandom.uuid) }
+    let(:provider) do
+      Saml::Kit::IdentityProviderMetadata.build do |builder|
+        builder.add_single_logout_service(FFaker::Internet.uri("https"), binding: :http_post)
+      end
+    end
+
+    it 'serializes a logout response for a particular user' do
+      allow(subject).to receive(:provider).and_return(provider)
+
+      _, saml_params = subject.response_for(user, binding: :http_post)
+      response_binding = provider.single_logout_service_for(binding: :http_post)
+      result = response_binding.deserialize(saml_params)
+      expect(result).to be_instance_of(Saml::Kit::LogoutResponse)
     end
   end
 end