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