Completed
Push — master ( d1bca4...94350e )
by Bai
10s
created

Auth   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.28%

Importance

Changes 13
Bugs 1 Features 2
Metric Value
c 13
b 1
f 2
dl 0
loc 157
ccs 65
cts 72
cp 0.9028
rs 10
wmc 23
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessKey() 0 4 1
A sign() 0 5 1
A __construct() 0 5 1
A signWithData() 0 5 1
B signRequest() 0 17 5
A verifyCallback() 0 5 1
A privateDownloadUrl() 0 15 2
B uploadToken() 0 30 4
B copyPolicy() 0 15 6
A authorization() 0 5 1
1
<?php
2
namespace Qiniu;
3
4
use Qiniu;
5
use Qiniu\Zone;
6
7
final class Auth
8
{
9
    private $accessKey;
10
    private $secretKey;
11
12
    public function __construct($accessKey, $secretKey)
13
    {
14
        $this->accessKey = $accessKey;
15
        $this->secretKey = $secretKey;
16
    }
17
18 6
    public function getAccessKey()
19
    {
20 6
        return $this->accessKey;
21
    }
22
23 90
    public function sign($data)
24
    {
25 90
        $hmac = hash_hmac('sha1', $data, $this->secretKey, true);
26 90
        return $this->accessKey . ':' . \Qiniu\base64_urlSafeEncode($hmac);
27
    }
28
29 24
    public function signWithData($data)
30
    {
31 24
        $data = \Qiniu\base64_urlSafeEncode($data);
32 24
        return $this->sign($data) . ':' . $data;
33
    }
34
35 51
    public function signRequest($urlString, $body, $contentType = null)
36
    {
37 51
        $url = parse_url($urlString);
38 51
        $data = '';
39 51
        if (array_key_exists('path', $url)) {
40 48
            $data = $url['path'];
41 32
        }
42 51
        if (array_key_exists('query', $url)) {
43 6
            $data .= '?' . $url['query'];
44 4
        }
45 51
        $data .= "\n";
46
47 51
        if ($body !== null && $contentType === 'application/x-www-form-urlencoded') {
48 24
            $data .= $body;
49 16
        }
50 51
        return $this->sign($data);
51
    }
52
53
    public function verifyCallback($contentType, $originAuthorization, $url, $body)
54
    {
55
        $authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
56
        return $originAuthorization === $authorization;
57
    }
58
59 12
    public function privateDownloadUrl($baseUrl, $expires = 3600)
60
    {
61 12
        $deadline = time() + $expires;
62
63 12
        $pos = strpos($baseUrl, '?');
64 12
        if ($pos !== false) {
65 9
            $baseUrl .= '&e=';
66 6
        } else {
67 3
            $baseUrl .= '?e=';
68
        }
69 12
        $baseUrl .= $deadline;
70
71 12
        $token = $this->sign($baseUrl);
72 12
        return "$baseUrl&token=$token";
73
    }
74
75 24
    public function uploadToken(
76
        $bucket,
77
        $key = null,
78
        $expires = 3600,
79
        $policy = null,
80
        $strictPolicy = true,
81
        Zone $zone = null
82
    ) {
83 24
        $deadline = time() + $expires;
84 24
        $scope = $bucket;
85 24
        if ($key !== null) {
86 15
            $scope .= ':' . $key;
87 10
        }
88 24
        $args = array();
89 24
        $args = self::copyPolicy($args, $policy, $strictPolicy);
90 21
        $args['scope'] = $scope;
91 21
        $args['deadline'] = $deadline;
92
93 21
        if ($zone === null) {
94 21
            $zone = new Zone();
95 14
        }
96
97 21
        list($upHosts, $err) = $zone->getUpHosts($this->accessKey, $bucket);
98 21
        if ($err === null) {
99 18
            $args['upHosts'] = $upHosts;
100 12
        }
101
        
102 21
        $b = json_encode($args);
103 21
        return $this->signWithData($b);
104
    }
105
106
    /**
107
     *上传策略,参数规格详见
108
     *http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
109
     */
110
    private static $policyFields = array(
111
        'callbackUrl',
112
        'callbackBody',
113
        'callbackHost',
114
        'callbackBodyType',
115
        'callbackFetchKey',
116
117
        'returnUrl',
118
        'returnBody',
119
120
        'endUser',
121
        'saveKey',
122
        'insertOnly',
123
124
        'detectMime',
125
        'mimeLimit',
126
        'fsizeMin',
127
        'fsizeLimit',
128
129
        'persistentOps',
130
        'persistentNotifyUrl',
131
        'persistentPipeline',
132
        
133
        'deleteAfterDays',
134
135
        'upHosts',
136
    );
137
138
    private static $deprecatedPolicyFields = array(
139
        'asyncOps',
140
    );
141
142 24
    private static function copyPolicy(&$policy, $originPolicy, $strictPolicy)
143
    {
144 24
        if ($originPolicy === null) {
145 18
            return array();
146
        }
147 6
        foreach ($originPolicy as $key => $value) {
148 6
            if (in_array((string) $key, self::$deprecatedPolicyFields, true)) {
149 3
                throw new \InvalidArgumentException("{$key} has deprecated");
150
            }
151 3
            if (!$strictPolicy || in_array((string) $key, self::$policyFields, true)) {
152 3
                $policy[$key] = $value;
153 2
            }
154 2
        }
155 3
        return $policy;
156
    }
157
158 48
    public function authorization($url, $body = null, $contentType = null)
159
    {
160 48
        $authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
161 48
        return array('Authorization' => $authorization);
162
    }
163
}
164