Sign.php
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace AlibabaCloud\Client\Support;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\UriInterface;
/**
* Class Sign
*
* @package AlibabaCloud\Client\Support
*/
class Sign
{
/**
* @var string
*/
private static $headerSeparator = "\n";
/**
* Construct standard Header for Alibaba Cloud.
*
* @param array $headers
*
* @return string
*/
private static function acsHeaderString(array $headers)
{
$array = [];
foreach ($headers as $headerKey => $headerValue) {
$key = strtolower($headerKey);
if (strncmp($key, 'x-acs-', 6) === 0) {
$array[$key] = $headerValue;
}
}
ksort($array);
$string = '';
foreach ($array as $sortMapKey => $sortMapValue) {
$string .= $sortMapKey . ':' . $sortMapValue[0] . self::$headerSeparator;
}
return $string;
}
/**
* @param UriInterface $uri
*
* @return string
*/
private static function resourceString(UriInterface $uri)
{
return $uri->getPath() . '?' . rawurldecode($uri->getQuery());
}
/**
* @param string $method
* @param array $headers
*
* @return string
*/
private static function headerString($method, array $headers)
{
$string = $method . self::$headerSeparator;
if (isset($headers['Accept'][0])) {
$string .= $headers['Accept'][0];
}
$string .= self::$headerSeparator;
if (isset($headers['Content-MD5'][0])) {
$string .= $headers['Content-MD5'][0];
}
$string .= self::$headerSeparator;
if (isset($headers['Content-Type'][0])) {
$string .= $headers['Content-Type'][0];
}
$string .= self::$headerSeparator;
if (isset($headers['Date'][0])) {
$string .= $headers['Date'][0];
}
$string .= self::$headerSeparator;
$string .= self::acsHeaderString($headers);
return $string;
}
/**
* @param string $string
*
* @return null|string|string[]
*/
private static function percentEncode($string)
{
$result = urlencode($string);
$result = str_replace(['+', '*'], ['%20', '%2A'], $result);
$result = preg_replace('/%7E/', '~', $result);
return $result;
}
/**
* @param string $method
* @param array $parameters
*
* @return string
*/
public static function rpcString($method, array $parameters)
{
ksort($parameters);
$canonicalized = '';
foreach ($parameters as $key => $value) {
if ($value === null || $value === '') {
continue;
}
$canonicalized .= '&' . self::percentEncode($key) . '=' . self::percentEncode($value);
}
return $method . '&%2F&' . self::percentEncode(substr($canonicalized, 1));
}
/**
* @param Request $request
*
* @return string
*/
public static function roaString(Request $request)
{
return self::headerString($request->getMethod(), $request->getHeaders()) .
self::resourceString($request->getUri());
}
/**
* @param string $salt
*
* @return string
*/
public static function uuid($salt)
{
return md5($salt . uniqid(md5(microtime(true)), true)) . microtime();
}
}