From b0f7d6416bdf52291798290413a2b903cdac08c4 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Mon, 20 Sep 2010 08:39:23 +0000
Subject: [PATCH] SAML: Add XML classes for NameID, SubjectConfirmation and
 SubjectConfirmationData.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2551 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SAML2/XML/saml/NameID.php                 | 125 ++++++++++++++++++
 lib/SAML2/XML/saml/SubjectConfirmation.php    |  93 +++++++++++++
 .../XML/saml/SubjectConfirmationData.php      | 115 ++++++++++++++++
 3 files changed, 333 insertions(+)
 create mode 100644 lib/SAML2/XML/saml/NameID.php
 create mode 100644 lib/SAML2/XML/saml/SubjectConfirmation.php
 create mode 100644 lib/SAML2/XML/saml/SubjectConfirmationData.php

diff --git a/lib/SAML2/XML/saml/NameID.php b/lib/SAML2/XML/saml/NameID.php
new file mode 100644
index 000000000..b31a00c91
--- /dev/null
+++ b/lib/SAML2/XML/saml/NameID.php
@@ -0,0 +1,125 @@
+<?php
+
+/**
+ * Class representing the saml:NameID element.
+ *
+ * @package simpleSAMLphp
+ * @version $Id$
+ */
+class SAML2_XML_saml_NameID {
+
+	/**
+	 * The NameQualifier or the NameID.
+	 *
+	 * @var string|NULL
+	 */
+	public $NameQualifier;
+
+	/**
+	 * The SPNameQualifier or the NameID.
+	 *
+	 * @var string|NULL
+	 */
+	public $SPNameQualifier;
+
+
+	/**
+	 * The Format or the NameID.
+	 *
+	 * @var string|NULL
+	 */
+	public $Format;
+
+
+	/**
+	 * The SPProvidedID or the NameID.
+	 *
+	 * @var string|NULL
+	 */
+	public $SPProvidedID;
+
+
+	/**
+	 * The value of this NameID.
+	 *
+	 * @var string
+	 */
+	public $value;
+
+
+	/**
+	 * Initialize a saml:NameID.
+	 *
+	 * @param DOMElement|NULL $xml  The XML element we should load.
+	 */
+	public function __construct(DOMElement $xml = NULL) {
+
+		if ($xml === NULL) {
+			return;
+		}
+
+		if ($xml->hasAttribute('SPNameQualifier')) {
+			$this->SPNameQualifier = $xml->getAttribute('SPNameQualifier');
+		}
+
+		if ($xml->hasAttribute('NameQualifier')) {
+			$this->NameQualifier = $xml->getAttribute('NameQualifier');
+		}
+
+		if ($xml->hasAttribute('Format')) {
+			$this->Format = $xml->getAttribute('Format');
+		}
+
+		if ($xml->hasAttribute('SPProvidedID')) {
+			$this->SPProvidedID = $xml->getAttribute('SPProvidedID');
+		}
+
+		$this->value = trim($xml->textContent);
+	}
+
+
+	/**
+	 * Convert this NameID to XML.
+	 *
+	 * @param DOMElement|NULL $parent  The element we should append to.
+	 * @return DOMElement  This AdditionalMetadataLocation-element.
+	 */
+	public function toXML(DOMElement $parent = NULL) {
+		assert('is_string($this->NameQualifier) || is_null($this->NameQualifier)');
+		assert('is_string($this->SPNameQualifier) || is_null($this->SPNameQualifier)');
+		assert('is_string($this->Format) || is_null($this->Format)');
+		assert('is_string($this->SPProvidedID) || is_null($this->SPProvidedID)');
+		assert('is_string($this->value)');
+
+		if ($parent === NULL) {
+			$parent = new DOMDocument();
+			$doc = $parent;
+		} else {
+			$doc = $parent->ownerDocument;
+		}
+		$e = $doc->createElementNS(SAML2_Const::NS_SAML, 'saml:NameID');
+		$parent->appendChild($e);
+
+		if ($this->NameQualifier !== NULL) {
+			$e->setAttribute('NameQualifier', $this->NameQualifier);
+		}
+
+		if ($this->SPNameQualifier !== NULL) {
+			$e->setAttribute('SPNameQualifier', $this->SPNameQualifier);
+		}
+
+		if ($this->Format !== NULL) {
+			$e->setAttribute('Format', $this->Format);
+		}
+
+		if ($this->SPProvidedID !== NULL) {
+			$e->setAttribute('SPProvidedID', $this->SPProvidedID);
+		}
+
+		$t = $doc->createTextNode($this->value);
+		$e->appendChild($t);
+
+		return $e;
+	}
+
+}
diff --git a/lib/SAML2/XML/saml/SubjectConfirmation.php b/lib/SAML2/XML/saml/SubjectConfirmation.php
new file mode 100644
index 000000000..857ee4b44
--- /dev/null
+++ b/lib/SAML2/XML/saml/SubjectConfirmation.php
@@ -0,0 +1,93 @@
+<?php
+
+/**
+ * Class representing SAML 2 SubjectConfirmation element.
+ *
+ * @package simpleSAMLphp
+ * @version $Id$
+ */
+class SAML2_XML_saml_SubjectConfirmation {
+
+	/**
+	 * The method we can use to verify this Subject.
+	 *
+	 * @var string
+	 */
+	public $Method;
+
+
+	/**
+	 * The NameID of the entity that can use this element to verify the Subject.
+	 *
+	 * @var SAML2_XML_saml_NameID|NULL
+	 */
+	public $NameID;
+
+
+	/**
+	 * SubjectConfirmationData element with extra data for verification of the Subject.
+	 *
+	 * @var SAML2_XML_saml_SubjectConfirmationData|NULL
+	 */
+	public $SubjectConfirmationData;
+
+
+	/**
+	 * Initialize (and parse? a SubjectConfirmation element.
+	 *
+	 * @param DOMElement|NULL $xml  The XML element we should load.
+	 */
+	public function __construct(DOMElement $xml = NULL) {
+
+		if ($xml === NULL) {
+			return;
+		}
+
+		if (!$xml->hasAttribute('Method')) {
+			throw new Exception('SubjectConfirmation element without Method attribute.');
+		}
+		$this->Method = $xml->getAttribute('Method');
+
+		$nid = SAML2_Utils::xpQuery($xml, './saml_assertion:NameID');
+		if (count($nid) > 1) {
+			throw new Exception('More than one NameID in a SubjectConfirmation element.');
+		} elseif (!empty($nid)) {
+			$this->NameID = new SAML2_XML_saml_NameID($nid[0]);
+		}
+
+		$scd = SAML2_Utils::xpQuery($xml, './saml_assertion:SubjectConfirmationData');
+		if (count($scd) > 1) {
+			throw new Exception('More than one SubjectConfirmationData child in a SubjectConfirmation element.');
+		} elseif (!empty($scd)) {
+			$this->SubjectConfirmationData = new SAML2_XML_saml_SubjectConfirmationData($scd[0]);
+		}
+	}
+
+
+	/**
+	 * Convert this element to XML.
+	 *
+	 * @param DOMElement $parent  The parent element we should append this element to.
+	 * @return DOMElement  This element, as XML.
+	 */
+	public function toXML(DOMElement $parent) {
+		assert('is_string($this->Method)');
+		assert('is_null($this->NameID) || $this->NameID instanceof SAML2_XML_saml_NameID');
+		assert('is_null($this->SubjectConfirmationData) || $this->SubjectConfirmationData instanceof SAML2_XML_saml_SubjectConfirmationData');
+
+		$e = $parent->ownerDocument->createElementNS(SAML2_Const::NS_SAML, 'saml:SubjectConfirmation');
+		$parent->appendChild($e);
+
+		$e->setAttribute('Method', $this->Method);
+
+		if (isset($this->NameID)) {
+			$this->NameID->toXML($e);
+		}
+		if (isset($this->SubjectConfirmationData)) {
+			$this->SubjectConfirmationData->toXML($e);
+		}
+
+		return $e;
+	}
+
+}
diff --git a/lib/SAML2/XML/saml/SubjectConfirmationData.php b/lib/SAML2/XML/saml/SubjectConfirmationData.php
new file mode 100644
index 000000000..74fac0a2c
--- /dev/null
+++ b/lib/SAML2/XML/saml/SubjectConfirmationData.php
@@ -0,0 +1,115 @@
+<?php
+
+/**
+ * Class representing SAML 2 SubjectConfirmationData element.
+ *
+ * @package simpleSAMLphp
+ * @version $Id$
+ */
+class SAML2_XML_saml_SubjectConfirmationData {
+
+	/**
+	 * The time before this element is valid, as an unix timestamp.
+	 *
+	 * @var int|NULL
+	 */
+	public $NotBefore;
+
+
+	/**
+	 * The time after which this element is invalid, as an unix timestamp.
+	 *
+	 * @var int|NULL
+	 */
+	public $NotOnOrAfter;
+
+
+	/**
+	 * The Recipient this Subject is valid for. Either an entity or a location.
+	 *
+	 * @var string|NULL
+	 */
+	public $Recipient;
+
+
+	/**
+	 * The ID of the AuthnRequest this is a response to.
+	 *
+	 * @var string|NULL
+	 */
+	public $InResponseTo;
+
+
+	/**
+	 * The IP(v6) address of the user.
+	 *
+	 * @var string|NULL
+	 */
+	public $Address;
+
+
+	/**
+	 * Initialize (and parse? a SubjectConfirmationData element.
+	 *
+	 * @param DOMElement|NULL $xml  The XML element we should load.
+	 */
+	public function __construct(DOMElement $xml = NULL) {
+
+		if ($xml === NULL) {
+			return;
+		}
+
+		if ($xml->hasAttribute('NotBefore')) {
+			$this->NotBefore = SimpleSAML_Utilities::parseSAML2Time($xml->getAttribute('NotBefore'));
+		}
+		if ($xml->hasAttribute('NotOnOrAfter')) {
+			$this->NotOnOrAfter = SimpleSAML_Utilities::parseSAML2Time($xml->getAttribute('NotOnOrAfter'));
+		}
+		if ($xml->hasAttribute('Recipient')) {
+			$this->Recipient = $xml->getAttribute('Recipient');
+		}
+		if ($xml->hasAttribute('InResponseTo')) {
+			$this->InResponseTo = $xml->getAttribute('InResponseTo');
+		}
+		if ($xml->hasAttribute('Address')) {
+			$this->Address = $xml->getAttribute('Address');
+		}
+	}
+
+
+	/**
+	 * Convert this element to XML.
+	 *
+	 * @param DOMElement $parent  The parent element we should append this element to.
+	 * @return DOMElement  This element, as XML.
+	 */
+	public function toXML(DOMElement $parent) {
+		assert('is_null($this->NotBefore) || is_int($this->NotBefore)');
+		assert('is_null($this->NotOnOrAfter) || is_int($this->NotOnOrAfter)');
+		assert('is_null($this->Recipient) || is_string($this->Recipient)');
+		assert('is_null($this->InResponseTo) || is_string($this->InResponseTo)');
+		assert('is_null($this->Address) || is_string($this->Address)');
+
+		$e = $parent->ownerDocument->createElementNS(SAML2_Const::NS_SAML, 'saml:SubjectConfirmationData');
+		$parent->appendChild($e);
+
+		if (isset($this->NotBefore)) {
+			$e->setAttribute('NotBefore', gmdate('Y-m-d\TH:i:s\Z', $this->NotBefore));
+		}
+		if (isset($this->NotOnOrAfter)) {
+			$e->setAttribute('NotOnOrAfter', gmdate('Y-m-d\TH:i:s\Z', $this->NotOnOrAfter));
+		}
+		if (isset($this->Recipient)) {
+			$e->setAttribute('Recipient', $this->Recipient);
+		}
+		if (isset($this->InResponseTo)) {
+			$e->setAttribute('InResponseTo', $this->InResponseTo);
+		}
+		if (isset($this->Address)) {
+			$e->setAttribute('Address', $this->Address);
+		}
+
+		return $e;
+	}
+
+}
-- 
GitLab