main
 1# frozen_string_literal: true
 2
 3module Saml
 4  module Kit
 5    module Builders
 6      # {include:file:lib/saml/kit/builders/templates/authentication_request.builder}
 7      # {include:file:spec/saml/kit/builders/authentication_request_spec.rb}
 8      class AuthenticationRequest
 9        include XmlTemplatable
10        attr_accessor :id, :now, :issuer, :assertion_consumer_service_url
11        attr_accessor :name_id_format, :destination
12        attr_accessor :version
13        attr_accessor :force_authn
14        attr_reader :configuration
15
16        def initialize(configuration: Saml::Kit.configuration)
17          @configuration = configuration
18          @id = ::Xml::Kit::Id.generate
19          @issuer = configuration.entity_id
20          @name_id_format = Namespaces::PERSISTENT
21          @now = Time.now.utc
22          @version = '2.0'
23        end
24
25        def build
26          Saml::Kit::AuthenticationRequest.new(to_xml)
27        end
28
29        private
30
31        def request_options
32          options = {
33            'xmlns:samlp' => Namespaces::PROTOCOL,
34            'xmlns:saml' => Namespaces::ASSERTION,
35            ID: id,
36            Version: version,
37            IssueInstant: now.utc.iso8601,
38            Destination: destination,
39          }
40          options[:ForceAuthn] = force_authn unless force_authn.nil?
41          options[:AssertionConsumerServiceURL] = assertion_consumer_service_url if assertion_consumer_service_url.present?
42          options
43        end
44      end
45    end
46  end
47end