<?php
/**
* AkamaiToken.php - An Akamai EdgeAuth Token 2.0 implementation for PHP
*
* author: James Mutton <jmutton@akamai.com>
*
* Copyright (c) 2011, Akamai Technologies, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Akamai Technologies nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AKAMAI TECHNOLOGIES BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* AkamaiToken
* Notes:
*/
/**
* Class for handling the configuration of the token generator
*/
class Akamai_EdgeAuth_ParameterException_PD extends Exception {
}
/**
* Class for handling the configuration of the token generator
*/
class Akamai_EdgeAuth_Config_PD {
protected $algo = "SHA256";
protected $ip = '';
protected $start_time = 0;
protected $window = 300;
protected $acl = '';
protected $url = '';
protected $session_id = '';
protected $data = '';
protected $salt = '';
protected $key = '5db5e3a884941e4eb79a798a';
protected $field_delimiter = '~';
protected $early_url_encoding = false;
protected function encode($val) {
if ($this->early_url_encoding === true) {
}
return $val;
}
public function set_algo($algo) {
$this->algo = $algo;
} else {
throw new Akamai_EdgeAuth_ParameterException_PD("Invalid algorithme, must be one of 'sha256', 'sha1' or 'md5'");
}
}
public function get_algo() {return $this->algo;}
public function set_ip($ip) {
// @TODO: Validate IPV4 & IPV6 addrs
$this->ip = $ip;
}
public function get_ip() {return $this->ip;}
public function get_ip_field() {
if ( $this->ip != "" ) {
return 'ip='.$this->ip.$this->field_delimiter;
}
return "";
}
public function set_start_time($start_time) {
// verify starttime is sane
$this->start_time = time(); } else {
if ( is_numeric($start_time) && $start_time > 0 && $start_time < 4294967295 ) { $this->start_time = 0+$start_time; // faster then intval
} else {
throw new Akamai_EdgeAuth_ParameterException_PD("start time input invalid or out of range");
}
}
}
public function get_start_time() {return $this->start_time;}
protected function get_start_time_value() {
if ( $this->start_time > 0 ) {
return $this->start_time;
} else {
}
}
public function get_start_time_field() {
if ( is_numeric($this->start_time) && $this->start_time > 0 && $this->start_time < 4294967295 ) { return 'st='.$this->get_start_time_value().$this->field_delimiter;
} else {
return '';
}
}
public function set_window($window) {
// verify window is sane
$this->window = 0+$window; // faster then intval
} else {
throw new Akamai_EdgeAuth_ParameterException_PD("window input invalid");
}
}
public function get_window() {return $this->window;}
public function get_expr_field() {
return 'exp='.($this->get_start_time_value()+$this->window).$this->field_delimiter;
}
public function set_acl($acl) {
if ($this->url != "") {
throw new Akamai_EdgeAuth_ParameterException_PD("Cannot set both an ACL and a URL at the same time");
}
$this->acl = $acl;
}
public function get_acl() {return $this->acl;}
public function get_acl_field() {
if ($this->acl) {
return 'acl='.$this->encode($this->acl).$this->field_delimiter;
} elseif (! $this->url) {
//return a default open acl
return 'acl='.$this->encode('/*').$this->field_delimiter;
}
return '';
}
public function set_url($url) {
if ($this->acl) {
throw new Akamai_EdgeAuth_ParameterException_PD("Cannot set both an ACL and a URL at the same time");
}
$this->url = $url;
}
public function get_url() {return $this->url;}
public function get_url_field() {
if ($this->url && ! $this->acl) {
return 'url='.$this->encode($this->url).$this->field_delimiter;
}
return '';
}
public function set_session_id
($session_id) {$this->session_id = $session_id;} public function get_session_id
() {return $this->session_id;} public function get_session_id_field() {
return 'id='.$this->session_id.$this->field_delimiter; }
return "";
}
public function set_data($data) {$this->data = $data;}
public function get_data() {return $this->data;}
public function get_data_field() {
if ($this->data) {
return 'data='.$this->data.$this->field_delimiter;
}
return "";
}
public function set_salt($salt) {$this->salt = $salt;}
public function get_salt() {return $this->salt;}
public function get_salt_field() {
if ($this->salt) {
return 'salt='.$this->salt.$this->field_delimiter;
}
return "";
}
public function set_key($key) {
//verify the key is valid hex
} else {
throw new Akamai_EdgeAuth_ParameterException_PD("Key must be a hex string (a-f,0-9 and even number of chars)");
}
}
public function get_key
() {return $this->key;}
public function set_field_delimiter($field_delimiter) {$this->field_delimiter = $field_delimiter;}
public function get_field_delimiter() {return $this->field_delimiter;}
public function set_early_url_encoding($early_url_encoding) {$this->early_url_encoding = $early_url_encoding;}
public function get_early_url_encoding() {return $this->early_url_encoding;}
}
class Akamai_EdgeAuth_Generate_PD {
protected function h2b($str) {
$bin = "";
$i = 0;
do {
$bin .= chr(hexdec($str{$i}.$str{($i + 1)})); $i += 2;
return $bin;
}
public function generate_token($config) {
// ASSUMES:($algo='sha256', $ip='', $start_time=null, $window=300, $acl=null, $acl_url="", $session_id="", $payload="", $salt="", $key="000000000000", $field_delimiter="~")
$m_token = $config->get_ip_field();
$m_token .= $config->get_start_time_field();
$m_token .= $config->get_expr_field();
$m_token .= $config->get_acl_field();
$m_token .= $config->get_session_id_field();
$m_token .= $config->get_data_field();
$m_token_digest = (string)$m_token;
$m_token_digest .= $config->get_url_field();
$m_token_digest .= $config->get_salt_field();
// produce the signature and append to the tokenized string
$signature = hash_hmac($config->get_algo(), rtrim($m_token_digest, $config->get_field_delimiter()), $this->h2b($config->get_key())); return $m_token.'hmac='.$signature;
}
}
$c=new Akamai_EdgeAuth_Config_PD();
print "hello<br/>";
//$c->set_url("http://z...content-available-to-author-only...d.net/i/zliving1m_1@150840/master.m3u8");
$c->set_acl("*");
//$c->set_url("http://t...content-available-to-author-only...v.com/ditto/akamaisec.php");
$c->set_key("3f17fd1f57b7f2f3e6aadc6c");
//$c->set_early_url_encoding(true);
$c->set_start_time($date);
$c->set_window(9000);
$akamaiauth = new Akamai_EdgeAuth_Generate_PD();
$token=$akamaiauth->generate_token($c);
// $url2="http://z...content-available-to-author-only...d.net/i/draco/shows/Zee_TV/Jodha_Akbar/December/10122014/Jodha_Akbar_Episode_394.smil/master.m3u8?hdnea=$token";
$url2="http://z...content-available-to-author-only...d.net/i/draco/shows/Zindagi/Khwahishein/January/21012015/Khwahishein_Episode_13.smil/master.m3u8?hdnea=$token";
$url1="http://d...content-available-to-author-only...d.net/i/draco/Movies/Barfi/Video/Barfi.smil/master.m3u8?hdnea=$token";
//$url2="http://p...content-available-to-author-only...e.net/325615/Aaj_Aur_Kal_300k.mp4?hdnea=$token";
//$url2= str_replace("/*", "%2F%2A", $url2);
// echo $url2."<br/>";
//$url2="http://z...content-available-to-author-only...d.net/z/draco/shows/Zee_TV/Jodha_Akbar/November/03112014/Jodha_Akbar_Episode_03112014.smil/manifest.f4m?hdnea=st=1422011733~exp=458211110498~acl=%2f*~hmac=b22440188d1f2941e3f64f0109c7bf276d85a51c94d64a12d64603c230df7840";
echo $url2."<br/>";
?>