ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
StatusResponse.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SAML2;
4 
25 abstract class StatusResponse extends Message
26 {
32  private $inResponseTo;
33 
34 
40  private $status;
41 
42 
50  protected function __construct($tagName, \DOMElement $xml = null)
51  {
52  parent::__construct($tagName, $xml);
53 
54  $this->status = array(
55  'Code' => Constants::STATUS_SUCCESS,
56  'SubCode' => null,
57  'Message' => null,
58  );
59 
60  if ($xml === null) {
61  return;
62  }
63 
64  if ($xml->hasAttribute('InResponseTo')) {
65  $this->inResponseTo = $xml->getAttribute('InResponseTo');
66  }
67 
68  $status = Utils::xpQuery($xml, './saml_protocol:Status');
69  if (empty($status)) {
70  throw new \Exception('Missing status code on response.');
71  }
72  $status = $status[0];
73 
74  $statusCode = Utils::xpQuery($status, './saml_protocol:StatusCode');
75  if (empty($statusCode)) {
76  throw new \Exception('Missing status code in status element.');
77  }
78  $statusCode = $statusCode[0];
79 
80  $this->status['Code'] = $statusCode->getAttribute('Value');
81 
82  $subCode = Utils::xpQuery($statusCode, './saml_protocol:StatusCode');
83  if (!empty($subCode)) {
84  $this->status['SubCode'] = $subCode[0]->getAttribute('Value');
85  }
86 
87  $message = Utils::xpQuery($status, './saml_protocol:StatusMessage');
88  if (!empty($message)) {
89  $this->status['Message'] = trim($message[0]->textContent);
90  }
91  }
92 
93 
99  public function isSuccess()
100  {
101  assert(array_key_exists("Code", $this->status));
102 
103  if ($this->status['Code'] === Constants::STATUS_SUCCESS) {
104  return true;
105  }
106 
107  return false;
108  }
109 
110 
116  public function getInResponseTo()
117  {
118  return $this->inResponseTo;
119  }
120 
121 
127  public function setInResponseTo($inResponseTo)
128  {
129  assert(is_string($inResponseTo) || is_null($inResponseTo));
130 
131  $this->inResponseTo = $inResponseTo;
132  }
133 
134 
140  public function getStatus()
141  {
142  return $this->status;
143  }
144 
145 
151  public function setStatus(array $status)
152  {
153  assert(array_key_exists("Code", $status));
154 
155  $this->status = $status;
156  if (!array_key_exists('SubCode', $status)) {
157  $this->status['SubCode'] = null;
158  }
159  if (!array_key_exists('Message', $status)) {
160  $this->status['Message'] = null;
161  }
162  }
163 
164 
170  public function toUnsignedXML()
171  {
172  $root = parent::toUnsignedXML();
173 
174  if ($this->inResponseTo !== null) {
175  $root->setAttribute('InResponseTo', $this->inResponseTo);
176  }
177 
178  $status = $this->document->createElementNS(Constants::NS_SAMLP, 'Status');
179  $root->appendChild($status);
180 
181  $statusCode = $this->document->createElementNS(Constants::NS_SAMLP, 'StatusCode');
182  $statusCode->setAttribute('Value', $this->status['Code']);
183  $status->appendChild($statusCode);
184 
185  if (!is_null($this->status['SubCode'])) {
186  $subStatusCode = $this->document->createElementNS(Constants::NS_SAMLP, 'StatusCode');
187  $subStatusCode->setAttribute('Value', $this->status['SubCode']);
188  $statusCode->appendChild($subStatusCode);
189  }
190 
191  if (!is_null($this->status['Message'])) {
192  Utils::addString($status, Constants::NS_SAMLP, 'StatusMessage', $this->status['Message']);
193  }
194 
195  return $root;
196  }
197 }
setStatus(array $status)
Set the status code.
toUnsignedXML()
Convert status response message to an XML element.
getStatus()
Retrieve the status code.
Base class for all SAML 2 messages.
Definition: Message.php:18
$xml
Definition: metadata.php:240
catch(Exception $e) $message
isSuccess()
Determine whether this is a successful response.
setInResponseTo($inResponseTo)
Set the ID of the request this is a response to.
Create styles array
The data for the language used.
__construct($tagName, \DOMElement $xml=null)
Constructor for SAML 2 response messages.
getInResponseTo()
Retrieve the ID of the request this is a response to.