1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Freyo\Flysystem\QcloudCOSv3\Client; |
4
|
|
|
|
5
|
|
|
class Auth |
6
|
|
|
{ |
7
|
|
|
const AUTH_URL_FORMAT_ERROR = -1; |
8
|
|
|
const AUTH_SECRET_ID_KEY_ERROR = -2; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* 生成多次有效签名函数(用于上传和下载资源,有效期内可重复对不同资源使用). |
12
|
|
|
* |
13
|
|
|
* @param int $expired 过期时间,unix时间戳 |
14
|
|
|
* @param string $bucketName 文件所在bucket |
15
|
|
|
* |
16
|
|
|
* @return string 签名 |
17
|
|
|
*/ |
18
|
10 |
|
public static function appSign($expired, $bucketName) |
19
|
|
|
{ |
20
|
10 |
|
$appId = Conf::getAppId(); |
21
|
10 |
|
$secretId = Conf::getSecretId(); |
22
|
10 |
|
$secretKey = Conf::getSecretKey(); |
23
|
|
|
|
24
|
10 |
|
if (empty($secretId) || empty($secretKey) || empty($appId)) { |
25
|
|
|
return self::AUTH_SECRET_ID_KEY_ERROR; |
26
|
|
|
} |
27
|
|
|
|
28
|
10 |
|
return self::appSignBase($appId, $secretId, $secretKey, $expired, null, $bucketName); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 生成单次有效签名函数(用于删除和更新指定fileId资源,使用一次即失效). |
33
|
|
|
* |
34
|
|
|
* @param string $bucketName 文件所在bucket |
35
|
|
|
* @param string $path |
36
|
|
|
* |
37
|
|
|
* @return string 签名 |
38
|
|
|
*/ |
39
|
4 |
|
public static function appSign_once($path, $bucketName) |
40
|
|
|
{ |
41
|
4 |
|
$appId = Conf::getAppId(); |
42
|
4 |
|
$secretId = Conf::getSecretId(); |
43
|
4 |
|
$secretKey = Conf::getSecretKey(); |
44
|
|
|
|
45
|
4 |
|
if (preg_match('/^\//', $path) == 0) { |
46
|
|
|
$path = '/'.$path; |
47
|
|
|
} |
48
|
4 |
|
$fileId = '/'.$appId.'/'.$bucketName.$path; |
49
|
|
|
|
50
|
4 |
|
if (empty($secretId) || empty($secretKey) || empty($appId)) { |
51
|
|
|
return self::AUTH_SECRET_ID_KEY_ERROR; |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
return self::appSignBase($appId, $secretId, $secretKey, 0, $fileId, $bucketName); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 生成绑定资源的多次有效签名. |
59
|
|
|
* |
60
|
|
|
* @param string $path 文件相对bucket的路径 /test/test.log 标识该bucket下test目录下的test.log文件 |
61
|
|
|
* @param string $bucketName bucket |
62
|
|
|
* @param int $expired 过期时间,unix时间戳 |
63
|
|
|
* |
64
|
|
|
* @return string 签名串 |
65
|
|
|
*/ |
66
|
|
|
public static function appSign_multiple($path, $bucketName, $expired) |
67
|
|
|
{ |
68
|
|
|
$appId = Conf::getAppId(); |
69
|
|
|
$secretId = Conf::getSecretId(); |
70
|
|
|
$secretKey = Conf::getSecretKey(); |
71
|
|
|
|
72
|
|
|
if (preg_match('/^\//', $path) == 0) { |
73
|
|
|
$path = '/'.$path; |
74
|
|
|
} |
75
|
|
|
$fileId = $path; |
76
|
|
|
|
77
|
|
|
if (empty($secretId) || empty($secretKey) || empty($appId)) { |
78
|
|
|
return self::AUTH_SECRET_ID_KEY_ERROR; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return self::appSignBase($appId, $secretId, $secretKey, $expired, $fileId, $bucketName); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* 签名函数(上传、下载会生成多次有效签名,删除资源会生成单次有效签名). |
86
|
|
|
* |
87
|
|
|
* @param string $appId |
88
|
|
|
* @param string $secretId |
89
|
|
|
* @param string $secretKey |
90
|
|
|
* @param int $expired 过期时间,unix时间戳 |
91
|
|
|
* @param string $fileId 文件路径,以 /{$appId}/{$bucketName} 开头 |
92
|
|
|
* @param string $bucketName 文件所在bucket |
93
|
|
|
* |
94
|
|
|
* @return string 签名 |
95
|
|
|
*/ |
96
|
14 |
|
private static function appSignBase($appId, $secretId, $secretKey, $expired, $fileId, $bucketName) |
97
|
|
|
{ |
98
|
14 |
|
if (empty($secretId) || empty($secretKey)) { |
99
|
|
|
return self::AUTH_SECRET_ID_KEY_ERROR; |
100
|
|
|
} |
101
|
|
|
|
102
|
14 |
|
$now = Cosapi::time(); |
103
|
14 |
|
$rdm = rand(); |
104
|
14 |
|
$plainText = "a=$appId&k=$secretId&e=$expired&t=$now&r=$rdm&f=$fileId&b=$bucketName"; |
105
|
14 |
|
$bin = hash_hmac('SHA1', $plainText, $secretKey, true); |
106
|
14 |
|
$bin = $bin.$plainText; |
107
|
|
|
|
108
|
14 |
|
$sign = base64_encode($bin); |
109
|
|
|
|
110
|
14 |
|
return $sign; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//end of script |
115
|
|
|
|