Passed
Push — master ( 69bba3...05009d )
by Luo
02:26
created

Verify::handle()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 30
c 1
b 0
f 0
nc 24
nop 0
dl 0
loc 54
ccs 0
cts 38
cp 0
crap 72
rs 8.1954

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $pubKeyUrl);
Loading history...
48
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
49
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
50
        $pubKey = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $pubKey = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
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
}