main
 1# frozen_string_literal: true
 2
 3module Saml
 4  module Kit
 5    # This class can be used to parse a LogoutRequest SAML document.
 6    #
 7    #   document = Saml::Kit::LogoutRequest.new(raw_xml)
 8    #
 9    # It can also be used to generate a new LogoutRequest.
10    #
11    #   document = Saml::Kit::LogoutRequest.build do |builder|
12    #     builder.issuer = "issuer"
13    #   end
14    #
15    #   puts document.to_xml(pretty: true)
16    #
17    # See {Saml::Kit::Builders::LogoutRequest} for a list of available settings.
18    #
19    # This class can also be used to generate the correspondong LogoutResponse
20    # for a LogoutRequest.
21    #
22    #   document = Saml::Kit::LogoutRequest.new(raw_xml)
23    #   url, saml_params = document.response_for(binding: :http_post)
24    #
25    # See {#response_for} for more information.
26    #
27    # {include:file:spec/examples/logout_request_spec.rb}
28    class LogoutRequest < Document
29      include Requestable
30      validates_presence_of :single_logout_service, if: :expected_type?
31
32      # A new instance of LogoutRequest
33      #
34      # @param xml [String] The raw xml string.
35      # @param configuration [Saml::Kit::Configuration] configuration to use.
36      def initialize(xml, configuration: Saml::Kit.configuration)
37        super(xml, name: 'LogoutRequest', configuration: configuration)
38      end
39
40      # Returns the NameID value.
41      def name_id
42        at_xpath('./*/saml:NameID').try(:text)
43      end
44
45      def name_id_format
46        at_xpath('./*/saml:NameID/@Format').try(:value)
47      end
48
49      # Generates a Serialized LogoutResponse using the encoding rules for
50      # the specified binding.
51      #
52      # @param binding [Symbol] The binding to use `:http_redirect` or
53      # `:http_post`.
54      # @param relay_state [Object] The RelayState to include in the
55      # RelayState param.
56      # @return [Array] Returns an array with a url and Hash of parameters to
57      # return to the requestor.
58      def response_for(binding:, relay_state: nil)
59        builder = Saml::Kit::LogoutResponse.builder(self) do |xxx|
60          yield xxx if block_given?
61        end
62        response_binding = provider.single_logout_service_for(binding: binding)
63        response_binding.serialize(builder, relay_state: relay_state)
64      end
65
66      private
67
68      def single_logout_service
69        return if provider.nil?
70
71        urls = provider.single_logout_services
72        urls.first
73      end
74    end
75  end
76end