ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilWACTokenTestTBD.php
Go to the documentation of this file.
1<?php
2
19use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
20use org\bovigo\vfs\vfsStream;
22use Psr\Http\Message\RequestInterface;
23use PHPUnit\Framework\Attributes\Test;
28use Mockery\Adapter\Phpunit\MockeryTestCase;
29use Mockery\MockInterface;
30use org\bovigo\vfs;
31use Psr\Http\Message\ResponseInterface;
33use Dflydev\FigCookies\SetCookie;
34
45#[RunTestsInSeparateProcesses]
46class ilWACTokenTest extends MockeryTestCase
47{
48 public const ADDITIONAL_TIME = 1;
49 public const LIFETIME = 2;
50 public const SALT = 'SALT';
51 public const CLIENT_NAME = 'client_name';
55 protected $backupGlobals = false;
59 protected $file_one;
71 protected $file_two;
75 protected $file_three;
79 protected $file_four;
83 protected $root;
87 private $http;
92
93
97 protected function setUp(): void
98 {
99 parent::setUp();
100
101 $this->root = vfsStream::setup('ilias.de');
102 $this->file_one = vfsStream::newFile('data/client_name/mobs/mm_123/dummy.jpg')
103 ->at($this->root)->setContent('dummy');
104 $this->file_one_subfolder = vfsStream::newFile('data/client_name/mobs/mm_123/mobile/dummy.jpg')
105 ->at($this->root)->setContent('dummy');
106 $this->file_one_subfolder_two = vfsStream::newFile('data/client_name/mobs/mm_123/mobile/device/dummy.jpg')
107 ->at($this->root)->setContent('dummy');
108 $this->file_two = vfsStream::newFile('data/client_name/mobs/mm_123/dummy2.jpg')
109 ->at($this->root)->setContent('dummy2');
110 $this->file_three = vfsStream::newFile('data/client_name/mobs/mm_124/dummy.jpg')
111 ->at($this->root)->setContent('dummy');
112 $this->file_four = vfsStream::newFile('data/client_name/sec/ilBlog/mm_124/dummy.jpg')
113 ->at($this->root)->setContent('dummy');
114
115 //setup container for HttpServiceAware classes
116 $container = new Container();
117 $container['http'] = fn($c) => Mockery::mock(GlobalHttpState::class);
118
119 $this->http = $container['http'];
120
121
122 $GLOBALS["DIC"] = $container;
123
124 $this->cookieFactory = Mockery::mock(CookieFactoryImpl::class);
125
126 //because the cookie have no logic except cloning it self therefore it should be no problem to defer the function calls
127 $this->cookieFactory->shouldDeferMissing();
128
129 ilWACToken::setSALT(self::SALT);
130 }
131
132
133 public function testWithoutSigning(): void
134 {
135 $this->markTestSkipped("Failed for some unknown reason.");
136
137 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_one->url()), $this->http, $this->cookieFactory);
138
139 $cookieJar = Mockery::mock(CookieJar::class);
140
141 $cookieJar
142 ->shouldReceive('getAll')
143 ->times(2)
144 ->withAnyArgs()
145 ->andReturn([]);
146
147 $this->http->shouldReceive('cookieJar')
148 ->twice()
149 ->withNoArgs()
150 ->andReturn($cookieJar);
151
152 $request = Mockery::mock(RequestInterface::class);
153 $request->shouldReceive('getCookieParams')
154 ->andReturn([]);
155
156 $this->http->shouldReceive('request')
157 ->withNoArgs()
158 ->andReturn($request);
159
160 $this->assertFalse($ilWACSignedPath->isSignedPath());
161 $this->assertFalse($ilWACSignedPath->isSignedPathValid());
162 $this->assertFalse($ilWACSignedPath->isFolderSigned());
163 $this->assertFalse($ilWACSignedPath->isFolderTokenValid());
164 }
165
166
167 public function testSomeBasics(): void
168 {
169 $this->markTestSkipped("Failed for some unknown reason.");
170 $query = 'myparam=1234';
171 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_four->url() . '?'
172 . $query), $this->http, $this->cookieFactory);
173
174 $this->assertEquals('dummy.jpg', $ilWACSignedPath->getPathObject()->getFileName());
175 $this->assertEquals($query, $ilWACSignedPath->getPathObject()->getQuery());
176 $this->assertEquals('./public/data/' . self::CLIENT_NAME
177 . '/sec/ilBlog/mm_124/', $ilWACSignedPath->getPathObject()
178 ->getSecurePath());
179 $this->assertEquals('ilBlog', $ilWACSignedPath->getPathObject()->getSecurePathId());
180 $this->assertFalse($ilWACSignedPath->getPathObject()->isStreamable());
181 }
182
183
184 public function testTokenGeneration(): void
185 {
186 $this->markTestSkipped("Failed for some unknown reason.");
187
188 $ilWacPath = new ilWacPath($this->file_four->url());
189 $ilWACToken = new ilWACToken($ilWacPath->getPath(), self::CLIENT_NAME, 123456, 20);
190 $ilWACToken->generateToken();
191 $this->assertEquals('SALT-client_name-123456-20', $ilWACToken->getRawToken());
192 $this->assertEquals('./data/client_name/sec/ilBlog/mm_124/dummy.jpg', $ilWACToken->getId());
193
194 $this->assertEquals(self::SALT, ilWACToken::getSALT());
195 $ilWACToken = new ilWACToken($ilWacPath->getPath(), self::CLIENT_NAME, 123456, 20);
196 $this->assertEquals('b541e2bae42ee222f9be959b7ad2ab8844cbb05b', $ilWACToken->getToken());
197 $this->assertEquals('e45b98f267dc891c8206c844f7df29ea', $ilWACToken->getHashedId());
198 }
199
200
201 public function testCookieGeneration(): void
202 {
203 $this->markTestSkipped('unable to use http cookies at this point');
204
205 $cookieJar = Mockery::mock(CookieJar::class);
206
207 $response = Mockery::mock(ResponseInterface::class);
208
209 $this->http
210 ->shouldReceive('response')
211 ->times(3)
212 ->withNoArgs()
213 ->andReturn($response)
214 ->getMock();
215
216 $cookieJar
217 ->shouldReceive('with')
218 ->times(3)
219 ->with(new CookieWrapper(SetCookie::create('')))
220 ->andReturnSelf()
221 ->getMock()
222
223 ->shouldReceive('with')
224 ->times(3)
225 ->with(new CookieWrapper(SetCookie::create('')))
226 ->andReturnSelf()
227 ->getMock()
228
229 ->shouldReceive('with')
230 ->times(3)
231 ->with(new CookieWrapper(SetCookie::create('')))
232 ->andReturnSelf()
233 ->getMock();
234
235 $this->http->shouldReceive('cookieJar')
236 ->withNoArgs()
237 ->andReturn($cookieJar);
238
239 ilWACSignedPath::signFolderOfStartFile($this->file_one->url());
240
241 // in subfolder
242 ilWACSignedPath::signFolderOfStartFile($this->file_one_subfolder->url());
243
244 // in sub-subfolder
245 ilWACSignedPath::signFolderOfStartFile($this->file_one_subfolder->url());
246 }
247
248
249 public function testFileToken(): void
250 {
251 $this->markTestSkipped("Failed for some unknown reason.");
254
255 // Request within lifetime
256 $signed_path = ilWACSignedPath::signFile($this->file_one->url());
257 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path), $this->http, $this->cookieFactory);
258
259 $this->assertTrue($ilWACSignedPath->isSignedPath());
260 $this->assertTrue($ilWACSignedPath->isSignedPathValid());
261 $this->assertEquals($ilWACSignedPath->getPathObject()->getClient(), self::CLIENT_NAME);
262 $this->assertFalse($ilWACSignedPath->getPathObject()->isInSecFolder());
263 $this->assertTrue($ilWACSignedPath->getPathObject()->isImage());
264 $this->assertFalse($ilWACSignedPath->getPathObject()->isAudio());
265 $this->assertFalse($ilWACSignedPath->getPathObject()->isVideo());
266 $this->assertTrue($ilWACSignedPath->getPathObject()->hasTimestamp());
267 $this->assertTrue($ilWACSignedPath->getPathObject()->hasToken());
268
269 // Request after lifetime
270 $signed_path = ilWACSignedPath::signFile($this->file_four->url());
271 sleep($lifetime + self::ADDITIONAL_TIME);
272 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path), $this->http, $this->cookieFactory);
273 $this->assertTrue($ilWACSignedPath->isSignedPath());
274 $this->assertFalse($ilWACSignedPath->isSignedPathValid());
275 }
276
277
278
279 #[Test]
280 public function testModifiedTimestampNoMod(): void
281 {
282 $this->markTestSkipped("Failed for some unknown reason.");
283 // self::markTestSkipped("WIP");
284 // return;
285 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 0)), $this->http, $this->cookieFactory);
286 $this->assertTrue($ilWACSignedPath->isSignedPath());
287 $this->assertTrue($ilWACSignedPath->isSignedPathValid());
288 }
289
290
291 #[Test]
292 public function testModifiedTimestampAddTime(): void
293 {
294 $this->markTestSkipped("Failed for some unknown reason.");
295 // self::markTestSkipped("WIP");
296 // return;
297 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(self::ADDITIONAL_TIME, 0)), $this->http, $this->cookieFactory);
298 $this->assertTrue($ilWACSignedPath->isSignedPath());
299 $this->assertFalse($ilWACSignedPath->isSignedPathValid());
300 }
301
302
303 public function testModifiedTimestampSubTime(): void
304 {
305 $this->markTestSkipped("Failed for some unknown reason.");
306 // self::markTestSkipped("WIP");
307 // return;
308 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(self::ADDITIONAL_TIME
309 * -1, 0)), $this->http, $this->cookieFactory);
310 $this->assertTrue($ilWACSignedPath->isSignedPath());
311 $this->assertFalse($ilWACSignedPath->isSignedPathValid());
312 }
313
314
315 public function testModifiedTTL(): void
316 {
317 $this->markTestSkipped("Failed for some unknown reason.");
318 // self::markTestSkipped("WIP");
319 // return;
320 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 1)), $this->http, $this->cookieFactory);
321 $this->assertTrue($ilWACSignedPath->isSignedPath());
322 $this->assertFalse($ilWACSignedPath->isSignedPathValid());
323 }
324
325
326 public function testModifiedTTLAndTimestamp(): void
327 {
328 $this->markTestSkipped("Failed for some unknown reason.");
329 // self::markTestSkipped("WIP");
330 // return;
331 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(1, 1)), $this->http, $this->cookieFactory);
332 $this->assertTrue($ilWACSignedPath->isSignedPath());
333 $this->assertFalse($ilWACSignedPath->isSignedPathValid());
334 }
335
336
337 public function testModifiedToken(): void
338 {
339 $this->markTestSkipped("Failed for some unknown reason.");
340 // self::markTestSkipped("WIP");
341 // return;
342 $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 0, md5('LOREM'))), $this->http, $this->cookieFactory);
343 $this->assertTrue($ilWACSignedPath->isSignedPath());
344 $this->assertFalse($ilWACSignedPath->isSignedPathValid());
345 }
346
347
351 protected function getModifiedSignedPath(int $add_ttl = 0, int $add_timestamp = 0, $override_token = null): string
352 {
354 $signed_path = ilWACSignedPath::signFile($this->file_one->url());
355
356 $parts = parse_url($signed_path);
357 $path = $parts['path'];
358 $query = $parts['query'];
359 parse_str($query, $query_array);
360 $token = $override_token ?: $query_array['il_wac_token'];
361 $ttl = (int) $query_array['il_wac_ttl'];
362 $ts = (int) $query_array['il_wac_ts'];
363 $path_with_token = $path . '?il_wac_token=' . $token;
364
365 $modified_ttl = $ttl + $add_ttl;
366 $modified_ts = $ts + $add_timestamp;
367
368 return $path_with_token . '&il_wac_ttl=' . $modified_ttl . '&il_wac_ts=' . $modified_ts;
369 }
370}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilWACSignedPath.
static setTokenMaxLifetimeInSeconds(int $token_max_lifetime_in_seconds)
static signFolderOfStartFile(string $start_file_path)
static signFile(string $path_to_file)
static getTokenMaxLifetimeInSeconds()
TestCase for the ilWACTokenTest.
getModifiedSignedPath(int $add_ttl=0, int $add_timestamp=0, $override_token=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getSALT()
static setSALT(string $salt)
const CLIENT_NAME
Definition: constants.php:42
$c
Definition: deliver.php:25
Interface GlobalHttpState.
$path
Definition: ltiservices.php:30
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
static http()
Fetches the global http state from ILIAS.
$container
@noRector
Definition: wac.php:37
$GLOBALS["DIC"]
Definition: wac.php:54
$token
Definition: xapitoken.php:70
$response
Definition: xapitoken.php:93