1 | <?php |
||
2 | |||
3 | /** @noinspection PhpMissingDocCommentInspection */ |
||
4 | |||
5 | namespace AlibabaCloud\Dybaseapi\MNS; |
||
6 | |||
7 | use AlibabaCloud\Dybaseapi\MNS\Requests\BaseRequest; |
||
8 | |||
9 | /** |
||
10 | * Class Signature |
||
11 | * |
||
12 | * @package AlibabaCloud\Dybaseapi\MNS |
||
13 | */ |
||
14 | class Signature |
||
15 | { |
||
16 | const MNS_HEADER_PREFIX = 'x-mns'; |
||
17 | |||
18 | /** |
||
19 | * @param string $accessKey |
||
20 | * @param BaseRequest $request |
||
21 | * |
||
22 | * @return string |
||
23 | */ |
||
24 | 1 | public static function SignRequest($accessKey, BaseRequest $request) |
|
25 | { |
||
26 | 1 | $headers = $request->getHeaders(); |
|
27 | 1 | $contentMd5 = ''; |
|
28 | 1 | if (isset($headers['Content-MD5'])) { |
|
29 | $contentMd5 = $headers['Content-MD5']; |
||
30 | } |
||
31 | 1 | $contentType = ''; |
|
32 | 1 | if (isset($headers['Content-Type'])) { |
|
33 | 1 | $contentType = $headers['Content-Type']; |
|
34 | 1 | } |
|
35 | 1 | $date = $headers['Date']; |
|
36 | 1 | $queryString = $request->getQueryString(); |
|
37 | 1 | $canonicalizedResource = $request->getResourcePath(); |
|
38 | 1 | if ($queryString !== null) { |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
39 | 1 | $canonicalizedResource .= '?' . $request->getQueryString(); |
|
40 | 1 | } |
|
41 | 1 | if (0 !== strpos($canonicalizedResource, '/')) { |
|
42 | 1 | $canonicalizedResource = '/' . $canonicalizedResource; |
|
43 | 1 | } |
|
44 | |||
45 | 1 | $tmpHeaders = []; |
|
46 | 1 | foreach ($headers as $key => $value) { |
|
47 | 1 | if (0 === strpos($key, Constants::MNS_HEADER_PREFIX)) { |
|
48 | 1 | $tmpHeaders[$key] = $value; |
|
49 | 1 | } |
|
50 | 1 | } |
|
51 | |||
52 | 1 | $canonicalizedMNSHeaders = implode( |
|
53 | 1 | "\n", |
|
54 | 1 | array_map( |
|
55 | 1 | static function ($v, $k) { |
|
56 | 1 | return $k . ':' . $v; |
|
57 | 1 | }, |
|
58 | 1 | $tmpHeaders, |
|
59 | 1 | array_keys($tmpHeaders) |
|
60 | 1 | ) |
|
61 | 1 | ); |
|
62 | |||
63 | $stringToSign = |
||
64 | 1 | strtoupper($request->getMethod()) . "\n" . $contentMd5 . "\n" . $contentType . "\n" . $date . "\n" . $canonicalizedMNSHeaders . "\n" . $canonicalizedResource; |
|
65 | |||
66 | 1 | return base64_encode(hash_hmac('sha1', $stringToSign, $accessKey, $raw_output = true)); |
|
67 | } |
||
68 | } |
||
69 |