Completed
Push — master ( 8c7ba7...1baeaf )
by
unknown
23:36 queued 22:16
created

Auth::sign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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 87
    public function sign($data)
23
    {
24 87
        $hmac = hash_hmac('sha1', $data, $this->secretKey, true);
25 87
        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 48
    public function signRequest($urlString, $body, $contentType = null)
35
    {
36 48
        $url = parse_url($urlString);
37 48
        $data = '';
38 48
        if (array_key_exists('path', $url)) {
39 45
            $data = $url['path'];
40 30
        }
41 48
        if (array_key_exists('query', $url)) {
42 9
            $data .= '?' . $url['query'];
43 6
        }
44 48
        $data .= "\n";
45
46 48
        if ($body !== null && $contentType === 'application/x-www-form-urlencoded') {
47 24
            $data .= $body;
48 16
        }
49 48
        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 6
        } 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
    {
82 21
        $deadline = time() + $expires;
83 21
        $scope = $bucket;
84 21
        if ($key !== null) {
85 15
            $scope .= ':' . $key;
86 10
        }
87
88 21
        $args = self::copyPolicy($args, $policy, $strictPolicy);
89 21
        $args['scope'] = $scope;
90 21
        $args['deadline'] = $deadline;
91
92 21
        $b = json_encode($args);
93 21
        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
128 21
    private static function copyPolicy(&$policy, $originPolicy, $strictPolicy)
129
    {
130 21
        if ($originPolicy === null) {
131 18
            return array();
132
        }
133 3
        foreach ($originPolicy as $key => $value) {
134 3
            if (!$strictPolicy || in_array((string)$key, self::$policyFields, true)) {
135 3
                $policy[$key] = $value;
136 2
            }
137 2
        }
138 3
        return $policy;
139
    }
140
141 45
    public function authorization($url, $body = null, $contentType = null)
142
    {
143 45
        $authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
144 45
        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