Skip to content

Commit

Permalink
Merge pull request #25276 from owncloud/delete-own-session-token
Browse files Browse the repository at this point in the history
prevent users from deleting their own session token
  • Loading branch information
Vincent Petry committed Jul 1, 2016
2 parents aaf4c30 + dc6c234 commit e42ce62
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
37 changes: 30 additions & 7 deletions settings/Controller/AuthSettingsController.php
Expand Up @@ -81,7 +81,28 @@ public function index() {
if (is_null($user)) {
return [];
}
return $this->tokenProvider->getTokenByUser($user);
$tokens = $this->tokenProvider->getTokenByUser($user);

try {
$sessionId = $this->session->getId();
} catch (SessionNotAvailableException $ex) {
return $this->getServiceNotAvailableResponse();
}
try {
$sessionToken = $this->tokenProvider->getToken($sessionId);
} catch (InvalidTokenException $ex) {
return $this->getServiceNotAvailableResponse();
}

return array_map(function(IToken $token) use ($sessionToken) {
$data = $token->jsonSerialize();
if ($sessionToken->getId() === $token->getId()) {
$data['canDelete'] = false;
} else {
$data['canDelete'] = true;
}
return $data;
}, $tokens);
}

/**
Expand All @@ -94,9 +115,7 @@ public function create($name) {
try {
$sessionId = $this->session->getId();
} catch (SessionNotAvailableException $ex) {
$resp = new JSONResponse();
$resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
return $resp;
return $this->getServiceNotAvailableResponse();
}

try {
Expand All @@ -108,9 +127,7 @@ public function create($name) {
$password = null;
}
} catch (InvalidTokenException $ex) {
$resp = new JSONResponse();
$resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
return $resp;
return $this->getServiceNotAvailableResponse();
}

$token = $this->generateRandomDeviceToken();
Expand All @@ -123,6 +140,12 @@ public function create($name) {
];
}

private function getServiceNotAvailableResponse() {
$resp = new JSONResponse();
$resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
return $resp;
}

/**
* Return a 20 digit device password
*
Expand Down
4 changes: 4 additions & 0 deletions settings/js/authtoken_view.js
Expand Up @@ -29,7 +29,11 @@
'<tr data-id="{{id}}">'
+ '<td class="has-tooltip" title="{{name}}"><span class="token-name">{{name}}</span></td>'
+ '<td><span class="last-activity has-tooltip" title="{{lastActivityTime}}">{{lastActivity}}</span></td>'
+ '{{#if canDelete}}'
+ '<td><a class="icon-delete has-tooltip" title="' + t('core', 'Disconnect') + '"></a></td>'
+ '{{else}}'
+ '<td></td>'
+ '{{/if}}'
+ '<tr>';

var SubView = OC.Backbone.View.extend({
Expand Down
41 changes: 36 additions & 5 deletions tests/Settings/Controller/AuthSettingsControllerTest.php
Expand Up @@ -24,6 +24,7 @@

use OC\AppFramework\Http;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\DefaultToken;
use OC\Authentication\Token\IToken;
use OC\Settings\Controller\AuthSettingsController;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -56,20 +57,49 @@ protected function setUp() {
}

public function testIndex() {
$result = [
'token1',
'token2',
$token1 = new DefaultToken();
$token1->setId(100);
$token2 = new DefaultToken();
$token2->setId(200);
$tokens = [
$token1,
$token2,
];
$sessionToken = new DefaultToken();
$sessionToken->setId(100);

$this->userManager->expects($this->once())
->method('get')
->with($this->uid)
->will($this->returnValue($this->user));
$this->tokenProvider->expects($this->once())
->method('getTokenByUser')
->with($this->user)
->will($this->returnValue($result));
->will($this->returnValue($tokens));
$this->session->expects($this->once())
->method('getId')
->will($this->returnValue('session123'));
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('session123')
->will($this->returnValue($sessionToken));

$this->assertEquals($result, $this->controller->index());
$this->assertEquals([
[
'id' => 100,
'name' => null,
'lastActivity' => null,
'type' => null,
'canDelete' => false,
],
[
'id' => 200,
'name' => null,
'lastActivity' => null,
'type' => null,
'canDelete' => true,
]
], $this->controller->index());
}

public function testCreate() {
Expand Down Expand Up @@ -107,6 +137,7 @@ public function testCreate() {
$expected = [
'token' => $newToken,
'deviceToken' => $deviceToken,
'loginName' => 'User13',
];
$this->assertEquals($expected, $this->controller->create($name));
}
Expand Down

0 comments on commit e42ce62

Please sign in to comment.