@@ -11,6 +11,8 @@ module RubySaml
1111 # SAML2 Authentication Response. SAML Response
1212 #
1313 class Response < SamlMessage
14+ include ErrorHandling
15+
1416 ASSERTION = "urn:oasis:names:tc:SAML:2.0:assertion"
1517 PROTOCOL = "urn:oasis:names:tc:SAML:2.0:protocol"
1618 DSIG = "http://www.w3.org/2000/09/xmldsig#"
@@ -21,9 +23,6 @@ class Response < SamlMessage
2123 # OneLogin::RubySaml::Settings Toolkit settings
2224 attr_accessor :settings
2325
24- # Array with the causes [Array of strings]
25- attr_accessor :errors
26-
2726 attr_reader :document
2827 attr_reader :decrypted_document
2928 attr_reader :response
@@ -39,16 +38,15 @@ class Response < SamlMessage
3938 # or :matches_request_id that will validate that the response matches the ID of the request,
4039 # or skip the subject confirmation validation with the :skip_subject_confirmation option
4140 def initialize ( response , options = { } )
42- @errors = [ ]
43-
4441 raise ArgumentError . new ( "Response cannot be nil" ) if response . nil?
45- @options = options
4642
43+ @errors = [ ]
44+ @options = options
4745 @soft = true
48- if ! options . empty? && ! options [ :settings ] . nil?
46+ unless options [ :settings ] . nil?
4947 @settings = options [ :settings ]
50- if ! options [ : settings] . soft . nil?
51- @soft = options [ : settings] . soft
48+ unless @ settings. soft . nil?
49+ @soft = @ settings. soft
5250 end
5351 end
5452
@@ -60,18 +58,6 @@ def initialize(response, options = {})
6058 end
6159 end
6260
63- # Append the cause to the errors array, and based on the value of soft, return false or raise
64- # an exception
65- def append_error ( error_msg )
66- @errors << error_msg
67- return soft ? false : validation_error ( error_msg )
68- end
69-
70- # Reset the errors array
71- def reset_errors!
72- @errors = [ ]
73- end
74-
7561 # Validates the SAML Response with the default values (soft = true)
7662 # @return [Boolean] TRUE if the SAML Response is valid
7763 #
@@ -284,21 +270,23 @@ def allowed_clock_drift
284270 def validate
285271 reset_errors!
286272
287- validate_response_state &&
288- validate_version &&
289- validate_id &&
290- validate_success_status &&
291- validate_num_assertion &&
292- validate_no_encrypted_attributes &&
293- validate_signed_elements &&
294- validate_structure &&
295- validate_in_response_to &&
296- validate_conditions &&
297- validate_audience &&
298- validate_issuer &&
299- validate_session_expiration &&
300- validate_subject_confirmation &&
273+ return false unless validate_response_state
274+ validate_version
275+ validate_id
276+ validate_success_status
277+ validate_num_assertion
278+ validate_no_encrypted_attributes
279+ validate_signed_elements
280+ validate_structure
281+ validate_in_response_to
282+ validate_conditions
283+ validate_audience
284+ validate_issuer
285+ validate_session_expiration
286+ validate_subject_confirmation
301287 validate_signature
288+
289+ @errors . empty?
302290 end
303291
304292
@@ -585,9 +573,8 @@ def validate_signature
585573 )
586574 doc = ( response_signed || decrypted_document . nil? ) ? document : decrypted_document
587575
588- unless fingerprint && doc . validate_document ( fingerprint , :fingerprint_alg => settings . idp_cert_fingerprint_algorithm )
589- error_msg = "Invalid Signature on SAML Response"
590- return append_error ( error_msg )
576+ unless fingerprint && doc . validate_document ( fingerprint , true , :fingerprint_alg => settings . idp_cert_fingerprint_algorithm )
577+ return append_error ( "Invalid Signature on SAML Response" )
591578 end
592579
593580 true
@@ -641,7 +628,7 @@ def xpath_from_signed_assertion(subelt=nil)
641628 #
642629 def generate_decrypted_document
643630 if settings . nil? || !settings . get_sp_key
644- validation_error ( 'An EncryptedAssertion found and no SP private key found on the settings to decrypt it. Be sure you provided the :settings parameter at the initialize method' )
631+ raise ValidationError . new ( 'An EncryptedAssertion found and no SP private key found on the settings to decrypt it. Be sure you provided the :settings parameter at the initialize method' )
645632 end
646633
647634 # Marshal at Ruby 1.8.7 throw an Exception
@@ -707,7 +694,7 @@ def decrypt_nameid(encryptedid_node)
707694 #
708695 def decrypt_element ( encrypt_node , rgrex )
709696 if settings . nil? || !settings . get_sp_key
710- return validation_error ( 'An ' + encrypt_node . name + ' found and no SP private key found on the settings to decrypt it' )
697+ raise ValidationError . new ( 'An ' + encrypt_node . name + ' found and no SP private key found on the settings to decrypt it' )
711698 end
712699
713700 elem_plaintext = OneLogin ::RubySaml ::Utils . decrypt_data ( encrypt_node , settings . get_sp_key )
0 commit comments