File indexing completed on 2024-12-29 05:28:03

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_WindowsAzure
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
0024  */
0025 // require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Service_WindowsAzure
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */ 
0033 class Zend_Service_WindowsAzure_Credentials_SharedKeyLite
0034     extends Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
0035 {
0036     /**
0037    * Sign request URL with credentials
0038    *
0039    * @param string $requestUrl Request URL
0040    * @param string $resourceType Resource type
0041    * @param string $requiredPermission Required permission
0042    * @return string Signed request URL
0043    */
0044   public function signRequestUrl(
0045     $requestUrl = '',
0046     $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
0047     $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
0048   ) {
0049       return $requestUrl;
0050   }
0051   
0052   /**
0053    * Sign request headers with credentials
0054    *
0055    * @param string $httpVerb HTTP verb the request will use
0056    * @param string $path Path for the request
0057    * @param string $queryString Query string for the request
0058    * @param array $headers x-ms headers to add
0059    * @param boolean $forTableStorage Is the request for table storage?
0060    * @param string $resourceType Resource type
0061    * @param string $requiredPermission Required permission
0062    * @param mixed  $rawData Raw post data
0063    * @return array Array of headers
0064    */
0065   public function signRequestHeaders(
0066     $httpVerb = Zend_Http_Client::GET,
0067     $path = '/',
0068     $queryString = '',
0069     $headers = null,
0070     $forTableStorage = false,
0071     $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
0072     $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
0073     $rawData = null
0074   ) {
0075     // Table storage?
0076     if (!$forTableStorage) {
0077       // require_once 'Zend/Service/WindowsAzure/Credentials/Exception.php';
0078       throw new Zend_Service_WindowsAzure_Credentials_Exception('The Windows Azure SDK for PHP does not support SharedKeyLite authentication on blob or queue storage. Use SharedKey authentication instead.');
0079     }
0080     
0081     // Determine path
0082     if ($this->_usePathStyleUri) {
0083       $path = substr($path, strpos($path, '/'));
0084     }
0085 
0086     // Determine query
0087     $queryString = $this->_prepareQueryStringForSigning($queryString);
0088 
0089     // Build canonicalized resource string
0090     $canonicalizedResource  = '/' . $this->_accountName;
0091     if ($this->_usePathStyleUri) {
0092       $canonicalizedResource .= '/' . $this->_accountName;
0093     }
0094     $canonicalizedResource .= $path;
0095     if ($queryString !== '') {
0096         $canonicalizedResource .= $queryString;
0097     }
0098 
0099     // Request date
0100     $requestDate = '';
0101     if (isset($headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
0102         $requestDate = $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
0103     } else {
0104         $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
0105     }
0106 
0107     // Create string to sign   
0108     $stringToSign   = array();
0109       $stringToSign[] = $requestDate; // Date
0110       $stringToSign[] = $canonicalizedResource;         // Canonicalized resource
0111       $stringToSign   = implode("\n", $stringToSign);
0112       $signString     = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
0113 
0114       // Sign request
0115       $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
0116       $headers['Authorization'] = 'SharedKeyLite ' . $this->_accountName . ':' . $signString;
0117       
0118       // Return headers
0119       return $headers;
0120   }
0121   
0122   /**
0123    * Prepare query string for signing
0124    * 
0125    * @param  string $value Original query string
0126    * @return string        Query string for signing
0127    */
0128   protected function _prepareQueryStringForSigning($value)
0129   {
0130       // Check for 'comp='
0131       if (strpos($value, 'comp=') === false) {
0132           // If not found, no query string needed
0133           return '';
0134       } else {
0135           // If found, make sure it is the only parameter being used      
0136         if (strlen($value) > 0 && strpos($value, '?') === 0) {
0137           $value = substr($value, 1);
0138         }
0139         
0140         // Split parts
0141         $queryParts = explode('&', $value);
0142         foreach ($queryParts as $queryPart) {
0143             if (strpos($queryPart, 'comp=') !== false) {
0144                 return '?' . $queryPart;
0145             }
0146         }
0147 
0148         // Should never happen...
0149       return '';
0150       }
0151   }
0152 }