Completed
Pull Request — master (#248)
by
unknown
22:17
created

Auth::authorizationV2()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 26
nc 64
nop 4
dl 0
loc 46
ccs 0
cts 0
cp 0
crap 56
rs 6.7272
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(
75
        $bucket,
76
        $key = null,
77
        $expires = 3600,
78
        $policy = null,
79
        $strictPolicy = true
80
    )
81 21
    {
82 21
        $deadline = time() + $expires;
83 21
        $scope = $bucket;
84 15
        if ($key !== null) {
85 15
            $scope .= ':' . $key;
86
        }
87 21
88 21
        $args = self::copyPolicy($args, $policy, $strictPolicy);
89 21
        $args['scope'] = $scope;
90
        $args['deadline'] = $deadline;
91 21
92 21
        $b = json_encode($args);
93
        return $this->signWithData($b);
94
    }
95
96
    /**
97
     *上传策略,参数规格详见
98
     *http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
99
     */
100
    private static $policyFields = array(
101
        'callbackUrl',
102
        'callbackBody',
103
        'callbackHost',
104
        'callbackBodyType',
105
        'callbackFetchKey',
106
107
        'returnUrl',
108
        'returnBody',
109
110
        'endUser',
111
        'saveKey',
112
        'insertOnly',
113
114
        'detectMime',
115
        'mimeLimit',
116
        'fsizeMin',
117
        'fsizeLimit',
118
119
        'persistentOps',
120
        'persistentNotifyUrl',
121
        'persistentPipeline',
122
123
        'deleteAfterDays',
124
        'fileType',
125
        'isPrefixalScope',
126
    );
127 21
128
    private static function copyPolicy(&$policy, $originPolicy, $strictPolicy)
129 21
    {
130 18
        if ($originPolicy === null) {
131
            return array();
132 3
        }
133 3
        foreach ($originPolicy as $key => $value) {
134 3
            if (!$strictPolicy || in_array((string)$key, self::$policyFields, true)) {
135 3
                $policy[$key] = $value;
136 3
            }
137 3
        }
138
        return $policy;
139
    }
140 51
141
    public function authorization($url, $body = null, $contentType = null)
142 51
    {
143 51
        $authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
144
        return array('Authorization' => $authorization);
145
    }
146
147
    public function authorizationV2($url, $method, $body = null, $contentType = null)
148
    {
149
        $urlItems = parse_url($url);
150
        $host = $urlItems['host'];
151
152
        if (isset($urlItems['port'])) {
153
            $port = $urlItems['port'];
154
        } else {
155
            $port = '';
156
        }
157
158
        $path = $urlItems['path'];
159
        if (isset($urlItems['query'])) {
160
            $query = $urlItems['query'];
161
        } else {
162
            $query = '';
163
        }
164
165
        //write request uri
166
        $toSignStr = $method . ' ' . $path;
167
        if (!empty($query)) {
168
            $toSignStr .= '?' . $query;
169
        }
170
171
        //write host and port
172
        $toSignStr .= "\nHost: " . $host;
173
        if (!empty($port)) {
174
            $toSignStr .= ":" . $port;
175
        }
176
177
        //write content type
178
        if (!empty($contentType)) {
179
            $toSignStr .= "\nContent-Type: " . $contentType;
180
        }
181
182
        $toSignStr .= "\n\n";
183
184
        //write body
185
        if (!empty($body)) {
186
            $toSignStr .= $body;
187
        }
188
189
        $sign = $this->sign($toSignStr);
190
        $auth = 'Qiniu ' . $sign;
191
        return array('Authorization' => $auth);
192
    }
193
}
194