Class: Saml::Kit::LogoutRequest

Inherits:
Document
  • Object
show all
Includes:
Requestable
Defined in:
lib/saml/kit/logout_request.rb

Overview

This class can be used to parse a LogoutRequest SAML document.

document = Saml::Kit::LogoutRequest.new(raw_xml)

It can also be used to generate a new LogoutRequest.

document = Saml::Kit::LogoutRequest.build do |builder|
builder.issuer = "issuer"
end

puts document.to_xml(pretty: true)

See Builders::LogoutRequest for a list of available settings.

This class can also be used to generate the correspondong LogoutResponse for a LogoutRequest.

document = Saml::Kit::LogoutRequest.new(raw_xml)
url, saml_params = document.response_for(binding: :http_post)

See #response_for for more information.

require_relative './principal'

RSpec.describe "Logout Request" do
  let(:user) { Principal.new(id: SecureRandom.uuid, email: "hello@example.com") }

  it 'produces a SAMLRequest' do
    xml = Saml::Kit::Metadata.build_xml do |builder|
      builder.contact_email = 'hi@example.com'
      builder.organization_name = "Acme, Inc"
      builder.organization_url = 'https://www.example.com'
      builder.build_identity_provider do |x|
        x.add_single_sign_on_service('https://www.example.com/login', binding: :http_post)
        x.add_single_sign_on_service('https://www.example.com/login', binding: :http_redirect)
        x.add_single_logout_service('https://www.example.com/logout', binding: :http_post)
        x.name_id_formats = [ Saml::Kit::Namespaces::EMAIL_ADDRESS ]
        x.attributes << :id
        x.attributes << :email
      end
      builder.build_service_provider do |x|
        x.add_assertion_consumer_service('https://www.example.com/consume', binding: :http_post)
        x.add_single_logout_service('https://www.example.com/logout', binding: :http_post)
      end
    end

    sp = Saml::Kit::IdentityProviderMetadata.new(xml)
    url, saml_params = sp.logout_request_for(user, binding: :http_post)
    expect(url).to eql("https://www.example.com/logout")
    expect(saml_params['SAMLRequest']).to be_present
  end
end

Constant Summary

Constants inherited from Document

Document::CONSTRUCTORS, Document::XPATH

Constants included from XsdValidatable

XsdValidatable::METADATA_XSD, XsdValidatable::PROTOCOL_XSD

Constants included from XmlParseable

XmlParseable::NAMESPACES

Instance Attribute Summary

Attributes inherited from Document

#name, #registry

Instance Method Summary collapse

Methods inherited from Document

#destination, #id, #issue_instant, #issuer, to_saml_document, #version

Methods included from XmlParseable

#present?, #to_h, #to_s, #to_xhtml, #to_xml

Methods included from Trustable

#signed?, #trusted?

Methods included from Validatable

#each_error

Constructor Details

#initialize(xml, configuration: Saml::Kit.configuration) ⇒ LogoutRequest

A new instance of LogoutRequest

Parameters:

  • xml (String)

    The raw xml string.

  • configuration (Saml::Kit::Configuration) (defaults to: Saml::Kit.configuration)

    configuration to use.



36
37
38
# File 'lib/saml/kit/logout_request.rb', line 36

def initialize(xml, configuration: Saml::Kit.configuration)
  super(xml, name: 'LogoutRequest', configuration: configuration)
end

Instance Method Details

#name_idObject

Returns the NameID value.



41
42
43
# File 'lib/saml/kit/logout_request.rb', line 41

def name_id
  at_xpath('./*/saml:NameID').try(:text)
end

#name_id_formatObject



45
46
47
# File 'lib/saml/kit/logout_request.rb', line 45

def name_id_format
  at_xpath('./*/saml:NameID/@Format').try(:value)
end

#response_for(binding:, relay_state: nil) ⇒ Array

Generates a Serialized LogoutResponse using the encoding rules for the specified binding.

:http_post. RelayState param. return to the requestor.

Parameters:

  • binding (Symbol)

    The binding to use :http_redirect or

  • relay_state (Object) (defaults to: nil)

    The RelayState to include in the

Returns:

  • (Array)

    Returns an array with a url and Hash of parameters to



58
59
60
61
62
63
64
# File 'lib/saml/kit/logout_request.rb', line 58

def response_for(binding:, relay_state: nil)
  builder = Saml::Kit::LogoutResponse.builder(self) do |xxx|
    yield xxx if block_given?
  end
  response_binding = provider.single_logout_service_for(binding: binding)
  response_binding.serialize(builder, relay_state: relay_state)
end