Skip to content

Commit ba25fe8

Browse files
authored
Merge pull request #17416 from nextcloud/backport/17397/stable16
[stable16] Fix updating and deleting authtokens
2 parents 6f02458 + c934786 commit ba25fe8

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

settings/Controller/AuthSettingsController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use BadMethodCallException;
3131
use OC\AppFramework\Http;
3232
use OC\Authentication\Exceptions\InvalidTokenException;
33+
use OC\Authentication\Exceptions\ExpiredTokenException;
3334
use OC\Authentication\Exceptions\PasswordlessTokenException;
3435
use OC\Authentication\Token\INamedToken;
3536
use OC\Authentication\Token\IProvider;
@@ -237,10 +238,13 @@ private function publishActivity(string $subject, int $id, array $parameters = [
237238
* @param int $id
238239
* @return IToken
239240
* @throws InvalidTokenException
240-
* @throws \OC\Authentication\Exceptions\ExpiredTokenException
241241
*/
242242
private function findTokenByIdAndUser(int $id): IToken {
243-
$token = $this->tokenProvider->getTokenById($id);
243+
try {
244+
$token = $this->tokenProvider->getTokenById($id);
245+
} catch (ExpiredTokenException $e) {
246+
$token = $e->getToken();
247+
}
244248
if ($token->getUID() !== $this->uid) {
245249
throw new InvalidTokenException('This token does not belong to you!');
246250
}

tests/Settings/Controller/AuthSettingsControllerTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use OC\AppFramework\Http;
2525
use OC\Authentication\Exceptions\InvalidTokenException;
26+
use OC\Authentication\Exceptions\ExpiredTokenException;
2627
use OC\Authentication\Token\DefaultToken;
2728
use OC\Authentication\Token\IProvider;
2829
use OC\Authentication\Token\IToken;
@@ -177,6 +178,30 @@ public function testDestroy() {
177178
$this->assertEquals([], $this->controller->destroy($tokenId));
178179
}
179180

181+
public function testDestroyExpired() {
182+
$tokenId = 124;
183+
$token = $this->createMock(DefaultToken::class);
184+
185+
$token->expects($this->exactly(2))
186+
->method('getId')
187+
->willReturn($tokenId);
188+
189+
$token->expects($this->once())
190+
->method('getUID')
191+
->willReturn($this->uid);
192+
193+
$this->tokenProvider->expects($this->once())
194+
->method('getTokenById')
195+
->with($this->equalTo($tokenId))
196+
->willThrowException(new ExpiredTokenException($token));
197+
198+
$this->tokenProvider->expects($this->once())
199+
->method('invalidateTokenById')
200+
->with($this->uid, $tokenId);
201+
202+
$this->assertSame([], $this->controller->destroy($tokenId));
203+
}
204+
180205
public function testDestroyWrongUser() {
181206
$tokenId = 124;
182207
$token = $this->createMock(DefaultToken::class);
@@ -307,6 +332,26 @@ public function testUpdateNoChange(): void {
307332
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
308333
}
309334

335+
public function testUpdateExpired() {
336+
$tokenId = 42;
337+
$token = $this->createMock(DefaultToken::class);
338+
339+
$token->expects($this->once())
340+
->method('getUID')
341+
->willReturn($this->uid);
342+
343+
$this->tokenProvider->expects($this->once())
344+
->method('getTokenById')
345+
->with($this->equalTo($tokenId))
346+
->willThrowException(new ExpiredTokenException($token));
347+
348+
$this->tokenProvider->expects($this->once())
349+
->method('updateToken')
350+
->with($this->equalTo($token));
351+
352+
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
353+
}
354+
310355
public function testUpdateTokenWrongUser() {
311356
$tokenId = 42;
312357
$token = $this->createMock(DefaultToken::class);

0 commit comments

Comments
 (0)