Skip to content

Commit

Permalink
Factored out and tested generation of the final string to sign
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Brasero committed Jul 16, 2010
1 parent 1466784 commit a966d48
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
19 changes: 12 additions & 7 deletions panda.php
Expand Up @@ -104,6 +104,17 @@ public function signed_params($verb, $request_path, $params = array(), $timestam
}

public static function signature_generator($verb, $request_path, $host, $secret_key, $params = array()) {
$string_to_sign = self::string_to_sign($verb, $request_path, $host, $params);
$context = hash_init('sha256', HASH_HMAC, $secret_key);
hash_update($context, $string_to_sign);
return base64_encode(hash_final($context, true));
}

public function generate_signature($verb, $request_path, $params = array()) {
return self::signature_generator($verb, $request_path, $this->api_host, $this->secret_key, $params);
}

public function string_to_sign($verb, $request_path, $host, $params = array()) {
$request_path = self::canonical_path($request_path);
$query_string = self::canonical_querystring($params);
$_verb = strtoupper($verb);
Expand All @@ -114,13 +125,7 @@ public static function signature_generator($verb, $request_path, $host, $secret_
$request_path
$query_string
END;
$context = hash_init('sha256', HASH_HMAC, $secret_key);
hash_update($context, $string_to_sign);
return base64_encode(hash_final($context, true));
}

public function generate_signature($verb, $request_path, $params = array()) {
return self::signature_generator($verb, $request_path, $this->api_host, $this->secret_key, $params);
return $string_to_sign;
}

//
Expand Down
29 changes: 25 additions & 4 deletions test/index.php
Expand Up @@ -31,7 +31,7 @@ function test_api_host_and_port() {
}

class SignatureTest extends UnitTestCase {
function test_0() {
function test_generator_simple() {
$signature = Panda::signature_generator(
'GET',
'/videos.json',
Expand All @@ -41,8 +41,8 @@ function test_0() {
);
$this->assertEqual('RZml1S8BcxSTCaTSQVnXj/7QA0vM4M6FqRIUuntTYjk=', $signature);
}

function test_1() {
function test_generator_complex() {
$signature = Panda::signature_generator(
'POST',
'/videos.json',
Expand All @@ -56,4 +56,25 @@ function test_1() {
);
$this->assertEqual('JKBd6ARhTYqxGeTsqfrPUoOHrvmmz59bkJK37XvrL/U=', $signature);
}
}

function test_string_to_sign() {
$expected =<<<EOF
POST
api.eu.pandastream.com
/videos.json
access_key=9c264aba-8d97-df11-b01b-12313c0091c1&cloud_id=5385adf38f3e39de1ddcf4c1b81ad056&timestamp=2010-07-16T06%3A27%3A54%2B00%3A00&upload_redirect_url=http%3A%2F%2Flocalhost%3A44444%2Fpanda%2Fsimplest%2Fplayer.php%3Fpanda_video_id%3D%24id
EOF;
$actual = Panda::string_to_sign(
'POST',
'/videos.json',
'api.eu.pandastream.com',
array(
'upload_redirect_url' => 'http://localhost:44444/panda/simplest/player.php?panda_video_id=$id',
'cloud_id' => '5385adf38f3e39de1ddcf4c1b81ad056',
'access_key' => '9c264aba-8d97-df11-b01b-12313c0091c1',
'timestamp' => '2010-07-16T06:27:54+00:00',
)
);
$this->assertEqual($expected, $actual);
}
}

0 comments on commit a966d48

Please sign in to comment.