@@ -11,6 +11,8 @@ module RubySaml
11
11
# SAML2 Authentication Response. SAML Response
12
12
#
13
13
class Response < SamlMessage
14
+ include ErrorHandling
15
+
14
16
ASSERTION = "urn:oasis:names:tc:SAML:2.0:assertion"
15
17
PROTOCOL = "urn:oasis:names:tc:SAML:2.0:protocol"
16
18
DSIG = "http://www.w3.org/2000/09/xmldsig#"
@@ -21,9 +23,6 @@ class Response < SamlMessage
21
23
# OneLogin::RubySaml::Settings Toolkit settings
22
24
attr_accessor :settings
23
25
24
- # Array with the causes [Array of strings]
25
- attr_accessor :errors
26
-
27
26
attr_reader :document
28
27
attr_reader :decrypted_document
29
28
attr_reader :response
@@ -39,16 +38,15 @@ class Response < SamlMessage
39
38
# or :matches_request_id that will validate that the response matches the ID of the request,
40
39
# or skip the subject confirmation validation with the :skip_subject_confirmation option
41
40
def initialize ( response , options = { } )
42
- @errors = [ ]
43
-
44
41
raise ArgumentError . new ( "Response cannot be nil" ) if response . nil?
45
- @options = options
46
42
43
+ @errors = [ ]
44
+ @options = options
47
45
@soft = true
48
- if ! options . empty? && ! options [ :settings ] . nil?
46
+ unless options [ :settings ] . nil?
49
47
@settings = options [ :settings ]
50
- if ! options [ : settings] . soft . nil?
51
- @soft = options [ : settings] . soft
48
+ unless @ settings. soft . nil?
49
+ @soft = @ settings. soft
52
50
end
53
51
end
54
52
@@ -60,18 +58,6 @@ def initialize(response, options = {})
60
58
end
61
59
end
62
60
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
-
75
61
# Validates the SAML Response with the default values (soft = true)
76
62
# @return [Boolean] TRUE if the SAML Response is valid
77
63
#
@@ -284,21 +270,23 @@ def allowed_clock_drift
284
270
def validate
285
271
reset_errors!
286
272
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
301
287
validate_signature
288
+
289
+ @errors . empty?
302
290
end
303
291
304
292
@@ -585,9 +573,8 @@ def validate_signature
585
573
)
586
574
doc = ( response_signed || decrypted_document . nil? ) ? document : decrypted_document
587
575
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" )
591
578
end
592
579
593
580
true
@@ -641,7 +628,7 @@ def xpath_from_signed_assertion(subelt=nil)
641
628
#
642
629
def generate_decrypted_document
643
630
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' )
645
632
end
646
633
647
634
# Marshal at Ruby 1.8.7 throw an Exception
@@ -707,7 +694,7 @@ def decrypt_nameid(encryptedid_node)
707
694
#
708
695
def decrypt_element ( encrypt_node , rgrex )
709
696
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' )
711
698
end
712
699
713
700
elem_plaintext = OneLogin ::RubySaml ::Utils . decrypt_data ( encrypt_node , settings . get_sp_key )
0 commit comments