Completed
Push — master ( 2144bf...4c4f9b )
by
unknown
21:02 queued 36s
created

Auth::authorizationV2()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 64
nop 4
dl 0
loc 46
ccs 0
cts 30
cp 0
crap 56
rs 8.2448
c 0
b 0
f 0
1
<?php
2
namespace Qiniu;
3
4
use Qiniu\Zone;
5
6
final class Auth
7
{
8
    private $accessKey;
9
    private $secretKey;
10
11
    public function __construct($accessKey, $secretKey)
12
    {
13
        $this->accessKey = $accessKey;
14
        $this->secretKey = $secretKey;
15
    }
16
17 6
    public function getAccessKey()
18
    {
19 6
        return $this->accessKey;
20
    }
21
22 93
    public function sign($data)
23
    {
24 93
        $hmac = hash_hmac('sha1', $data, $this->secretKey, true);
25 93
        return $this->accessKey . ':' . \Qiniu\base64_urlSafeEncode($hmac);
26
    }
27
28 24
    public function signWithData($data)
29
    {
30 24
        $encodedData = \Qiniu\base64_urlSafeEncode($data);
31 24
        return $this->sign($encodedData) . ':' . $encodedData;
32
    }
33
34 54
    public function signRequest($urlString, $body, $contentType = null)
35
    {
36 54
        $url = parse_url($urlString);
37 54
        $data = '';
38 54
        if (array_key_exists('path', $url)) {
39 51
            $data = $url['path'];
40 51
        }
41 54
        if (array_key_exists('query', $url)) {
42 9
            $data .= '?' . $url['query'];
43 9
        }
44 54
        $data .= "\n";
45
46 54
        if ($body !== null && $contentType === 'application/x-www-form-urlencoded') {
47 24
            $data .= $body;
48 24
        }
49 54
        return $this->sign($data);
50
    }
51
52
    public function verifyCallback($contentType, $originAuthorization, $url, $body)
53
    {
54
        $authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
55
        return $originAuthorization === $authorization;
56
    }
57
58 12
    public function privateDownloadUrl($baseUrl, $expires = 3600)
59
    {
60 12
        $deadline = time() + $expires;
61
62 12
        $pos = strpos($baseUrl, '?');
63 12
        if ($pos !== false) {
64 9
            $baseUrl .= '&e=';
65 9
        } else {
66 3
            $baseUrl .= '?e=';
67
        }
68 12
        $baseUrl .= $deadline;
69
70 12
        $token = $this->sign($baseUrl);
71 12
        return "$baseUrl&token=$token";
72
    }
73
74 21
    public function uploadToken($bucket, $key = null, $expires = 3600, $policy = null, $strictPolicy = true)
75
    {
76 21
        $deadline = time() + $expires;
77 21
        $scope = $bucket;
78 21
        if ($key !== null) {
79 15
            $scope .= ':' . $key;
80 15
        }
81
82 21
        $args = self::copyPolicy($args, $policy, $strictPolicy);
83 21
        $args['scope'] = $scope;
84 21
        $args['deadline'] = $deadline;
85
86 21
        $b = json_encode($args);
87 21
        return $this->signWithData($b);
88
    }
89
90
    /**
91
     *上传策略,参数规格详见
92
     *http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
93
     */
94
    private static $policyFields = array(
95
        'callbackUrl',
96
        'callbackBody',
97
        'callbackHost',
98
        'callbackBodyType',
99
        'callbackFetchKey',
100
101
        'returnUrl',
102
        'returnBody',
103
104
        'endUser',
105
        'saveKey',
106
        'insertOnly',
107
108
        'detectMime',
109
        'mimeLimit',
110
        'fsizeMin',
111
        'fsizeLimit',
112
113
        'persistentOps',
114
        'persistentNotifyUrl',
115
        'persistentPipeline',
116
117
        'deleteAfterDays',
118
        'fileType',
119
        'isPrefixalScope',
120
    );
121
122 21
    private static function copyPolicy(&$policy, $originPolicy, $strictPolicy)
123
    {
124 21
        if ($originPolicy === null) {
125 18
            return array();
126
        }
127 3
        foreach ($originPolicy as $key => $value) {
128 3
            if (!$strictPolicy || in_array((string)$key, self::$policyFields, true)) {
129 3
                $policy[$key] = $value;
130 3
            }
131 3
        }
132 3
        return $policy;
133
    }
134
135 51
    public function authorization($url, $body = null, $contentType = null)
136
    {
137 51
        $authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
138 51
        return array('Authorization' => $authorization);
139
    }
140
141
    public function authorizationV2($url, $method, $body = null, $contentType = null)
142
    {
143
        $urlItems = parse_url($url);
144
        $host = $urlItems['host'];
145
146
        if (isset($urlItems['port'])) {
147
            $port = $urlItems['port'];
148
        } else {
149
            $port = '';
150
        }
151
152
        $path = $urlItems['path'];
153
        if (isset($urlItems['query'])) {
154
            $query = $urlItems['query'];
155
        } else {
156
            $query = '';
157
        }
158
159
        //write request uri
160
        $toSignStr = $method . ' ' . $path;
161
        if (!empty($query)) {
162
            $toSignStr .= '?' . $query;
163
        }
164
165
        //write host and port
166
        $toSignStr .= "\nHost: " . $host;
167
        if (!empty($port)) {
168
            $toSignStr .= ":" . $port;
169
        }
170
171
        //write content type
172
        if (!empty($contentType)) {
173
            $toSignStr .= "\nContent-Type: " . $contentType;
174
        }
175
176
        $toSignStr .= "\n\n";
177
178
        //write body
179
        if (!empty($body)) {
180
            $toSignStr .= $body;
181
        }
182
183
        $sign = $this->sign($toSignStr);
184
        $auth = 'Qiniu ' . $sign;
185
        return array('Authorization' => $auth);
186
    }
187
}
188