PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/InfoCard/Xml/SecurityTokenReference.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 173 lines | 76 code | 30 blank | 67 comment | 18 complexity | 32c73d25643d863bfe7c84f6b4193f6c 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: SecurityTokenReference.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * Zend_InfoCard_Xml_Element
  24. */
  25. require_once 'Zend/InfoCard/Xml/Element.php';
  26. /**
  27. * Represents a SecurityTokenReference XML block
  28. *
  29. * @category Zend
  30. * @package Zend_InfoCard
  31. * @subpackage Zend_InfoCard_Xml
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_InfoCard_Xml_SecurityTokenReference extends Zend_InfoCard_Xml_Element
  36. {
  37. /**
  38. * Base64 Binary Encoding URI
  39. */
  40. const ENCODING_BASE64BIN = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';
  41. /**
  42. * Return an instance of the object based on the input XML
  43. *
  44. * @param string $xmlData The SecurityTokenReference XML Block
  45. * @return Zend_InfoCard_Xml_SecurityTokenReference
  46. * @throws Zend_InfoCard_Xml_Exception
  47. */
  48. static public function getInstance($xmlData)
  49. {
  50. if($xmlData instanceof Zend_InfoCard_Xml_Element) {
  51. $strXmlData = $xmlData->asXML();
  52. } else if (is_string($xmlData)) {
  53. $strXmlData = $xmlData;
  54. } else {
  55. throw new Zend_InfoCard_Xml_Exception("Invalid Data provided to create instance");
  56. }
  57. $sxe = simplexml_load_string($strXmlData);
  58. if($sxe->getName() != "SecurityTokenReference") {
  59. throw new Zend_InfoCard_Xml_Exception("Invalid XML Block provided for SecurityTokenReference");
  60. }
  61. return simplexml_load_string($strXmlData, "Zend_InfoCard_Xml_SecurityTokenReference");
  62. }
  63. /**
  64. * Return the Key Identifier XML Object
  65. *
  66. * @return Zend_InfoCard_Xml_Element
  67. * @throws Zend_InfoCard_Xml_Exception
  68. */
  69. protected function _getKeyIdentifier()
  70. {
  71. $this->registerXPathNamespace('o', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
  72. list($keyident) = $this->xpath('//o:KeyIdentifier');
  73. if(!($keyident instanceof Zend_InfoCard_Xml_Element)) {
  74. throw new Zend_InfoCard_Xml_Exception("Failed to retrieve Key Identifier");
  75. }
  76. return $keyident;
  77. }
  78. /**
  79. * Return the Key URI identifying the thumbprint type used
  80. *
  81. * @return string The thumbprint type URI
  82. * @throws Zend_InfoCard_Xml_Exception
  83. */
  84. public function getKeyThumbprintType()
  85. {
  86. $keyident = $this->_getKeyIdentifier();
  87. $dom = self::convertToDOM($keyident);
  88. if(!$dom->hasAttribute('ValueType')) {
  89. throw new Zend_InfoCard_Xml_Exception("Key Identifier did not provide a type for the value");
  90. }
  91. return $dom->getAttribute('ValueType');
  92. }
  93. /**
  94. * Return the thumbprint encoding type used as a URI
  95. *
  96. * @return string the URI of the thumbprint encoding used
  97. * @throws Zend_InfoCard_Xml_Exception
  98. */
  99. public function getKeyThumbprintEncodingType()
  100. {
  101. $keyident = $this->_getKeyIdentifier();
  102. $dom = self::convertToDOM($keyident);
  103. if(!$dom->hasAttribute('EncodingType')) {
  104. throw new Zend_InfoCard_Xml_Exception("Unable to determine the encoding type for the key identifier");
  105. }
  106. return $dom->getAttribute('EncodingType');
  107. }
  108. /**
  109. * Get the key reference data used to identify the public key
  110. *
  111. * @param bool $decode if true, will return a decoded version of the key
  112. * @return string the key reference thumbprint, either in binary or encoded form
  113. * @throws Zend_InfoCard_Xml_Exception
  114. */
  115. public function getKeyReference($decode = true)
  116. {
  117. $keyIdentifier = $this->_getKeyIdentifier();
  118. $dom = self::convertToDOM($keyIdentifier);
  119. $encoded = $dom->nodeValue;
  120. if(empty($encoded)) {
  121. throw new Zend_InfoCard_Xml_Exception("Could not find the Key Reference Encoded Value");
  122. }
  123. if($decode) {
  124. $decoded = "";
  125. switch($this->getKeyThumbprintEncodingType()) {
  126. case self::ENCODING_BASE64BIN:
  127. if(version_compare(PHP_VERSION, "5.2.0", ">=")) {
  128. $decoded = base64_decode($encoded, true);
  129. } else {
  130. $decoded = base64_decode($encoded);
  131. }
  132. break;
  133. default:
  134. throw new Zend_InfoCard_Xml_Exception("Unknown Key Reference Encoding Type: {$this->getKeyThumbprintEncodingType()}");
  135. }
  136. if(!$decoded || empty($decoded)) {
  137. throw new Zend_InfoCard_Xml_Exception("Failed to decode key reference");
  138. }
  139. return $decoded;
  140. }
  141. return $encoded;
  142. }
  143. }