Passed
Pull Request — master (#54)
by
unknown
02:04
created

Verify::handle()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 29
nc 24
nop 0
dl 0
loc 54
rs 8.2114
c 0
b 0
f 0

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 Jason\Flysystem\Oss\Plugins;
4
5
use League\Flysystem\Plugin\AbstractPlugin;
6
7
class Verify extends AbstractPlugin
8
{
9
10
    public function getMethod()
11
    {
12
        return 'verify';
13
    }
14
15
    /**
16
     * 验签.
17
     * @return array
18
     */
19
    public function handle()
20
    {
21
        // oss 前面header、公钥 header
22
        $authorizationBase64 = '';
23
        $pubKeyUrlBase64     = '';
24
25
        if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
26
            $authorizationBase64 = $_SERVER['HTTP_AUTHORIZATION'];
27
        }
28
29
        if (isset($_SERVER['HTTP_X_OSS_PUB_KEY_URL'])) {
30
            $pubKeyUrlBase64 = $_SERVER['HTTP_X_OSS_PUB_KEY_URL'];
31
        }
32
33
        // 验证失败
34
        if ('' == $authorizationBase64 || '' == $pubKeyUrlBase64) {
35
            return [false, ['CallbackFailed' => 'authorization or pubKeyUrl is null']];
36
        }
37
38
        // 获取OSS的签名
39
        $authorization = base64_decode($authorizationBase64);
40
        // 获取公钥
41
        $pubKeyUrl = base64_decode($pubKeyUrlBase64);
42
        // 请求验证
43
        $ch = curl_init();
44
        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

44
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $pubKeyUrl);
Loading history...
45
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
46
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
47
        $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

47
        $pubKey = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
48
49
        if ('' == $pubKey) {
50
            return [false, ['CallbackFailed' => 'curl is fail']];
51
        }
52
53
        // 获取回调 body
54
        $body = file_get_contents('php://input');
55
        // 拼接待签名字符串
56
        $path = $_SERVER['REQUEST_URI'];
57
        $pos  = strpos($path, '?');
58
        if (false === $pos) {
59
            $authStr = urldecode($path) . "\n" . $body;
60
        } else {
61
            $authStr = urldecode(substr($path, 0, $pos)) . substr($path, $pos, strlen($path) - $pos) . "\n" . $body;
62
        }
63
        // 验证签名
64
        $ok = openssl_verify($authStr, $authorization, $pubKey, OPENSSL_ALGO_MD5);
65
66
        if (1 !== $ok) {
67
            return [false, ['CallbackFailed' => 'verify is fail, Illegal data']];
68
        }
69
70
        parse_str($body, $data);
71
72
        return [true, $data];
73
    }
74
75
}
76