Skip to content

Commit 92d6caf

Browse files
committed
See #563 Add ValidUntil and cacheDuration support on Metadata generate method
1 parent 4fe698c commit 92d6caf

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,14 @@ class SamlController < ApplicationController
765765
end
766766
```
767767
768+
You can add ValidUntil and CacheDuration to the XML Metadata using instead
769+
```ruby
770+
# Valid until => 2 days from now
771+
# Cache duration = 604800s = 1 week
772+
valid_until = Time.now + 172800
773+
cache_duration = 604800
774+
meta.generate(settings, false, valid_until, cache_duration)
775+
```
768776
769777
## Clock Drift
770778

lib/onelogin/ruby-saml/metadata.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ class Metadata
1515
# @param settings [OneLogin::RubySaml::Settings|nil] Toolkit settings
1616
# @param pretty_print [Boolean] Pretty print or not the response
1717
# (No pretty print if you gonna validate the signature)
18+
# @param valid_until [DateTime] Metadata's valid time
19+
# @param cache_duration [Integer] Duration of the cache in seconds
1820
# @return [String] XML Metadata of the Service Provider
1921
#
20-
def generate(settings, pretty_print=false)
22+
def generate(settings, pretty_print=false, valid_until=nil, cache_duration=nil)
2123
meta_doc = XMLSecurity::Document.new
2224
namespaces = {
2325
"xmlns:md" => "urn:oasis:names:tc:SAML:2.0:metadata"
@@ -60,6 +62,12 @@ def generate(settings, pretty_print=false)
6062
if settings.sp_entity_id
6163
root.attributes["entityID"] = settings.sp_entity_id
6264
end
65+
if valid_until
66+
root.attributes["validUntil"] = valid_until.strftime('%Y-%m-%dT%H:%M:%S%z')
67+
end
68+
if cache_duration
69+
root.attributes["cacheDuration"] = "PT" + cache_duration.to_s + "S"
70+
end
6371
if settings.single_logout_service_url
6472
sp_sso.add_element "md:SingleLogoutService", {
6573
"Binding" => settings.single_logout_service_binding,

test/metadata_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ class MetadataTest < Minitest::Test
7575
assert validate_xml!(xml_text, "saml-schema-metadata-2.0.xsd")
7676
end
7777

78+
it "generates Service Provider Metadata with ValidUntil and CacheDuration" do
79+
valid_until = Time.now + 172800
80+
cache_duration = 604800
81+
xml_metadata = OneLogin::RubySaml::Metadata.new.generate(settings, false, valid_until, cache_duration)
82+
start = "<?xml version='1.0' encoding='UTF-8'?><md:EntityDescriptor"
83+
assert_equal xml_metadata[0..start.length-1],start
84+
85+
doc_metadata = REXML::Document.new(xml_metadata)
86+
assert_equal valid_until.strftime('%Y-%m-%dT%H:%M:%S%z'), REXML::XPath.first(doc_metadata, "//md:EntityDescriptor").attribute("validUntil").value
87+
assert_equal "PT604800S", REXML::XPath.first(doc_metadata, "//md:EntityDescriptor").attribute("cacheDuration").value
88+
end
89+
7890
describe "WantAssertionsSigned" do
7991
it "generates Service Provider Metadata with WantAssertionsSigned = false" do
8092
settings.security[:want_assertions_signed] = false

0 commit comments

Comments
 (0)