Skip to content

Commit

Permalink
Add Akamai token generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Tocker committed Jan 30, 2017
1 parent c2e092a commit 565a8aa
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Akamai.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Created by PhpStorm.
* User: amir
* Date: 26/01/2017
* Time: 15:16
*/

namespace Cloudinary;


trait Akamai {

public static function generate_akamai_token($options=array()){
$key = \Cloudinary::option_get($options, "key", \Cloudinary::config_get("akamai_key"));
if(!isset($key)) throw new \Exception("Missing akamai_key configuration");
$name = \Cloudinary::option_get($options, "token_name", "__cld_token__");
$start = \Cloudinary::option_get($options, "start_time");
$expiration = \Cloudinary::option_get($options, "end_time");
$ip = \Cloudinary::option_get($options, "ip");
$acl = \Cloudinary::option_get($options, "acl");
$window = \Cloudinary::option_get($options, "window");

if(!strcasecmp($start, "now")) {
$start = time();
} elseif (is_numeric($start)) {
$start = 0 + $start;
}
if(!isset($expiration)){
if(isset($window)){
$expiration = (isset($start) ? $start : time()) + $window;
} else {
throw new \Cloudinary\Error("Must provide 'end_time' or 'window'.");
}
}
$token = array();
if(isset($ip)) array_push($token, "ip=$ip");
if(isset($start)) array_push($token, "st=$start");
array_push($token, "exp=$expiration");
array_push($token, "acl=$acl");
$auth = self::digest(join("~", $token), $key);
array_push($token, "hmac=$auth");
return "$name=" . join("~", $token);
}

private static function digest($message, $key = NULL) {
if(!isset($key)) $key = \Cloudinary::config_get("akamai_key");
$bin_key = pack("H*", $key);
return hash_hmac( "sha256", $message, $bin_key);
}
}
2 changes: 2 additions & 0 deletions src/Cloudinary.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
require_once 'Akamai.php';

class Cloudinary {

use \Cloudinary\Akamai;
const CF_SHARED_CDN = "d3jpl91pxevbkh.cloudfront.net";
const OLD_AKAMAI_SHARED_CDN = "cloudinary-a.akamaihd.net";
const AKAMAI_SHARED_CDN = "res.cloudinary.com";
Expand Down
48 changes: 48 additions & 0 deletions tests/CloudinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,54 @@ public function test_translate_if() {
}


public function test_akamai_token() {
\Cloudinary::config(array("akamai_key"=>"00112233FF99"));
$token = Cloudinary::generate_akamai_token(
array(
"start_time" => 1111111111,
"acl" => '/image/*',
"window" => 300
) );
$this->assertEquals(
'__cld_token__=st=1111111111~exp=1111111411~acl=/image/*~' .
'hmac=0854e8b6b6a46471a80b2dc28c69bd352d977a67d031755cc6f3486c121b43af',
$token,
"should generate an Akamai token with start_time and window" );
$first_exp = time() + 300;
$token = Cloudinary::generate_akamai_token(
array(
"acl" => '*',
"window" => 300
) );
$second_exp = time() + 300;
preg_match('/exp=(\d+)/', $token, $matches);
$this->assertNotEmpty($matches);
$this->assertNotEmpty($matches[1]);
$expiration = 0+ $matches[1];
$this->assertGreaterThanOrEqual($first_exp, $expiration);
$this->assertLessThanOrEqual($second_exp, $expiration);
}
public function test_accepts_key() {
$this->assertEquals('__cld_token__=exp=10000000~acl=*~' .
'hmac=030eafb6b19e499659d699b3d43e7595e35e3c0060e8a71904b3b8c8759f4890',
Cloudinary::generate_akamai_token(
array(
"acl" => '*',
"end_time" => 10000000,
"key" => '00aabbff'
)
));
}
/**
* @expectedException Cloudinary\Error
*/
public function test_requires_end_time_or_window() {
Cloudinary::generate_akamai_token(
array(
"acl" => '*'
) );
}

private function cloudinary_url_assertion($source, $options, $expected, $expected_options = array()) {
$url = Cloudinary::cloudinary_url($source, $options);
$this->assertEquals($expected_options, $options);
Expand Down

0 comments on commit 565a8aa

Please sign in to comment.