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