-
Notifications
You must be signed in to change notification settings - Fork 566
/
Copy pathauth-json.php
156 lines (128 loc) · 5.14 KB
/
auth-json.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* php 签名样例
*/
function isActionAllow($method, $pathname, $query, $headers)
{
$allow = true;
// // TODO 这里判断自己网站的登录态
// if ($!logined) {
// $allow = false;
// return $allow;
// }
// 请求可能带有点所有 action
// acl,cors,policy,location,tagging,lifecycle,versioning,replication,versions,delete,restore,uploads
// 请求跟路径,只允许获取 UploadId
if ($pathname === '/' && !($method === 'get' && isset($query['uploads']))) {
$allow = false;
}
// 不允许前端获取和修改文件权限
if ($pathname !== '/' && isset($query['acl'])) {
$allow = false;
}
// 这里应该根据需要,限制当前站点的用户只允许操作什么样的路径
if ($method === 'delete' && $pathname !== '/') { // 这里控制是否允许删除文件
// TODO 这里控制是否允许删除文件
}
if ($method === 'put' && $pathname !== '/') { // 这里控制是否允许上传和修改文件
// TODO 这里控制是否允许上传和修改文件
}
if ($method === 'get' && $pathname !== '/') { // 这里控制是否获取文件和文件相关信息
// TODO 这里控制是否允许获取文件和文件相关信息
}
return $allow;
}
/*
* 获取签名
* @param string $method 请求类型 method
* @param string $pathname 文件名称
* @param array $query query参数
* @param array $headers headers
* @return string 签名字符串
*/
function getAuthorization($method, $pathname, $query, $headers)
{
// 获取个人 API 密钥 https://console.qcloud.com/capi
$SecretId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$SecretKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// 整理参数
!$query && ($query = array());
!$headers && ($headers = array());
$method = strtolower($method ? $method : 'get');
$pathname = $pathname ? $pathname : '/';
substr($pathname, 0, 1) != '/' && ($pathname = '/' . $pathname);
// 注意这里要过滤好允许什么样的操作
if (!isActionAllow($method, $pathname, $query, $headers)) {
return 'action deny';
}
// 工具方法
function getObjectKeys($obj)
{
$list = array_keys($obj);
sort($list);
return $list;
}
function obj2str($obj)
{
$list = array();
$keyList = getObjectKeys($obj);
$len = count($keyList);
for ($i = 0; $i < $len; $i++) {
$key = $keyList[$i];
$val = isset($obj[$key]) ? $obj[$key] : '';
$key = strtolower($key);
$list[] = rawurlencode($key) . '=' . rawurlencode($val);
}
return implode('&', $list);
}
// 签名有效起止时间
$now = time() - 1;
$expired = $now + 600; // 签名过期时刻,600 秒后
// 要用到的 Authorization 参数列表
$qSignAlgorithm = 'sha1';
$qAk = $SecretId;
$qSignTime = $now . ';' . $expired;
$qKeyTime = $now . ';' . $expired;
$qHeaderList = strtolower(implode(';', getObjectKeys($headers)));
$qUrlParamList = strtolower(implode(';', getObjectKeys($query)));
// 签名算法说明文档:https://www.qcloud.com/document/product/436/7778
// 步骤一:计算 SignKey
$signKey = hash_hmac("sha1", $qKeyTime, $SecretKey);
// 步骤二:构成 FormatString
$formatString = implode("\n", array(strtolower($method), $pathname, obj2str($query), obj2str($headers), ''));
// 步骤三:计算 StringToSign
$stringToSign = implode("\n", array('sha1', $qSignTime, sha1($formatString), ''));
// 步骤四:计算 Signature
$qSignature = hash_hmac('sha1', $stringToSign, $signKey);
// 步骤五:构造 Authorization
$authorization = implode('&', array(
'q-sign-algorithm=' . $qSignAlgorithm,
'q-ak=' . $qAk,
'q-sign-time=' . $qSignTime,
'q-key-time=' . $qKeyTime,
'q-header-list=' . $qHeaderList,
'q-url-param-list=' . $qUrlParamList,
'q-signature=' . $qSignature
));
return $authorization;
}
// 获取前端过来的参数
$inputBody = file_get_contents("php://input");
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $inputBody){
$params = json_decode($inputBody, 1);
$pathname = isset($params['pathname']) ? $params['pathname'] : '/';
$method = isset($params['method']) ? $params['method'] : 'get';
$query = isset($params['query']) ? $params['query'] : array();
$headers = isset($params['headers']) ? $params['headers'] : array();
} else {
$pathname = isset($_GET['pathname']) ? $_GET['pathname'] : '/';
$method = isset($_GET['method']) ? $_GET['method'] : 'get';
$query = isset($_GET['query']) && $_GET['query'] ? json_decode($_GET['query'], 1) : array();
$headers = isset($_GET['headers']) && $_GET['headers'] ? json_decode($_GET['headers'], 1) : array();
}
// 返回数据给前端
header('Content-Type: text/plain');
header('Allow-Control-Allow-Origin: http://127.0.0.1'); // 这里修改允许跨域访问的网站
header('Allow-Control-Allow-Headers: origin,accept,content-type');
$sign = getAuthorization($method, $pathname, $query, $headers);
echo '{"sign":"' . $sign .'"}';