Skip to content

Commit

Permalink
Refactored AzureStorage, added, SharedKey Authorization, Started Func…
Browse files Browse the repository at this point in the history
…tional tests for storage.
  • Loading branch information
beberlei committed Mar 26, 2012
1 parent 865e227 commit 9de0dff
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ interface AuthorizationSchema
* Sign a request by returning a new header to be appended to headers.
*
* @param string $method
* @param string $path
* @param string $queryString
* @param string $body
* @param array $headers
* @return string
*/
function signRequest($method, $body, array $headers);
function signRequest($method, $path, $queryString, $body, array $headers);
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,34 @@

class SharedKeyAuthorization implements AuthorizationSchema
{
/**
* @var string
*/
private $accountName;
/**
* @var string
*/
private $accountKey;

public function __construct($accountName, $accountKey)
{
$this->accountName = $accountName;
$this->accountKey = $accountKey;
}

/**
* @override
* {@inheritDocs}
*/
public function signRequest($method, $body, array $headers)
public function signRequest($method, $path, $queryString, $body, array $headers)
{
return "";
$canonicalResource = "/" . $this->accountName . '/' . $path;
$stringToSign = $method . "\n" .
md5($body) . "\n" .
"application/atom+xml\n" .
$headers['x-ms-date'] . "\n" .
$canonicalResource;
return "Authorization: SharedKey " . base64_encode(hash_hmac('sha256', $stringToSign, $this->accountKey, true));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class WindowsAzureTableStorage implements Storage
* @param HttpClient $client
* @param AuthorizationSchema $authorization
*/
public function __construct(Client $client, $accountName, AuthorizationSchema $authorization, \DateTime $now)
public function __construct(Client $client, $accountName, AuthorizationSchema $authorization, \DateTime $now = null)
{
$this->client = $client;
$this->authorization = $authorization;
Expand Down Expand Up @@ -175,7 +175,8 @@ private function serializeKeys($propertiesNode, $key)

private function request($method, $url, $xml, $headers)
{
$authorizationHeader = $this->authorization->signRequest($method, "", $headers);
$parts = parse_url($url);
$authorizationHeader = $this->authorization->signRequest($method, $xml, $parts['path'], isset($parts['query']) ? $parts['query'] : '', $headers);
$authorizationParts = explode(":" , $authorizationHeader, 2);
$headers['Content-Length'] = strlen($xml);
$headers[$authorizationParts[0]] = ltrim($authorizationParts[1]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace Doctrine\Tests\KeyValueStore\Functional\Storage;

use Doctrine\Tests\KeyValueStoreTestCase;
use Doctrine\KeyValueStore\Storage\WindowsAzureTableStorage;
use Doctrine\KeyValueStore\Storage\WindowsAzureTable\SharedKeyAuthorization;
use Doctrine\KeyValueStore\Http\StreamClient;

class WindowsAzureTableTest extends KeyValueStoreTestCase
{
public function testCrud()
{
if (empty($GLOBALS['DOCTRINE_KEYVALUE_AZURE_NAME']) || empty($GLOBALS['DOCTRINE_KEYVALUE_AZURE_KEY'])) {
$this->markTestSkipped("Missing Azure credentials.");
}

switch ($GLOBALS['DOCTRINE_KEYVALUE_AZURE_AUTHSCHEMA']) {
case 'shared':
$auth = new SharedKeyAuthorization(
$GLOBALS['DOCTRINE_KEYVALUE_AZURE_NAME'],
$GLOBALS['DOCTRINE_KEYVALUE_AZURE_KEY']
);
break;
}

$storage = new WindowsAzureTableStorage(
new StreamClient(),
$GLOBALS['DOCTRINE_KEYVALUE_AZURE_NAME'],
$auth
);

$storage->insert("test", array("partition" => "foo", "range" => 100), array("foo" => "bar"));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function mockFindCompositeKey($key)
$this->equalTo(''),
$this->equalTo($expectedHeaders)
)->will($this->returnValue(
new Response(201, <<<XML
new Response(200, <<<XML
<?xml version="1.0" ?>
<entry xml:base="http://myaccount.table.core.windows.net/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:etag="W/&quot;datetime'2008-09-18T23%3A46%3A19.4277424Z'&quot;" xmlns="http://www.w3.org/2005/Atom">
<id>http://myaccount.table.core.windows.net/mytable(PartitionKey='foo',RowKey='100')</id>
Expand Down

0 comments on commit 9de0dff

Please sign in to comment.