Class: OneLogin::RubySaml::SamlMessage

Inherits:
Object
  • Object
show all
Includes:
REXML
Defined in:
lib/onelogin/ruby-saml/saml_message.rb

Overview

SAML2 Message

Constant Summary collapse

ASSERTION =
"urn:oasis:names:tc:SAML:2.0:assertion".freeze
PROTOCOL =
"urn:oasis:names:tc:SAML:2.0:protocol".freeze
BASE64_FORMAT =
%r(\A([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\Z)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.schemaNokogiri::XML::Schema

Returns Gets the schema object of the SAML 2.0 Protocol schema.

Returns:

  • (Nokogiri::XML::Schema)

    Gets the schema object of the SAML 2.0 Protocol schema



25
26
27
28
29
30
# File 'lib/onelogin/ruby-saml/saml_message.rb', line 25

def self.schema
  path = File.expand_path("../../../schemas/saml-schema-protocol-2.0.xsd", __FILE__)
  File.open(path) do |file|
    ::Nokogiri::XML::Schema(file)
  end
end

Instance Method Details

#id(document) ⇒ String|nil

Returns Gets the ID attribute from the SAML Message if exists.

Returns:

  • (String|nil)

    Gets the ID attribute from the SAML Message if exists.



47
48
49
50
51
52
53
54
55
56
# File 'lib/onelogin/ruby-saml/saml_message.rb', line 47

def id(document)
  @id ||= begin
    node = REXML::XPath.first(
      document,
      "/p:AuthnRequest | /p:Response | /p:LogoutResponse | /p:LogoutRequest",
      { "p" => PROTOCOL }
    )
    node.nil? ? nil : node.attributes['ID']
  end
end

#valid_saml?(document, soft = true, check_malformed_doc = true) ⇒ Boolean

Validates the SAML Message against the specified schema.

Parameters:

  • document (REXML::Document)

    The message that will be validated

  • soft (Boolean) (defaults to: true)

    soft Enable or Disable the soft mode (In order to raise exceptions when the message is invalid or not)

  • check_malformed_doc (Boolean) (defaults to: true)

    check_malformed_doc Enable or Disable the check for malformed XML

Returns:

  • (Boolean)

    True if the XML is valid, otherwise False, if soft=True

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/onelogin/ruby-saml/saml_message.rb', line 65

def valid_saml?(document, soft = true, check_malformed_doc = true)
  begin
    xml = XMLSecurity::BaseDocument.safe_load_xml(document, check_malformed_doc)
  rescue StandardError => error
    return false if soft
    raise ValidationError.new("XML load failed: #{error.message}")
  end

  SamlMessage.schema.validate(xml).map do |schema_error|
    return false if soft
    raise ValidationError.new("#{schema_error.message}\n\n#{xml}")
  end
end

#version(document) ⇒ String|nil

Returns Gets the Version attribute from the SAML Message if exists.

Returns:

  • (String|nil)

    Gets the Version attribute from the SAML Message if exists.



34
35
36
37
38
39
40
41
42
43
# File 'lib/onelogin/ruby-saml/saml_message.rb', line 34

def version(document)
  @version ||= begin
    node = REXML::XPath.first(
      document,
      "/p:AuthnRequest | /p:Response | /p:LogoutResponse | /p:LogoutRequest",
      { "p" => PROTOCOL }
    )
    node.nil? ? nil : node.attributes['Version']
  end
end