Commit 52fa7fb

mo <mo@mokhan.ca>
2017-11-29 02:35:18
move authentication_request_builder to a separate file.
1 parent 842469c
lib/saml/kit/builders/authentication_request.rb
@@ -0,0 +1,48 @@
+module Saml
+  module Kit
+    class AuthenticationRequest < Document
+      class Builder
+        attr_accessor :id, :now, :issuer, :acs_url, :name_id_format, :sign, :destination
+        attr_accessor :version
+
+        def initialize(configuration: Saml::Kit.configuration, sign: true)
+          @id = SecureRandom.uuid
+          @issuer = configuration.issuer
+          @name_id_format = Namespaces::PERSISTENT
+          @now = Time.now.utc
+          @version = "2.0"
+          @sign = sign
+        end
+
+        def to_xml
+          Signature.sign(sign: sign) do |xml, signature|
+            xml.tag!('samlp:AuthnRequest', request_options) do
+              xml.tag!('saml:Issuer', issuer)
+              signature.template(id)
+              xml.tag!('samlp:NameIDPolicy', Format: name_id_format)
+            end
+          end
+        end
+
+        def build
+          AuthenticationRequest.new(to_xml)
+        end
+
+        private
+
+        def request_options
+          options = {
+            "xmlns:samlp" => Namespaces::PROTOCOL,
+            "xmlns:saml" => Namespaces::ASSERTION,
+            ID: "_#{id}",
+            Version: version,
+            IssueInstant: now.utc.iso8601,
+            Destination: destination,
+          }
+          options[:AssertionConsumerServiceURL] = acs_url if acs_url.present?
+          options
+        end
+      end
+    end
+  end
+end
lib/saml/kit/authentication_request.rb
@@ -18,51 +18,6 @@ module Saml
       def response_for(user)
         Response::Builder.new(user, self)
       end
-
-      private
-
-      class Builder
-        attr_accessor :id, :now, :issuer, :acs_url, :name_id_format, :sign, :destination
-        attr_accessor :version
-
-        def initialize(configuration: Saml::Kit.configuration, sign: true)
-          @id = SecureRandom.uuid
-          @issuer = configuration.issuer
-          @name_id_format = Namespaces::PERSISTENT
-          @now = Time.now.utc
-          @version = "2.0"
-          @sign = sign
-        end
-
-        def to_xml
-          Signature.sign(sign: sign) do |xml, signature|
-            xml.tag!('samlp:AuthnRequest', request_options) do
-              xml.tag!('saml:Issuer', issuer)
-              signature.template(id)
-              xml.tag!('samlp:NameIDPolicy', Format: name_id_format)
-            end
-          end
-        end
-
-        def build
-          AuthenticationRequest.new(to_xml)
-        end
-
-        private
-
-        def request_options
-          options = {
-            "xmlns:samlp" => Namespaces::PROTOCOL,
-            "xmlns:saml" => Namespaces::ASSERTION,
-            ID: "_#{id}",
-            Version: version,
-            IssueInstant: now.utc.iso8601,
-            Destination: destination,
-          }
-          options[:AssertionConsumerServiceURL] = acs_url if acs_url.present?
-          options
-        end
-      end
     end
   end
 end
lib/saml/kit/builders.rb
@@ -0,0 +1,1 @@
+require 'saml/kit/builders/authentication_request'
lib/saml/kit.rb
@@ -40,6 +40,8 @@ require "saml/kit/service_provider_metadata"
 require "saml/kit/signature"
 require "saml/kit/xml"
 
+require "saml/kit/builders"
+
 I18n.load_path += Dir[File.expand_path("kit/locales/*.yml", File.dirname(__FILE__))]
 
 module Saml
spec/saml/builders/authentication_request_spec.rb
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+RSpec.describe Saml::Kit::AuthenticationRequest::Builder do
+  subject { described_class.new(configuration: configuration) }
+  let(:configuration) do
+    config = Saml::Kit::Configuration.new
+    config.issuer = issuer
+    config
+  end
+
+  describe "#to_xml" do
+    let(:issuer) { FFaker::Movie.title }
+    let(:acs_url) { "https://airport.dev/session/acs" }
+
+    it 'returns a valid authentication request' do
+      travel_to 1.second.from_now
+      subject.acs_url = acs_url
+      result = Hash.from_xml(subject.to_xml)
+
+      expect(result['AuthnRequest']['ID']).to be_present
+      expect(result['AuthnRequest']['Version']).to eql('2.0')
+      expect(result['AuthnRequest']['IssueInstant']).to eql(Time.now.utc.iso8601)
+      expect(result['AuthnRequest']['AssertionConsumerServiceURL']).to eql(acs_url)
+      expect(result['AuthnRequest']['Issuer']).to eql(issuer)
+      expect(result['AuthnRequest']['NameIDPolicy']['Format']).to eql(Saml::Kit::Namespaces::PERSISTENT)
+    end
+  end
+end