Completed
Pull Request — master (#267)
by
unknown
15:12 queued 02:03
created

Auth   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 58.5%

Importance

Changes 0
Metric Value
dl 0
loc 187
ccs 55
cts 94
cp 0.585
rs 10
c 0
b 0
f 0
wmc 28
lcom 1
cbo 0

12 Methods

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