From 85e8dfa02046570dce8501b0ade845b300af63c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Thu, 8 Jan 2009 10:13:16 +0000
Subject: [PATCH] Adding to aggregator module support for including maxDuration
 and validUntil attributes to entitydecriptor metadata

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1092 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Metadata/SAMLBuilder.php       | 25 +++++++++++++++++--
 .../aggregator/config-template/aggregator.php |  3 +++
 modules/aggregator/www/index.php              |  4 ++-
 3 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/lib/SimpleSAML/Metadata/SAMLBuilder.php b/lib/SimpleSAML/Metadata/SAMLBuilder.php
index 116843bf1..894ac465c 100644
--- a/lib/SimpleSAML/Metadata/SAMLBuilder.php
+++ b/lib/SimpleSAML/Metadata/SAMLBuilder.php
@@ -24,20 +24,40 @@ class SimpleSAML_Metadata_SAMLBuilder {
 	private $entityDescriptor;
 
 
+	private $maxCache = NULL;
+	private $maxDuration = NULL;
+	
 	/**
 	 * Initialize the builder.
 	 *
 	 * @param string $entityId  The entity id of the entity.
 	 */
-	public function __construct($entityId) {
+	public function __construct($entityId, $maxCache = NULL, $maxDuration = NULL) {
 		assert('is_string($entityId)');
 
+		$this->maxCache = $maxCache;
+		$this->maxDuration = $maxDuration;
+
 		$this->document = new DOMDocument();
 		$this->entityDescriptor = $this->createElement('EntityDescriptor');
 		$this->entityDescriptor->setAttribute('entityID', $entityId);
+		
 		$this->document->appendChild($this->entityDescriptor);
 	}
 
+	private function setExpiration($metadata) {
+	
+		if (array_key_exists('expire', $metadata)) {
+			if ($metadata['expire'] - time() < $this->maxDuration)
+				$this->maxDuration = $metadata['expire'] - time();
+		}
+			
+		if ($this->maxCache !== NULL) 
+			$this->entityDescriptor->setAttribute('cacheDuration', $this->maxCache);
+		if ($this->maxDuration !== NULL) 
+			$this->entityDescriptor->setAttribute('validUntil', SimpleSAML_Utilities::generateTimestamp(time() + $this->maxDuration));
+	}
+
 
 	/**
 	 * Retrieve the EntityDescriptor.
@@ -175,6 +195,8 @@ class SimpleSAML_Metadata_SAMLBuilder {
 	public function addMetadata($set, $metadata) {
 		assert('is_string($set)');
 		assert('is_array($metadata)');
+		
+		$this->setExpiration($metadata);
 
 		switch ($set) {
 		case 'saml20-sp-remote':
@@ -194,7 +216,6 @@ class SimpleSAML_Metadata_SAMLBuilder {
 		}
 	}
 
-
 	/**
 	 * Add SAML 2.0 SP metadata.
 	 *
diff --git a/modules/aggregator/config-template/aggregator.php b/modules/aggregator/config-template/aggregator.php
index 772f59a2f..5405b59a1 100644
--- a/modules/aggregator/config-template/aggregator.php
+++ b/modules/aggregator/config-template/aggregator.php
@@ -12,6 +12,9 @@ $config = array(
 		),
 	),
 
+	
+	'maxCache' 		=> 60*60*24, // 24 hour cache time
+	'maxDuration' 	=> 60*60*24*5, // Maximum 5 days duration on ValidUntil.
 
 	/* Whether metadata should be signed. */
 	'sign.enable' => FALSE,
diff --git a/modules/aggregator/www/index.php b/modules/aggregator/www/index.php
index 3afe2d0ff..5cf8b605c 100644
--- a/modules/aggregator/www/index.php
+++ b/modules/aggregator/www/index.php
@@ -100,7 +100,9 @@ foreach ($entities as $entity => $sets) {
 		$tmp->loadXML(base64_decode($entityDescriptor));
 		$entityDescriptor = $tmp->documentElement;
 	} else {
-		$tmp = new SimpleSAML_Metadata_SAMLBuilder($entity);
+		$tmp = new SimpleSAML_Metadata_SAMLBuilder($entity, 
+			$aggregatorConfig->getValue('maxCache', NULL), $aggregatorConfig->getValue('maxDuration', NULL));
+			
 		foreach ($sets as $set => $metadata) {
 			$tmp->addMetadata($set, $metadata);
 		}
-- 
GitLab