Skip to content

Conversation

@kinolaev
Copy link
Member

@kinolaev kinolaev commented Oct 4, 2019

Problem: updating and deleting expired authtokens don't works because PublicKeyTokenProvider->getTokenById throws ExpiredTokenException and client receives response 404.
Solution: we need to catch ExpiredTokenException and get token from this exception (as already implemented in OauthApiController)

@kinolaev kinolaev force-pushed the fix-authtoken-update-delete branch from 7e64658 to 39ab291 Compare October 4, 2019 00:39
@rullzer rullzer requested a review from ChristophWurst October 4, 2019 09:10
@rullzer rullzer added 3. to review Waiting for reviews bug labels Oct 4, 2019
@rullzer rullzer added this to the Nextcloud 18 milestone Oct 4, 2019
Copy link
Member

@rullzer rullzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

@rullzer
Copy link
Member

rullzer commented Oct 4, 2019

/backport to stable17

@rullzer
Copy link
Member

rullzer commented Oct 4, 2019

/backport to stable16

@kesselb
Copy link
Collaborator

kesselb commented Oct 4, 2019

Nice catch! I would prefer that update and destroy catch the exception. findTokenByIdAndUser should act like getTokenById with the additon that the token ownership is validated. However it's a internal method so not super important.

@kesselb
Copy link
Collaborator

kesselb commented Oct 4, 2019

Index: apps/settings/tests/Controller/AuthSettingsControllerTest.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- apps/settings/tests/Controller/AuthSettingsControllerTest.php	(revision 9bf55c33e15e6c2a576604e35e86b344fa6f534f)
+++ apps/settings/tests/Controller/AuthSettingsControllerTest.php	(date 1570182212152)
@@ -22,6 +22,7 @@
 namespace Test\Settings\Controller;
 
 use OC\AppFramework\Http;
+use OC\Authentication\Exceptions\ExpiredTokenException;
 use OC\Authentication\Exceptions\InvalidTokenException;
 use OC\Authentication\Token\DefaultToken;
 use OC\Authentication\Token\IProvider;
@@ -188,6 +189,28 @@
 		$this->assertEquals([], $this->controller->destroy($tokenId));
 	}
 
+	public function testDestroyExpired() {
+		$tokenId = 124;
+
+		/** @var DefaultToken $token */
+		$token = $this->createMock(DefaultToken::class);
+
+		$token->expects($this->exactly(2))
+			->method('getId')
+			->willReturn($tokenId);
+
+		$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);
@@ -354,6 +377,27 @@
 		$this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
 	}
 
+	public function testUpdateTokenExpired() {
+		$tokenId = 42;
+
+		/** @var DefaultToken $token */
+		$token = $this->createMock(DefaultToken::class);
+
+		$token->expects($this->exactly(2))
+			->method('getId')
+			->willReturn($tokenId);
+
+		$this->tokenProvider->expects($this->once())
+			->method('getTokenById')
+			->with($this->equalTo($tokenId))
+			->willThrowException(new ExpiredTokenException($token));
+
+		$this->tokenProvider->expects($this->once())
+			->method('updateToken');
+
+		$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
+	}
+
 	private function mockActivityManager(): void {
 		$this->activityManager->expects($this->once())
 			->method('generateEvent')

Added some tests for this pr. Feel free to push them.

@kesselb kesselb added 4. to release Ready to be released and/or waiting for tests to finish and removed 3. to review Waiting for reviews labels Oct 4, 2019
@kinolaev kinolaev force-pushed the fix-authtoken-update-delete branch from 39ab291 to 56b90db Compare October 4, 2019 18:41
@kinolaev
Copy link
Member Author

kinolaev commented Oct 4, 2019

Hello @kesselb, I added tests, thank you!

Signed-off-by: Sergej Nikolaev <kinolaev@gmail.com>
@skjnldsv skjnldsv force-pushed the fix-authtoken-update-delete branch from 56b90db to 9aa992e Compare October 5, 2019 08:28
@skjnldsv skjnldsv merged commit 37dbe82 into nextcloud:master Oct 5, 2019
@welcome
Copy link

welcome bot commented Oct 5, 2019

Thanks for your first pull request and welcome to the community! Feel free to keep them coming! If you are looking for issues to tackle then have a look at this selection: https://github.com/nextcloud/server/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22
Most developers hang out on IRC. So join #nextcloud-dev on Freenode for a chat!

@backportbot-nextcloud
Copy link

backport to stable17 in #17415

@skjnldsv
Copy link
Member

skjnldsv commented Oct 5, 2019

/backport to stable16

@backportbot-nextcloud
Copy link

backport to stable16 in #17416

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4. to release Ready to be released and/or waiting for tests to finish bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants