No Description

AuthTest.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // Hack to override the time returned from the S3SignatureV4
  3. // @codingStandardsIgnoreStart
  4. namespace Qiniu {
  5. function time()
  6. {
  7. return isset($_SERVER['override_qiniu_auth_time'])
  8. ? 1234567890
  9. : \time();
  10. }
  11. }
  12. namespace Qiniu\Tests {
  13. use Qiniu\Auth;
  14. use Qiniu\Http\Header;
  15. // @codingStandardsIgnoreEnd
  16. class AuthTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testSign()
  19. {
  20. global $dummyAuth;
  21. $token = $dummyAuth->sign('test');
  22. $this->assertEquals('abcdefghklmnopq:mSNBTR7uS2crJsyFr2Amwv1LaYg=', $token);
  23. }
  24. public function testSignWithData()
  25. {
  26. global $dummyAuth;
  27. $token = $dummyAuth->signWithData('test');
  28. $this->assertEquals('abcdefghklmnopq:-jP8eEV9v48MkYiBGs81aDxl60E=:dGVzdA==', $token);
  29. }
  30. public function testSignRequest()
  31. {
  32. global $dummyAuth;
  33. $token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', '');
  34. $this->assertEquals('abcdefghklmnopq:cFyRVoWrE3IugPIMP5YJFTO-O-Y=', $token);
  35. $ctype = 'application/x-www-form-urlencoded';
  36. $token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', $ctype);
  37. $this->assertEquals($token, 'abcdefghklmnopq:svWRNcacOE-YMsc70nuIYdaa1e4=');
  38. }
  39. public function testPrivateDownloadUrl()
  40. {
  41. global $dummyAuth;
  42. $_SERVER['override_qiniu_auth_time'] = true;
  43. $url = $dummyAuth->privateDownloadUrl('http://www.qiniu.com?go=1');
  44. $expect = 'http://www.qiniu.com?go=1&e=1234571490&token=abcdefghklmnopq:8vzBeLZ9W3E4kbBLFLW0Xe0u7v4=';
  45. $this->assertEquals($expect, $url);
  46. unset($_SERVER['override_qiniu_auth_time']);
  47. }
  48. public function testUploadToken()
  49. {
  50. global $dummyAuth;
  51. $_SERVER['override_qiniu_auth_time'] = true;
  52. $token = $dummyAuth->uploadToken('1', '2', 3600, array('endUser' => 'y'));
  53. // @codingStandardsIgnoreStart
  54. $exp = 'abcdefghklmnopq:yyeexeUkPOROoTGvwBjJ0F0VLEo=:eyJlbmRVc2VyIjoieSIsInNjb3BlIjoiMToyIiwiZGVhZGxpbmUiOjEyMzQ1NzE0OTB9';
  55. // @codingStandardsIgnoreEnd
  56. $this->assertEquals($exp, $token);
  57. unset($_SERVER['override_qiniu_auth_time']);
  58. }
  59. public function testVerifyCallback()
  60. {
  61. }
  62. public function testSignQiniuAuthorization()
  63. {
  64. $auth = new Auth("ak", "sk");
  65. // ---
  66. $url = "";
  67. $method = "";
  68. $headers = new Header(array(
  69. "X-Qiniu-" => array("a"),
  70. "X-Qiniu" => array("b"),
  71. "Content-Type" => array("application/x-www-form-urlencoded"),
  72. ));
  73. $body = "{\"name\": \"test\"}";
  74. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  75. $this->assertNull($err);
  76. $this->assertEquals("ak:0i1vKClRDWFyNkcTFzwcE7PzX74=", $sign);
  77. // ---
  78. $url = "";
  79. $method = "";
  80. $headers = new Header(array(
  81. "Content-Type" => array("application/json"),
  82. ));
  83. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  84. $this->assertNull($err);
  85. $this->assertEquals("ak:K1DI0goT05yhGizDFE5FiPJxAj4=", $sign);
  86. // ---
  87. $url = "";
  88. $method = "GET";
  89. $headers = new Header(array(
  90. "X-Qiniu-" => array("a"),
  91. "X-Qiniu" => array("b"),
  92. "Content-Type" => array("application/x-www-form-urlencoded"),
  93. ));
  94. $body = "{\"name\": \"test\"}";
  95. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  96. $this->assertNull($err);
  97. $this->assertEquals("ak:0i1vKClRDWFyNkcTFzwcE7PzX74=", $sign);
  98. // ---
  99. $url = "";
  100. $method = "POST";
  101. $headers = new Header(array(
  102. "Content-Type" => array("application/json"),
  103. "X-Qiniu" => array("b"),
  104. ));
  105. $body = "{\"name\": \"test\"}";
  106. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  107. $this->assertNull($err);
  108. $this->assertEquals("ak:0ujEjW_vLRZxebsveBgqa3JyQ-w=", $sign);
  109. // ---
  110. $url = "http://upload.qiniup.com";
  111. $method = "";
  112. $headers = new Header(array(
  113. "X-Qiniu-" => array("a"),
  114. "X-Qiniu" => array("b"),
  115. "Content-Type" => array("application/x-www-form-urlencoded"),
  116. ));
  117. $body = "{\"name\": \"test\"}";
  118. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  119. $this->assertNull($err);
  120. $this->assertEquals("ak:GShw5NitGmd5TLoo38nDkGUofRw=", $sign);
  121. // ---
  122. $url = "http://upload.qiniup.com";
  123. $method = "";
  124. $headers = new Header(array(
  125. "Content-Type" => array("application/json"),
  126. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  127. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  128. "X-Qiniu-" => array("a"),
  129. "X-Qiniu" => array("b"),
  130. ));
  131. $body = "{\"name\": \"test\"}";
  132. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  133. $this->assertNull($err);
  134. $this->assertEquals("ak:DhNA1UCaBqSHCsQjMOLRfVn63GQ=", $sign);
  135. // ---
  136. $url = "http://upload.qiniup.com";
  137. $method = "";
  138. $headers = new Header(array(
  139. "Content-Type" => array("application/x-www-form-urlencoded"),
  140. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  141. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  142. "X-Qiniu-" => array("a"),
  143. "X-Qiniu" => array("b"),
  144. ));
  145. $body = "name=test&language=go";
  146. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  147. $this->assertNull($err);
  148. $this->assertEquals("ak:KUAhrYh32P9bv0COD8ugZjDCmII=", $sign);
  149. // ---
  150. $url = "http://upload.qiniup.com";
  151. $method = "";
  152. $headers = new Header(array(
  153. "Content-Type" => array("application/x-www"),
  154. "Content-Type" => array("application/x-www-form-urlencoded"),
  155. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  156. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  157. ));
  158. $body = "name=test&language=go";
  159. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  160. $this->assertNull($err);
  161. $this->assertEquals("ak:KUAhrYh32P9bv0COD8ugZjDCmII=", $sign);
  162. // ---
  163. $url = "http://upload.qiniup.com/mkfile/sdf.jpg";
  164. $method = "";
  165. $headers = new Header(array(
  166. "Content-Type" => array("application/x-www-form-urlencoded"),
  167. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  168. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  169. "X-Qiniu-" => array("a"),
  170. "X-Qiniu" => array("b"),
  171. ));
  172. $body = "name=test&language=go";
  173. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  174. $this->assertNull($err);
  175. $this->assertEquals("ak:fkRck5_LeyfwdkyyLk-hyNwGKac=", $sign);
  176. $url = "http://upload.qiniup.com/mkfile/sdf.jpg?s=er3&df";
  177. $method = "";
  178. $headers = new Header(array(
  179. "Content-Type" => array("application/x-www-form-urlencoded"),
  180. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  181. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  182. "X-Qiniu-" => array("a"),
  183. "X-Qiniu" => array("b"),
  184. ));
  185. $body = "name=test&language=go";
  186. list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
  187. $this->assertNull($err);
  188. $this->assertEquals("ak:PUFPWsEUIpk_dzUvvxTTmwhp3p4=", $sign);
  189. }
  190. }
  191. }