-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
427 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# 允许操作的判断例子 | ||
|
||
以下按照 JavaScript 为例子,列举签名允许操作的判断规则 | ||
|
||
```js | ||
var exist = function (obj, key) { | ||
return obj[key] === undefined; | ||
}; | ||
``` | ||
|
||
## 分片上传 | ||
|
||
```js | ||
// multipartList 获取已有上传任务 | ||
if (pathname === '/' && method === 'get' && exist(query['uploads'])) allow = true; | ||
// multipartListPart 获取单个上传任务的分片列表 | ||
if (pathname !== '/' && method === 'get' && exist(query['uploadId'])) allow = true; | ||
// multipartInit 初始化分片上传 | ||
if (pathname !== '/' && method === 'post' && exist(query['uploads'])) allow = true; | ||
// multipartUpload 上传文件的单个分片 | ||
if (pathname !== '/' && method === 'put' && exist(query['uploadId']) && exist(query['partNumber'])) allow = true; | ||
// multipartComplete 完成一次分片上传 | ||
if (pathname !== '/' && method === 'post' && exist(query['uploadId'])) allow = true; | ||
``` | ||
|
||
## 简单上传 | ||
|
||
```js | ||
// putObject 简单上传文件 | ||
if (pathname !== '/' && method === 'put' && !exist(query['acl'])) allow = true; | ||
// postObject 允许表单上传文件 | ||
if (pathname === '/' && method === 'post' && !exist(query['delete'])) allow = true; | ||
``` | ||
|
||
## 获取和修改权限策略 | ||
|
||
```js | ||
// getBucketAcl 获取 Bucket 权限 | ||
if (pathname === '/' && method === 'get' && !exist(query['acl'])) allow = true; | ||
// putBucketAcl 修改 Bucket 权限 | ||
if (pathname === '/' && method === 'put' && !exist(query['acl'])) allow = true; | ||
// getBucketPolicy 获取权限策略 | ||
if (pathname === '/' && method === 'get' && !exist(query['policy'])) allow = true; | ||
// putBucketPolicy 修改权限策略 | ||
if (pathname === '/' && method === 'put' && !exist(query['policy'])) allow = true; | ||
// getObjectAcl 获取 Object 权限 | ||
if (pathname !== '/' && method === 'get' && !exist(query['acl'])) allow = true; | ||
// putObjectAcl 修改 Object 权限 | ||
if (pathname !== '/' && method === 'put' && !exist(query['acl'])) allow = true; | ||
``` | ||
|
||
## 获取和修改生命周期 | ||
|
||
```js | ||
// getBucketLifecycle 获取 Bucket Lifecycle | ||
if (pathname === '/' && method === 'get' && !exist(query['lifecycle'])) allow = true; | ||
// putBucketLifecycle 修改 Bucket Lifecycle | ||
if (pathname === '/' && method === 'put' && !exist(query['lifecycle'])) allow = true; | ||
``` | ||
|
||
## 获取和修改 Tagging | ||
|
||
```js | ||
// getBucketTagging 获取 Bucket Tagging | ||
if (pathname === '/' && method === 'get' && !exist(query['tagging'])) allow = true; | ||
// putBucketTagging 修改 Bucket Tagging | ||
if (pathname === '/' && method === 'put' && !exist(query['tagging'])) allow = true; | ||
// deleteBucketTagging 删除 Bucket Tagging | ||
if (pathname === '/' && method === 'delete' && !exist(query['tagging'])) allow = true; | ||
``` | ||
|
||
## 删除文件 | ||
|
||
```js | ||
// deleteMultipleObject 批量删除文件 | ||
if (pathname === '/' && method === 'post' && !exist(query['delete'])) allow = true; | ||
// deleteObject 删除单个文件 | ||
if (pathname !== '/' && method === 'delete') allow = true; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?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 = 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | ||
$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; | ||
} | ||
|
||
|
||
// 获取前端过来的参数 | ||
$params = json_decode(file_get_contents("php://input"), 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(); | ||
|
||
// 返回数据给前端 | ||
header('Content-Type: text/plain'); | ||
header('Allow-Control-Allow-Origin: http://127.0.0.1'); // 这里修改允许跨域访问的网站 | ||
header('Allow-Control-Allow-Headers: origin,accept,content-type'); | ||
echo getAuthorization($method, $pathname, $query, $headers); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.