From 5be4705f1d0c6765ffe42e96dc4ec572a4bb9fa3 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Fri, 12 Feb 2010 08:35:08 +0000
Subject: [PATCH] SAMLBuilder: Add AttributeConsumingService to SAML 1.1
 metadata.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2174 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Metadata/SAMLBuilder.php | 89 +++++++++++++++----------
 1 file changed, 53 insertions(+), 36 deletions(-)

diff --git a/lib/SimpleSAML/Metadata/SAMLBuilder.php b/lib/SimpleSAML/Metadata/SAMLBuilder.php
index 598c2eb51..3a832d507 100644
--- a/lib/SimpleSAML/Metadata/SAMLBuilder.php
+++ b/lib/SimpleSAML/Metadata/SAMLBuilder.php
@@ -261,6 +261,56 @@ class SimpleSAML_Metadata_SAMLBuilder {
 	}
 
 
+	/**
+	 * Add an AttributeConsumingService element to the metadata.
+	 *
+	 * @param DOMElement $spDesc  The SPSSODescriptor element.
+	 * @param SimpleSAML_Configuration $metadata  The metadata.
+	 */
+	private function addAttributeConsumingService(DOMElement $spDesc, SimpleSAML_Configuration $metadata) {
+		$attributes = $metadata->getArray('attributes', array());
+		$name = $metadata->getLocalizedString('name', NULL);
+
+		if ($name === NULL || count($attributes) == 0) {
+			/* We cannot add an AttributeConsumingService without name and attributes. */
+			return;
+		}
+
+		/*
+		 * Add an AttributeConsumingService element with information as name and description and list
+		 * of requested attributes
+		 */
+		$attributeconsumer = $this->createElement('AttributeConsumingService');
+		$attributeconsumer->setAttribute('index', '0');
+
+		foreach($name AS $lang => $localname) {
+			$t = $this->createTextElement('ServiceName', $localname);
+			$t->setAttribute('xml:lang', $lang);
+			$attributeconsumer->appendChild($t);
+		}
+
+		$description = $metadata->getLocalizedString('description', array());
+		foreach ($description as $lang => $localname) {
+			$t = $this->createTextElement('ServiceDescription', $localname);
+			$t->setAttribute('xml:lang', $lang);
+			$attributeconsumer->appendChild($t);
+		}
+
+		$nameFormat = $metadata->getString('attributes.NameFormat', SAML2_Const::NAMEFORMAT_UNSPECIFIED);
+
+		foreach ($attributes as $attribute) {
+			$t = $this->createElement('RequestedAttribute');
+			$t->setAttribute('Name', $attribute);
+			if ($nameFormat !== SAML2_Const::NAMEFORMAT_UNSPECIFIED) {
+				$t->setAttribute('NameFormat', $nameFormat);
+			}
+			$attributeconsumer->appendChild($t);
+		}
+
+		$spDesc->appendChild($attributeconsumer);
+	}
+
+
 	/**
 	 * Add metadata set for entity.
 	 *
@@ -334,42 +384,7 @@ class SimpleSAML_Metadata_SAMLBuilder {
 		}
 		$this->addEndpoints($e, 'AssertionConsumerService', $endpoints);
 
-		$attributes = $metadata->getArray('attributes', array());
-		if ( $metadata->hasValue('name') && count($attributes) > 0 ) {
-			/*
-			 * Add an AttributeConsumingService element with information as name and description and list
-			 * of requested attributes
-			 */
-			$attributeconsumer = $this->createElement('AttributeConsumingService');
-			$attributeconsumer->setAttribute('index', '0');
-
-			$name = $metadata->getLocalizedString('name');
-			foreach($name AS $lang => $localname) {
-				$t = $this->createTextElement('ServiceName', $localname);
-				$t->setAttribute('xml:lang', $lang);
-				$attributeconsumer->appendChild($t);
-			}
-
-			$description = $metadata->getLocalizedString('description', array());
-			foreach ($description as $lang => $localname) {
-				$t = $this->createTextElement('ServiceDescription', $localname);
-				$t->setAttribute('xml:lang', $lang);
-				$attributeconsumer->appendChild($t);
-			}
-
-			$nameFormat = $metadata->getString('attributes.NameFormat', SAML2_Const::NAMEFORMAT_UNSPECIFIED);
-
-			foreach ($attributes as $attribute) {
-				$t = $this->createElement('RequestedAttribute');
-				$t->setAttribute('Name', $attribute);
-				if ($nameFormat !== SAML2_Const::NAMEFORMAT_UNSPECIFIED) {
-					$t->setAttribute('NameFormat', $nameFormat);
-				}
-				$attributeconsumer->appendChild($t);
-			}
-
-			$e->appendChild($attributeconsumer);
-		}
+		$this->addAttributeConsumingService($e, $metadata);
 
 		$this->entityDescriptor->appendChild($e);
 
@@ -462,6 +477,8 @@ class SimpleSAML_Metadata_SAMLBuilder {
 		}
 		$this->addEndpoints($e, 'AssertionConsumerService', $endpoints);
 
+		$this->addAttributeConsumingService($e, $metadata);
+
 		$this->entityDescriptor->appendChild($e);
 	}
 
-- 
GitLab