fork download
  1. <?php
  2. /**
  3.  * AkamaiToken.php - An Akamai EdgeAuth Token 2.0 implementation for PHP
  4.  *
  5.  * author: James Mutton <jmutton@akamai.com>
  6.  *
  7.  * Copyright (c) 2011, Akamai Technologies, Inc.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions are met:
  12.  * * Redistributions of source code must retain the above copyright
  13.  * notice, this list of conditions and the following disclaimer.
  14.  * * Redistributions in binary form must reproduce the above copyright
  15.  * notice, this list of conditions and the following disclaimer in the
  16.  * documentation and/or other materials provided with the distribution.
  17.  * * Neither the name of Akamai Technologies nor the
  18.  * names of its contributors may be used to endorse or promote products
  19.  * derived from this software without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24.  * DISCLAIMED. IN NO EVENT SHALL AKAMAI TECHNOLOGIES BE LIABLE FOR ANY
  25.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * AkamaiToken
  33.  * Notes:
  34.  */
  35.  
  36.  
  37. /**
  38.  * Class for handling the configuration of the token generator
  39.  */
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. class Akamai_EdgeAuth_ParameterException_PD extends Exception {
  47. }
  48.  
  49. /**
  50.  * Class for handling the configuration of the token generator
  51.  */
  52. class Akamai_EdgeAuth_Config_PD {
  53. protected $algo = "SHA256";
  54. protected $ip = '';
  55. protected $start_time = 0;
  56. protected $window = 300;
  57. protected $acl = '';
  58. protected $url = '';
  59. protected $session_id = '';
  60. protected $data = '';
  61. protected $salt = '';
  62. protected $key = '5db5e3a884941e4eb79a798a';
  63. protected $field_delimiter = '~';
  64. protected $early_url_encoding = false;
  65.  
  66.  
  67. protected function encode($val) {
  68. if ($this->early_url_encoding === true) {
  69. return rawurlencode($val);
  70. }
  71. return $val;
  72. }
  73.  
  74. public function set_algo($algo) {
  75. if (in_array($algo, array('sha256','sha1','md5'))) {
  76. $this->algo = $algo;
  77. } else {
  78. throw new Akamai_EdgeAuth_ParameterException_PD("Invalid algorithme, must be one of 'sha256', 'sha1' or 'md5'");
  79. }
  80. }
  81. public function get_algo() {return $this->algo;}
  82.  
  83. public function set_ip($ip) {
  84. // @TODO: Validate IPV4 & IPV6 addrs
  85. $this->ip = $ip;
  86. }
  87. public function get_ip() {return $this->ip;}
  88. public function get_ip_field() {
  89. if ( $this->ip != "" ) {
  90. return 'ip='.$this->ip.$this->field_delimiter;
  91. }
  92. return "";
  93. }
  94.  
  95. public function set_start_time($start_time) {
  96. // verify starttime is sane
  97. if ( strcasecmp($start_time, "now") == 0 ) {
  98. $this->start_time = time();
  99. } else {
  100. if ( is_numeric($start_time) && $start_time > 0 && $start_time < 4294967295 ) {
  101. $this->start_time = 0+$start_time; // faster then intval
  102. } else {
  103. throw new Akamai_EdgeAuth_ParameterException_PD("start time input invalid or out of range");
  104. }
  105. }
  106. }
  107. public function get_start_time() {return $this->start_time;}
  108. protected function get_start_time_value() {
  109. if ( $this->start_time > 0 ) {
  110. return $this->start_time;
  111. } else {
  112. return time();
  113. }
  114. }
  115. public function get_start_time_field() {
  116. if ( is_numeric($this->start_time) && $this->start_time > 0 && $this->start_time < 4294967295 ) {
  117. return 'st='.$this->get_start_time_value().$this->field_delimiter;
  118. } else {
  119. return '';
  120. }
  121. }
  122.  
  123. public function set_window($window) {
  124. // verify window is sane
  125. if ( is_numeric($window) && $window > 0 ) {
  126. $this->window = 0+$window; // faster then intval
  127. } else {
  128. throw new Akamai_EdgeAuth_ParameterException_PD("window input invalid");
  129. }
  130. }
  131. public function get_window() {return $this->window;}
  132. public function get_expr_field() {
  133. return 'exp='.($this->get_start_time_value()+$this->window).$this->field_delimiter;
  134. }
  135.  
  136. public function set_acl($acl) {
  137. if ($this->url != "") {
  138. throw new Akamai_EdgeAuth_ParameterException_PD("Cannot set both an ACL and a URL at the same time");
  139. }
  140. $this->acl = $acl;
  141. }
  142. public function get_acl() {return $this->acl;}
  143. public function get_acl_field() {
  144. if ($this->acl) {
  145. return 'acl='.$this->encode($this->acl).$this->field_delimiter;
  146. } elseif (! $this->url) {
  147. //return a default open acl
  148. return 'acl='.$this->encode('/*').$this->field_delimiter;
  149. }
  150. return '';
  151. }
  152.  
  153. public function set_url($url) {
  154. if ($this->acl) {
  155. throw new Akamai_EdgeAuth_ParameterException_PD("Cannot set both an ACL and a URL at the same time");
  156. }
  157. $this->url = $url;
  158. }
  159. public function get_url() {return $this->url;}
  160. public function get_url_field() {
  161. if ($this->url && ! $this->acl) {
  162. return 'url='.$this->encode($this->url).$this->field_delimiter;
  163. }
  164. return '';
  165. }
  166.  
  167. public function set_session_id($session_id) {$this->session_id = $session_id;}
  168. public function get_session_id() {return $this->session_id;}
  169. public function get_session_id_field() {
  170. if ($this->session_id) {
  171. return 'id='.$this->session_id.$this->field_delimiter;
  172. }
  173. return "";
  174. }
  175.  
  176. public function set_data($data) {$this->data = $data;}
  177. public function get_data() {return $this->data;}
  178. public function get_data_field() {
  179. if ($this->data) {
  180. return 'data='.$this->data.$this->field_delimiter;
  181. }
  182. return "";
  183. }
  184.  
  185. public function set_salt($salt) {$this->salt = $salt;}
  186. public function get_salt() {return $this->salt;}
  187. public function get_salt_field() {
  188. if ($this->salt) {
  189. return 'salt='.$this->salt.$this->field_delimiter;
  190. }
  191. return "";
  192. }
  193.  
  194. public function set_key($key) {
  195. //verify the key is valid hex
  196. if (preg_match('/^[a-fA-F0-9]+$/',$key) && (strlen($key)%2) == 0) {
  197. $this->key = $key;
  198. } else {
  199. throw new Akamai_EdgeAuth_ParameterException_PD("Key must be a hex string (a-f,0-9 and even number of chars)");
  200. }
  201. }
  202. public function get_key() {return $this->key;}
  203.  
  204. public function set_field_delimiter($field_delimiter) {$this->field_delimiter = $field_delimiter;}
  205. public function get_field_delimiter() {return $this->field_delimiter;}
  206.  
  207. public function set_early_url_encoding($early_url_encoding) {$this->early_url_encoding = $early_url_encoding;}
  208. public function get_early_url_encoding() {return $this->early_url_encoding;}
  209. }
  210. class Akamai_EdgeAuth_Generate_PD {
  211.  
  212. protected function h2b($str) {
  213. $bin = "";
  214. $i = 0;
  215. do {
  216. $bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
  217. $i += 2;
  218. } while ($i < strlen($str));
  219. return $bin;
  220. }
  221.  
  222. public function generate_token($config) {
  223. // ASSUMES:($algo='sha256', $ip='', $start_time=null, $window=300, $acl=null, $acl_url="", $session_id="", $payload="", $salt="", $key="000000000000", $field_delimiter="~")
  224. $m_token = $config->get_ip_field();
  225. $m_token .= $config->get_start_time_field();
  226. $m_token .= $config->get_expr_field();
  227. $m_token .= $config->get_acl_field();
  228. $m_token .= $config->get_session_id_field();
  229. $m_token .= $config->get_data_field();
  230. $m_token_digest = (string)$m_token;
  231. $m_token_digest .= $config->get_url_field();
  232. $m_token_digest .= $config->get_salt_field();
  233.  
  234. // produce the signature and append to the tokenized string
  235. $signature = hash_hmac($config->get_algo(), rtrim($m_token_digest, $config->get_field_delimiter()), $this->h2b($config->get_key()));
  236. return $m_token.'hmac='.$signature;
  237. }
  238. }
  239. $c=new Akamai_EdgeAuth_Config_PD();
  240. print "hello<br/>";
  241. //$c->set_url("http://z...content-available-to-author-only...d.net/i/zliving1m_1@150840/master.m3u8");
  242. $c->set_acl("*");
  243.  
  244. //$c->set_url("http://t...content-available-to-author-only...v.com/ditto/akamaisec.php");
  245.  
  246. $c->set_key("3f17fd1f57b7f2f3e6aadc6c");
  247. //$c->set_early_url_encoding(true);
  248. $date = strtotime("2015-02-04 19:17:59");
  249. $c->set_start_time($date);
  250. $c->set_window(9000);
  251. $akamaiauth = new Akamai_EdgeAuth_Generate_PD();
  252. $token=$akamaiauth->generate_token($c);
  253.  
  254.  
  255. // $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";
  256.  
  257. $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";
  258.  
  259. $url1="http://d...content-available-to-author-only...d.net/i/draco/Movies/Barfi/Video/Barfi.smil/master.m3u8?hdnea=$token";
  260. //$url2="http://p...content-available-to-author-only...e.net/325615/Aaj_Aur_Kal_300k.mp4?hdnea=$token";
  261.  
  262.  
  263. //$url2= str_replace("/*", "%2F%2A", $url2);
  264. // echo $url2."<br/>";
  265. //$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";
  266. echo $url2."<br/>";
  267. ?>
  268.  
  269.  
Success #stdin #stdout 0.03s 26336KB
stdin
Standard input is empty
stdout
hello<br/>http://z...content-available-to-author-only...d.net/i/draco/shows/Zindagi/Khwahishein/January/21012015/Khwahishein_Episode_13.smil/master.m3u8?hdnea=st=1423077479~exp=1423086479~acl=*~hmac=62e232ca54cfb199a5d06f6c698cd866ba66551e51e2529c85f3f6e92b08b16e<br/>