Skip to content

Commit

Permalink
Merge pull request #15590 from nextcloud/enh/15480/delete_apptoken_oc…
Browse files Browse the repository at this point in the history
…s_endpoint

Allow clients to delete their own apptoken
  • Loading branch information
rullzer committed May 17, 2019
2 parents 0857d92 + 2dcb4cf commit f9d30b9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/Controller/AppPasswordController.php
Expand Up @@ -24,6 +24,7 @@

namespace OC\Core\Controller;

use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OCP\AppFramework\Http\DataResponse;
Expand Down Expand Up @@ -115,4 +116,26 @@ public function getAppPassword(): DataResponse {
'apppassword' => $token
]);
}

/**
* @NoAdminRequired
*
* @return DataResponse
*/
public function deleteAppPassword() {
if (!$this->session->exists('app_password')) {
throw new OCSForbiddenException('no app password in use');
}

$appPassword = $this->session->get('app_password');

try {
$token = $this->tokenProvider->getToken($appPassword);
} catch (InvalidTokenException $e) {
throw new OCSForbiddenException('could not remove apptoken');
}

$this->tokenProvider->invalidateTokenById($token->getUID(), $token->getId());
return new DataResponse();
}
}
1 change: 1 addition & 0 deletions core/routes.php
Expand Up @@ -102,6 +102,7 @@
['root' => '/core', 'name' => 'WhatsNew#get', 'url' => '/whatsnew', 'verb' => 'GET'],
['root' => '/core', 'name' => 'WhatsNew#dismiss', 'url' => '/whatsnew', 'verb' => 'POST'],
['root' => '/core', 'name' => 'AppPassword#getAppPassword', 'url' => '/getapppassword', 'verb' => 'GET'],
['root' => '/core', 'name' => 'AppPassword#deleteAppPassword', 'url' => '/apppassword', 'verb' => 'DELETE'],

['root' => '/collaboration', 'name' => 'CollaborationResources#searchCollections', 'url' => '/resources/collections/search/{filter}', 'verb' => 'GET'],
['root' => '/collaboration', 'name' => 'CollaborationResources#listCollection', 'url' => '/resources/collections/{collectionId}', 'verb' => 'GET'],
Expand Down
57 changes: 57 additions & 0 deletions tests/Core/Controller/AppPasswordControllerTest.php
Expand Up @@ -24,9 +24,11 @@

namespace Tests\Core\Controller;

use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OC\Core\Controller\AppPasswordController;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
use OCP\Authentication\Exceptions\PasswordUnavailableException;
Expand Down Expand Up @@ -187,5 +189,60 @@ public function testGetAppPasswordNoPassword() {
$this->controller->getAppPassword();
}

public function testDeleteAppPasswordNoAppPassword() {
$this->session->method('exists')
->with('app_password')
->willReturn(false);

$this->expectException(OCSForbiddenException::class);

$this->controller->deleteAppPassword();
}

public function testDeleteAppPasswordFails() {
$this->session->method('exists')
->with('app_password')
->willReturn(true);
$this->session->method('get')
->with('app_password')
->willReturn('myAppPassword');

$this->tokenProvider->method('getToken')
->with('myAppPassword')
->willThrowException(new InvalidTokenException());

$this->expectException(OCSForbiddenException::class);

$this->controller->deleteAppPassword();
}

public function testDeleteAppPasswordSuccess() {
$this->session->method('exists')
->with('app_password')
->willReturn(true);
$this->session->method('get')
->with('app_password')
->willReturn('myAppPassword');

$token = $this->createMock(IToken::class);
$this->tokenProvider->method('getToken')
->with('myAppPassword')
->willReturn($token);

$token->method('getUID')
->willReturn('myUID');
$token->method('getId')
->willReturn(42);

$this->tokenProvider->expects($this->once())
->method('invalidateTokenById')
->with(
'myUID',
42
);

$result = $this->controller->deleteAppPassword();

$this->assertEquals(new DataResponse(), $result);
}
}

0 comments on commit f9d30b9

Please sign in to comment.