ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ilWACTokenTestTBD.php
Go to the documentation of this file.
1 <?php
2 
26 use org\bovigo\vfs;
30 
42 class ilWACTokenTest extends MockeryTestCase
43 {
44  public const ADDITIONAL_TIME = 1;
45  public const LIFETIME = 2;
46  public const SALT = 'SALT';
47  public const CLIENT_NAME = 'client_name';
51  protected $backupGlobals = false;
55  protected $file_one;
67  protected $file_two;
71  protected $file_three;
75  protected $file_four;
79  protected $root;
83  private $http;
87  private $cookieFactory;
88 
89 
93  protected function setUp(): void
94  {
95  parent::setUp();
96 
97  $this->root = vfs\vfsStream::setup('ilias.de');
98  $this->file_one = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/dummy.jpg')
99  ->at($this->root)->setContent('dummy');
100  $this->file_one_subfolder = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/mobile/dummy.jpg')
101  ->at($this->root)->setContent('dummy');
102  $this->file_one_subfolder_two = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/mobile/device/dummy.jpg')
103  ->at($this->root)->setContent('dummy');
104  $this->file_two = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/dummy2.jpg')
105  ->at($this->root)->setContent('dummy2');
106  $this->file_three = vfs\vfsStream::newFile('data/client_name/mobs/mm_124/dummy.jpg')
107  ->at($this->root)->setContent('dummy');
108  $this->file_four = vfs\vfsStream::newFile('data/client_name/sec/ilBlog/mm_124/dummy.jpg')
109  ->at($this->root)->setContent('dummy');
110 
111  //setup container for HttpServiceAware classes
112  $container = new \ILIAS\DI\Container();
113  $container['http'] = fn($c) => Mockery::mock(GlobalHttpState::class);
114 
115  $this->http = $container['http'];
116 
117 
118  $GLOBALS["DIC"] = $container;
119 
120  $this->cookieFactory = Mockery::mock(CookieFactoryImpl::class);
121 
122  //because the cookie have no logic except cloning it self therefore it should be no problem to defer the function calls
123  $this->cookieFactory->shouldDeferMissing();
124 
125  ilWACToken::setSALT(self::SALT);
126  }
127 
128 
129  public function testWithoutSigning(): void
130  {
131  $this->markTestSkipped("Failed for some unknown reason.");
132 
133  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_one->url()), $this->http, $this->cookieFactory);
134 
135  $cookieJar = Mockery::mock(CookieJar::class);
136 
137  $cookieJar
138  ->shouldReceive('getAll')
139  ->times(2)
140  ->withAnyArgs()
141  ->andReturn([]);
142 
143  $this->http->shouldReceive('cookieJar')
144  ->twice()
145  ->withNoArgs()
146  ->andReturn($cookieJar);
147 
148  $request = Mockery::mock(Psr\Http\Message\RequestInterface::class);
149  $request->shouldReceive('getCookieParams')
150  ->andReturn([]);
151 
152  $this->http->shouldReceive('request')
153  ->withNoArgs()
154  ->andReturn($request);
155 
156  $this->assertFalse($ilWACSignedPath->isSignedPath());
157  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
158  $this->assertFalse($ilWACSignedPath->isFolderSigned());
159  $this->assertFalse($ilWACSignedPath->isFolderTokenValid());
160  }
161 
162 
163  public function testSomeBasics(): void
164  {
165  $this->markTestSkipped("Failed for some unknown reason.");
166  $query = 'myparam=1234';
167  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_four->url() . '?'
168  . $query), $this->http, $this->cookieFactory);
169 
170  $this->assertEquals('dummy.jpg', $ilWACSignedPath->getPathObject()->getFileName());
171  $this->assertEquals($query, $ilWACSignedPath->getPathObject()->getQuery());
172  $this->assertEquals('./public/data/' . self::CLIENT_NAME
173  . '/sec/ilBlog/mm_124/', $ilWACSignedPath->getPathObject()
174  ->getSecurePath());
175  $this->assertEquals('ilBlog', $ilWACSignedPath->getPathObject()->getSecurePathId());
176  $this->assertFalse($ilWACSignedPath->getPathObject()->isStreamable());
177  }
178 
179 
180  public function testTokenGeneration(): void
181  {
182  $this->markTestSkipped("Failed for some unknown reason.");
183 
184  $ilWacPath = new ilWacPath($this->file_four->url());
185  $ilWACToken = new ilWACToken($ilWacPath->getPath(), self::CLIENT_NAME, 123456, 20);
186  $ilWACToken->generateToken();
187  $this->assertEquals('SALT-client_name-123456-20', $ilWACToken->getRawToken());
188  $this->assertEquals('./data/client_name/sec/ilBlog/mm_124/dummy.jpg', $ilWACToken->getId());
189 
190  $this->assertEquals(self::SALT, ilWACToken::getSALT());
191  $ilWACToken = new ilWACToken($ilWacPath->getPath(), self::CLIENT_NAME, 123456, 20);
192  $this->assertEquals('b541e2bae42ee222f9be959b7ad2ab8844cbb05b', $ilWACToken->getToken());
193  $this->assertEquals('e45b98f267dc891c8206c844f7df29ea', $ilWACToken->getHashedId());
194  }
195 
196 
197  public function testCookieGeneration(): void
198  {
199  $this->markTestSkipped('unable to use http cookies at this point');
200 
201  $cookieJar = Mockery::mock(CookieJar::class);
202 
203  $response = Mockery::mock(ResponseInterface::class);
204 
205  $this->http
206  ->shouldReceive('response')
207  ->times(3)
208  ->withNoArgs()
209  ->andReturn($response)
210  ->getMock();
211 
212  $cookieJar
213  ->shouldReceive('with')
214  ->times(3)
215  ->with(new CookieWrapper(SetCookie::create('')))
216  ->andReturnSelf()
217  ->getMock()
218 
219  ->shouldReceive('with')
220  ->times(3)
221  ->with(new CookieWrapper(SetCookie::create('')))
222  ->andReturnSelf()
223  ->getMock()
224 
225  ->shouldReceive('with')
226  ->times(3)
227  ->with(new CookieWrapper(SetCookie::create('')))
228  ->andReturnSelf()
229  ->getMock();
230 
231  $this->http->shouldReceive('cookieJar')
232  ->withNoArgs()
233  ->andReturn($cookieJar);
234 
235  ilWACSignedPath::signFolderOfStartFile($this->file_one->url());
236 
237  // in subfolder
238  ilWACSignedPath::signFolderOfStartFile($this->file_one_subfolder->url());
239 
240  // in sub-subfolder
241  ilWACSignedPath::signFolderOfStartFile($this->file_one_subfolder->url());
242  }
243 
244 
245  public function testFileToken(): void
246  {
247  $this->markTestSkipped("Failed for some unknown reason.");
250 
251  // Request within lifetime
252  $signed_path = ilWACSignedPath::signFile($this->file_one->url());
253  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path), $this->http, $this->cookieFactory);
254 
255  $this->assertTrue($ilWACSignedPath->isSignedPath());
256  $this->assertTrue($ilWACSignedPath->isSignedPathValid());
257  $this->assertEquals($ilWACSignedPath->getPathObject()->getClient(), self::CLIENT_NAME);
258  $this->assertFalse($ilWACSignedPath->getPathObject()->isInSecFolder());
259  $this->assertTrue($ilWACSignedPath->getPathObject()->isImage());
260  $this->assertFalse($ilWACSignedPath->getPathObject()->isAudio());
261  $this->assertFalse($ilWACSignedPath->getPathObject()->isVideo());
262  $this->assertTrue($ilWACSignedPath->getPathObject()->hasTimestamp());
263  $this->assertTrue($ilWACSignedPath->getPathObject()->hasToken());
264 
265  // Request after lifetime
266  $signed_path = ilWACSignedPath::signFile($this->file_four->url());
267  sleep($lifetime + self::ADDITIONAL_TIME);
268  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path), $this->http, $this->cookieFactory);
269  $this->assertTrue($ilWACSignedPath->isSignedPath());
270  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
271  }
272 
273 
274 
278  public function testModifiedTimestampNoMod(): void
279  {
280  $this->markTestSkipped("Failed for some unknown reason.");
281  // self::markTestSkipped("WIP");
282  // return;
283  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 0)), $this->http, $this->cookieFactory);
284  $this->assertTrue($ilWACSignedPath->isSignedPath());
285  $this->assertTrue($ilWACSignedPath->isSignedPathValid());
286  }
287 
288 
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 ? $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 }
static setSALT(string $salt)
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
const CLIENT_NAME
Definition: constants.php:42
$response
Definition: xapitoken.php:90
getModifiedSignedPath(int $add_ttl=0, int $add_timestamp=0, $override_token=null)
$c
Definition: deliver.php:9
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static setTokenMaxLifetimeInSeconds(int $token_max_lifetime_in_seconds)
$path
Definition: ltiservices.php:30
$container
Definition: wac.php:13
TestCase for the ilWACTokenTest.
static http()
Fetches the global http state from ILIAS.
$GLOBALS["DIC"]
Definition: wac.php:30
static signFolderOfStartFile(string $start_file_path)
$token
Definition: xapitoken.php:67
static getSALT()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static signFile(string $path_to_file)
static getTokenMaxLifetimeInSeconds()