Class: OneLogin::RubySaml::Response

Inherits:
SamlMessage show all
Includes:
ErrorHandling
Defined in:
lib/onelogin/ruby-saml/response.rb

Overview

SAML2 Authentication Response. SAML Response

Constant Summary collapse

ASSERTION =
"urn:oasis:names:tc:SAML:2.0:assertion"
PROTOCOL =
"urn:oasis:names:tc:SAML:2.0:protocol"
DSIG =
"http://www.w3.org/2000/09/xmldsig#"
XENC =
"http://www.w3.org/2001/04/xmlenc#"

Constants inherited from SamlMessage

SamlMessage::BASE64_FORMAT

Instance Attribute Summary collapse

Attributes included from ErrorHandling

#errors

Instance Method Summary collapse

Methods included from ErrorHandling

#append_error, #reset_errors!

Methods inherited from SamlMessage

#id, schema, #valid_saml?, #version

Constructor Details

#initialize(response, options = {}) ⇒ Response

Constructs the SAML Response. A Response Object that is an extension of the SamlMessage class.

Parameters:

  • response (String)

    A UUEncoded SAML response from the IdP.

  • options (Hash) (defaults to: {})

    :settings to provide the OneLogin::RubySaml::Settings object Or some options for the response validation process like skip the conditions validation with the :skip_conditions, or allow a clock_drift when checking dates with :allowed_clock_drift or :matches_request_id that will validate that the response matches the ID of the request, or skip the subject confirmation validation with the :skip_subject_confirmation option

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/onelogin/ruby-saml/response.rb', line 40

def initialize(response, options = {})
  raise ArgumentError.new("Response cannot be nil") if response.nil?

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

  @response = decode_raw_saml(response)
  @document = XMLSecurity::SignedDocument.new(@response, @errors)

  if assertion_encrypted?
    @decrypted_document = generate_decrypted_document
  end
end

Instance Attribute Details

#decrypted_documentObject (readonly)

Returns the value of attribute decrypted_document.



27
28
29
# File 'lib/onelogin/ruby-saml/response.rb', line 27

def decrypted_document
  @decrypted_document
end

#documentObject (readonly)

Returns the value of attribute document.



26
27
28
# File 'lib/onelogin/ruby-saml/response.rb', line 26

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/onelogin/ruby-saml/response.rb', line 29

def options
  @options
end

#responseObject (readonly)

Returns the value of attribute response.



28
29
30
# File 'lib/onelogin/ruby-saml/response.rb', line 28

def response
  @response
end

#settingsObject

OneLogin::RubySaml::Settings Toolkit settings



24
25
26
# File 'lib/onelogin/ruby-saml/response.rb', line 24

def settings
  @settings
end

#softObject

Returns the value of attribute soft.



31
32
33
# File 'lib/onelogin/ruby-saml/response.rb', line 31

def soft
  @soft
end

Instance Method Details

#allowed_clock_driftInteger

returns the allowed clock drift on timing validation

Returns:

  • (Integer)


280
281
282
# File 'lib/onelogin/ruby-saml/response.rb', line 280

def allowed_clock_drift
  return options[:allowed_clock_drift] || 0
end

#attributesAttributes

Gets the Attributes from the AttributeStatement element.

All attributes can be iterated over attributes.each or returned as array by attributes.all For backwards compatibility ruby-saml returns by default only the first value for a given attribute with

attributes['name']

To get all of the attributes, use:

attributes.multi('name')

Or turn off the compatibility:

OneLogin::RubySaml::Attributes.single_value_compatibility = false

Now this will return an array:

attributes['name']

Returns:

  • (Attributes)

    OneLogin::RubySaml::Attributes enumerable collection.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/onelogin/ruby-saml/response.rb', line 119

def attributes
  @attr_statements ||= begin
    attributes = Attributes.new

    stmt_element = xpath_first_from_signed_assertion('/a:AttributeStatement')
    return attributes if stmt_element.nil?

    stmt_element.elements.each do |attr_element|
      name  = attr_element.attributes["Name"]
      values = attr_element.elements.collect{|e|
        if (e.elements.nil? || e.elements.size == 0)
          # SAMLCore requires that nil AttributeValues MUST contain xsi:nil XML attribute set to "true" or "1"
          # otherwise the value is to be regarded as empty.
          ["true", "1"].include?(e.attributes['xsi:nil']) ? nil : e.text.to_s
        # explicitly support saml2:NameID with saml2:NameQualifier if supplied in attributes
        # this is useful for allowing eduPersonTargetedId to be passed as an opaque identifier to use to 
        # identify the subject in an SP rather than email or other less opaque attributes
        # NameQualifier, if present is prefixed with a "/" to the value
        else 
         REXML::XPath.match(e,'a:NameID', { "a" => ASSERTION }).collect{|n|
            (n.attributes['NameQualifier'] ? n.attributes['NameQualifier'] +"/" : '') + n.text.to_s
          }
        end
      }

      attributes.add(name, values.flatten)
    end

    attributes
  end
end

#audiencesArray

Returns The Audience elements from the Contitions of the SAML Response.

Returns:

  • (Array)

    The Audience elements from the Contitions of the SAML Response.



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/onelogin/ruby-saml/response.rb', line 265

def audiences
  @audiences ||= begin
    audiences = []
    nodes = xpath_from_signed_assertion('/a:Conditions/a:AudienceRestriction/a:Audience')
    nodes.each do |node|
      if node && node.text
        audiences << node.text
      end
    end
    audiences
  end
end

#conditionsREXML::Element

Gets the Condition Element of the SAML Response if exists. (returns the first node that matches the supplied xpath)

Returns:

  • (REXML::Element)

    Conditions Element if exists



199
200
201
# File 'lib/onelogin/ruby-saml/response.rb', line 199

def conditions
  @conditions ||= xpath_first_from_signed_assertion('/a:Conditions')
end

#destinationString|nil

Returns Destination attribute from the SAML Response.

Returns:

  • (String|nil)

    Destination attribute from the SAML Response.



252
253
254
255
256
257
258
259
260
261
# File 'lib/onelogin/ruby-saml/response.rb', line 252

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

#in_response_toString|nil

Returns The InResponseTo attribute from the SAML Response.

Returns:

  • (String|nil)

    The InResponseTo attribute from the SAML Response.



239
240
241
242
243
244
245
246
247
248
# File 'lib/onelogin/ruby-saml/response.rb', line 239

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

#is_valid?(collect_errors = false) ⇒ Boolean

Validates the SAML Response with the default values (soft = true)

Parameters:

  • collect_errors (Boolean) (defaults to: false)

    Stop validation when first error appears or keep validating. (if soft=true)

Returns:

  • (Boolean)

    TRUE if the SAML Response is valid



65
66
67
# File 'lib/onelogin/ruby-saml/response.rb', line 65

def is_valid?(collect_errors = false)
  validate(collect_errors)
end

#issuersArray

Gets the Issuers (from Response and Assertion). (returns the first node that matches the supplied xpath from the Response and from the Assertion)

Returns:

  • (Array)

    Array with the Issuers (REXML::Element)



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/onelogin/ruby-saml/response.rb', line 221

def issuers
  @issuers ||= begin
    issuers = []
    nodes = REXML::XPath.match(
      document,
      "/p:Response/a:Issuer",
      { "p" => PROTOCOL, "a" => ASSERTION }
    )
    nodes += xpath_from_signed_assertion("/a:Issuer")
    nodes.each do |node|
      issuers << node.text if node.text
    end
    issuers.uniq
  end
end

#name_idString Also known as: nameid

Returns the NameID provided by the SAML response from the IdP.

Returns:

  • (String)

    the NameID provided by the SAML response from the IdP.



71
72
73
74
75
76
# File 'lib/onelogin/ruby-saml/response.rb', line 71

def name_id
  @name_id ||=
    if name_id_node
      name_id_node.text
    end
end

#name_id_formatString Also known as: nameid_format

Returns the NameID Format provided by the SAML response from the IdP.

Returns:

  • (String)

    the NameID Format provided by the SAML response from the IdP.



82
83
84
85
86
87
# File 'lib/onelogin/ruby-saml/response.rb', line 82

def name_id_format
  @name_id_format ||=
    if name_id_node && name_id_node.attribute("Format")
      name_id_node.attribute("Format").value
    end
end

#not_beforeTime

Gets the NotBefore Condition Element value.

Returns:

  • (Time)

    The NotBefore value in Time format



206
207
208
# File 'lib/onelogin/ruby-saml/response.rb', line 206

def not_before
  @not_before ||= parse_time(conditions, "NotBefore")
end

#not_on_or_afterTime

Gets the NotOnOrAfter Condition Element value.

Returns:

  • (Time)

    The NotOnOrAfter value in Time format



213
214
215
# File 'lib/onelogin/ruby-saml/response.rb', line 213

def not_on_or_after
  @not_on_or_after ||= parse_time(conditions, "NotOnOrAfter")
end

#session_expires_atString

Gets the SessionNotOnOrAfter from the AuthnStatement. Could be used to set the local session expiration (expire at latest)

Returns:

  • (String)

    The SessionNotOnOrAfter value



155
156
157
158
159
160
# File 'lib/onelogin/ruby-saml/response.rb', line 155

def session_expires_at
  @expires_at ||= begin
    node = xpath_first_from_signed_assertion('/a:AuthnStatement')
    node.nil? ? nil : parse_time(node, "SessionNotOnOrAfter")
  end
end

#sessionindexString

Gets the SessionIndex from the AuthnStatement. Could be used to be stored in the local session in order to be used in a future Logout Request that the SP could send to the IdP, to set what specific session must be deleted

Returns:

  • (String)

    SessionIndex Value



98
99
100
101
102
103
# File 'lib/onelogin/ruby-saml/response.rb', line 98

def sessionindex
  @sessionindex ||= begin
    node = xpath_first_from_signed_assertion('/a:AuthnStatement')
    node.nil? ? nil : node.attributes['SessionIndex']
  end
end

#status_codeString

Returns StatusCode value from a SAML Response.

Returns:

  • (String)

    StatusCode value from a SAML Response.



171
172
173
174
175
176
177
178
179
180
# File 'lib/onelogin/ruby-saml/response.rb', line 171

def status_code
  @status_code ||= begin
    node = REXML::XPath.first(
      document,
      "/p:Response/p:Status/p:StatusCode",
      { "p" => PROTOCOL }
    )
    node.attributes["Value"] if node && node.attributes
  end
end

#status_messageString

Returns the StatusMessage value from a SAML Response.

Returns:

  • (String)

    the StatusMessage value from a SAML Response.



184
185
186
187
188
189
190
191
192
193
# File 'lib/onelogin/ruby-saml/response.rb', line 184

def status_message
  @status_message ||= begin
    node = REXML::XPath.first(
      document,
      "/p:Response/p:Status/p:StatusMessage",
      { "p" => PROTOCOL }
    )
    node.text if node
  end
end

#success?Boolean

Checks if the Status has the “Success” code

Returns:

  • (Boolean)

    True if the StatusCode is Sucess



165
166
167
# File 'lib/onelogin/ruby-saml/response.rb', line 165

def success?
  status_code == "urn:oasis:names:tc:SAML:2.0:status:Success"
end