class OneLogin::RubySaml::SloLogoutrequest

SAML2 Logout Request (SLO IdP initiated, Parser)

Attributes

document[R]
options[R]
request[R]
settings[RW]

OneLogin::RubySaml::Settings Toolkit settings

soft[RW]

Public Class Methods

new(request, options = {}) click to toggle source

Constructs the Logout Request. A Logout Request Object that is an extension of the SamlMessage class. @param request [String] A UUEncoded Logout Request from the IdP. @param options [Hash] :settings to provide the OneLogin::RubySaml::Settings object

Or :allowed_clock_drift for the logout request validation process to allow a clock drift when checking dates with
Or :relax_signature_validation to accept signatures if no idp certificate registered on settings

@raise [ArgumentError] If Request is nil

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 33
def initialize(request, options = {})
  raise ArgumentError.new("Request cannot be nil") if request.nil?

  @errors = []
  @options = options
  @soft = true
  unless options[:settings].nil?
    @settings = options[:settings]
    unless @settings.soft.nil?
      @soft = @settings.soft
    end
  end

  @request = decode_raw_saml(request)
  @document = REXML::Document.new(@request)
end

Public Instance Methods

id() click to toggle source

@return [String|nil] Gets the ID attribute from the Logout Request. if exists.

Calls superclass method OneLogin::RubySaml::SamlMessage#id
# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 83
def id
  super(document)
end
is_valid?(collect_errors = false) click to toggle source

Validates the Logout Request with the default values (soft = true) @param collect_errors [Boolean] Stop validation when first error appears or keep validating. @return [Boolean] TRUE if the Logout Request is valid

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 54
def is_valid?(collect_errors = false)
  validate(collect_errors)
end
issuer() click to toggle source

@return [String] Gets the Issuer from the Logout Request.

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 89
def issuer
  @issuer ||= begin
    node = REXML::XPath.first(
      document,
      "/p:LogoutRequest/a:Issuer",
      { "p" => PROTOCOL, "a" => ASSERTION }
    )
    Utils.element_text(node)
  end
end
name_id() click to toggle source

@return [String] Gets the NameID of the Logout Request.

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 60
def name_id
  @name_id ||= begin
    node = REXML::XPath.first(document, "/p:LogoutRequest/a:NameID", { "p" => PROTOCOL, "a" => ASSERTION })
    Utils.element_text(node)
  end
end
Also aliased as: nameid
name_id_format() click to toggle source

@return [String] Gets the NameID Format of the Logout Request.

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 71
def name_id_format
  @name_id_node ||= REXML::XPath.first(document, "/p:LogoutRequest/a:NameID", { "p" => PROTOCOL, "a" => ASSERTION })
  @name_id_format ||=
    if @name_id_node && @name_id_node.attribute("Format")
      @name_id_node.attribute("Format").value
    end
end
Also aliased as: nameid_format
nameid()
Alias for: name_id
nameid_format()
Alias for: name_id_format
not_on_or_after() click to toggle source

@return [Time|nil] Gets the NotOnOrAfter Attribute value if exists.

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 102
def not_on_or_after
  @not_on_or_after ||= begin
    node = REXML::XPath.first(
      document,
      "/p:LogoutRequest",
      { "p" => PROTOCOL }
    )
    if node && node.attributes["NotOnOrAfter"]
      Time.parse(node.attributes["NotOnOrAfter"])
    end
  end
end
session_indexes() click to toggle source

@return [Array] Gets the SessionIndex if exists (Supported multiple values). Empty Array if none found

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 117
def session_indexes
  nodes = REXML::XPath.match(
    document,
    "/p:LogoutRequest/p:SessionIndex",
    { "p" => PROTOCOL }
  )

  nodes.map { |node| Utils.element_text(node) }
end

Private Instance Methods

validate(collect_errors = false) click to toggle source

Hard aux function to validate the Logout Request @param collect_errors [Boolean] Stop validation when first error appears or keep validating. (if soft=true) @return [Boolean] TRUE if the Logout Request is valid @raise [ValidationError] if soft == false and validation fails

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 134
def validate(collect_errors = false)
  reset_errors!

  validations = [
    :validate_request_state,
    :validate_id,
    :validate_version,
    :validate_structure,
    :validate_not_on_or_after,
    :validate_issuer,
    :validate_signature
  ]

  if collect_errors
    validations.each { |validation| send(validation) }
    @errors.empty?
  else
    validations.all? { |validation| send(validation) }
  end
end
validate_id() click to toggle source

Validates that the Logout Request contains an ID If fails, the error is added to the errors array. @return [Boolean] True if the Logout Request contains an ID, otherwise returns False

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 159
def validate_id
  unless id
    return append_error("Missing ID attribute on Logout Request")
  end

  true
end
validate_issuer() click to toggle source

Validates the Issuer of the Logout Request If fails, the error is added to the errors array @return [Boolean] True if the Issuer matchs the IdP entityId, otherwise False if soft=True @raise [ValidationError] if soft == false and validation fails

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 220
def validate_issuer
  return true if settings.nil? || settings.idp_entity_id.nil? || issuer.nil?

  unless OneLogin::RubySaml::Utils.uri_match?(issuer, settings.idp_entity_id)
    return append_error("Doesn't match the issuer, expected: <#{settings.idp_entity_id}>, but was: <#{issuer}>")
  end

  true
end
validate_not_on_or_after() click to toggle source

Validates the time. (If the logout request was initialized with the :allowed_clock_drift option, the timing validations are relaxed by the allowed_clock_drift value) If fails, the error is added to the errors array @return [Boolean] True if satisfies the conditions, otherwise False if soft=True @raise [ValidationError] if soft == false and validation fails

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 184
def validate_not_on_or_after
  now = Time.now.utc
  if not_on_or_after && now >= (not_on_or_after + (options[:allowed_clock_drift] || 0))
    return append_error("Current time is on or after NotOnOrAfter (#{now} >= #{not_on_or_after})")
  end

  true
end
validate_request_state() click to toggle source

Validates that the Logout Request provided in the initialization is not empty, @return [Boolean] True if the required info is found, otherwise False if soft=True @raise [ValidationError] if soft == false and validation fails

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 209
def validate_request_state
  return append_error("Blank logout request") if request.nil? || request.empty?

  true
end
validate_signature() click to toggle source

Validates the Signature if exists and GET parameters are provided @return [Boolean] True if not contains a Signature or if the Signature is valid, otherwise False if soft=True @raise [ValidationError] if soft == false and validation fails

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 234
def validate_signature
  return true if options.nil?
  return true unless options.has_key? :get_params
  return true unless options[:get_params].has_key? 'Signature'

  # SAML specifies that the signature should be derived from a concatenation
  # of URI-encoded values _as sent by the IDP_:
  #
  # > Further, note that URL-encoding is not canonical; that is, there are multiple legal encodings for a given
  # > value. The relying party MUST therefore perform the verification step using the original URL-encoded
  # > values it received on the query string. It is not sufficient to re-encode the parameters after they have been
  # > processed by software because the resulting encoding may not match the signer's encoding.
  #
  # <http://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf>
  #
  # If we don't have the original parts (for backward compatibility) required to correctly verify the signature,
  # then fabricate them by re-encoding the parsed URI parameters, and hope that we're lucky enough to use
  # the exact same URI-encoding as the IDP. (This is not the case if the IDP is ADFS!)
  options[:raw_get_params] ||= {}
  if options[:raw_get_params]['SAMLRequest'].nil? && !options[:get_params]['SAMLRequest'].nil?
    options[:raw_get_params]['SAMLRequest'] = CGI.escape(options[:get_params]['SAMLRequest'])
  end
  if options[:raw_get_params]['RelayState'].nil? && !options[:get_params]['RelayState'].nil?
    options[:raw_get_params]['RelayState'] = CGI.escape(options[:get_params]['RelayState'])
  end
  if options[:raw_get_params]['SigAlg'].nil? && !options[:get_params]['SigAlg'].nil?
    options[:raw_get_params]['SigAlg'] = CGI.escape(options[:get_params]['SigAlg'])
  end

  # If we only received the raw version of SigAlg,
  # then parse it back into the decoded params hash for convenience.
  if options[:get_params]['SigAlg'].nil? && !options[:raw_get_params]['SigAlg'].nil?
    options[:get_params]['SigAlg'] = CGI.unescape(options[:raw_get_params]['SigAlg'])
  end

  idp_cert = settings.get_idp_cert
  idp_certs = settings.get_idp_cert_multi

  if idp_cert.nil? && (idp_certs.nil? || idp_certs[:signing].empty?)
    return options.has_key? :relax_signature_validation
  end

  query_string = OneLogin::RubySaml::Utils.build_query_from_raw_parts(
    :type            => 'SAMLRequest',
    :raw_data        => options[:raw_get_params]['SAMLRequest'],
    :raw_relay_state => options[:raw_get_params]['RelayState'],
    :raw_sig_alg     => options[:raw_get_params]['SigAlg']
  )

  if idp_certs.nil? || idp_certs[:signing].empty?
    valid = OneLogin::RubySaml::Utils.verify_signature(
      :cert         => settings.get_idp_cert,
      :sig_alg      => options[:get_params]['SigAlg'],
      :signature    => options[:get_params]['Signature'],
      :query_string => query_string
    )
  else
    valid = false
    idp_certs[:signing].each do |idp_cert|
      valid = OneLogin::RubySaml::Utils.verify_signature(
        :cert         => idp_cert,
        :sig_alg      => options[:get_params]['SigAlg'],
        :signature    => options[:get_params]['Signature'],
        :query_string => query_string
      )
      if valid
        break
      end
    end
  end

  unless valid
    return append_error("Invalid Signature on Logout Request")
  end

  true
end
validate_structure() click to toggle source

Validates the Logout Request against the specified schema. @return [Boolean] True if the XML is valid, otherwise False if soft=True @raise [ValidationError] if soft == false and validation fails

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 197
def validate_structure
  unless valid_saml?(document, soft)
    return append_error("Invalid SAML Logout Request. Not match the saml-schema-protocol-2.0.xsd")
  end

  true
end
validate_version() click to toggle source

Validates the SAML version (2.0) If fails, the error is added to the errors array. @return [Boolean] True if the Logout Request is 2.0, otherwise returns False

# File lib/onelogin/ruby-saml/slo_logoutrequest.rb, line 171
def validate_version
  unless version(document) == "2.0"
    return append_error("Unsupported SAML version")
  end

  true
end