人人商城

auth.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. $appId = Conf::APP_ID;
  21. $secretId = Conf::SECRET_ID;
  22. $secretKey = Conf::SECRET_KEY;
  23. if (empty($appId) || empty($secretId) || empty($secretKey)) {
  24. return self::AUTH_SECRET_ID_KEY_ERROR;
  25. }
  26. if (empty($filepath)) {
  27. return self::createSignature($appId, $secretId, $secretKey, $expiration, $bucket, null);
  28. } else {
  29. if (preg_match('/^\//', $filepath) == 0) {
  30. $filepath = '/' . $filepath;
  31. }
  32. return self::createSignature($appId, $secretId, $secretKey, $expiration, $bucket, $filepath);
  33. }
  34. }
  35. /**
  36. * Create nonreusable signature for delete $filepath in $bucket.
  37. * This signature will expire after single usage.
  38. * Return the signature on success.
  39. * Return error code if parameter is not valid.
  40. */
  41. public static function createNonreusableSignature($bucket, $filepath) {
  42. $appId = Conf::APP_ID;
  43. $secretId = Conf::SECRET_ID;
  44. $secretKey = Conf::SECRET_KEY;
  45. if (empty($appId) || empty($secretId) || empty($secretKey)) {
  46. return self::AUTH_SECRET_ID_KEY_ERROR;
  47. }
  48. if (preg_match('/^\//', $filepath) == 0) {
  49. $filepath = '/' . $filepath;
  50. }
  51. $fileId = '/' . $appId . '/' . $bucket . $filepath;
  52. return self::createSignature($appId, $secretId, $secretKey, 0, $bucket, $fileId);
  53. }
  54. /**
  55. * A helper function for creating signature.
  56. * Return the signature on success.
  57. * Return error code if parameter is not valid.
  58. */
  59. private static function createSignature(
  60. $appId, $secretId, $secretKey, $expiration, $bucket, $fileId) {
  61. if (empty($secretId) || empty($secretKey)) {
  62. return self::AUTH_SECRET_ID_KEY_ERROR;
  63. }
  64. $now = time();
  65. $random = rand();
  66. $plainText = "a=$appId&k=$secretId&e=$expiration&t=$now&r=$random&f=$fileId&b=$bucket";
  67. $bin = hash_hmac('SHA1', $plainText, $secretKey, true);
  68. $bin = $bin.$plainText;
  69. $signature = base64_encode($bin);
  70. return $signature;
  71. }
  72. }