main
 1# frozen_string_literal: true
 2
 3module Saml
 4  module Kit
 5    module Builders
 6      # {include:file:lib/saml/kit/builders/templates/service_provider_metadata.builder}
 7      # {include:file:spec/saml/kit/builders/service_provider_metadata_spec.rb}
 8      class ServiceProviderMetadata
 9        include XmlTemplatable
10        extend Forwardable
11        attr_accessor :acs_urls, :logout_urls, :name_id_formats
12        attr_accessor :want_assertions_signed
13        attr_reader :configuration
14        attr_reader :metadata
15        def_delegators :metadata, :id, :id=, :entity_id, :entity_id=,
16          :organization_name, :organization_name=, :organization_url,
17          :organization_url=, :contact_email, :contact_email=, :to_xml
18
19        def initialize(configuration: Saml::Kit.configuration)
20          @acs_urls = []
21          @configuration = configuration
22          @logout_urls = []
23          @name_id_formats = [Namespaces::PERSISTENT]
24          @want_assertions_signed = true
25          @metadata = Saml::Kit::Builders::Metadata.new(
26            configuration: configuration
27          )
28          @metadata.service_provider = self
29        end
30
31        def add_assertion_consumer_service(url, binding: :http_post)
32          @acs_urls.push(location: url, binding: Bindings.binding_for(binding))
33        end
34
35        def add_single_logout_service(url, binding: :http_post)
36          @logout_urls.push(
37            location: url,
38            binding: Bindings.binding_for(binding)
39          )
40        end
41
42        def build
43          Saml::Kit::ServiceProviderMetadata.new(to_xml)
44        end
45
46        private
47
48        def entity_descriptor_options
49          {
50            'xmlns': Namespaces::METADATA,
51            ID: id,
52            entityID: entity_id,
53          }
54        end
55
56        def descriptor_options
57          {
58            AuthnRequestsSigned: sign?,
59            WantAssertionsSigned: want_assertions_signed,
60            protocolSupportEnumeration: Namespaces::PROTOCOL,
61          }
62        end
63      end
64    end
65  end
66end