Skip to content

Commit 2f0d6d3

Browse files
committed
Allow RubySaml::Utils.is_cert_expired and is_cert_active to accept an optional time argument
1 parent 2caea57 commit 2f0d6d3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/ruby_saml/utils.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Utils
3838
# @param now [Time|Integer] The time to compare.
3939
# @return [true|false] Whether the certificate is expired.
4040
def is_cert_expired(cert, now = Time.now)
41+
now = Time.at(now) if now.is_a?(Integer)
4142
cert = build_cert_object(cert) if cert.is_a?(String)
4243
cert.not_after < now
4344
end
@@ -48,6 +49,7 @@ def is_cert_expired(cert, now = Time.now)
4849
# @param now [Time|Integer] The time to compare.
4950
# @return [true|false] Whether the certificate is currently active.
5051
def is_cert_active(cert, now = Time.now)
52+
now = Time.at(now) if now.is_a?(Integer)
5153
cert = build_cert_object(cert) if cert.is_a?(String)
5254
cert.not_before <= now && cert.not_after >= now
5355
end

test/utils_test.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def result(duration, reference = 0)
441441
end
442442
end
443443

444-
describe 'time argument specified' do
444+
describe 'time argument specified as Time' do
445445
let(:now) { Time.at(10000) }
446446

447447
it 'returns true for expired certificate' do
@@ -474,6 +474,20 @@ def result(duration, reference = 0)
474474
refute RubySaml::Utils.is_cert_expired(valid_cert_string, now)
475475
end
476476
end
477+
478+
describe 'time argument specified as Integer' do
479+
let(:int) { 10000 }
480+
481+
it 'returns true for expired certificate' do
482+
expired_cert = CertificateHelper.generate_cert(not_after: Time.at(int) - 60)
483+
assert RubySaml::Utils.is_cert_expired(expired_cert, int)
484+
end
485+
486+
it 'returns false for not-started certificate' do
487+
not_started_cert = CertificateHelper.generate_cert(not_before: Time.at(int) + 60)
488+
refute RubySaml::Utils.is_cert_active(not_started_cert, int)
489+
end
490+
end
477491
end
478492

479493
describe '.is_cert_active' do
@@ -510,7 +524,7 @@ def result(duration, reference = 0)
510524
end
511525
end
512526

513-
describe 'time argument specified' do
527+
describe 'time argument specified as Time' do
514528
let(:now) { Time.at(10000) }
515529

516530
it 'returns true for active certificate' do
@@ -543,5 +557,19 @@ def result(duration, reference = 0)
543557
refute RubySaml::Utils.is_cert_active(expired_cert_string, now)
544558
end
545559
end
560+
561+
describe 'time argument specified as Integer' do
562+
let(:int) { 10000 }
563+
564+
it 'returns true for active certificate' do
565+
valid_cert = CertificateHelper.generate_cert(not_before: Time.at(int) - 60, not_after: Time.at(int) + 60)
566+
assert RubySaml::Utils.is_cert_active(valid_cert, int)
567+
end
568+
569+
it 'returns false for expired certificate' do
570+
expired_cert = CertificateHelper.generate_cert(not_after: Time.at(int) - 60)
571+
refute RubySaml::Utils.is_cert_active(expired_cert, int)
572+
end
573+
end
546574
end
547575
end

0 commit comments

Comments
 (0)