web/lib/Zend/InfoCard/Xml/SecurityTokenReference.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: SecurityTokenReference.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * Zend_InfoCard_Xml_Element
       
    25  */
       
    26 require_once 'Zend/InfoCard/Xml/Element.php';
       
    27 
       
    28 /**
       
    29  * Represents a SecurityTokenReference XML block
       
    30  *
       
    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 Zend_InfoCard_Xml_SecurityTokenReference extends Zend_InfoCard_Xml_Element
       
    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     /**
       
    45      * Return an instance of the object based on the input XML
       
    46      *
       
    47      * @param string $xmlData The SecurityTokenReference XML Block
       
    48      * @return Zend_InfoCard_Xml_SecurityTokenReference
       
    49      * @throws Zend_InfoCard_Xml_Exception
       
    50      */
       
    51     static public function getInstance($xmlData)
       
    52     {
       
    53         if($xmlData instanceof Zend_InfoCard_Xml_Element) {
       
    54             $strXmlData = $xmlData->asXML();
       
    55         } else if (is_string($xmlData)) {
       
    56             $strXmlData = $xmlData;
       
    57         } else {
       
    58             throw new Zend_InfoCard_Xml_Exception("Invalid Data provided to create instance");
       
    59         }
       
    60 
       
    61         $sxe = simplexml_load_string($strXmlData);
       
    62 
       
    63         if($sxe->getName() != "SecurityTokenReference") {
       
    64             throw new Zend_InfoCard_Xml_Exception("Invalid XML Block provided for SecurityTokenReference");
       
    65         }
       
    66 
       
    67         return simplexml_load_string($strXmlData, "Zend_InfoCard_Xml_SecurityTokenReference");
       
    68     }
       
    69 
       
    70     /**
       
    71      * Return the Key Identifier XML Object
       
    72      *
       
    73      * @return Zend_InfoCard_Xml_Element
       
    74      * @throws Zend_InfoCard_Xml_Exception
       
    75      */
       
    76     protected function _getKeyIdentifier()
       
    77     {
       
    78         $this->registerXPathNamespace('o', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
       
    79         list($keyident) = $this->xpath('//o:KeyIdentifier');
       
    80 
       
    81         if(!($keyident instanceof Zend_InfoCard_Xml_Element)) {
       
    82             throw new Zend_InfoCard_Xml_Exception("Failed to retrieve Key Identifier");
       
    83         }
       
    84 
       
    85         return $keyident;
       
    86     }
       
    87 
       
    88     /**
       
    89      * Return the Key URI identifying the thumbprint type used
       
    90      *
       
    91      * @return string The thumbprint type URI
       
    92      * @throws  Zend_InfoCard_Xml_Exception
       
    93      */
       
    94     public function getKeyThumbprintType()
       
    95     {
       
    96 
       
    97         $keyident = $this->_getKeyIdentifier();
       
    98 
       
    99         $dom = self::convertToDOM($keyident);
       
   100 
       
   101         if(!$dom->hasAttribute('ValueType')) {
       
   102             throw new Zend_InfoCard_Xml_Exception("Key Identifier did not provide a type for the value");
       
   103         }
       
   104 
       
   105         return $dom->getAttribute('ValueType');
       
   106     }
       
   107 
       
   108 
       
   109     /**
       
   110      * Return the thumbprint encoding type used as a URI
       
   111      *
       
   112      * @return string the URI of the thumbprint encoding used
       
   113      * @throws Zend_InfoCard_Xml_Exception
       
   114      */
       
   115     public function getKeyThumbprintEncodingType()
       
   116     {
       
   117 
       
   118         $keyident = $this->_getKeyIdentifier();
       
   119 
       
   120         $dom = self::convertToDOM($keyident);
       
   121 
       
   122         if(!$dom->hasAttribute('EncodingType')) {
       
   123             throw new Zend_InfoCard_Xml_Exception("Unable to determine the encoding type for the key identifier");
       
   124         }
       
   125 
       
   126         return $dom->getAttribute('EncodingType');
       
   127     }
       
   128 
       
   129     /**
       
   130      * Get the key reference data used to identify the public key
       
   131      *
       
   132      * @param bool $decode if true, will return a decoded version of the key
       
   133      * @return string the key reference thumbprint, either in binary or encoded form
       
   134      * @throws Zend_InfoCard_Xml_Exception
       
   135      */
       
   136     public function getKeyReference($decode = true)
       
   137     {
       
   138         $keyIdentifier = $this->_getKeyIdentifier();
       
   139 
       
   140         $dom = self::convertToDOM($keyIdentifier);
       
   141         $encoded = $dom->nodeValue;
       
   142 
       
   143         if(empty($encoded)) {
       
   144             throw new Zend_InfoCard_Xml_Exception("Could not find the Key Reference Encoded Value");
       
   145         }
       
   146 
       
   147         if($decode) {
       
   148 
       
   149             $decoded = "";
       
   150             switch($this->getKeyThumbprintEncodingType()) {
       
   151                 case self::ENCODING_BASE64BIN:
       
   152 
       
   153                     if(version_compare(PHP_VERSION, "5.2.0", ">=")) {
       
   154                         $decoded = base64_decode($encoded, true);
       
   155                     } else {
       
   156                         $decoded = base64_decode($encoded);
       
   157                     }
       
   158 
       
   159                     break;
       
   160                 default:
       
   161                     throw new Zend_InfoCard_Xml_Exception("Unknown Key Reference Encoding Type: {$this->getKeyThumbprintEncodingType()}");
       
   162             }
       
   163 
       
   164             if(!$decoded || empty($decoded)) {
       
   165                 throw new Zend_InfoCard_Xml_Exception("Failed to decode key reference");
       
   166             }
       
   167 
       
   168             return $decoded;
       
   169         }
       
   170 
       
   171         return $encoded;
       
   172     }
       
   173 }