PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/simplesamlphp-1.10.0/lib/SAML2/StatusResponse.php

https://bitbucket.org/sahkoinenaanestys/sahkoinenaanestys
PHP | 193 lines | 86 code | 40 blank | 67 comment | 12 complexity | 52d07ecc2633530b6f14e740ea3720a0 MD5 | raw file
  1. <?php
  2. /**
  3. * Base class for all SAML 2 response messages.
  4. *
  5. * Implements samlp:StatusResponseType. All of the elements in that type is
  6. * stored in the SAML2_Message class, and this class is therefore more
  7. * or less empty. It is included mainly to make it easy to separate requests from
  8. * responses.
  9. *
  10. * The status code is represented as an array on the following form:
  11. * array(
  12. * 'Code' => '<top-level status code>',
  13. * 'SubCode' => '<second-level status code>',
  14. * 'Message' => '<status message>',
  15. * )
  16. *
  17. * Only the 'Code' field is required. The others will be set to NULL if they
  18. * aren't present.
  19. *
  20. * @package simpleSAMLphp
  21. * @version $Id$
  22. */
  23. abstract class SAML2_StatusResponse extends SAML2_Message {
  24. /**
  25. * The ID of the request this is a response to, or NULL if this is an unsolicited response.
  26. *
  27. * @var string|NULL
  28. */
  29. private $inResponseTo;
  30. /**
  31. * The status code of the response.
  32. *
  33. * @var array
  34. */
  35. private $status;
  36. /**
  37. * Constructor for SAML 2 response messages.
  38. *
  39. * @param string $tagName The tag name of the root element.
  40. * @param DOMElement|NULL $xml The input message.
  41. */
  42. protected function __construct($tagName, DOMElement $xml = NULL) {
  43. parent::__construct($tagName, $xml);
  44. $this->status = array(
  45. 'Code' => SAML2_Const::STATUS_SUCCESS,
  46. 'SubCode' => NULL,
  47. 'Message' => NULL,
  48. );
  49. if ($xml === NULL) {
  50. return;
  51. }
  52. if ($xml->hasAttribute('InResponseTo')) {
  53. $this->inResponseTo = $xml->getAttribute('InResponseTo');
  54. }
  55. $status = SAML2_Utils::xpQuery($xml, './saml_protocol:Status');
  56. if (empty($status)) {
  57. throw new Exception('Missing status code on response.');
  58. }
  59. $status = $status[0];
  60. $statusCode = SAML2_Utils::xpQuery($status, './saml_protocol:StatusCode');
  61. if (empty($statusCode)) {
  62. throw new Exception('Missing status code in status element.');
  63. }
  64. $statusCode = $statusCode[0];
  65. $this->status['Code'] = $statusCode->getAttribute('Value');
  66. $subCode = SAML2_Utils::xpQuery($statusCode, './saml_protocol:StatusCode');
  67. if (!empty($subCode)) {
  68. $this->status['SubCode'] = $subCode[0]->getAttribute('Value');
  69. }
  70. $message = SAML2_Utils::xpQuery($status, './saml_protocol:StatusMessage');
  71. if (!empty($message)) {
  72. $this->status['Message'] = trim($message[0]->textContent);
  73. }
  74. }
  75. /**
  76. * Determine whether this is a successful response.
  77. *
  78. * @return boolean TRUE if the status code is success, FALSE if not.
  79. */
  80. public function isSuccess() {
  81. assert('array_key_exists("Code", $this->status)');
  82. if ($this->status['Code'] === SAML2_Const::STATUS_SUCCESS) {
  83. return TRUE;
  84. }
  85. return FALSE;
  86. }
  87. /**
  88. * Retrieve the ID of the request this is a response to.
  89. *
  90. * @return string|NULL The ID of the request.
  91. */
  92. public function getInResponseTo() {
  93. return $this->inResponseTo;
  94. }
  95. /**
  96. * Set the ID of the request this is a response to.
  97. *
  98. * @param string|NULL $inResponseTo The ID of the request.
  99. */
  100. public function setInResponseTo($inResponseTo) {
  101. assert('is_string($inResponseTo) || is_null($inResponseTo)');
  102. $this->inResponseTo = $inResponseTo;
  103. }
  104. /**
  105. * Retrieve the status code.
  106. *
  107. * @return array The status code.
  108. */
  109. public function getStatus() {
  110. return $this->status;
  111. }
  112. /**
  113. * Set the status code.
  114. *
  115. * @param array $status The status code.
  116. */
  117. public function setStatus(array $status) {
  118. assert('array_key_exists("Code", $status)');
  119. $this->status = $status;
  120. if (!array_key_exists('SubCode', $status)) {
  121. $this->status['SubCode'] = NULL;
  122. }
  123. if (!array_key_exists('Message', $status)) {
  124. $this->status['Message'] = NULL;
  125. }
  126. }
  127. /**
  128. * Convert status response message to an XML element.
  129. *
  130. * @return DOMElement This status response.
  131. */
  132. public function toUnsignedXML() {
  133. $root = parent::toUnsignedXML();
  134. if ($this->inResponseTo !== NULL) {
  135. $root->setAttribute('InResponseTo', $this->inResponseTo);
  136. }
  137. $status = $this->document->createElementNS(SAML2_Const::NS_SAMLP, 'Status');
  138. $root->appendChild($status);
  139. $statusCode = $this->document->createElementNS(SAML2_Const::NS_SAMLP, 'StatusCode');
  140. $statusCode->setAttribute('Value', $this->status['Code']);
  141. $status->appendChild($statusCode);
  142. if (!is_null($this->status['SubCode'])) {
  143. $subStatusCode = $this->document->createElementNS(SAML2_Const::NS_SAMLP, 'StatusCode');
  144. $subStatusCode->setAttribute('Value', $this->status['SubCode']);
  145. $statusCode->appendChild($subStatusCode);
  146. }
  147. if (!is_null($this->status['Message'])) {
  148. SAML2_Utils::addString($status, SAML2_Const::NS_SAMLP, 'StatusMessage', $this->status['Message']);
  149. }
  150. return $root;
  151. }
  152. }
  153. ?>