ethan: add 'php' server authorization

This commit is contained in:
ethan.zhang 2019-01-07 16:38:16 +08:00
parent 18578faa33
commit 231f5fa869
4 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,8 @@
<?php
// bucket操作公钥,可以在UCloud控制台查到
$UCLOUD_PUBLIC_KEY = '你的bucket操作公钥';
// bucket操作私钥,可以在UCloud控制台查到
$UCLOUD_PRIVATE_KEY = '你的bucket操作私钥';
?>

View File

@ -0,0 +1,23 @@
<?php
require_once("UCloud_Auth.php");
require_once("Auth_Config.php");
if ($_SERVER['REQUEST_METHOD'] == "GET") {
error_log("file address author : does not support 'HTTP-GET' method...", 3, "/tmp/php.log");
echo "file address author: does not support 'HTTP-GET' method...";
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$param_obj = json_decode(file_get_contents('php://input'));
$method=$param_obj->method;
$bucket=$param_obj->bucket;
$key=$param_obj->key;
$expires=$param_obj->expires;
$auth=new UCloud_Auth($UCLOUD_PUBLIC_KEY, $UCLOUD_PRIVATE_KEY);
echo $auth->signFileAddressRequest($method, $bucket, $key, $expires);
}
?>

View File

@ -0,0 +1,27 @@
<?php
require_once("UCloud_Auth.php");
require_once("Auth_Config.php");
if ($_SERVER['REQUEST_METHOD'] == "GET") {
error_log("server author : does not support 'HTTP-GET' method...", 3, "/tmp/php.log");
echo "does not support 'HTTP-GET' method...";
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$param_obj = json_decode(file_get_contents('php://input'));
$method=$param_obj->method;
$bucket=$param_obj->bucket;
$key=$param_obj->key;
$content_md5=$param_obj->content_md5;
$content_type=$param_obj->content_type;
$date=$param_obj->date;
$put_policy=$param_obj->put_policy;
$auth=new UCloud_Auth($UCLOUD_PUBLIC_KEY, $UCLOUD_PRIVATE_KEY);
echo $auth->signFileOperateRequest($method, $bucket, $key, $content_md5, $content_type, $date, $put_policy);
}
?>

View File

@ -0,0 +1,86 @@
<?php
function CanonicalizedResource($bucket, $key)
{
return "/" . $bucket . "/" . $key;
}
function CanonicalizedUCloudHeaders($headers)
{
$keys = array();
foreach($headers as $header) {
$header = trim($header);
$arr = explode(':', $header);
if (count($arr) < 2) continue;
list($k, $v) = $arr;
$k = strtolower($k);
if (strncasecmp($k, "x-ucloud") === 0) {
$keys[] = $k;
}
}
$c = '';
sort($keys, SORT_STRING);
foreach($keys as $k) {
$c .= $k . ":" . trim($headers[$v], " ") . "\n";
}
return $c;
}
class UCloud_Auth{
public $publicToken;
public $privateToken;
public function __construct($publicToken,$privateToken)
{
$this->publicToken = $publicToken;
$this->privateToken = $privateToken;
}
public function signFileOpeate($data,$put_policy)
{
$sign = base64_encode(hash_hmac('sha1', $data, $this->privateToken, true));
$singStr = "UCloud " . $this->publicToken . ":" . $sign;
// 上传回调put_policy
if ($put_policy) {
$policystr = base64_encode(str_replace('"','\\"',json_encode($put_policy)));
$singStr = $singStr . ":" . $policystr;
}
return $singStr;
}
/* 文件操作签名 */
public function signFileOperateRequest($method, $bucket, $key, $content_md5, $content_type, $date, $put_policy)
{
$data = '';
$data .= strtoupper($method) . "\n";
$data .= $content_md5 . "\n";
$data .= $content_type . "\n";
$data .= $date . "\n";
$data .= CanonicalizedResource($bucket, $key);
error_log($data, 3, "/tmp/php.log");
return $this->signFileOpeate($data, $put_policy);
}
/* 获取私有bucket下文件的 URL 签名 */
public function signFileAddressRequest($method,$bucket,$key,$expires)
{
$data = '';
$data .= strtoupper($method) . "\n";
$data .= "\n";
$data .= "\n";
$data .= $expires . "\n";
$data .= CanonicalizedResource($bucket, $key);
$sign = base64_encode(hash_hmac('sha1', $data, $this->privateToken, true));
return $sign;
}
}
?>