1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Iidestiny\Flysystem\Oss\Plugins; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use League\Flysystem\Plugin\AbstractPlugin; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
class Verify extends AbstractPlugin |
10
|
|
|
{ |
11
|
|
|
public function getMethod() |
12
|
|
|
{ |
13
|
|
|
return 'verify'; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* 验签 |
18
|
|
|
* |
19
|
|
|
* @return false|string |
20
|
|
|
* @throws Exception |
21
|
|
|
*/ |
22
|
|
|
public function handle() |
23
|
|
|
{ |
24
|
|
|
// oss 前面header、公钥 header |
25
|
|
|
$authorizationBase64 = ""; |
26
|
|
|
$pubKeyUrlBase64 = ""; |
27
|
|
|
|
28
|
|
|
if (isset($_SERVER['HTTP_AUTHORIZATION'])) { |
29
|
|
|
$authorizationBase64 = $_SERVER['HTTP_AUTHORIZATION']; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (isset($_SERVER['HTTP_X_OSS_PUB_KEY_URL'])) { |
33
|
|
|
$pubKeyUrlBase64 = $_SERVER['HTTP_X_OSS_PUB_KEY_URL']; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// 验证失败 |
37
|
|
|
if ($authorizationBase64 == '' || $pubKeyUrlBase64 == '') { |
38
|
|
|
throw new Exception('403 Forbidden', 403); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// 获取OSS的签名 |
42
|
|
|
$authorization = base64_decode($authorizationBase64); |
43
|
|
|
// 获取公钥 |
44
|
|
|
$pubKeyUrl = base64_decode($pubKeyUrlBase64); |
45
|
|
|
// 请求验证 |
46
|
|
|
$ch = curl_init(); |
47
|
|
|
curl_setopt($ch, CURLOPT_URL, $pubKeyUrl); |
|
|
|
|
48
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
49
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); |
50
|
|
|
$pubKey = curl_exec($ch); |
|
|
|
|
51
|
|
|
|
52
|
|
|
if ($pubKey == "") { |
53
|
|
|
throw new Exception('403 Forbidden', 403); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// 获取回调 body |
57
|
|
|
$body = file_get_contents('php://input'); |
58
|
|
|
// 拼接待签名字符串 |
59
|
|
|
$path = $_SERVER['REQUEST_URI']; |
60
|
|
|
$pos = strpos($path, '?'); |
61
|
|
|
if ($pos === false) { |
62
|
|
|
$authStr = urldecode($path) . "\n" . $body; |
63
|
|
|
} else { |
64
|
|
|
$authStr = urldecode(substr($path, 0, $pos)) . substr($path, $pos, strlen($path) - $pos) . "\n" . $body; |
65
|
|
|
} |
66
|
|
|
// 验证签名 |
67
|
|
|
$ok = openssl_verify($authStr, $authorization, $pubKey, OPENSSL_ALGO_MD5); |
68
|
|
|
|
69
|
|
|
if ($ok !== 1) { |
70
|
|
|
throw new Exception('403 Forbidden', 403); |
71
|
|
|
} |
72
|
|
|
header("Content-Type: application/json"); |
73
|
|
|
$data = ["Status" => "Ok"]; |
74
|
|
|
|
75
|
|
|
return json_encode($data); |
76
|
|
|
} |
77
|
|
|
} |