forked from sabre-io/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAWSTest.php
More file actions
222 lines (174 loc) · 6.66 KB
/
Copy pathAWSTest.php
File metadata and controls
222 lines (174 loc) · 6.66 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
namespace Sabre\HTTP\Auth;
use Sabre\HTTP\Request;
use Sabre\HTTP\Response;
class AWSTest extends \PHPUnit\Framework\TestCase
{
private Response $response;
private Request $request;
private AWS $auth;
public const REALM = 'SabreDAV unittest';
public function setUp(): void
{
$this->response = new Response();
$this->request = new Request('GET', '/');
$this->auth = new AWS(self::REALM, $this->request, $this->response);
}
public function testNoHeader(): void
{
$this->request->setMethod('GET');
$result = $this->auth->init();
self::assertFalse($result, 'No AWS Authorization header was supplied, so we should have gotten false');
self::assertEquals(AWS::ERR_NOAWSHEADER, $this->auth->errorCode);
}
public function testInvalidAuthorizationHeader(): void
{
$this->request->setMethod('GET');
$this->request->setHeader('Authorization', 'Invalid Auth Header');
self::assertFalse($this->auth->init(), 'The Invalid AWS authorization header');
}
public function testIncorrectContentMD5(): void
{
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$this->request->setMethod('GET');
$this->request->setHeaders([
'Authorization' => "AWS $accessKey:sig",
'Content-MD5' => 'garbage',
]);
$this->request->setUrl('/');
$this->auth->init();
$result = $this->auth->validate($secretKey);
self::assertFalse($result);
self::assertEquals(AWS::ERR_MD5CHECKSUMWRONG, $this->auth->errorCode);
}
public function testNoDate(): void
{
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content, true));
$this->request->setMethod('POST');
$this->request->setHeaders([
'Authorization' => "AWS $accessKey:sig",
'Content-MD5' => $contentMD5,
]);
$this->request->setUrl('/');
$this->request->setBody($content);
$this->auth->init();
$result = $this->auth->validate($secretKey);
self::assertFalse($result);
self::assertEquals(AWS::ERR_INVALIDDATEFORMAT, $this->auth->errorCode);
}
public function testFutureDate(): void
{
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content, true));
$date = new \DateTime('@'.(time() + (60 * 20)));
$date->setTimezone(new \DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');
$this->request->setMethod('POST');
$this->request->setHeaders([
'Authorization' => "AWS $accessKey:sig",
'Content-MD5' => $contentMD5,
'Date' => $date,
]);
$this->request->setBody($content);
$this->auth->init();
$result = $this->auth->validate($secretKey);
self::assertFalse($result);
self::assertEquals(AWS::ERR_REQUESTTIMESKEWED, $this->auth->errorCode);
}
public function testPastDate(): void
{
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content, true));
$date = new \DateTime('@'.(time() - (60 * 20)));
$date->setTimezone(new \DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');
$this->request->setMethod('POST');
$this->request->setHeaders([
'Authorization' => "AWS $accessKey:sig",
'Content-MD5' => $contentMD5,
'Date' => $date,
]);
$this->request->setBody($content);
$this->auth->init();
$result = $this->auth->validate($secretKey);
self::assertFalse($result);
self::assertEquals(AWS::ERR_REQUESTTIMESKEWED, $this->auth->errorCode);
}
public function testIncorrectSignature(): void
{
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content, true));
$date = new \DateTime('now');
$date->setTimezone(new \DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');
$this->request->setUrl('/');
$this->request->setMethod('POST');
$this->request->setHeaders([
'Authorization' => "AWS $accessKey:sig",
'Content-MD5' => $contentMD5,
'X-amz-date' => $date,
]);
$this->request->setBody($content);
$this->auth->init();
$result = $this->auth->validate($secretKey);
self::assertFalse($result);
self::assertEquals(AWS::ERR_INVALIDSIGNATURE, $this->auth->errorCode);
}
public function testValidRequest(): void
{
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content, true));
$date = new \DateTime('now');
$date->setTimezone(new \DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');
$sig = base64_encode($this->hmacsha1($secretKey,
"POST\n$contentMD5\n\n$date\nx-amz-date:$date\n/evert"
));
$this->request->setUrl('/evert');
$this->request->setMethod('POST');
$this->request->setHeaders([
'Authorization' => "AWS $accessKey:$sig",
'Content-MD5' => $contentMD5,
'X-amz-date' => $date,
]);
$this->request->setBody($content);
$this->auth->init();
$result = $this->auth->validate($secretKey);
self::assertTrue($result, 'Signature did not validate, got errorcode '.$this->auth->errorCode);
self::assertEquals($accessKey, $this->auth->getAccessKey());
}
public function test401(): void
{
$this->auth->requireLogin();
$test = preg_match('/^AWS$/', (string) $this->response->getHeader('WWW-Authenticate'), $matches);
self::assertTrue(1 === $test, 'The WWW-Authenticate response didn\'t match our pattern');
}
/**
* Generates an HMAC-SHA1 signature.
*/
private function hmacsha1(string $key, string $message): string
{
$blocksize = 64;
if (strlen($key) > $blocksize) {
$key = pack('H*', sha1($key));
}
$key = str_pad($key, $blocksize, chr(0x00));
$ipad = str_repeat(chr(0x36), $blocksize);
$opad = str_repeat(chr(0x5C), $blocksize);
$hmac = pack('H*', sha1(($key ^ $opad).pack('H*', sha1(($key ^ $ipad).$message))));
return $hmac;
}
}