Skip to content

Commit 0661c67

Browse files
committed
Ruby 1.8.7 compatibility
1 parent f1b6c98 commit 0661c67

5 files changed

Lines changed: 18 additions & 10 deletions

File tree

lib/onelogin/ruby-saml/idp_metadata_parser.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ def parse(idp_metadata, options = {})
119119

120120
unless parsed_metadata[:cache_duration].nil?
121121
cache_valid_until_timestamp = OneLogin::RubySaml::Utils.parse_duration(parsed_metadata[:cache_duration])
122-
if parsed_metadata[:valid_until].nil? || cache_valid_until_timestamp < Time.parse(parsed_metadata[:valid_until], Time.now.utc).to_i
123-
parsed_metadata[:valid_until] = Time.at(cache_valid_until_timestamp).utc.strftime("%Y-%m-%dT%H:%M:%SZ")
122+
unless cache_valid_until_timestamp.nil?
123+
if parsed_metadata[:valid_until].nil? || cache_valid_until_timestamp < Time.parse(parsed_metadata[:valid_until], Time.now.utc).to_i
124+
parsed_metadata[:valid_until] = Time.at(cache_valid_until_timestamp).utc.strftime("%Y-%m-%dT%H:%M:%SZ")
125+
end
124126
end
125127
end
126128
# Remove the cache_duration because on the settings

lib/onelogin/ruby-saml/utils.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module RubySaml
1313
class Utils
1414
@@uuid_generator = UUID.new if RUBY_VERSION < '1.9'
1515

16-
BINDINGS = { post: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST".freeze,
17-
redirect: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect".freeze }.freeze
16+
BINDINGS = { :post => "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST".freeze,
17+
:redirect => "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect".freeze }.freeze
1818
DSIG = "http://www.w3.org/2000/09/xmldsig#".freeze
1919
XENC = "http://www.w3.org/2001/04/xmlenc#".freeze
2020
DURATION_FORMAT = %r(^
@@ -55,6 +55,8 @@ def self.is_cert_expired(cert)
5555
# @return [Integer] The new timestamp, after the duration is applied.
5656
#
5757
def self.parse_duration(duration, timestamp=Time.now.utc)
58+
return nil if RUBY_VERSION < '1.9' # 1.8.7 not supported
59+
5860
matches = duration.match(DURATION_FORMAT)
5961

6062
if matches.nil?

test/idp_metadata_parser_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,16 @@ def initialize; end
354354
end
355355

356356
it "if no ValidUntil but CacheDuration return CacheDuration converted in ValidUntil" do
357+
return if RUBY_VERSION < '1.9'
357358
Timecop.freeze(Time.parse("2020-01-02T10:02:33Z", Time.now.utc)) do
358359
settings = @idp_metadata_parser.parse(idp_metadata_descriptor5)
359360
assert_equal '2020-01-03T10:02:33Z', settings.valid_until
360361
end
361362
end
362363

363364
it "if ValidUntil and CacheDuration return the sooner timestamp" do
365+
return if RUBY_VERSION < '1.9'
366+
364367
Timecop.freeze(Time.parse("2020-01-01T10:12:55Z", Time.now.utc)) do
365368
settings = @idp_metadata_parser.parse(idp_metadata_descriptor6)
366369
assert_equal '2020-01-03T10:12:55Z', settings.valid_until
@@ -585,7 +588,7 @@ def initialize; end
585588
it "should return idp_cert and idp_cert_fingerprint and no idp_cert_multi" do
586589
assert_equal(expected_cert, @settings.idp_cert)
587590
assert_equal("2D:A9:40:88:28:EE:67:BB:4A:5B:E0:58:A7:CC:71:95:2D:1B:C9:D3", @settings.idp_cert_fingerprint)
588-
assert_equal({ signing: [expected_cert], encryption: [expected_cert] }, @settings.idp_cert_multi)
591+
assert_equal({ :signing => [expected_cert], :encryption => [expected_cert] }, @settings.idp_cert_multi)
589592
assert_equal("https://app.onelogin.com/saml/metadata/383123", @settings.idp_entity_id)
590593
assert_equal("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", @settings.name_identifier_format)
591594
assert_equal("https://app.onelogin.com/trust/saml2/http-post/sso/383123", @settings.idp_sso_service_url)

test/utils_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,22 @@ class UtilsTest < Minitest::Test
2222
}
2323

2424
def result(duration, reference = 0)
25+
return nil if RUBY_VERSION < '1.9'
2526
Time.at(
2627
OneLogin::RubySaml::Utils.parse_duration(duration, reference)
2728
).utc.iso8601(3)
2829
end
2930

3031
DURATIONS_FROM_EPOCH.each do |duration, expected|
3132
it "parses #{duration} to return #{expected} from the given timestamp" do
33+
return if RUBY_VERSION < '1.9'
3234
assert_equal expected, result(duration)
3335
end
3436
end
3537

3638
it "returns the last calendar day of the next month when advancing from a longer month to a shorter one" do
3739
initial_timestamp = Time.iso8601("1970-01-31T00:00:00.000Z").to_i
38-
40+
return if RUBY_VERSION < '1.9'
3941
assert_equal "1970-02-28T00:00:00.000Z", result("P1M", initial_timestamp)
4042
end
4143
end

test/xml_security_test.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,12 @@ class XmlSecurityTest < Minitest::Test
402402

403403
describe 'with invalid document ' do
404404
describe 'when certificate is invalid' do
405-
let(:document_data) { read_response('response_with_signed_message_and_assertion.xml')
406-
.sub(/<ds:X509Certificate>.*<\/ds:X509Certificate>/, "<ds:X509Certificate>invalid<\/ds:X509Certificate>") }
407405
let(:document) { OneLogin::RubySaml::Response.new(document_data).document }
408-
let(:idp_cert) { OpenSSL::X509::Certificate.new(ruby_saml_cert_text) }
409406

410407
it 'is invalid' do
411-
refute document.validate_document_with_cert(idp_cert), 'Document should be invalid'
408+
wrong_document_data = document_data.sub(/<ds:X509Certificate>.*<\/ds:X509Certificate>/, "<ds:X509Certificate>invalid<\/ds:X509Certificate>")
409+
wrong_document = OneLogin::RubySaml::Response.new(wrong_document_data).document
410+
refute wrong_document.validate_document_with_cert(idp_cert), 'Document should be invalid'
412411
end
413412
end
414413
end

0 commit comments

Comments
 (0)