Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions settings/Controller/AuthSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use BadMethodCallException;
use OC\AppFramework\Http;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Exceptions\ExpiredTokenException;
use OC\Authentication\Exceptions\PasswordlessTokenException;
use OC\Authentication\Token\INamedToken;
use OC\Authentication\Token\IProvider;
Expand Down Expand Up @@ -237,10 +238,13 @@ private function publishActivity(string $subject, int $id, array $parameters = [
* @param int $id
* @return IToken
* @throws InvalidTokenException
* @throws \OC\Authentication\Exceptions\ExpiredTokenException
*/
private function findTokenByIdAndUser(int $id): IToken {
$token = $this->tokenProvider->getTokenById($id);
try {
$token = $this->tokenProvider->getTokenById($id);
} catch (ExpiredTokenException $e) {
$token = $e->getToken();
}
if ($token->getUID() !== $this->uid) {
throw new InvalidTokenException('This token does not belong to you!');
}
Expand Down
45 changes: 45 additions & 0 deletions tests/Settings/Controller/AuthSettingsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use OC\AppFramework\Http;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Exceptions\ExpiredTokenException;
use OC\Authentication\Token\DefaultToken;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
Expand Down Expand Up @@ -177,6 +178,30 @@ public function testDestroy() {
$this->assertEquals([], $this->controller->destroy($tokenId));
}

public function testDestroyExpired() {
$tokenId = 124;
$token = $this->createMock(DefaultToken::class);

$token->expects($this->exactly(2))
->method('getId')
->willReturn($tokenId);

$token->expects($this->once())
->method('getUID')
->willReturn($this->uid);

$this->tokenProvider->expects($this->once())
->method('getTokenById')
->with($this->equalTo($tokenId))
->willThrowException(new ExpiredTokenException($token));

$this->tokenProvider->expects($this->once())
->method('invalidateTokenById')
->with($this->uid, $tokenId);

$this->assertSame([], $this->controller->destroy($tokenId));
}

public function testDestroyWrongUser() {
$tokenId = 124;
$token = $this->createMock(DefaultToken::class);
Expand Down Expand Up @@ -307,6 +332,26 @@ public function testUpdateNoChange(): void {
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
}

public function testUpdateExpired() {
$tokenId = 42;
$token = $this->createMock(DefaultToken::class);

$token->expects($this->once())
->method('getUID')
->willReturn($this->uid);

$this->tokenProvider->expects($this->once())
->method('getTokenById')
->with($this->equalTo($tokenId))
->willThrowException(new ExpiredTokenException($token));

$this->tokenProvider->expects($this->once())
->method('updateToken')
->with($this->equalTo($token));

$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
}

public function testUpdateTokenWrongUser() {
$tokenId = 42;
$token = $this->createMock(DefaultToken::class);
Expand Down