auth.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Signature create related functions for authenticating with cos system.
  4. */
  5. namespace qcloudcos;
  6. /**
  7. * Auth class for creating reusable or nonreusable signature.
  8. */
  9. class Auth {
  10. // Secret id or secret key is not valid.
  11. const AUTH_SECRET_ID_KEY_ERROR = -1;
  12. /**
  13. * Create reusable signature for listDirectory in $bucket or uploadFile into $bucket.
  14. * If $filepath is not null, this signature will be binded with this $filepath.
  15. * This signature will expire at $expiration timestamp.
  16. * Return the signature on success.
  17. * Return error code if parameter is not valid.
  18. */
  19. public static function createReusableSignature($expiration, $bucket, $filepath = null) {
  20. global $_W;
  21. $appId = !empty($_W['setting']['remote']['cos']['appid']) ?$_W['setting']['remote']['cos']['appid']: Conf::APP_ID;
  22. $secretId = !empty($_W['setting']['remote']['cos']['secretid']) ? $_W['setting']['remote']['cos']['secretid'] : Conf::SECRET_ID;
  23. $secretKey = !empty($_W['setting']['remote']['cos']['secretkey']) ? $_W['setting']['remote']['cos']['secretkey'] : Conf::SECRET_KEY;
  24. if (empty($appId) || empty($secretId) || empty($secretKey)) {
  25. return self::AUTH_SECRET_ID_KEY_ERROR;
  26. }
  27. if (empty($filepath)) {
  28. return self::createSignature($appId, $secretId, $secretKey, $expiration, $bucket, null);
  29. } else {
  30. if (preg_match('/^\//', $filepath) == 0) {
  31. $filepath = '/' . $filepath;
  32. }
  33. return self::createSignature($appId, $secretId, $secretKey, $expiration, $bucket, $filepath);
  34. }
  35. }
  36. /**
  37. * Create nonreusable signature for delete $filepath in $bucket.
  38. * This signature will expire after single usage.
  39. * Return the signature on success.
  40. * Return error code if parameter is not valid.
  41. */
  42. public static function createNonreusableSignature($bucket, $filepath) {
  43. global $_W;
  44. $appId = !empty($_W['setting']['remote']['cos']['appid']) ? $_W['setting']['remote']['cos']['appid'] : Conf::APP_ID;
  45. $secretId = !empty($_W['setting']['remote']['cos']['secretid']) ? $_W['setting']['remote']['cos']['secretid'] : Conf::SECRET_ID;
  46. $secretKey = !empty($_W['setting']['remote']['cos']['secretkey']) ? $_W['setting']['remote']['cos']['secretkey'] : Conf::SECRET_KEY;
  47. if (empty($appId) || empty($secretId) || empty($secretKey)) {
  48. return self::AUTH_SECRET_ID_KEY_ERROR;
  49. }
  50. if (preg_match('/^\//', $filepath) == 0) {
  51. $filepath = '/' . $filepath;
  52. }
  53. $fileId = '/' . $appId . '/' . $bucket . $filepath;
  54. return self::createSignature($appId, $secretId, $secretKey, 0, $bucket, $fileId);
  55. }
  56. /**
  57. * A helper function for creating signature.
  58. * Return the signature on success.
  59. * Return error code if parameter is not valid.
  60. */
  61. private static function createSignature(
  62. $appId, $secretId, $secretKey, $expiration, $bucket, $fileId) {
  63. if (empty($secretId) || empty($secretKey)) {
  64. return self::AUTH_SECRET_ID_KEY_ERROR;
  65. }
  66. $now = time();
  67. $random = rand();
  68. $plainText = "a=$appId&k=$secretId&e=$expiration&t=$now&r=$random&f=$fileId&b=$bucket";
  69. $bin = hash_hmac('SHA1', $plainText, $secretKey, true);
  70. $bin = $bin.$plainText;
  71. $signature = base64_encode($bin);
  72. return $signature;
  73. }
  74. }