Advertisement
Guest User

Akamai Token Authentication Test

a guest
Dec 9th, 2019
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.93 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Amco\Security\Akamai;
  4.  
  5. /**
  6. * @author Jorge Matricali <jorgematricali@amco.mx>
  7. *
  8. * @group PLAYER
  9. * @group Security
  10. */
  11. class TokenAuthenticationTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testGeneration()
  14. {
  15. $generator = new TokenAuthentication('aabbccddeeff00112233445566778899');
  16. $token = $generator->generateToken();
  17. $this->assertNotEmpty($token);
  18. }
  19.  
  20. /**
  21. * @expectedException Amco\Security\Akamai\Exceptions\ParameterException
  22. */
  23. public function testInvalidAlgorithm()
  24. {
  25. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  26. $auth->setAlgorithm('inVaLid');
  27. }
  28.  
  29. public function testValidIPv4()
  30. {
  31. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  32. $this->assertEquals('', $auth->getIp());
  33. $this->assertEquals('', $auth->getIpField());
  34.  
  35. // Valid IPv4
  36. $auth->setIp('127.0.0.1');
  37. $this->assertEquals('127.0.0.1', $auth->getIp());
  38. $this->assertEquals('ip=127.0.0.1'.$auth->getFieldDelimiter(), $auth->getIpField());
  39. }
  40.  
  41. /**
  42. * @expectedException Amco\Security\Akamai\Exceptions\ParameterException
  43. */
  44. public function testInvalidIPv4()
  45. {
  46. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  47. $auth->setIp('127.0.0.300');
  48. }
  49.  
  50. public function testValidIPv6()
  51. {
  52. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  53. $this->assertEquals('', $auth->getIp());
  54. $this->assertEquals('', $auth->getIpField());
  55.  
  56. // Valid IPv4
  57. $auth->setIp('2001:0db8:85a3:08d3:1319:8a2e:0370:7334');
  58. $this->assertEquals('2001:0db8:85a3:08d3:1319:8a2e:0370:7334', $auth->getIp());
  59. $this->assertEquals('ip=2001:0db8:85a3:08d3:1319:8a2e:0370:7334'.$auth->getFieldDelimiter(), $auth->getIpField());
  60. }
  61.  
  62. /**
  63. * @expectedException Amco\Security\Akamai\Exceptions\ParameterException
  64. */
  65. public function testInvalidIPv6()
  66. {
  67. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  68. $auth->setIp('2001:0db8:85a3:08d3:xxxx:8a2e:0370:7334');
  69. }
  70.  
  71. public function testStartTime()
  72. {
  73. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  74. $gstv = new \ReflectionMethod('Amco\Security\Akamai\TokenAuthentication', 'getStartTimeValue');
  75. $gstv->setAccessible(true);
  76.  
  77. $this->assertEquals(0, $auth->getStartTime());
  78. $this->assertEquals(time(), $gstv->invoke($auth));
  79. $this->assertEquals('', $auth->getStartTimeField());
  80.  
  81. $auth->setStartTime('now');
  82. $this->assertEquals(time(), $auth->getStartTime());
  83. $this->assertEquals(time(), $gstv->invoke($auth));
  84. $this->assertEquals('st='.time().$auth->getFieldDelimiter(), $auth->getStartTimeField());
  85.  
  86. $auth->setStartTime(12345);
  87. $this->assertEquals(12345, $auth->getStartTime());
  88. $this->assertEquals(12345, $gstv->invoke($auth));
  89. $this->assertEquals('st='. 12345 .$auth->getFieldDelimiter(), $auth->getStartTimeField());
  90.  
  91. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  92. try {
  93. $auth->setStartTime('');
  94. } catch (\Exception $e1) {
  95. }
  96. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
  97. $this->assertEquals(0, $auth->getStartTime());
  98. $this->assertEquals(time(), $gstv->invoke($auth));
  99. $this->assertEquals('', $auth->getStartTimeField());
  100. }
  101.  
  102. public function testWindow()
  103. {
  104. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  105. // Default window time
  106. $this->assertEquals(300, $auth->getWindow());
  107.  
  108. $auth->setWindow(500);
  109. $this->assertEquals(500, $auth->getWindow());
  110.  
  111. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  112. try {
  113. $auth->setWindow('abc');
  114. } catch (\Exception $e1) {
  115. }
  116. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
  117. $this->assertEquals(300, $auth->getWindow());
  118.  
  119. try {
  120. $auth->setWindow(0);
  121. } catch (\Exception $e2) {
  122. }
  123. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
  124. $this->assertEquals(300, $auth->getWindow());
  125. }
  126.  
  127. public function testAcl()
  128. {
  129. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  130. $this->assertEquals('', $auth->getAcl());
  131. $this->assertEquals('acl=/*'.$auth->getFieldDelimiter(), $auth->getAclField());
  132.  
  133. $auth->setAcl('test');
  134. $this->assertEquals('test', $auth->getAcl());
  135. $this->assertEquals('acl=test'.$auth->getFieldDelimiter(), $auth->getAclField());
  136.  
  137. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  138. $auth->setUrl('https://example.com/protected/resource');
  139. $this->assertEquals('', $auth->getAclField()); // If we have an URL we shouldn't have an ACL
  140.  
  141. try {
  142. $auth->setAcl('test');
  143. } catch (\Exception $e1) {
  144. }
  145. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
  146. }
  147.  
  148. public function testUrl()
  149. {
  150. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  151. $this->assertEquals('', $auth->getUrl());
  152. $this->assertEquals('', $auth->getUrlField());
  153.  
  154. $auth->setUrl('https://example.com/protected/resource');
  155. $this->assertEquals('https://example.com/protected/resource', $auth->getUrl());
  156. $this->assertEquals('url=https://example.com/protected/resource'.$auth->getFieldDelimiter(), $auth->getUrlField());
  157.  
  158. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  159. $auth->setAcl('test');
  160. $this->assertEquals('', $auth->getUrlField()); // If we have an URL we shouldn't have an ACL
  161.  
  162. try {
  163. $auth->setUrl('https://example.com/');
  164. } catch (\Exception $e1) {
  165. }
  166. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
  167. }
  168.  
  169. public function testSession()
  170. {
  171. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  172. $this->assertEquals('', $auth->getSessionId());
  173. $this->assertEquals('', $auth->getSessionIdField());
  174.  
  175. $auth->setSessionId('e10adc3949ba59abbe56e057f20f883e');
  176. $this->assertEquals('e10adc3949ba59abbe56e057f20f883e', $auth->getSessionId());
  177. $this->assertEquals('id=e10adc3949ba59abbe56e057f20f883e'.$auth->getFieldDelimiter(), $auth->getSessionIdField());
  178.  
  179. $auth->setSessionId(123456778);
  180. $this->assertEquals('123456778', $auth->getSessionId());
  181. $this->assertEquals('id=123456778'.$auth->getFieldDelimiter(), $auth->getSessionIdField());
  182.  
  183. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  184. try {
  185. $auth->setSessionId(array());
  186. } catch (\Exception $e1) {
  187. }
  188. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
  189. $this->assertEquals('', $auth->getSessionId());
  190. $this->assertEquals('', $auth->getSessionIdField());
  191.  
  192. try {
  193. $auth->setSessionId(new \StdClass());
  194. } catch (\Exception $e2) {
  195. }
  196. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
  197. $this->assertEquals('', $auth->getSessionId());
  198. $this->assertEquals('', $auth->getSessionIdField());
  199. }
  200.  
  201. public function testData()
  202. {
  203. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  204. $this->assertEquals('', $auth->getData());
  205. $this->assertEquals('', $auth->getDataField());
  206.  
  207. $auth->setData('e10adc3949ba59abbe56e057f20f883e');
  208. $this->assertEquals('e10adc3949ba59abbe56e057f20f883e', $auth->getData());
  209. $this->assertEquals('data=e10adc3949ba59abbe56e057f20f883e'.$auth->getFieldDelimiter(), $auth->getDataField());
  210.  
  211. $auth->setData(123456778);
  212. $this->assertEquals('123456778', $auth->getData());
  213. $this->assertEquals('data=123456778'.$auth->getFieldDelimiter(), $auth->getDataField());
  214.  
  215. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  216. try {
  217. $auth->setData(array());
  218. } catch (\Exception $e1) {
  219. }
  220. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
  221. $this->assertEquals('', $auth->getData());
  222. $this->assertEquals('', $auth->getDataField());
  223.  
  224. try {
  225. $auth->setData(new \StdClass());
  226. } catch (\Exception $e2) {
  227. }
  228. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
  229. $this->assertEquals('', $auth->getData());
  230. $this->assertEquals('', $auth->getDataField());
  231. }
  232.  
  233. public function testSalt()
  234. {
  235. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  236. $this->assertEquals('', $auth->getSalt());
  237. $this->assertEquals('', $auth->getSaltField());
  238.  
  239. $auth->setSalt('s4lt');
  240. $this->assertEquals('s4lt', $auth->getSalt());
  241. $this->assertEquals('salt=s4lt'.$auth->getFieldDelimiter(), $auth->getSaltField());
  242.  
  243. $auth->setSalt(123456778);
  244. $this->assertEquals('123456778', $auth->getSalt());
  245. $this->assertEquals('salt=123456778'.$auth->getFieldDelimiter(), $auth->getSaltField());
  246.  
  247. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  248. try {
  249. $auth->setSalt(array());
  250. } catch (\Exception $e1) {
  251. }
  252. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
  253. $this->assertEquals('', $auth->getSalt());
  254. $this->assertEquals('', $auth->getSaltField());
  255.  
  256. try {
  257. $auth->setSalt(new \StdClass());
  258. } catch (\Exception $e2) {
  259. }
  260. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
  261. $this->assertEquals('', $auth->getSalt());
  262. $this->assertEquals('', $auth->getSaltField());
  263. }
  264.  
  265. public function testKey()
  266. {
  267. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  268. $this->assertEquals('aabbccddeeff00112233445566778899', $auth->getKey());
  269.  
  270. $auth->setKey('abcd1234');
  271. $this->assertEquals('abcd1234', $auth->getKey());
  272.  
  273. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  274. try {
  275. $auth->setKey('zxvft');
  276. } catch (\Exception $e1) {
  277. }
  278. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
  279. $this->assertEquals('aabbccddeeff00112233445566778899', $auth->getKey());
  280.  
  281. try {
  282. $auth->setKey('abcd1234f'); // Must be an even number of characters
  283. } catch (\Exception $e2) {
  284. }
  285. $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
  286. $this->assertEquals('aabbccddeeff00112233445566778899', $auth->getKey());
  287. }
  288.  
  289. public function testFieldDelimiter()
  290. {
  291. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  292. $this->assertEquals('~', $auth->getFieldDelimiter());
  293.  
  294. $auth->setFieldDelimiter('|');
  295. $this->assertEquals('|', $auth->getFieldDelimiter());
  296. }
  297.  
  298. public function testEarlyUrlEncoding()
  299. {
  300. $encode = new \ReflectionMethod('Amco\Security\Akamai\TokenAuthentication', 'encode');
  301. $encode->setAccessible(true);
  302.  
  303. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  304. $this->assertFalse($auth->getEarlyUrlEncoding());
  305. $this->assertEquals('test ', $encode->invoke($auth, 'test '));
  306.  
  307. $auth->setEarlyUrlEncoding(true);
  308. $this->assertTrue($auth->getEarlyUrlEncoding());
  309. $this->assertEquals('test%20', $encode->invoke($auth, 'test '));
  310. }
  311.  
  312. public function testSha256()
  313. {
  314. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  315. $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA256);
  316. $token = $auth->generateToken();
  317. $this->assertNotEmpty($token);
  318. }
  319.  
  320. public function testSha1()
  321. {
  322. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  323. $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA1);
  324. $token = $auth->generateToken();
  325. $this->assertNotEmpty($token);
  326. }
  327.  
  328. public function testMd5()
  329. {
  330. $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
  331. $auth->setAlgorithm(TokenAuthentication::ALGORITHM_MD5);
  332. $token = $auth->generateToken();
  333. $this->assertNotEmpty($token);
  334. }
  335.  
  336. public function testLegacySha256()
  337. {
  338. $auth = new TokenAuthentication('ae834f5dde69cd965f674606be7cd683');
  339. $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA256);
  340. $auth->setFieldDelimiter('&');
  341. $auth->setUrl('/multimediav81/plataforma_vod/MP4/201301/WMP4H00525MTDS_full/WMP4H00525MTDS_full_HLS.ism/WMP4H00525MTDS_full_HLS-audio=128000-video=2524000.m3u8');
  342. $token = $auth->generateCustomToken();
  343. $this->assertNotEmpty($token);
  344. }
  345.  
  346. public function testLegacySha1()
  347. {
  348. $auth = new TokenAuthentication('ae834f5dde69cd965f674606be7cd683');
  349. $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA1);
  350. $auth->setFieldDelimiter('&');
  351. $auth->setUrl('/multimediav81/plataforma_vod/MP4/201301/WMP4H00525MTDS_full/WMP4H00525MTDS_full_HLS.ism/WMP4H00525MTDS_full_HLS-audio=128000-video=2524000.m3u8');
  352. $token = $auth->generateCustomToken();
  353. $this->assertNotEmpty($token);
  354. }
  355.  
  356. public function testLegacyMd5()
  357. {
  358. $auth = new TokenAuthentication('ae834f5dde69cd965f674606be7cd683');
  359. $auth->setAlgorithm(TokenAuthentication::ALGORITHM_MD5);
  360. $auth->setFieldDelimiter('&');
  361. $auth->setUrl('/multimediav81/plataforma_vod/MP4/201301/WMP4H00525MTDS_full/WMP4H00525MTDS_full_HLS.ism/WMP4H00525MTDS_full_HLS-audio=128000-video=2524000.m3u8');
  362. $token = $auth->generateCustomToken();
  363. $this->assertNotEmpty($token);
  364. }
  365.  
  366. public function testLegacySha1IP()
  367. {
  368. $auth = new TokenAuthentication('ae834f5dde69cd965f674606be7cd683');
  369. $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA1);
  370. $auth->setFieldDelimiter('&');
  371. $auth->setIp('200.80.149.68');
  372. $auth->setUrl('/multimediav81/plataforma_vod/MP4/201301/WMP4H00525MTDS_full/WMP4H00525MTDS_full_HLS.ism/WMP4H00525MTDS_full_HLS-audio=128000-video=2524000.m3u8');
  373. $token = $auth->generateCustomToken();
  374. $this->assertNotEmpty($token);
  375. }
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement