/library/Zend/InfoCard/XML/SecurityTokenReference.php

https://github.com/Exercise/zf2 · PHP · 175 lines · 76 code · 30 blank · 69 comment · 18 complexity · e32eed18df039957ac7640954b834f2c MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_InfoCard
  17. * @subpackage Zend_InfoCard_Xml
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\InfoCard\XML;
  26. /**
  27. * Represents a SecurityTokenReference XML block
  28. *
  29. * @uses \Zend\InfoCard\XML\AbstractElement
  30. * @uses \Zend\InfoCard\XML\Exception
  31. * @category Zend
  32. * @package Zend_InfoCard
  33. * @subpackage Zend_InfoCard_Xml
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class SecurityTokenReference extends AbstractElement
  38. {
  39. /**
  40. * Base64 Binary Encoding URI
  41. */
  42. const ENCODING_BASE64BIN = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';
  43. /**
  44. * Return an instance of the object based on the input XML
  45. *
  46. * @param string $xmlData The SecurityTokenReference XML Block
  47. * @return \Zend\InfoCard\XML\SecurityTokenReference
  48. * @throws \Zend\InfoCard\XML\Exception
  49. */
  50. static public function getInstance($xmlData)
  51. {
  52. if($xmlData instanceof AbstractElement) {
  53. $strXmlData = $xmlData->asXML();
  54. } else if (is_string($xmlData)) {
  55. $strXmlData = $xmlData;
  56. } else {
  57. throw new Exception("Invalid Data provided to create instance");
  58. }
  59. $sxe = simplexml_load_string($strXmlData);
  60. if($sxe->getName() != "SecurityTokenReference") {
  61. throw new Exception("Invalid XML Block provided for SecurityTokenReference");
  62. }
  63. return simplexml_load_string($strXmlData, 'Zend\InfoCard\XML\SecurityTokenReference');
  64. }
  65. /**
  66. * Return the Key Identifier XML Object
  67. *
  68. * @return \Zend\InfoCard\XML\AbstractElement
  69. * @throws \Zend\InfoCard\XML\Exception
  70. */
  71. protected function _getKeyIdentifier()
  72. {
  73. $this->registerXPathNamespace('o', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
  74. list($keyident) = $this->xpath('//o:KeyIdentifier');
  75. if(!($keyident instanceof AbstractElement)) {
  76. throw new Exception("Failed to retrieve Key Identifier");
  77. }
  78. return $keyident;
  79. }
  80. /**
  81. * Return the Key URI identifying the thumbprint type used
  82. *
  83. * @return string The thumbprint type URI
  84. * @throws \Zend\InfoCard\XML\Exception
  85. */
  86. public function getKeyThumbprintType()
  87. {
  88. $keyident = $this->_getKeyIdentifier();
  89. $dom = self::convertToDOM($keyident);
  90. if(!$dom->hasAttribute('ValueType')) {
  91. throw new Exception("Key Identifier did not provide a type for the value");
  92. }
  93. return $dom->getAttribute('ValueType');
  94. }
  95. /**
  96. * Return the thumbprint encoding type used as a URI
  97. *
  98. * @return string the URI of the thumbprint encoding used
  99. * @throws \Zend\InfoCard\XML\Exception
  100. */
  101. public function getKeyThumbprintEncodingType()
  102. {
  103. $keyident = $this->_getKeyIdentifier();
  104. $dom = self::convertToDOM($keyident);
  105. if(!$dom->hasAttribute('EncodingType')) {
  106. throw new Exception("Unable to determine the encoding type for the key identifier");
  107. }
  108. return $dom->getAttribute('EncodingType');
  109. }
  110. /**
  111. * Get the key reference data used to identify the public key
  112. *
  113. * @param bool $decode if true, will return a decoded version of the key
  114. * @return string the key reference thumbprint, either in binary or encoded form
  115. * @throws \Zend\InfoCard\XML\Exception
  116. */
  117. public function getKeyReference($decode = true)
  118. {
  119. $keyIdentifier = $this->_getKeyIdentifier();
  120. $dom = self::convertToDOM($keyIdentifier);
  121. $encoded = $dom->nodeValue;
  122. if(empty($encoded)) {
  123. throw new Exception("Could not find the Key Reference Encoded Value");
  124. }
  125. if($decode) {
  126. $decoded = "";
  127. switch($this->getKeyThumbprintEncodingType()) {
  128. case self::ENCODING_BASE64BIN:
  129. if(version_compare(PHP_VERSION, "5.2.0", ">=")) {
  130. $decoded = base64_decode($encoded, true);
  131. } else {
  132. $decoded = base64_decode($encoded);
  133. }
  134. break;
  135. default:
  136. throw new Exception("Unknown Key Reference Encoding Type: {$this->getKeyThumbprintEncodingType()}");
  137. }
  138. if(!$decoded || empty($decoded)) {
  139. throw new Exception("Failed to decode key reference");
  140. }
  141. return $decoded;
  142. }
  143. return $encoded;
  144. }
  145. }