Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用token防盗链时,添加生成防盗链_upt参数的方法 #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Upyun/Config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Upyun;

/**
Expand Down Expand Up @@ -72,6 +73,17 @@ class Config
*/
private $formApiKey;

/**
* @var string token防盗链使用的与CDN平台约定的签名密钥
* https://help.upyun.com/knowledge-base/cdn-token-limite
*/
private $uptSecret;

/**
* @var string 防盗链绑定的过期时间
*/
private $uptExpiration = 1800;

/**
* @var string rest api 和 form api 的接口地址
*/
Expand Down Expand Up @@ -159,4 +171,24 @@ public function setConcurrency($concurrency)
{
$this->concurrency = $concurrency;
}

public function setUptSecret($secret)
{
$this->uptSecret = $secret;
}

public function getUptSecret()
{
return $this->uptSecret;
}

public function setUptExpiration($expiration)
{
$this->uptExpiration = $expiration;
}

public function getUptExpiration()
{
return $this->uptExpiration;
}
}
34 changes: 34 additions & 0 deletions src/Upyun/Signature.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Upyun;

/**
Expand Down Expand Up @@ -95,4 +96,37 @@ public static function getBodySignature(Config $serviceConfig, $method, $uri, $d
$signature = base64_encode(hash_hmac('sha1', implode('&', $data), $serviceConfig->operatorPassword, true));
return 'UPYUN ' . $serviceConfig->operatorName . ':' . $signature;
}

/**
* 获取防盗链token,前提是已开启token防盗链
* https://help.upyun.com/knowledge-base/cdn-token-limite
* 使用防盗链生成token时,需要提前设置secret
* Config.php里的setUptSecret()方法
* @param Config $serviceConfig
* @param $uri 请求路径
*
* @return string _upt参数的值
*/
public static function getUptToken(Config $serviceConfig, $uri)
{
$etime = time() + $serviceConfig->getUptExpiration();
$sign = md5("{$serviceConfig->getUptSecret()}&{$etime}&{$uri}");
$upt = substr($sign, 12, 8) . $etime;
return $upt;
}

/**
* 根据传入的URL直接处理成携带upt参数的URL
* @param Config $serviceConfig
* @param $method 请求方法
* @param $url 访问URL
*
* @return string 携带upt参数的URL
*/
public static function getUptUrl(Config $serviceConfig, $url)
{
$urlObj = parse_url($url);
$upt = self::getUptToken($serviceConfig, $urlObj['path']);
return $urlObj['scheme'] . $urlObj['host'] . '?_upt=' . $upt;
}
}