ruby-saml 1.9.0 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ruby-saml might be problematic. Click here for more details.

Files changed (157) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +30 -14
  3. data/README.md +108 -22
  4. data/changelog.md +38 -0
  5. data/lib/onelogin/ruby-saml/attributes.rb +24 -1
  6. data/lib/onelogin/ruby-saml/authrequest.rb +23 -6
  7. data/lib/onelogin/ruby-saml/idp_metadata_parser.rb +239 -171
  8. data/lib/onelogin/ruby-saml/logging.rb +3 -3
  9. data/lib/onelogin/ruby-saml/logoutrequest.rb +20 -5
  10. data/lib/onelogin/ruby-saml/logoutresponse.rb +25 -9
  11. data/lib/onelogin/ruby-saml/metadata.rb +11 -3
  12. data/lib/onelogin/ruby-saml/response.rb +67 -21
  13. data/lib/onelogin/ruby-saml/saml_message.rb +12 -2
  14. data/lib/onelogin/ruby-saml/setting_error.rb +6 -0
  15. data/lib/onelogin/ruby-saml/settings.rb +73 -7
  16. data/lib/onelogin/ruby-saml/slo_logoutrequest.rb +20 -1
  17. data/lib/onelogin/ruby-saml/slo_logoutresponse.rb +38 -16
  18. data/lib/onelogin/ruby-saml/utils.rb +74 -1
  19. data/lib/onelogin/ruby-saml/version.rb +1 -1
  20. data/lib/xml_security.rb +34 -6
  21. data/ruby-saml.gemspec +15 -7
  22. metadata +36 -278
  23. data/test/certificates/certificate1 +0 -12
  24. data/test/certificates/certificate_without_head_foot +0 -1
  25. data/test/certificates/formatted_certificate +0 -14
  26. data/test/certificates/formatted_chained_certificate +0 -42
  27. data/test/certificates/formatted_private_key +0 -12
  28. data/test/certificates/formatted_rsa_private_key +0 -12
  29. data/test/certificates/invalid_certificate1 +0 -1
  30. data/test/certificates/invalid_certificate2 +0 -1
  31. data/test/certificates/invalid_certificate3 +0 -12
  32. data/test/certificates/invalid_chained_certificate1 +0 -1
  33. data/test/certificates/invalid_private_key1 +0 -1
  34. data/test/certificates/invalid_private_key2 +0 -1
  35. data/test/certificates/invalid_private_key3 +0 -10
  36. data/test/certificates/invalid_rsa_private_key1 +0 -1
  37. data/test/certificates/invalid_rsa_private_key2 +0 -1
  38. data/test/certificates/invalid_rsa_private_key3 +0 -10
  39. data/test/certificates/ruby-saml-2.crt +0 -15
  40. data/test/certificates/ruby-saml.crt +0 -14
  41. data/test/certificates/ruby-saml.key +0 -15
  42. data/test/idp_metadata_parser_test.rb +0 -579
  43. data/test/logging_test.rb +0 -62
  44. data/test/logout_requests/invalid_slo_request.xml +0 -6
  45. data/test/logout_requests/slo_request.xml +0 -4
  46. data/test/logout_requests/slo_request.xml.base64 +0 -1
  47. data/test/logout_requests/slo_request_deflated.xml.base64 +0 -1
  48. data/test/logout_requests/slo_request_with_name_id_format.xml +0 -4
  49. data/test/logout_requests/slo_request_with_session_index.xml +0 -5
  50. data/test/logout_responses/logoutresponse_fixtures.rb +0 -67
  51. data/test/logoutrequest_test.rb +0 -226
  52. data/test/logoutresponse_test.rb +0 -402
  53. data/test/metadata/idp_descriptor.xml +0 -26
  54. data/test/metadata/idp_descriptor_2.xml +0 -56
  55. data/test/metadata/idp_descriptor_3.xml +0 -14
  56. data/test/metadata/idp_descriptor_4.xml +0 -72
  57. data/test/metadata/idp_metadata_different_sign_and_encrypt_cert.xml +0 -72
  58. data/test/metadata/idp_metadata_multi_certs.xml +0 -75
  59. data/test/metadata/idp_metadata_multi_signing_certs.xml +0 -52
  60. data/test/metadata/idp_metadata_same_sign_and_encrypt_cert.xml +0 -71
  61. data/test/metadata/idp_multiple_descriptors.xml +0 -53
  62. data/test/metadata/no_idp_descriptor.xml +0 -21
  63. data/test/metadata_test.rb +0 -331
  64. data/test/request_test.rb +0 -323
  65. data/test/response_test.rb +0 -1619
  66. data/test/responses/adfs_response_sha1.xml +0 -46
  67. data/test/responses/adfs_response_sha256.xml +0 -46
  68. data/test/responses/adfs_response_sha384.xml +0 -46
  69. data/test/responses/adfs_response_sha512.xml +0 -46
  70. data/test/responses/adfs_response_xmlns.xml +0 -45
  71. data/test/responses/attackxee.xml +0 -13
  72. data/test/responses/invalids/duplicated_attributes.xml.base64 +0 -1
  73. data/test/responses/invalids/empty_destination.xml.base64 +0 -1
  74. data/test/responses/invalids/empty_nameid.xml.base64 +0 -1
  75. data/test/responses/invalids/encrypted_new_attack.xml.base64 +0 -1
  76. data/test/responses/invalids/invalid_audience.xml.base64 +0 -1
  77. data/test/responses/invalids/invalid_issuer_assertion.xml.base64 +0 -1
  78. data/test/responses/invalids/invalid_issuer_message.xml.base64 +0 -1
  79. data/test/responses/invalids/invalid_signature_position.xml.base64 +0 -1
  80. data/test/responses/invalids/invalid_subjectconfirmation_inresponse.xml.base64 +0 -1
  81. data/test/responses/invalids/invalid_subjectconfirmation_nb.xml.base64 +0 -1
  82. data/test/responses/invalids/invalid_subjectconfirmation_noa.xml.base64 +0 -1
  83. data/test/responses/invalids/invalid_subjectconfirmation_recipient.xml.base64 +0 -1
  84. data/test/responses/invalids/multiple_assertions.xml.base64 +0 -2
  85. data/test/responses/invalids/multiple_signed.xml.base64 +0 -1
  86. data/test/responses/invalids/no_authnstatement.xml.base64 +0 -1
  87. data/test/responses/invalids/no_conditions.xml.base64 +0 -1
  88. data/test/responses/invalids/no_id.xml.base64 +0 -1
  89. data/test/responses/invalids/no_issuer_assertion.xml.base64 +0 -1
  90. data/test/responses/invalids/no_issuer_response.xml.base64 +0 -1
  91. data/test/responses/invalids/no_nameid.xml.base64 +0 -1
  92. data/test/responses/invalids/no_saml2.xml.base64 +0 -1
  93. data/test/responses/invalids/no_signature.xml.base64 +0 -1
  94. data/test/responses/invalids/no_status.xml.base64 +0 -1
  95. data/test/responses/invalids/no_status_code.xml.base64 +0 -1
  96. data/test/responses/invalids/no_subjectconfirmation_data.xml.base64 +0 -1
  97. data/test/responses/invalids/no_subjectconfirmation_method.xml.base64 +0 -1
  98. data/test/responses/invalids/response_invalid_signed_element.xml.base64 +0 -1
  99. data/test/responses/invalids/response_with_concealed_signed_assertion.xml +0 -51
  100. data/test/responses/invalids/response_with_doubled_signed_assertion.xml +0 -49
  101. data/test/responses/invalids/signature_wrapping_attack.xml.base64 +0 -1
  102. data/test/responses/invalids/status_code_responder.xml.base64 +0 -1
  103. data/test/responses/invalids/status_code_responer_and_msg.xml.base64 +0 -1
  104. data/test/responses/invalids/wrong_spnamequalifier.xml.base64 +0 -1
  105. data/test/responses/no_signature_ns.xml +0 -48
  106. data/test/responses/open_saml_response.xml +0 -56
  107. data/test/responses/response_assertion_wrapped.xml.base64 +0 -93
  108. data/test/responses/response_audience_self_closed_tag.xml.base64 +0 -1
  109. data/test/responses/response_double_status_code.xml.base64 +0 -1
  110. data/test/responses/response_encrypted_attrs.xml.base64 +0 -1
  111. data/test/responses/response_encrypted_nameid.xml.base64 +0 -1
  112. data/test/responses/response_eval.xml +0 -7
  113. data/test/responses/response_no_cert_and_encrypted_attrs.xml +0 -29
  114. data/test/responses/response_node_text_attack.xml.base64 +0 -1
  115. data/test/responses/response_node_text_attack2.xml.base64 +0 -1
  116. data/test/responses/response_node_text_attack3.xml.base64 +0 -1
  117. data/test/responses/response_unsigned_xml_base64 +0 -1
  118. data/test/responses/response_with_ampersands.xml +0 -139
  119. data/test/responses/response_with_ampersands.xml.base64 +0 -93
  120. data/test/responses/response_with_ds_namespace_at_the_root.xml.base64 +0 -1
  121. data/test/responses/response_with_multiple_attribute_statements.xml +0 -72
  122. data/test/responses/response_with_multiple_attribute_values.xml +0 -67
  123. data/test/responses/response_with_retrieval_method.xml +0 -26
  124. data/test/responses/response_with_saml2_namespace.xml.base64 +0 -102
  125. data/test/responses/response_with_signed_assertion.xml.base64 +0 -66
  126. data/test/responses/response_with_signed_assertion_2.xml.base64 +0 -1
  127. data/test/responses/response_with_signed_assertion_3.xml +0 -30
  128. data/test/responses/response_with_signed_message_and_assertion.xml +0 -34
  129. data/test/responses/response_with_undefined_recipient.xml.base64 +0 -1
  130. data/test/responses/response_without_attributes.xml.base64 +0 -79
  131. data/test/responses/response_without_reference_uri.xml.base64 +0 -1
  132. data/test/responses/response_wrapped.xml.base64 +0 -150
  133. data/test/responses/signed_message_encrypted_signed_assertion.xml.base64 +0 -1
  134. data/test/responses/signed_message_encrypted_unsigned_assertion.xml.base64 +0 -1
  135. data/test/responses/signed_nameid_in_atts.xml +0 -47
  136. data/test/responses/signed_unqual_nameid_in_atts.xml +0 -47
  137. data/test/responses/simple_saml_php.xml +0 -71
  138. data/test/responses/starfield_response.xml.base64 +0 -1
  139. data/test/responses/test_sign.xml +0 -43
  140. data/test/responses/unsigned_encrypted_adfs.xml +0 -23
  141. data/test/responses/unsigned_message_aes128_encrypted_signed_assertion.xml.base64 +0 -1
  142. data/test/responses/unsigned_message_aes192_encrypted_signed_assertion.xml.base64 +0 -1
  143. data/test/responses/unsigned_message_aes256_encrypted_signed_assertion.xml.base64 +0 -1
  144. data/test/responses/unsigned_message_des192_encrypted_signed_assertion.xml.base64 +0 -1
  145. data/test/responses/unsigned_message_encrypted_assertion_without_saml_namespace.xml.base64 +0 -1
  146. data/test/responses/unsigned_message_encrypted_signed_assertion.xml.base64 +0 -1
  147. data/test/responses/unsigned_message_encrypted_unsigned_assertion.xml.base64 +0 -1
  148. data/test/responses/valid_response.xml.base64 +0 -1
  149. data/test/responses/valid_response_with_formatted_x509certificate.xml.base64 +0 -1
  150. data/test/responses/valid_response_without_x509certificate.xml.base64 +0 -1
  151. data/test/saml_message_test.rb +0 -56
  152. data/test/settings_test.rb +0 -329
  153. data/test/slo_logoutrequest_test.rb +0 -448
  154. data/test/slo_logoutresponse_test.rb +0 -199
  155. data/test/test_helper.rb +0 -327
  156. data/test/utils_test.rb +0 -254
  157. data/test/xml_security_test.rb +0 -421
@@ -1,199 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
2
-
3
- require 'onelogin/ruby-saml/slo_logoutresponse'
4
-
5
- class SloLogoutresponseTest < Minitest::Test
6
-
7
- describe "SloLogoutresponse" do
8
- let(:settings) { OneLogin::RubySaml::Settings.new }
9
- let(:logout_request) { OneLogin::RubySaml::SloLogoutrequest.new(logout_request_document) }
10
-
11
- before do
12
- settings.idp_entity_id = 'https://app.onelogin.com/saml/metadata/SOMEACCOUNT'
13
- settings.idp_slo_target_url = "http://unauth.com/logout"
14
- settings.name_identifier_value = "f00f00"
15
- settings.compress_request = true
16
- settings.certificate = ruby_saml_cert_text
17
- settings.private_key = ruby_saml_key_text
18
- logout_request.settings = settings
19
- end
20
-
21
- it "create the deflated SAMLResponse URL parameter" do
22
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id)
23
- assert_match /^http:\/\/unauth\.com\/logout\?SAMLResponse=/, unauth_url
24
-
25
- inflated = decode_saml_response_payload(unauth_url)
26
- assert_match /^<samlp:LogoutResponse/, inflated
27
- end
28
-
29
- it "support additional params" do
30
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id, nil, { :hello => nil })
31
- assert_match /&hello=$/, unauth_url
32
-
33
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id, nil, { :foo => "bar" })
34
- assert_match /&foo=bar$/, unauth_url
35
-
36
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id, nil, { :RelayState => "http://idp.example.com" })
37
- assert_match /&RelayState=http%3A%2F%2Fidp.example.com$/, unauth_url
38
- end
39
-
40
- it "RelayState cases" do
41
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id, nil, { :RelayState => nil })
42
- assert !unauth_url.include?('RelayState')
43
-
44
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id, nil, { :RelayState => "http://example.com" })
45
- assert unauth_url.include?('&RelayState=http%3A%2F%2Fexample.com')
46
-
47
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id, nil, { 'RelayState' => nil })
48
- assert !unauth_url.include?('RelayState')
49
-
50
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id, nil, { 'RelayState' => "http://example.com" })
51
- assert unauth_url.include?('&RelayState=http%3A%2F%2Fexample.com')
52
- end
53
-
54
- it "set InResponseTo to the ID from the logout request" do
55
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id)
56
-
57
- inflated = decode_saml_response_payload(unauth_url)
58
- assert_match /InResponseTo='_c0348950-935b-0131-1060-782bcb56fcaa'/, inflated
59
- end
60
-
61
- it "set a custom successful logout message on the response" do
62
- unauth_url = OneLogin::RubySaml::SloLogoutresponse.new.create(settings, logout_request.id, "Custom Logout Message")
63
-
64
- inflated = decode_saml_response_payload(unauth_url)
65
- assert_match /<samlp:StatusMessage>Custom Logout Message<\/samlp:StatusMessage>/, inflated
66
- end
67
-
68
- describe "when the settings indicate to sign (embedded) logout response" do
69
-
70
- before do
71
- settings.compress_response = false
72
- settings.security[:logout_responses_signed] = true
73
- settings.security[:embed_sign] = true
74
- end
75
-
76
- it "create a signed logout response" do
77
- logout_request.settings = settings
78
- params = OneLogin::RubySaml::SloLogoutresponse.new.create_params(settings, logout_request.id, "Custom Logout Message")
79
-
80
- response_xml = Base64.decode64(params["SAMLResponse"])
81
- assert_match %r[<ds:SignatureValue>([a-zA-Z0-9/+=]+)</ds:SignatureValue>], response_xml
82
- assert_match /<ds:SignatureMethod Algorithm='http:\/\/www.w3.org\/2000\/09\/xmldsig#rsa-sha1'\/>/, response_xml
83
- assert_match /<ds:DigestMethod Algorithm='http:\/\/www.w3.org\/2000\/09\/xmldsig#sha1'\/>/, response_xml
84
- end
85
-
86
- it "create a signed logout response with 256 digest and signature methods" do
87
- settings.security[:signature_method] = XMLSecurity::Document::RSA_SHA256
88
- settings.security[:digest_method] = XMLSecurity::Document::SHA256
89
-
90
- params = OneLogin::RubySaml::SloLogoutresponse.new.create_params(settings, logout_request.id, "Custom Logout Message")
91
-
92
- response_xml = Base64.decode64(params["SAMLResponse"])
93
- assert_match %r[<ds:SignatureValue>([a-zA-Z0-9/+=]+)</ds:SignatureValue>], response_xml
94
- assert_match /<ds:SignatureMethod Algorithm='http:\/\/www.w3.org\/2001\/04\/xmldsig-more#rsa-sha256'\/>/, response_xml
95
- assert_match /<ds:DigestMethod Algorithm='http:\/\/www.w3.org\/2001\/04\/xmlenc#sha256'\/>/, response_xml
96
- end
97
-
98
- it "create a signed logout response with 512 digest and signature method RSA_SHA384" do
99
- settings.security[:signature_method] = XMLSecurity::Document::RSA_SHA384
100
- settings.security[:digest_method] = XMLSecurity::Document::SHA512
101
- logout_request.settings = settings
102
-
103
- params = OneLogin::RubySaml::SloLogoutresponse.new.create_params(settings, logout_request.id, "Custom Logout Message")
104
-
105
- response_xml = Base64.decode64(params["SAMLResponse"])
106
- assert_match %r[<ds:SignatureValue>([a-zA-Z0-9/+=]+)</ds:SignatureValue>], response_xml
107
- assert_match /<ds:SignatureMethod Algorithm='http:\/\/www.w3.org\/2001\/04\/xmldsig-more#rsa-sha384'\/>/, response_xml
108
- assert_match /<ds:DigestMethod Algorithm='http:\/\/www.w3.org\/2001\/04\/xmlenc#sha512'\/>/, response_xml
109
- end
110
- end
111
-
112
- describe "#create_params when the settings indicate to sign the logout response" do
113
-
114
- let(:cert) { OpenSSL::X509::Certificate.new(ruby_saml_cert_text) }
115
-
116
- before do
117
- settings.compress_response = false
118
- settings.security[:logout_responses_signed] = true
119
- settings.security[:embed_sign] = false
120
- end
121
-
122
- it "create a signature parameter with RSA_SHA1 and validate it" do
123
- settings.security[:signature_method] = XMLSecurity::Document::RSA_SHA1
124
-
125
- params = OneLogin::RubySaml::SloLogoutresponse.new.create_params(settings, logout_request.id, "Custom Logout Message", :RelayState => 'http://example.com')
126
- assert params['SAMLResponse']
127
- assert params[:RelayState]
128
- assert params['Signature']
129
- assert_equal params['SigAlg'], XMLSecurity::Document::RSA_SHA1
130
-
131
- query_string = "SAMLResponse=#{CGI.escape(params['SAMLResponse'])}"
132
- query_string << "&RelayState=#{CGI.escape(params[:RelayState])}"
133
- query_string << "&SigAlg=#{CGI.escape(params['SigAlg'])}"
134
-
135
- signature_algorithm = XMLSecurity::BaseDocument.new.algorithm(params['SigAlg'])
136
- assert_equal signature_algorithm, OpenSSL::Digest::SHA1
137
- assert cert.public_key.verify(signature_algorithm.new, Base64.decode64(params['Signature']), query_string)
138
- end
139
-
140
- it "create a signature parameter with RSA_SHA256 /SHA256 and validate it" do
141
- settings.security[:signature_method] = XMLSecurity::Document::RSA_SHA256
142
-
143
- params = OneLogin::RubySaml::SloLogoutresponse.new.create_params(settings, logout_request.id, "Custom Logout Message", :RelayState => 'http://example.com')
144
- assert params['SAMLResponse']
145
- assert params[:RelayState]
146
- assert params['Signature']
147
-
148
- assert_equal params['SigAlg'], XMLSecurity::Document::RSA_SHA256
149
-
150
- query_string = "SAMLResponse=#{CGI.escape(params['SAMLResponse'])}"
151
- query_string << "&RelayState=#{CGI.escape(params[:RelayState])}"
152
- query_string << "&SigAlg=#{CGI.escape(params['SigAlg'])}"
153
-
154
- signature_algorithm = XMLSecurity::BaseDocument.new.algorithm(params['SigAlg'])
155
- assert_equal signature_algorithm, OpenSSL::Digest::SHA256
156
- assert cert.public_key.verify(signature_algorithm.new, Base64.decode64(params['Signature']), query_string)
157
- end
158
-
159
- it "create a signature parameter with RSA_SHA384 / SHA384 and validate it" do
160
- settings.security[:signature_method] = XMLSecurity::Document::RSA_SHA384
161
-
162
- params = OneLogin::RubySaml::SloLogoutresponse.new.create_params(settings, logout_request.id, "Custom Logout Message", :RelayState => 'http://example.com')
163
- assert params['SAMLResponse']
164
- assert params[:RelayState]
165
- assert params['Signature']
166
-
167
- assert_equal params['SigAlg'], XMLSecurity::Document::RSA_SHA384
168
-
169
- query_string = "SAMLResponse=#{CGI.escape(params['SAMLResponse'])}"
170
- query_string << "&RelayState=#{CGI.escape(params[:RelayState])}"
171
- query_string << "&SigAlg=#{CGI.escape(params['SigAlg'])}"
172
-
173
- signature_algorithm = XMLSecurity::BaseDocument.new.algorithm(params['SigAlg'])
174
- assert_equal signature_algorithm, OpenSSL::Digest::SHA384
175
- assert cert.public_key.verify(signature_algorithm.new, Base64.decode64(params['Signature']), query_string)
176
- end
177
-
178
- it "create a signature parameter with RSA_SHA512 / SHA512 and validate it" do
179
- settings.security[:signature_method] = XMLSecurity::Document::RSA_SHA512
180
-
181
- params = OneLogin::RubySaml::SloLogoutresponse.new.create_params(settings, logout_request.id, "Custom Logout Message", :RelayState => 'http://example.com')
182
- assert params['SAMLResponse']
183
- assert params[:RelayState]
184
- assert params['Signature']
185
-
186
- assert_equal params['SigAlg'], XMLSecurity::Document::RSA_SHA512
187
-
188
- query_string = "SAMLResponse=#{CGI.escape(params['SAMLResponse'])}"
189
- query_string << "&RelayState=#{CGI.escape(params[:RelayState])}"
190
- query_string << "&SigAlg=#{CGI.escape(params['SigAlg'])}"
191
-
192
- signature_algorithm = XMLSecurity::BaseDocument.new.algorithm(params['SigAlg'])
193
- assert_equal signature_algorithm, OpenSSL::Digest::SHA512
194
- assert cert.public_key.verify(signature_algorithm.new, Base64.decode64(params['Signature']), query_string)
195
- end
196
-
197
- end
198
- end
199
- end
data/test/test_helper.rb DELETED
@@ -1,327 +0,0 @@
1
- require 'simplecov'
2
-
3
- SimpleCov.start do
4
- add_filter "test/"
5
- add_filter "vendor/"
6
- add_filter "lib/onelogin/ruby-saml/logging.rb"
7
- end
8
-
9
- require 'stringio'
10
- require 'rubygems'
11
- require 'bundler'
12
- require 'minitest/autorun'
13
- require 'mocha/setup'
14
- require 'timecop'
15
-
16
- Bundler.require :default, :test
17
-
18
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
19
- $LOAD_PATH.unshift(File.dirname(__FILE__))
20
-
21
- require 'onelogin/ruby-saml/logging'
22
-
23
- TEST_LOGGER = Logger.new(StringIO.new)
24
- OneLogin::RubySaml::Logging.logger = TEST_LOGGER
25
-
26
- class Minitest::Test
27
- def fixture(document, base64 = true)
28
- response = Dir.glob(File.join(File.dirname(__FILE__), "responses", "#{document}*")).first
29
- if base64 && response =~ /\.xml$/
30
- Base64.encode64(File.read(response))
31
- else
32
- File.read(response)
33
- end
34
- end
35
-
36
- def read_response(response)
37
- File.read(File.join(File.dirname(__FILE__), "responses", response))
38
- end
39
-
40
- def read_invalid_response(response)
41
- File.read(File.join(File.dirname(__FILE__), "responses", "invalids", response))
42
- end
43
-
44
- def read_logout_request(request)
45
- File.read(File.join(File.dirname(__FILE__), "logout_requests", request))
46
- end
47
-
48
- def read_certificate(certificate)
49
- File.read(File.join(File.dirname(__FILE__), "certificates", certificate))
50
- end
51
-
52
- def response_document_valid_signed
53
- @response_document_valid_signed ||= read_response("valid_response.xml.base64")
54
- end
55
-
56
- def response_document_valid_signed_without_x509certificate
57
- @response_document_valid_signed_without_x509certificate ||= read_response("valid_response_without_x509certificate.xml.base64")
58
- end
59
-
60
- def response_document_without_recipient
61
- @response_document_without_recipient ||= read_response("response_with_undefined_recipient.xml.base64")
62
- end
63
-
64
- def response_document_without_recipient_with_time_updated
65
- doc = Base64.decode64(response_document_without_recipient)
66
- doc.gsub!(/NotBefore=\"(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z\"/, "NotBefore=\"#{(Time.now-300).getutc.strftime("%Y-%m-%dT%XZ")}\"")
67
- doc.gsub!(/NotOnOrAfter=\"(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z\"/, "NotOnOrAfter=\"#{(Time.now+300).getutc.strftime("%Y-%m-%dT%XZ")}\"")
68
- Base64.encode64(doc)
69
- end
70
-
71
- def response_document_without_attributes
72
- @response_document_without_attributes ||= read_response("response_without_attributes.xml.base64")
73
- end
74
-
75
- def response_document_without_reference_uri
76
- @response_document_without_reference_uri ||= read_response("response_without_reference_uri.xml.base64")
77
- end
78
-
79
- def response_document_with_signed_assertion
80
- @response_document_with_signed_assertion ||= read_response("response_with_signed_assertion.xml.base64")
81
- end
82
-
83
- def response_document_with_signed_assertion_2
84
- @response_document_with_signed_assertion_2 ||= read_response("response_with_signed_assertion_2.xml.base64")
85
- end
86
-
87
- def response_document_with_ds_namespace_at_the_root
88
- @response_document_with_ds_namespace_at_the_root ||= read_response("response_with_ds_namespace_at_the_root.xml.base64")
89
- end
90
-
91
- def response_document_unsigned
92
- @response_document_unsigned ||= read_response("response_unsigned_xml_base64")
93
- end
94
-
95
- def response_document_with_saml2_namespace
96
- @response_document_with_saml2_namespace ||= read_response("response_with_saml2_namespace.xml.base64")
97
- end
98
-
99
- def ampersands_document
100
- @ampersands_response ||= read_response("response_with_ampersands.xml.base64")
101
- end
102
-
103
- def response_document_no_cert_and_encrypted_attrs
104
- @response_document_no_cert_and_encrypted_attrs ||= Base64.encode64(read_response("response_no_cert_and_encrypted_attrs.xml"))
105
- end
106
-
107
- def response_document_wrapped
108
- @response_document_wrapped ||= read_response("response_wrapped.xml.base64")
109
- end
110
-
111
- def response_document_assertion_wrapped
112
- @response_document_assertion_wrapped ||= read_response("response_assertion_wrapped.xml.base64")
113
- end
114
-
115
- def response_document_encrypted_nameid
116
- @response_document_encrypted_nameid ||= File.read(File.join(File.dirname(__FILE__), 'responses', 'response_encrypted_nameid.xml.base64'))
117
- end
118
-
119
- def signed_message_encrypted_unsigned_assertion
120
- @signed_message_encrypted_unsigned_assertion ||= File.read(File.join(File.dirname(__FILE__), 'responses', 'signed_message_encrypted_unsigned_assertion.xml.base64'))
121
- end
122
-
123
- def signed_message_encrypted_signed_assertion
124
- @signed_message_encrypted_signed_assertion ||= File.read(File.join(File.dirname(__FILE__), 'responses', 'signed_message_encrypted_signed_assertion.xml.base64'))
125
- end
126
-
127
- def unsigned_message_encrypted_signed_assertion
128
- @unsigned_message_encrypted_signed_assertion ||= File.read(File.join(File.dirname(__FILE__), 'responses', 'unsigned_message_encrypted_signed_assertion.xml.base64'))
129
- end
130
-
131
- def unsigned_message_encrypted_unsigned_assertion
132
- @unsigned_message_encrypted_unsigned_assertion ||= File.read(File.join(File.dirname(__FILE__), 'responses', 'unsigned_message_encrypted_unsigned_assertion.xml.base64'))
133
- end
134
-
135
- def response_document_encrypted_attrs
136
- @response_document_encrypted_attrs ||= File.read(File.join(File.dirname(__FILE__), 'responses', 'response_encrypted_attrs.xml.base64'))
137
- end
138
-
139
- def response_document_double_status_code
140
- @response_document_double_status_code ||= File.read(File.join(File.dirname(__FILE__), 'responses', 'response_double_status_code.xml.base64'))
141
- end
142
-
143
- def signature_fingerprint_1
144
- @signature_fingerprint1 ||= "C5:19:85:D9:47:F1:BE:57:08:20:25:05:08:46:EB:27:F6:CA:B7:83"
145
- end
146
-
147
- # certificate used on response_with_undefined_recipient
148
- def signature_1
149
- @signature1 ||= read_certificate("certificate1")
150
- end
151
-
152
- # certificate used on response_document_with_signed_assertion_2
153
- def certificate_without_head_foot
154
- @certificate_without_head_foot ||= read_certificate("certificate_without_head_foot")
155
- end
156
-
157
- def idp_metadata_descriptor
158
- @idp_metadata_descriptor ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'idp_descriptor.xml'))
159
- end
160
-
161
- def idp_metadata_descriptor2
162
- @idp_metadata_descriptor2 ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'idp_descriptor_2.xml'))
163
- end
164
-
165
- def idp_metadata_descriptor3
166
- @idp_metadata_descriptor3 ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'idp_descriptor_3.xml'))
167
- end
168
-
169
- def idp_metadata_descriptor4
170
- @idp_metadata_descriptor4 ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'idp_descriptor_4.xml'))
171
- end
172
-
173
- def no_idp_metadata_descriptor
174
- @no_idp_metadata_descriptor ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'no_idp_descriptor.xml'))
175
- end
176
-
177
- def idp_metadata_multiple_descriptors
178
- @idp_metadata_multiple_descriptors ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'idp_multiple_descriptors.xml'))
179
- end
180
-
181
- def idp_metadata_multiple_certs
182
- @idp_metadata_multiple_descriptors ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'idp_metadata_multi_certs.xml'))
183
- end
184
-
185
- def idp_metadata_multiple_signing_certs
186
- @idp_metadata_multiple_signing_certs ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'idp_metadata_multi_signing_certs.xml'))
187
- end
188
-
189
- def idp_metadata_same_sign_and_encrypt_cert
190
- @idp_metadata_same_sign_and_encrypt_cert ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'idp_metadata_same_sign_and_encrypt_cert.xml'))
191
- end
192
-
193
- def idp_metadata_different_sign_and_encrypt_cert
194
- @idp_metadata_different_sign_and_encrypt_cert ||= File.read(File.join(File.dirname(__FILE__), 'metadata', 'idp_metadata_different_sign_and_encrypt_cert.xml'))
195
- end
196
-
197
- def logout_request_document
198
- unless @logout_request_document
199
- xml = read_logout_request("slo_request.xml")
200
- deflated = Zlib::Deflate.deflate(xml, 9)[2..-5]
201
- @logout_request_document = Base64.encode64(deflated)
202
- end
203
- @logout_request_document
204
- end
205
-
206
- def logout_request_document_with_name_id_format
207
- unless @logout_request_document_with_name_id_format
208
- xml = read_logout_request("slo_request_with_name_id_format.xml")
209
- deflated = Zlib::Deflate.deflate(xml, 9)[2..-5]
210
- @logout_request_document_with_name_id_format = Base64.encode64(deflated)
211
- end
212
- @logout_request_document_with_name_id_format
213
- end
214
-
215
- def logout_request_xml_with_session_index
216
- @logout_request_xml_with_session_index ||= File.read(File.join(File.dirname(__FILE__), 'logout_requests', 'slo_request_with_session_index.xml'))
217
- end
218
-
219
- def invalid_logout_request_document
220
- unless @invalid_logout_request_document
221
- xml = File.read(File.join(File.dirname(__FILE__), 'logout_requests', 'invalid_slo_request.xml'))
222
- deflated = Zlib::Deflate.deflate(xml, 9)[2..-5]
223
- @invalid_logout_request_document = Base64.encode64(deflated)
224
- end
225
- @invalid_logout_request_document
226
- end
227
-
228
- def logout_request_base64
229
- @logout_request_base64 ||= File.read(File.join(File.dirname(__FILE__), 'logout_requests', 'slo_request.xml.base64'))
230
- end
231
-
232
- def logout_request_deflated_base64
233
- @logout_request_deflated_base64 ||= File.read(File.join(File.dirname(__FILE__), 'logout_requests', 'slo_request_deflated.xml.base64'))
234
- end
235
-
236
- def ruby_saml_cert
237
- @ruby_saml_cert ||= OpenSSL::X509::Certificate.new(ruby_saml_cert_text)
238
- end
239
-
240
- def ruby_saml_cert2
241
- @ruby_saml_cert2 ||= OpenSSL::X509::Certificate.new(ruby_saml_cert_text2)
242
- end
243
-
244
- def ruby_saml_cert_fingerprint
245
- @ruby_saml_cert_fingerprint ||= Digest::SHA1.hexdigest(ruby_saml_cert.to_der).scan(/../).join(":")
246
- end
247
-
248
- def ruby_saml_cert_text
249
- read_certificate("ruby-saml.crt")
250
- end
251
-
252
- def ruby_saml_cert_text2
253
- read_certificate("ruby-saml-2.crt")
254
- end
255
-
256
- def ruby_saml_key
257
- @ruby_saml_key ||= OpenSSL::PKey::RSA.new(ruby_saml_key_text)
258
- end
259
-
260
- def ruby_saml_key_text
261
- read_certificate("ruby-saml.key")
262
- end
263
-
264
- #
265
- # logoutresponse fixtures
266
- #
267
- def random_id
268
- "_#{OneLogin::RubySaml::Utils.uuid}"
269
- end
270
-
271
- #
272
- # decodes a base64 encoded SAML response for use in SloLogoutresponse tests
273
- #
274
- def decode_saml_response_payload(unauth_url)
275
- payload = CGI.unescape(unauth_url.split("SAMLResponse=").last)
276
- decoded = Base64.decode64(payload)
277
-
278
- zstream = Zlib::Inflate.new(-Zlib::MAX_WBITS)
279
- inflated = zstream.inflate(decoded)
280
- zstream.finish
281
- zstream.close
282
- inflated
283
- end
284
-
285
- #
286
- # decodes a base64 encoded SAML request for use in Logoutrequest tests
287
- #
288
- def decode_saml_request_payload(unauth_url)
289
- payload = CGI.unescape(unauth_url.split("SAMLRequest=").last)
290
- decoded = Base64.decode64(payload)
291
-
292
- zstream = Zlib::Inflate.new(-Zlib::MAX_WBITS)
293
- inflated = zstream.inflate(decoded)
294
- zstream.finish
295
- zstream.close
296
- inflated
297
- end
298
-
299
- SCHEMA_DIR = File.expand_path(File.join(__FILE__, '../../lib/schemas'))
300
-
301
- #
302
- # validate an xml document against the given schema
303
- #
304
- def validate_xml!(document, schema)
305
- Dir.chdir(SCHEMA_DIR) do
306
- xsd = if schema.is_a? Nokogiri::XML::Schema
307
- schema
308
- else
309
- Nokogiri::XML::Schema(File.read(schema))
310
- end
311
-
312
- xml = if document.is_a? Nokogiri::XML::Document
313
- document
314
- else
315
- Nokogiri::XML(document) { |c| c.strict }
316
- end
317
-
318
- result = xsd.validate(xml)
319
-
320
- if result.length != 0
321
- raise "Schema validation failed! XSD validation errors: #{result.join(", ")}"
322
- else
323
- true
324
- end
325
- end
326
- end
327
- end