Skip to content
Draft
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
92 changes: 36 additions & 56 deletions apps/settings/lib/Controller/AuthSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace OCA\Settings\Controller;

use BadMethodCallException;
use OC\Authentication\Exceptions\InvalidTokenException as OcInvalidTokenException;
use OC\Authentication\Exceptions\PasswordlessTokenException;
use OC\Authentication\Token\INamedToken;
use OC\Authentication\Token\IProvider;
Expand All @@ -25,9 +24,7 @@
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Authentication\Exceptions\ExpiredTokenException;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Exceptions\WipeTokenException;
use OCP\Authentication\Token\IToken;
use OCP\IConfig;
use OCP\IL10N;
Expand Down Expand Up @@ -177,29 +174,26 @@ public function destroy(int $id): JSONResponse {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}

$subject = Provider::APP_TOKEN_DELETED;
try {
$token = $this->findTokenByIdAndUser($id);
} catch (WipeTokenException $e) {
// Deleting a wipe-pending token cancels the pending wipe; the device
// may already be uninstalled so we allow it, but record it under a
// distinct subject so the audit trail captures the consequence.
$token = $e->getToken();
$subject = Provider::APP_TOKEN_DELETED_WIPE_CANCELLED;
} catch (InvalidTokenException $e) {
$token = $this->tokenProvider->getLoginOfUser($this->userId, $id);
if ($token === null) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}

$this->tokenProvider->invalidateTokenById($this->userId, $token->getId());
$this->publishActivity($subject, $token->getId(), ['name' => $token->getName()]);
// Deleting a wipe-pending token cancels the pending wipe; the device
// may already be uninstalled so we allow it, but record it under a
// distinct subject so the audit trail captures the consequence.
$subject = $token->getType() === IToken::WIPE_TOKEN ? Provider::APP_TOKEN_DELETED_WIPE_CANCELLED : Provider::APP_TOKEN_DELETED;

$this->tokenProvider->invalidateTokenById($this->userId, $id);
$this->publishActivity($subject, $id, ['name' => $token->getName()]);
return new JSONResponse([]);
}

/**
* Revoke the tokens of the current user other than the session's own.
* Revoke the logins of the current user other than the session's own.
*
* Wipe-pending tokens are kept too: revoking one cancels its pending wipe, so
* that stays a per-token decision.
* Wipe-pending logins are kept too: revoking one cancels its pending wipe, so
* that stays a per-login decision.
*/
#[NoSubAdminRequired]
#[NoAdminRequired]
Expand All @@ -214,23 +208,26 @@ public function destroyOthers(): JSONResponse {
}

try {
$currentTokenId = $this->tokenProvider->getToken($this->session->getId())->getId();
$currentToken = $this->tokenProvider->getToken($this->session->getId());
$currentLoginId = $this->tokenProvider->getLoginId($currentToken);
} catch (SessionNotAvailableException|InvalidTokenException) {
return $this->getServiceNotAvailableResponse();
}

$revoked = [];
foreach ($this->tokenProvider->getTokenByUser($this->userId) as $token) {
if ($token->getId() === $currentTokenId || $token->getType() === IToken::WIPE_TOKEN) {
foreach ($this->tokenProvider->getLoginsOfUser($this->userId) as $loginId => $token) {
if ($loginId === $currentLoginId || $token->getType() === IToken::WIPE_TOKEN) {
continue;
}

$this->tokenProvider->invalidateTokenById($this->userId, $token->getId());
$revoked[] = $token->getId();
$this->tokenProvider->invalidateTokenById($this->userId, $loginId);
$revoked[] = $loginId;
}
// The other session tokens of this browser login
$this->tokenProvider->invalidateOtherTokensOfLogin($currentToken);

if ($revoked !== []) {
// One aggregate entry rather than one per token, so a bulk revoke does not bury the feed.
// One aggregate entry rather than one per login, so a bulk revoke does not bury the feed.
$this->publishActivity(Provider::APP_TOKEN_DELETED_ALL, null, ['count' => count($revoked)]);
}

Expand All @@ -245,9 +242,8 @@ public function update(int $id, array $scope, string $name): JSONResponse {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}

try {
$token = $this->findTokenByIdAndUser($id);
} catch (InvalidTokenException $e) {
$token = $this->tokenProvider->getLoginOfUser($this->userId, $id);
if ($token === null || $token->getType() === IToken::WIPE_TOKEN) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}

Expand All @@ -260,31 +256,35 @@ public function update(int $id, array $scope, string $name): JSONResponse {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}

$changedScope = null;
if ($newScope !== $currentScope) {
$token->setScope($newScope);
$changedScope = $newScope;
if ($this->tokenScopes->fromBlob($newScope) !== $this->tokenScopes->fromBlob($currentScope)) {
$this->publishActivity(Provider::APP_TOKEN_SCOPES_CHANGED, $token->getId(), ['name' => $currentName]);
$this->publishActivity(Provider::APP_TOKEN_SCOPES_CHANGED, $id, ['name' => $currentName]);
} elseif ($newScope[IToken::SCOPE_FILESYSTEM] !== ($currentScope[IToken::SCOPE_FILESYSTEM] ?? false)) {
// Only legacy tokens without scopes flip filesystem on its own
$this->publishActivity($newScope[IToken::SCOPE_FILESYSTEM] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]);
$this->publishActivity($newScope[IToken::SCOPE_FILESYSTEM] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $id, ['name' => $currentName]);
}
}

if (mb_strlen($name) > 128) {
$name = mb_substr($name, 0, 120) . '…';
}

$newName = null;
if ($token instanceof INamedToken && $name !== $currentName) {
$token->setName($name);
$this->publishActivity(Provider::APP_TOKEN_RENAMED, $token->getId(), ['name' => $currentName, 'newName' => $name]);
$newName = $name;
$this->publishActivity(Provider::APP_TOKEN_RENAMED, $id, ['name' => $currentName, 'newName' => $name]);
}

$this->tokenProvider->updateToken($token);
if ($newName !== null || $changedScope !== null) {
$this->tokenProvider->updateLogin($this->userId, $id, $newName, $changedScope);
}
return new JSONResponse(['scope' => $newScope]);
}

/**
* @param int|null $id Token the event is about, or null for events that span several tokens
* @param int|null $id Login the event is about, or null for events that span several logins
*/
private function publishActivity(string $subject, ?int $id, array $parameters = []): void {
$event = $this->activityManager->generateEvent();
Expand All @@ -305,27 +305,8 @@ private function publishActivity(string $subject, ?int $id, array $parameters =
}
}

/**
* Find a token by given id and check if uid for current session belongs to this token
*
* @throws InvalidTokenException
*/
private function findTokenByIdAndUser(int $id): IToken {
try {
$token = $this->tokenProvider->getTokenById($id);
} catch (ExpiredTokenException $e) {
$token = $e->getToken();
}
if ($token->getUID() !== $this->userId) {
/** @psalm-suppress DeprecatedClass We have to throw the OC version so both OC and OCP catches catch it */
throw new OcInvalidTokenException('This token does not belong to you!');
}
return $token;
}

/**
* @throws InvalidTokenException
* @throws ExpiredTokenException
*/
#[NoSubAdminRequired]
#[NoAdminRequired]
Expand All @@ -335,9 +316,8 @@ public function wipe(int $id): JSONResponse {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}

try {
$token = $this->findTokenByIdAndUser($id);
} catch (InvalidTokenException $e) {
$token = $this->tokenProvider->getLoginOfUser($this->userId, $id);
if ($token === null || $token->getType() === IToken::WIPE_TOKEN) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}

Expand Down
23 changes: 14 additions & 9 deletions apps/settings/lib/Settings/Personal/Security/Authtokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@

use OC\Authentication\Token\INamedToken;
use OC\Authentication\Token\IProvider as IAuthTokenProvider;
use OC\Authentication\Token\IToken;
use OCA\Settings\AppInfo\Application;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Token\IToken;
use OCP\IConfig;
use OCP\ISession;
use OCP\IUserSession;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\Settings\ISettings;
use OCP\Util;
use function array_map;

class Authtokens implements ISettings {

Expand Down Expand Up @@ -65,30 +64,36 @@ public function getPriority(): int {
return 100;
}

/**
* One entry per login, so a browser keeps one entry with the same id while it renews its session token
*/
private function getAppTokens(): array {
$tokens = $this->tokenProvider->getTokenByUser($this->userId);
$logins = $this->tokenProvider->getLoginsOfUser($this->userId);

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

return array_map(function (IToken $token) use ($sessionToken) {
$entries = [];
foreach ($logins as $loginId => $token) {
$data = $token->jsonSerialize();
$data['id'] = $loginId;
$data['canDelete'] = true;
$data['canRename'] = $token instanceof INamedToken && $data['type'] !== IToken::WIPE_TOKEN;
if ($sessionToken->getId() === $token->getId()) {
$data['canRename'] = $token instanceof INamedToken && $token->getType() !== IToken::WIPE_TOKEN;
if ($loginId === $currentLoginId) {
$data['canDelete'] = false;
$data['canRename'] = false;
$data['current'] = true;
}
return $data;
}, $tokens);
$entries[] = $data;
}
return $entries;
}
}
Loading