Skip to content

Fix updating and deleting authtokens - #17397

Merged
skjnldsv merged 1 commit into
nextcloud:masterfrom
kinolaev:fix-authtoken-update-delete
Oct 5, 2019
Merged

Fix updating and deleting authtokens#17397
skjnldsv merged 1 commit into
nextcloud:masterfrom
kinolaev:fix-authtoken-update-delete

Conversation

@kinolaev

Copy link
Copy Markdown
Member

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
kinolaevforce-pushed the fix-authtoken-update-delete branch from 7e64658 to 39ab291CompareOctober 4, 2019 00:39
@rullzerrullzer added 3. to review Waiting for reviews bug labels Oct 4, 2019
@rullzerrullzer added this to the Nextcloud 18 milestone Oct 4, 2019

@rullzerrullzer left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

/backport to stable17

@rullzer

Copy link
Copy Markdown
Member

/backport to stable16

@kesselb

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor
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.

@kesselbkesselb 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
kinolaevforce-pushed the fix-authtoken-update-delete branch from 39ab291 to 56b90dbCompareOctober 4, 2019 18:41
@kinolaev

Copy link
Copy Markdown
MemberAuthor

Hello @kesselb, I added tests, thank you!

Signed-off-by: Sergej Nikolaev <kinolaev@gmail.com>
@skjnldsv
skjnldsvforce-pushed the fix-authtoken-update-delete branch from 56b90db to 9aa992eCompareOctober 5, 2019 08:28
@skjnldsv
skjnldsv merged commit 37dbe82 into nextcloud:masterOct 5, 2019
@welcome

welcomeBot commented Oct 5, 2019

Copy link
Copy Markdown

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
Copy Markdown

backport to stable17 in #17415

@skjnldsv

Copy link
Copy Markdown
Member

/backport to stable16

@backportbot-nextcloud

Copy link
Copy Markdown

backport to stable16 in #17416

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

Labels

4. to releaseReady to be released and/or waiting for tests to finishbug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@kinolaev@rullzer@kesselb@skjnldsv@ChristophWurst