AuthTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 PHPUnit\Framework\TestCase;
  14. use Qiniu\Auth;
  15. use Qiniu\Http\Header;
  16. // @codingStandardsIgnoreEnd
  17. class AuthTest extends TestCase
  18. {
  19. public function testSign()
  20. {
  21. global $dummyAuth;
  22. $token = $dummyAuth->sign('test');
  23. $this->assertEquals('abcdefghklmnopq:mSNBTR7uS2crJsyFr2Amwv1LaYg=', $token);
  24. }
  25. public function testSignWithData()
  26. {
  27. global $dummyAuth;
  28. $token = $dummyAuth->signWithData('test');
  29. $this->assertEquals('abcdefghklmnopq:-jP8eEV9v48MkYiBGs81aDxl60E=:dGVzdA==', $token);
  30. }
  31. public function testSignRequest()
  32. {
  33. global $dummyAuth;
  34. $token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', '');
  35. $this->assertEquals('abcdefghklmnopq:cFyRVoWrE3IugPIMP5YJFTO-O-Y=', $token);
  36. $ctype = 'application/x-www-form-urlencoded';
  37. $token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', $ctype);
  38. $this->assertEquals($token, 'abcdefghklmnopq:svWRNcacOE-YMsc70nuIYdaa1e4=');
  39. }
  40. public function testPrivateDownloadUrl()
  41. {
  42. global $dummyAuth;
  43. $_SERVER['override_qiniu_auth_time'] = true;
  44. $url = $dummyAuth->privateDownloadUrl('http://www.qiniu.com?go=1');
  45. $expect = 'http://www.qiniu.com?go=1&e=1234571490&token=abcdefghklmnopq:8vzBeLZ9W3E4kbBLFLW0Xe0u7v4=';
  46. $this->assertEquals($expect, $url);
  47. unset($_SERVER['override_qiniu_auth_time']);
  48. }
  49. public function testUploadToken()
  50. {
  51. global $dummyAuth;
  52. $_SERVER['override_qiniu_auth_time'] = true;
  53. $token = $dummyAuth->uploadToken('1', '2', 3600, array('endUser' => 'y'));
  54. // @codingStandardsIgnoreStart
  55. $exp = 'abcdefghklmnopq:yyeexeUkPOROoTGvwBjJ0F0VLEo=:eyJlbmRVc2VyIjoieSIsInNjb3BlIjoiMToyIiwiZGVhZGxpbmUiOjEyMzQ1NzE0OTB9';
  56. // @codingStandardsIgnoreEnd
  57. $this->assertEquals($exp, $token);
  58. unset($_SERVER['override_qiniu_auth_time']);
  59. }
  60. public function testSignQiniuAuthorization()
  61. {
  62. $auth = new Auth("ak", "sk");
  63. $testCases = array(
  64. array(
  65. "url" => "",
  66. "method" => "",
  67. "headers" => array(
  68. "X-Qiniu-" => array("a"),
  69. "X-Qiniu" => array("b"),
  70. "Content-Type" => array("application/x-www-form-urlencoded")
  71. ),
  72. "body" => "{\"name\": \"test\"}",
  73. "expectedToken" => "ak:0i1vKClRDWFyNkcTFzwcE7PzX74="
  74. ),
  75. array(
  76. "url" => "",
  77. "method" => "",
  78. "headers" => array(
  79. "Content-Type" => array("application/json")
  80. ),
  81. "body" => "{\"name\": \"test\"}",
  82. "expectedToken" => "ak:K1DI0goT05yhGizDFE5FiPJxAj4="
  83. ),
  84. array(
  85. "url" => "",
  86. "method" => "GET",
  87. "headers" => array(
  88. "X-Qiniu-" => array("a"),
  89. "X-Qiniu" => array("b"),
  90. "Content-Type" => array("application/x-www-form-urlencoded"),
  91. ),
  92. "body" => "{\"name\": \"test\"}",
  93. "expectedToken" => "ak:0i1vKClRDWFyNkcTFzwcE7PzX74="
  94. ),
  95. array(
  96. "url" => "",
  97. "method" => "POST",
  98. "headers" => array(
  99. "Content-Type" => array("application/json"),
  100. "X-Qiniu" => array("b"),
  101. ),
  102. "body" => "{\"name\": \"test\"}",
  103. "expectedToken" => "ak:0ujEjW_vLRZxebsveBgqa3JyQ-w="
  104. ),
  105. array(
  106. "url" => "http://upload.qiniup.com",
  107. "method" => "",
  108. "headers" => array(
  109. "X-Qiniu-" => array("a"),
  110. "X-Qiniu" => array("b"),
  111. "Content-Type" => array("application/x-www-form-urlencoded"),
  112. ),
  113. "body" => "{\"name\": \"test\"}",
  114. "expectedToken" => "ak:GShw5NitGmd5TLoo38nDkGUofRw="
  115. ),
  116. array(
  117. "url" => "http://upload.qiniup.com",
  118. "method" => "",
  119. "headers" => array(
  120. "Content-Type" => array("application/json"),
  121. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  122. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  123. "X-Qiniu-" => array("a"),
  124. "X-Qiniu" => array("b"),
  125. ),
  126. "body" => "{\"name\": \"test\"}",
  127. "expectedToken" => "ak:DhNA1UCaBqSHCsQjMOLRfVn63GQ="
  128. ),
  129. array(
  130. "url" => "http://upload.qiniup.com",
  131. "method" => "",
  132. "headers" => array(
  133. "Content-Type" => array("application/x-www-form-urlencoded"),
  134. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  135. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  136. "X-Qiniu-" => array("a"),
  137. "X-Qiniu" => array("b"),
  138. ),
  139. "body" => "name=test&language=go",
  140. "expectedToken" => "ak:KUAhrYh32P9bv0COD8ugZjDCmII="
  141. ),
  142. array(
  143. "url" => "http://upload.qiniup.com",
  144. "method" => "",
  145. "headers" => array(
  146. "Content-Type" => array("application/x-www"),
  147. "Content-Type" => array("application/x-www-form-urlencoded"),
  148. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  149. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  150. ),
  151. "body" => "name=test&language=go",
  152. "expectedToken" => "ak:KUAhrYh32P9bv0COD8ugZjDCmII="
  153. ),
  154. array(
  155. "url" => "http://upload.qiniup.com/mkfile/sdf.jpg",
  156. "method" => "",
  157. "headers" => array(
  158. "Content-Type" => array("application/x-www-form-urlencoded"),
  159. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  160. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  161. "X-Qiniu-" => array("a"),
  162. "X-Qiniu" => array("b"),
  163. ),
  164. "body" => "name=test&language=go",
  165. "expectedToken" => "ak:fkRck5_LeyfwdkyyLk-hyNwGKac="
  166. ),
  167. array(
  168. "url" => "http://upload.qiniup.com/mkfile/sdf.jpg?s=er3&df",
  169. "method" => "",
  170. "headers" => array(
  171. "Content-Type" => array("application/x-www-form-urlencoded"),
  172. "X-Qiniu-Bbb" => array("BBB", "AAA"),
  173. "X-Qiniu-Aaa" => array("DDD", "CCC"),
  174. "X-Qiniu-" => array("a"),
  175. "X-Qiniu" => array("b"),
  176. ),
  177. "body" => "name=test&language=go",
  178. "expectedToken" => "ak:PUFPWsEUIpk_dzUvvxTTmwhp3p4="
  179. )
  180. );
  181. foreach ($testCases as $testCase) {
  182. list($sign, $err) = $auth->signQiniuAuthorization(
  183. $testCase["url"],
  184. $testCase["method"],
  185. $testCase["body"],
  186. new Header($testCase["headers"])
  187. );
  188. $this->assertNull($err);
  189. $this->assertEquals($testCase["expectedToken"], $sign);
  190. }
  191. }
  192. public function testDisableQiniuTimestampSignatureDefault()
  193. {
  194. $auth = new Auth("ak", "sk");
  195. $authedHeaders = $auth->authorizationV2("https://example.com", "GET");
  196. $this->assertArrayHasKey("X-Qiniu-Date", $authedHeaders);
  197. }
  198. public function testDisableQiniuTimestampSignature()
  199. {
  200. $auth = new Auth("ak", "sk", array(
  201. "disableQiniuTimestampSignature" => true
  202. ));
  203. $authedHeaders = $auth->authorizationV2("https://example.com", "GET");
  204. $this->assertArrayNotHasKey("X-Qiniu-Date", $authedHeaders);
  205. }
  206. public function testDisableQiniuTimestampSignatureEnv()
  207. {
  208. putenv("DISABLE_QINIU_TIMESTAMP_SIGNATURE=true");
  209. $auth = new Auth("ak", "sk");
  210. $authedHeaders = $auth->authorizationV2("https://example.com", "GET");
  211. $this->assertArrayNotHasKey("X-Qiniu-Date", $authedHeaders);
  212. putenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE');
  213. }
  214. public function testDisableQiniuTimestampSignatureEnvBeIgnored()
  215. {
  216. putenv("DISABLE_QINIU_TIMESTAMP_SIGNATURE=true");
  217. $auth = new Auth("ak", "sk", array(
  218. "disableQiniuTimestampSignature" => false
  219. ));
  220. $authedHeaders = $auth->authorizationV2("https://example.com", "GET");
  221. $this->assertArrayHasKey("X-Qiniu-Date", $authedHeaders);
  222. putenv('DISABLE_QINIU_TIMESTAMP_SIGNATURE');
  223. }
  224. }
  225. }