Completed
Push — master ( 09a10c...a1d7e5 )
by
unknown
08:01
created

Auth::verifyCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
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 34
        }
41 54
        if (array_key_exists('query', $url)) {
42 9
            $data .= '?' . $url['query'];
43 6
        }
44 54
        $data .= "\n";
45
46 54
        if ($body !== null && $contentType === 'application/x-www-form-urlencoded') {
47 24
            $data .= $body;
48 16
        }
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 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 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 51
    public function authorization($url, $body = null, $contentType = null)
141
    {
142 51
        $authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
143 51
        return array('Authorization' => $authorization);
144
    }
145
}
146