first commit
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link https://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
|
||||
namespace WindowsAzure\Common\Internal\Authentication;
|
||||
|
||||
/**
|
||||
* Interface for azure authentication schemes.
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @version Release: 0.4.1_2015-03
|
||||
* @link https://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
interface IAuthScheme
|
||||
{
|
||||
/**
|
||||
* Returns authorization header to be included in the request.
|
||||
*
|
||||
* @param array $headers request headers.
|
||||
* @param string $url reuqest url.
|
||||
* @param array $queryParams query variables.
|
||||
* @param string $httpMethod request http method.
|
||||
*
|
||||
* @see Specifying the Authorization Header section at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @abstract
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAuthorizationHeader($headers, $url, $queryParams,
|
||||
$httpMethod
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link http://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
|
||||
namespace WindowsAzure\Common\Internal\Authentication;
|
||||
use WindowsAzure\Common\Internal\Authentication\IAuthScheme;
|
||||
use WindowsAzure\Common\Internal\Resources;
|
||||
use WindowsAzure\Common\Internal\Utilities;
|
||||
use WindowsAzure\Common\Internal\Validate;
|
||||
use WindowsAzure\Common\Internal\OAuthRestProxy;
|
||||
use WindowsAzure\Common\Models\OAuthAccessToken;
|
||||
|
||||
/**
|
||||
* Provides shared key authentication scheme for OAuth.
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @version Release: 0.4.1_2015-03
|
||||
* @link http://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
class OAuthScheme implements IAuthScheme
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $accountName;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $accountKey;
|
||||
|
||||
/**
|
||||
* @var WindowsAzure\Common\Models\OAuthAccessToken
|
||||
*/
|
||||
protected $accessToken;
|
||||
|
||||
/**
|
||||
* @var WindowsAzure\Common\Internal\OAuthRestProxy
|
||||
*/
|
||||
protected $oauthService;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $grantType;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $scope;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $accountName account name.
|
||||
* @param string $accountKey account
|
||||
* secondary key.
|
||||
*
|
||||
* @param string $grantType grant type
|
||||
* for OAuth request.
|
||||
*
|
||||
* @param string $scope scope for
|
||||
* OAurh request.
|
||||
*
|
||||
* @param WindowsAzure\Common\Internal\OAuthRestProxy $oauthService account
|
||||
* primary or secondary key.
|
||||
*/
|
||||
public function __construct(
|
||||
$accountName,
|
||||
$accountKey,
|
||||
$grantType,
|
||||
$scope,
|
||||
$oauthService
|
||||
) {
|
||||
Validate::isString($accountName, 'accountName');
|
||||
Validate::isString($accountKey, 'accountKey');
|
||||
Validate::isString($grantType, 'grantType');
|
||||
Validate::isString($scope, 'scope');
|
||||
Validate::notNull($oauthService, 'oauthService');
|
||||
|
||||
$this->accountName = $accountName;
|
||||
$this->accountKey = $accountKey;
|
||||
$this->grantType = $grantType;
|
||||
$this->scope = $scope;
|
||||
$this->oauthService = $oauthService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns authorization header to be included in the request.
|
||||
*
|
||||
* @param array $headers request headers.
|
||||
* @param string $url reuqest url.
|
||||
* @param array $queryParams query variables.
|
||||
* @param string $httpMethod request http method.
|
||||
*
|
||||
* @see Specifying the Authorization Header section at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAuthorizationHeader($headers, $url, $queryParams, $httpMethod)
|
||||
{
|
||||
if (($this->accessToken == null)
|
||||
|| ($this->accessToken->getExpiresIn() < time())
|
||||
) {
|
||||
$this->accessToken = $this->oauthService->getAccessToken(
|
||||
$this->grantType,
|
||||
$this->accountName,
|
||||
$this->accountKey,
|
||||
$this->scope
|
||||
);
|
||||
}
|
||||
|
||||
return Resources::OAUTH_ACCESS_TOKEN_PREFIX .
|
||||
$this->accessToken->getAccessToken();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright 2012 Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link https://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
|
||||
namespace WindowsAzure\Common\Internal\Authentication;
|
||||
use WindowsAzure\Common\Internal\Authentication\StorageAuthScheme;
|
||||
use WindowsAzure\Common\Internal\Resources;
|
||||
use WindowsAzure\Common\Internal\Utilities;
|
||||
|
||||
/**
|
||||
* Provides shared key authentication scheme for blob and queue. For more info
|
||||
* check: http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright 2012 Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @version Release: 0.4.1_2015-03
|
||||
* @link https://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
class SharedKeyAuthScheme extends StorageAuthScheme
|
||||
{
|
||||
protected $includedHeaders;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $accountName storage account name.
|
||||
* @param string $accountKey storage account primary or secondary key.
|
||||
*
|
||||
* @return
|
||||
* WindowsAzure\Common\Internal\Authentication\SharedKeyAuthScheme
|
||||
*/
|
||||
public function __construct($accountName, $accountKey)
|
||||
{
|
||||
parent::__construct($accountName, $accountKey);
|
||||
|
||||
$this->includedHeaders = array();
|
||||
$this->includedHeaders[] = Resources::CONTENT_ENCODING;
|
||||
$this->includedHeaders[] = Resources::CONTENT_LANGUAGE;
|
||||
$this->includedHeaders[] = Resources::CONTENT_LENGTH;
|
||||
$this->includedHeaders[] = Resources::CONTENT_MD5;
|
||||
$this->includedHeaders[] = Resources::CONTENT_TYPE;
|
||||
$this->includedHeaders[] = Resources::DATE;
|
||||
$this->includedHeaders[] = Resources::IF_MODIFIED_SINCE;
|
||||
$this->includedHeaders[] = Resources::IF_MATCH;
|
||||
$this->includedHeaders[] = Resources::IF_NONE_MATCH;
|
||||
$this->includedHeaders[] = Resources::IF_UNMODIFIED_SINCE;
|
||||
$this->includedHeaders[] = Resources::RANGE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the authorization signature for blob and queue shared key.
|
||||
*
|
||||
* @param array $headers request headers.
|
||||
* @param string $url reuqest url.
|
||||
* @param array $queryParams query variables.
|
||||
* @param string $httpMethod request http method.
|
||||
*
|
||||
* @see Blob and Queue Services (Shared Key Authentication) at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function computeSignature($headers, $url, $queryParams, $httpMethod)
|
||||
{
|
||||
$canonicalizedHeaders = parent::computeCanonicalizedHeaders($headers);
|
||||
|
||||
$canonicalizedResource = parent::computeCanonicalizedResource(
|
||||
$url, $queryParams
|
||||
);
|
||||
|
||||
|
||||
$stringToSign = array();
|
||||
$stringToSign[] = strtoupper($httpMethod);
|
||||
|
||||
foreach ($this->includedHeaders as $header) {
|
||||
$stringToSign[] = Utilities::tryGetValue($headers, $header);
|
||||
}
|
||||
|
||||
if (count($canonicalizedHeaders) > 0) {
|
||||
$stringToSign[] = implode("\n", $canonicalizedHeaders);
|
||||
}
|
||||
|
||||
$stringToSign[] = $canonicalizedResource;
|
||||
$stringToSign = implode("\n", $stringToSign);
|
||||
|
||||
return $stringToSign;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns authorization header to be included in the request.
|
||||
*
|
||||
* @param array $headers request headers.
|
||||
* @param string $url reuqest url.
|
||||
* @param array $queryParams query variables.
|
||||
* @param string $httpMethod request http method.
|
||||
*
|
||||
* @see Specifying the Authorization Header section at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAuthorizationHeader($headers, $url, $queryParams, $httpMethod)
|
||||
{
|
||||
$signature = $this->computeSignature(
|
||||
$headers, $url, $queryParams, $httpMethod
|
||||
);
|
||||
|
||||
return 'SharedKey ' . $this->accountName . ':' . base64_encode(
|
||||
hash_hmac('sha256', $signature, base64_decode($this->accountKey), true)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright 2012 Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link https://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
|
||||
namespace WindowsAzure\Common\Internal\Authentication;
|
||||
use WindowsAzure\Common\Internal\Resources;
|
||||
use WindowsAzure\Common\Internal\Utilities;
|
||||
use WindowsAzure\Common\Internal\Authentication\IAuthScheme;
|
||||
|
||||
|
||||
/**
|
||||
* Base class for azure authentication schemes.
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright 2012 Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @version Release: 0.4.1_2015-03
|
||||
* @link https://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
abstract class StorageAuthScheme implements IAuthScheme
|
||||
{
|
||||
protected $accountName;
|
||||
protected $accountKey;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $accountName storage account name.
|
||||
* @param string $accountKey storage account primary or secondary key.
|
||||
*
|
||||
* @return
|
||||
* WindowsAzure\Common\Internal\Authentication\StorageAuthScheme
|
||||
*/
|
||||
public function __construct($accountName, $accountKey)
|
||||
{
|
||||
$this->accountKey = $accountKey;
|
||||
$this->accountName = $accountName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes canonicalized headers for headers array.
|
||||
*
|
||||
* @param array $headers request headers.
|
||||
*
|
||||
* @see Constructing the Canonicalized Headers String section at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function computeCanonicalizedHeaders($headers)
|
||||
{
|
||||
$canonicalizedHeaders = array();
|
||||
$normalizedHeaders = array();
|
||||
$validPrefix = Resources::X_MS_HEADER_PREFIX;
|
||||
|
||||
if (is_null($normalizedHeaders)) {
|
||||
return $canonicalizedHeaders;
|
||||
}
|
||||
|
||||
foreach ($headers as $header => $value) {
|
||||
// Convert header to lower case.
|
||||
$header = strtolower($header);
|
||||
|
||||
// Retrieve all headers for the resource that begin with x-ms-,
|
||||
// including the x-ms-date header.
|
||||
if (Utilities::startsWith($header, $validPrefix)) {
|
||||
// Unfold the string by replacing any breaking white space
|
||||
// (meaning what splits the headers, which is \r\n) with a single
|
||||
// space.
|
||||
$value = str_replace("\r\n", ' ', $value);
|
||||
|
||||
// Trim any white space around the colon in the header.
|
||||
$value = ltrim($value);
|
||||
$header = rtrim($header);
|
||||
|
||||
$normalizedHeaders[$header] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the headers lexicographically by header name, in ascending order.
|
||||
// Note that each header may appear only once in the string.
|
||||
ksort($normalizedHeaders);
|
||||
|
||||
foreach ($normalizedHeaders as $key => $value) {
|
||||
$canonicalizedHeaders[] = $key . ':' . $value;
|
||||
}
|
||||
|
||||
return $canonicalizedHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes canonicalized resources from URL using Table formar
|
||||
*
|
||||
* @param string $url request url.
|
||||
* @param array $queryParams request query variables.
|
||||
*
|
||||
* @see Constructing the Canonicalized Resource String section at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function computeCanonicalizedResourceForTable($url, $queryParams)
|
||||
{
|
||||
$queryParams = array_change_key_case($queryParams);
|
||||
|
||||
// 1. Beginning with an empty string (""), append a forward slash (/),
|
||||
// followed by the name of the account that owns the accessed resource.
|
||||
$canonicalizedResource = '/' . $this->accountName;
|
||||
|
||||
// 2. Append the resource's encoded URI path, without any query parameters.
|
||||
$canonicalizedResource .= parse_url($url, PHP_URL_PATH);
|
||||
|
||||
// 3. The query string should include the question mark and the comp
|
||||
// parameter (for example, ?comp=metadata). No other parameters should
|
||||
// be included on the query string.
|
||||
if (array_key_exists(Resources::QP_COMP, $queryParams)) {
|
||||
$canonicalizedResource .= '?' . Resources::QP_COMP . '=';
|
||||
$canonicalizedResource .= $queryParams[Resources::QP_COMP];
|
||||
}
|
||||
|
||||
return $canonicalizedResource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes canonicalized resources from URL.
|
||||
*
|
||||
* @param string $url request url.
|
||||
* @param array $queryParams request query variables.
|
||||
*
|
||||
* @see Constructing the Canonicalized Resource String section at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function computeCanonicalizedResource($url, $queryParams)
|
||||
{
|
||||
$queryParams = array_change_key_case($queryParams);
|
||||
|
||||
// 1. Beginning with an empty string (""), append a forward slash (/),
|
||||
// followed by the name of the account that owns the accessed resource.
|
||||
$canonicalizedResource = '/' . $this->accountName;
|
||||
|
||||
// 2. Append the resource's encoded URI path, without any query parameters.
|
||||
$canonicalizedResource .= parse_url($url, PHP_URL_PATH);
|
||||
|
||||
// 3. Retrieve all query parameters on the resource URI, including the comp
|
||||
// parameter if it exists.
|
||||
// 4. Sort the query parameters lexicographically by parameter name, in
|
||||
// ascending order.
|
||||
if (count($queryParams) > 0) {
|
||||
ksort($queryParams);
|
||||
}
|
||||
|
||||
// 5. Convert all parameter names to lowercase.
|
||||
// 6. URL-decode each query parameter name and value.
|
||||
// 7. Append each query parameter name and value to the string in the
|
||||
// following format:
|
||||
// parameter-name:parameter-value
|
||||
// 9. Group query parameters
|
||||
// 10. Append a new line character (\n) after each name-value pair.
|
||||
foreach ($queryParams as $key => $value) {
|
||||
// Grouping query parameters
|
||||
$values = explode(Resources::SEPARATOR, $value);
|
||||
sort($values);
|
||||
$separated = implode(Resources::SEPARATOR, $values);
|
||||
|
||||
$canonicalizedResource .= "\n" . $key . ':' . $separated;
|
||||
}
|
||||
|
||||
return $canonicalizedResource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the authorization signature.
|
||||
*
|
||||
* @param array $headers request headers.
|
||||
* @param string $url reuqest url.
|
||||
* @param array $queryParams query variables.
|
||||
* @param string $httpMethod request http method.
|
||||
*
|
||||
* @see check all authentication schemes at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @abstract
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function computeSignature($headers, $url, $queryParams,
|
||||
$httpMethod
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright 2012 Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link http://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
|
||||
namespace WindowsAzure\Common\Internal\Authentication;
|
||||
use WindowsAzure\Common\Internal\Authentication\StorageAuthScheme;
|
||||
use WindowsAzure\Common\Internal\Resources;
|
||||
use WindowsAzure\Common\Internal\Utilities;
|
||||
|
||||
/**
|
||||
* Provides shared key authentication scheme for blob and queue. For more info
|
||||
* check: http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @category Microsoft
|
||||
* @package WindowsAzure\Common\Internal\Authentication
|
||||
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
|
||||
* @copyright 2012 Microsoft Corporation
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @version Release: 0.4.1_2015-03
|
||||
* @link http://github.com/windowsazure/azure-sdk-for-php
|
||||
*/
|
||||
class TableSharedKeyLiteAuthScheme extends StorageAuthScheme
|
||||
{
|
||||
protected $includedHeaders;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $accountName storage account name.
|
||||
* @param string $accountKey storage account primary or secondary key.
|
||||
*
|
||||
* @return TableSharedKeyLiteAuthScheme
|
||||
*/
|
||||
public function __construct($accountName, $accountKey)
|
||||
{
|
||||
parent::__construct($accountName, $accountKey);
|
||||
|
||||
$this->includedHeaders = array();
|
||||
$this->includedHeaders[] = Resources::DATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the authorization signature for blob and queue shared key.
|
||||
*
|
||||
* @param array $headers request headers.
|
||||
* @param string $url reuqest url.
|
||||
* @param array $queryParams query variables.
|
||||
* @param string $httpMethod request http method.
|
||||
*
|
||||
* @see Blob and Queue Services (Shared Key Authentication) at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function computeSignature($headers, $url, $queryParams, $httpMethod)
|
||||
{
|
||||
$canonicalizedResource = parent::computeCanonicalizedResourceForTable(
|
||||
$url, $queryParams
|
||||
);
|
||||
|
||||
$stringToSign = array();
|
||||
|
||||
foreach ($this->includedHeaders as $header) {
|
||||
$stringToSign[] = Utilities::tryGetValue($headers, $header);
|
||||
}
|
||||
|
||||
$stringToSign[] = $canonicalizedResource;
|
||||
$stringToSign = implode("\n", $stringToSign);
|
||||
|
||||
return $stringToSign;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns authorization header to be included in the request.
|
||||
*
|
||||
* @param array $headers request headers.
|
||||
* @param string $url reuqest url.
|
||||
* @param array $queryParams query variables.
|
||||
* @param string $httpMethod request http method.
|
||||
*
|
||||
* @see Specifying the Authorization Header section at
|
||||
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAuthorizationHeader($headers, $url, $queryParams, $httpMethod)
|
||||
{
|
||||
$signature = $this->computeSignature(
|
||||
$headers, $url, $queryParams, $httpMethod
|
||||
);
|
||||
|
||||
return 'SharedKeyLite ' . $this->accountName . ':' . base64_encode(
|
||||
hash_hmac('sha256', $signature, base64_decode($this->accountKey), true)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user