Skip to content

Commit cf0376f

Browse files
authored
Merge pull request #17415 from nextcloud/backport/17397/stable17
[stable17] Fix updating and deleting authtokens
2 parents b5ad2f9 + 5c5d658 commit cf0376f

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
@@ -29,6 +29,7 @@
2929

3030
use BadMethodCallException;
3131
use OC\Authentication\Exceptions\InvalidTokenException;
32+
use OC\Authentication\Exceptions\ExpiredTokenException;
3233
use OC\Authentication\Exceptions\PasswordlessTokenException;
3334
use OC\Authentication\Exceptions\WipeTokenException;
3435
use OC\Authentication\Token\INamedToken;
@@ -248,10 +249,13 @@ private function publishActivity(string $subject, int $id, array $parameters = [
248249
* @param int $id
249250
* @return IToken
250251
* @throws InvalidTokenException
251-
* @throws \OC\Authentication\Exceptions\ExpiredTokenException
252252
*/
253253
private function findTokenByIdAndUser(int $id): IToken {
254-
$token = $this->tokenProvider->getTokenById($id);
254+
try {
255+
$token = $this->tokenProvider->getTokenById($id);
256+
} catch (ExpiredTokenException $e) {
257+
$token = $e->getToken();
258+
}
255259
if ($token->getUID() !== $this->uid) {
256260
throw new InvalidTokenException('This token does not belong to you!');
257261
}

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;
@@ -183,6 +184,30 @@ public function testDestroy() {
183184
$this->assertEquals([], $this->controller->destroy($tokenId));
184185
}
185186

187+
public function testDestroyExpired() {
188+
$tokenId = 124;
189+
$token = $this->createMock(DefaultToken::class);
190+
191+
$token->expects($this->exactly(2))
192+
->method('getId')
193+
->willReturn($tokenId);
194+
195+
$token->expects($this->once())
196+
->method('getUID')
197+
->willReturn($this->uid);
198+
199+
$this->tokenProvider->expects($this->once())
200+
->method('getTokenById')
201+
->with($this->equalTo($tokenId))
202+
->willThrowException(new ExpiredTokenException($token));
203+
204+
$this->tokenProvider->expects($this->once())
205+
->method('invalidateTokenById')
206+
->with($this->uid, $tokenId);
207+
208+
$this->assertSame([], $this->controller->destroy($tokenId));
209+
}
210+
186211
public function testDestroyWrongUser() {
187212
$tokenId = 124;
188213
$token = $this->createMock(DefaultToken::class);
@@ -315,6 +340,26 @@ public function testUpdateNoChange(): void {
315340
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
316341
}
317342

343+
public function testUpdateExpired() {
344+
$tokenId = 42;
345+
$token = $this->createMock(DefaultToken::class);
346+
347+
$token->expects($this->once())
348+
->method('getUID')
349+
->willReturn($this->uid);
350+
351+
$this->tokenProvider->expects($this->once())
352+
->method('getTokenById')
353+
->with($this->equalTo($tokenId))
354+
->willThrowException(new ExpiredTokenException($token));
355+
356+
$this->tokenProvider->expects($this->once())
357+
->method('updateToken')
358+
->with($this->equalTo($token));
359+
360+
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
361+
}
362+
318363
public function testUpdateTokenWrongUser() {
319364
$tokenId = 42;
320365
$token = $this->createMock(DefaultToken::class);

0 commit comments

Comments
 (0)