From 5bb22d7734bac4e9711bca4d9f356b82585d1c52 Mon Sep 17 00:00:00 2001 From: Olav Morken <olav.morken@uninett.no> Date: Fri, 5 Mar 2010 15:12:21 +0000 Subject: [PATCH] SAML2_Utils: Add the new addString()-function. git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2200 44740490-163a-0410-bde0-09ae8108e29a --- lib/SAML2/Assertion.php | 9 ++------- lib/SAML2/AttributeQuery.php | 4 +--- lib/SAML2/AuthnRequest.php | 4 +--- lib/SAML2/LogoutRequest.php | 4 +--- lib/SAML2/Message.php | 4 +--- lib/SAML2/StatusResponse.php | 4 +--- lib/SAML2/Utils.php | 29 +++++++++++++++++++++++++---- 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/lib/SAML2/Assertion.php b/lib/SAML2/Assertion.php index 6bbe5621f..4144964d3 100644 --- a/lib/SAML2/Assertion.php +++ b/lib/SAML2/Assertion.php @@ -995,9 +995,7 @@ class SAML2_Assertion implements SAML2_SignedElement { $root->setAttribute('Version', '2.0'); $root->setAttribute('IssueInstant', gmdate('Y-m-d\TH:i:s\Z', $this->issueInstant)); - $issuer = $document->createElementNS(SAML2_Const::NS_SAML, 'saml:Issuer'); - $issuer->appendChild($document->createTextNode($this->issuer)); - $root->appendChild($issuer); + $issuer = SAML2_Utils::addString($root, SAML2_Const::NS_SAML, 'saml:Issuer', $this->issuer); $this->addSubject($root); $this->addConditions($root); @@ -1111,10 +1109,7 @@ class SAML2_Assertion implements SAML2_SignedElement { $ac = $document->createElementNS(SAML2_Const::NS_SAML, 'saml:AuthnContext'); $as->appendChild($ac); - $accr = $document->createElementNS(SAML2_Const::NS_SAML, 'saml:AuthnContextClassRef'); - $ac->appendChild($accr); - - $accr->appendChild($document->createTextNode($this->authnContext)); + SAML2_Utils::addString($ac, SAML2_Const::NS_SAML, 'saml:AuthnContextClassRef', $this->authnContext); } diff --git a/lib/SAML2/AttributeQuery.php b/lib/SAML2/AttributeQuery.php index 85e617455..09b70b784 100644 --- a/lib/SAML2/AttributeQuery.php +++ b/lib/SAML2/AttributeQuery.php @@ -162,12 +162,10 @@ class SAML2_AttributeQuery extends SAML2_SubjectQuery { $type = NULL; } - $attributeValue = $root->ownerDocument->createElementNS(SAML2_Const::NS_SAML, 'saml:AttributeValue'); - $attribute->appendChild($attributeValue); + $attributeValue = SAML2_Utils::addString($attribute, SAML2_Const::NS_SAML, 'saml:AttributeValue', $value); if ($type !== NULL) { $attributeValue->setAttributeNS(SAML2_Const::NS_XSI, 'xsi:type', $type); } - $attributeValue->appendChild($root->ownerDocument->createTextNode($value)); } } diff --git a/lib/SAML2/AuthnRequest.php b/lib/SAML2/AuthnRequest.php index 094a0e4bd..10c7defe3 100644 --- a/lib/SAML2/AuthnRequest.php +++ b/lib/SAML2/AuthnRequest.php @@ -348,9 +348,7 @@ class SAML2_AuthnRequest extends SAML2_Request { $e->setAttribute('Comparison', $rac['Comparison']); } foreach ($rac['AuthnContextClassRef'] as $accr) { - $i = $this->document->createElementNS(SAML2_Const::NS_SAML, 'AuthnContextClassRef'); - $i->appendChild($this->document->createTextNode($accr)); - $e->appendChild($i); + SAML2_Utils::addString($e, SAML2_Const::NS_SAML, 'AuthnContextClassRef', $accr); } } diff --git a/lib/SAML2/LogoutRequest.php b/lib/SAML2/LogoutRequest.php index 7cfc7204b..c7a8a9c6e 100644 --- a/lib/SAML2/LogoutRequest.php +++ b/lib/SAML2/LogoutRequest.php @@ -109,9 +109,7 @@ class SAML2_LogoutRequest extends SAML2_Request { SAML2_Utils::addNameId($root, $this->nameId); if ($this->sessionIndex !== NULL) { - $sessionIndex = $this->document->createElementNS(SAML2_Const::NS_SAMLP, 'SessionIndex'); - $sessionIndex->appendChild($this->document->createTextNode($this->sessionIndex)); - $root->appendChild($sessionIndex); + SAML2_Utils::addString($root, SAML2_Const::NS_SAMLP, 'SessionIndex', $this->sessionIndex); } return $root; diff --git a/lib/SAML2/Message.php b/lib/SAML2/Message.php index f17e89826..c2d12a8ea 100644 --- a/lib/SAML2/Message.php +++ b/lib/SAML2/Message.php @@ -357,9 +357,7 @@ abstract class SAML2_Message implements SAML2_SignedElement { } if ($this->issuer !== NULL) { - $issuer = $this->document->createElementNS(SAML2_Const::NS_SAML, 'saml:Issuer'); - $issuer->appendChild($this->document->createTextNode($this->issuer)); - $root->appendChild($issuer); + SAML2_Utils::addString($root, SAML2_Const::NS_SAML, 'saml:Issuer', $this->issuer); } return $root; diff --git a/lib/SAML2/StatusResponse.php b/lib/SAML2/StatusResponse.php index 6b9f7c760..bc264fbf0 100644 --- a/lib/SAML2/StatusResponse.php +++ b/lib/SAML2/StatusResponse.php @@ -181,9 +181,7 @@ abstract class SAML2_StatusResponse extends SAML2_Message { } if (!is_null($this->status['Message'])) { - $statusMessage = $this->document->createElementNS(SAML2_Const::NS_SAMLP, 'StatusMessage'); - $statusMessage->appendChild($this->document->createTextNode($this->status['Message'])); - $status->appendChild($statusMessage); + SAML2_Utils::addString($status, SAML2_Const::NS_SAMLP, 'StatusMessage', $this->status['Message']); } return $root; diff --git a/lib/SAML2/Utils.php b/lib/SAML2/Utils.php index 8cf84426a..42b5c5544 100644 --- a/lib/SAML2/Utils.php +++ b/lib/SAML2/Utils.php @@ -175,8 +175,7 @@ class SAML2_Utils { public static function addNameId(DOMElement $node, array $nameId) { assert('array_key_exists("Value", $nameId)'); - $xml = $node->ownerDocument->createElementNS(SAML2_Const::NS_SAML, 'saml:NameID'); - $node->appendChild($xml); + $xml = SAML2_Utils::addString($node, SAML2_Const::NS_SAML, 'saml:NameID', $nameId['Value']); if (array_key_exists('NameQualifier', $nameId) && $nameId['NameQualifier'] !== NULL) { $xml->setAttribute('NameQualifier', $nameId['NameQualifier']); @@ -187,8 +186,6 @@ class SAML2_Utils { if (array_key_exists('Format', $nameId) && $nameId['Format'] !== NULL) { $xml->setAttribute('Format', $nameId['Format']); } - - $xml->appendChild($node->ownerDocument->createTextNode($nameId['Value'])); } @@ -367,4 +364,28 @@ class SAML2_Utils { return $ret; } + + /** + * Append string element. + * + * @param DOMElement $parent The parent element we should append the new nodes to. + * @param string $namespace The namespace of the created element. + * @param string $name The name of the created element. + * @param string $value The value of the element. + * @return DOMElement The generated element. + */ + public static function addString(DOMElement $parent, $namespace, $name, $value) { + assert('is_string($namespace)'); + assert('is_string($name)'); + assert('is_string($value)'); + + $doc = $parent->ownerDocument; + + $n = $doc->createElementNS($namespace, $name); + $n->appendChild($doc->createTextNode($value)); + $parent->appendChild($n); + + return $n; + } + } -- GitLab