<?php
/**
* 前端上传视频获取签名
* @author
*/
class Auth {
private $SecretId;
private $SecretKey;
public function init(){
$CI = &get_instance();
$this->SecretId = $CI->config->item('SecretId');
$this->SecretKey = $CI->config->item('SecretKey');
}
//调用这个方法
public function getAuth($params=array()){
// 获取前端过来的参数
$params = file_get_contents("php://input");
$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');
return $this->getAuthorization($method, $pathname, $query, $headers);
}
/**
* 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 = $this->SecretId;
$SecretKey = $this->SecretKey;
// 整理参数
!$query && ($query = array());
!$headers && ($headers = array());
$method = strtolower($method ? $method : 'get');
$pathname = $pathname ? $pathname : '/';
substr($pathname, 0, 1) != '/' && ($pathname = '/' . $pathname);
// 注意这里要过滤好允许什么样的操作
// if (!$this->isActionAllow($method, $pathname, $query, $headers)) {
// return 'action deny';
// }
// 签名有效起止时间
$now = time() - 1;
$expired = $now + 600; // 签名过期时刻,600 秒后
// 要用到的 Authorization 参数列表
$qSignAlgorithm = 'sha1';
$qAk = $SecretId;
$qSignTime = $now . ';' . $expired;
$qKeyTime = $now . ';' . $expired;
$qHeaderList = strtolower(implode(';',$this->getObjectKeys($headers)));
$qUrlParamList = strtolower(implode(';', $this->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, $this->obj2str($query), $this->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;
}
// 工具方法
public function getObjectKeys($obj)
{
$list = array_keys($obj);
sort($list);
return $list;
}
public function obj2str($obj)
{
$list = array();
$keyList = $this->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);
}
}
腾讯cos返给前端的签名
最新推荐文章于 2025-07-04 13:12:44 发布