2016-05-18 06:03:22 -04:00
|
|
|
<?php
|
2024-06-03 04:23:34 -04:00
|
|
|
|
2016-05-18 06:03:22 -04:00
|
|
|
/**
|
2024-06-03 04:23:34 -04:00
|
|
|
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-05-18 06:03:22 -04:00
|
|
|
*/
|
2019-09-17 10:33:27 -04:00
|
|
|
namespace OCA\Settings\Controller;
|
2016-05-18 06:03:22 -04:00
|
|
|
|
2019-01-12 11:48:27 -05:00
|
|
|
use BadMethodCallException;
|
2024-01-04 06:20:14 -05:00
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException as OcInvalidTokenException;
|
2016-05-31 04:48:14 -04:00
|
|
|
use OC\Authentication\Exceptions\PasswordlessTokenException;
|
2019-01-21 14:45:27 -05:00
|
|
|
use OC\Authentication\Token\INamedToken;
|
2016-05-18 06:03:22 -04:00
|
|
|
use OC\Authentication\Token\IProvider;
|
2019-07-03 03:44:37 -04:00
|
|
|
use OC\Authentication\Token\RemoteWipe;
|
2019-09-17 10:33:27 -04:00
|
|
|
use OCA\Settings\Activity\Provider;
|
2019-01-12 11:48:27 -05:00
|
|
|
use OCP\Activity\IManager;
|
2016-05-18 06:03:22 -04:00
|
|
|
use OCP\AppFramework\Controller;
|
2019-04-03 10:00:46 -04:00
|
|
|
use OCP\AppFramework\Http;
|
2024-07-25 07:14:49 -04:00
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
|
|
|
|
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
|
2016-05-18 06:03:22 -04:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2024-01-04 06:20:14 -05:00
|
|
|
use OCP\Authentication\Exceptions\ExpiredTokenException;
|
|
|
|
|
use OCP\Authentication\Exceptions\InvalidTokenException;
|
|
|
|
|
use OCP\Authentication\Exceptions\WipeTokenException;
|
2024-01-08 12:01:23 -05:00
|
|
|
use OCP\Authentication\Token\IToken;
|
2016-05-18 06:03:22 -04:00
|
|
|
use OCP\IRequest;
|
2016-05-18 12:25:05 -04:00
|
|
|
use OCP\ISession;
|
2019-06-12 08:26:01 -04:00
|
|
|
use OCP\IUserSession;
|
2016-05-18 12:25:05 -04:00
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException;
|
2021-03-01 14:53:41 -05:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-05-18 06:03:22 -04:00
|
|
|
|
|
|
|
|
class AuthSettingsController extends Controller {
|
|
|
|
|
/** @var IProvider */
|
|
|
|
|
private $tokenProvider;
|
|
|
|
|
|
2016-05-18 12:25:05 -04:00
|
|
|
/** @var ISession */
|
|
|
|
|
private $session;
|
|
|
|
|
|
2023-10-23 04:57:26 -04:00
|
|
|
/** @var IUserSession */
|
2020-04-10 10:48:31 -04:00
|
|
|
private $userSession;
|
2019-06-12 08:26:01 -04:00
|
|
|
|
2016-05-18 06:03:22 -04:00
|
|
|
/** @var string */
|
|
|
|
|
private $uid;
|
|
|
|
|
|
2016-05-18 12:25:05 -04:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
|
private $random;
|
|
|
|
|
|
2019-01-12 11:48:27 -05:00
|
|
|
/** @var IManager */
|
|
|
|
|
private $activityManager;
|
|
|
|
|
|
2019-07-03 03:44:37 -04:00
|
|
|
/** @var RemoteWipe */
|
|
|
|
|
private $remoteWipe;
|
|
|
|
|
|
2021-03-01 14:53:41 -05:00
|
|
|
/** @var LoggerInterface */
|
2019-01-12 11:48:27 -05:00
|
|
|
private $logger;
|
|
|
|
|
|
2016-05-18 06:03:22 -04:00
|
|
|
/**
|
|
|
|
|
* @param string $appName
|
|
|
|
|
* @param IRequest $request
|
|
|
|
|
* @param IProvider $tokenProvider
|
2016-05-18 12:25:05 -04:00
|
|
|
* @param ISession $session
|
|
|
|
|
* @param ISecureRandom $random
|
2019-01-12 11:48:27 -05:00
|
|
|
* @param string|null $userId
|
2019-06-12 08:26:01 -04:00
|
|
|
* @param IUserSession $userSession
|
2019-01-12 11:48:27 -05:00
|
|
|
* @param IManager $activityManager
|
2019-07-03 03:44:37 -04:00
|
|
|
* @param RemoteWipe $remoteWipe
|
2021-03-01 14:53:41 -05:00
|
|
|
* @param LoggerInterface $logger
|
2016-05-18 06:03:22 -04:00
|
|
|
*/
|
2019-01-12 11:48:27 -05:00
|
|
|
public function __construct(string $appName,
|
2023-11-23 04:22:34 -05:00
|
|
|
IRequest $request,
|
|
|
|
|
IProvider $tokenProvider,
|
|
|
|
|
ISession $session,
|
|
|
|
|
ISecureRandom $random,
|
|
|
|
|
?string $userId,
|
|
|
|
|
IUserSession $userSession,
|
|
|
|
|
IManager $activityManager,
|
|
|
|
|
RemoteWipe $remoteWipe,
|
|
|
|
|
LoggerInterface $logger) {
|
2016-05-18 06:03:22 -04:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
$this->tokenProvider = $tokenProvider;
|
2016-10-05 10:31:28 -04:00
|
|
|
$this->uid = $userId;
|
2019-06-12 08:26:01 -04:00
|
|
|
$this->userSession = $userSession;
|
2016-05-18 12:25:05 -04:00
|
|
|
$this->session = $session;
|
|
|
|
|
$this->random = $random;
|
2019-01-12 11:48:27 -05:00
|
|
|
$this->activityManager = $activityManager;
|
2019-07-03 03:44:37 -04:00
|
|
|
$this->remoteWipe = $remoteWipe;
|
2019-01-12 11:48:27 -05:00
|
|
|
$this->logger = $logger;
|
2016-05-18 06:03:22 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-18 12:25:05 -04:00
|
|
|
/**
|
2020-06-23 14:18:38 -04:00
|
|
|
* @NoSubAdminRequired
|
2016-05-18 12:25:05 -04:00
|
|
|
*
|
2016-09-19 11:13:30 -04:00
|
|
|
* @param string $name
|
2016-05-18 12:25:05 -04:00
|
|
|
* @return JSONResponse
|
|
|
|
|
*/
|
2024-07-25 07:14:49 -04:00
|
|
|
#[NoAdminRequired]
|
|
|
|
|
#[PasswordConfirmationRequired]
|
2016-05-18 12:25:05 -04:00
|
|
|
public function create($name) {
|
2021-05-17 06:04:21 -04:00
|
|
|
if ($this->checkAppToken()) {
|
|
|
|
|
return $this->getServiceNotAvailableResponse();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 12:25:05 -04:00
|
|
|
try {
|
|
|
|
|
$sessionId = $this->session->getId();
|
|
|
|
|
} catch (SessionNotAvailableException $ex) {
|
2016-06-27 09:23:52 -04:00
|
|
|
return $this->getServiceNotAvailableResponse();
|
2016-05-18 12:25:05 -04:00
|
|
|
}
|
2020-04-10 08:19:56 -04:00
|
|
|
if ($this->userSession->getImpersonatingUserID() !== null) {
|
2019-06-12 08:26:01 -04:00
|
|
|
return $this->getServiceNotAvailableResponse();
|
|
|
|
|
}
|
2016-05-18 12:25:05 -04:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$sessionToken = $this->tokenProvider->getToken($sessionId);
|
2016-05-24 04:50:18 -04:00
|
|
|
$loginName = $sessionToken->getLoginName();
|
2016-05-31 04:48:14 -04:00
|
|
|
try {
|
|
|
|
|
$password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
|
|
|
|
|
} catch (PasswordlessTokenException $ex) {
|
|
|
|
|
$password = null;
|
|
|
|
|
}
|
2016-05-18 12:25:05 -04:00
|
|
|
} catch (InvalidTokenException $ex) {
|
2016-06-27 09:23:52 -04:00
|
|
|
return $this->getServiceNotAvailableResponse();
|
2016-05-18 12:25:05 -04:00
|
|
|
}
|
|
|
|
|
|
2022-03-22 05:51:54 -04:00
|
|
|
if (mb_strlen($name) > 128) {
|
|
|
|
|
$name = mb_substr($name, 0, 120) . '…';
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 12:25:05 -04:00
|
|
|
$token = $this->generateRandomDeviceToken();
|
2016-05-24 04:50:18 -04:00
|
|
|
$deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);
|
2016-08-03 10:24:12 -04:00
|
|
|
$tokenData = $deviceToken->jsonSerialize();
|
|
|
|
|
$tokenData['canDelete'] = true;
|
2019-01-20 15:30:51 -05:00
|
|
|
$tokenData['canRename'] = true;
|
2016-05-18 12:25:05 -04:00
|
|
|
|
2019-02-12 17:40:53 -05:00
|
|
|
$this->publishActivity(Provider::APP_TOKEN_CREATED, $deviceToken->getId(), ['name' => $deviceToken->getName()]);
|
2019-01-12 11:48:27 -05:00
|
|
|
|
2016-09-19 11:13:30 -04:00
|
|
|
return new JSONResponse([
|
2016-05-18 12:25:05 -04:00
|
|
|
'token' => $token,
|
2016-06-24 09:31:48 -04:00
|
|
|
'loginName' => $loginName,
|
2016-09-19 11:13:30 -04:00
|
|
|
'deviceToken' => $tokenData,
|
|
|
|
|
]);
|
2016-05-18 12:25:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-07-19 12:28:06 -04:00
|
|
|
/**
|
|
|
|
|
* @return JSONResponse
|
|
|
|
|
*/
|
2016-06-27 09:23:52 -04:00
|
|
|
private function getServiceNotAvailableResponse() {
|
|
|
|
|
$resp = new JSONResponse();
|
|
|
|
|
$resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
|
|
|
|
|
return $resp;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 12:25:05 -04:00
|
|
|
/**
|
2017-05-07 17:10:02 -04:00
|
|
|
* Return a 25 digit device password
|
2016-05-18 12:25:05 -04:00
|
|
|
*
|
2017-07-21 03:48:14 -04:00
|
|
|
* Example: AbCdE-fGhJk-MnPqR-sTwXy-23456
|
2016-05-18 12:25:05 -04:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function generateRandomDeviceToken() {
|
|
|
|
|
$groups = [];
|
2017-05-07 17:10:02 -04:00
|
|
|
for ($i = 0; $i < 5; $i++) {
|
|
|
|
|
$groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE);
|
2016-05-18 12:25:05 -04:00
|
|
|
}
|
|
|
|
|
return implode('-', $groups);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 06:04:21 -04:00
|
|
|
private function checkAppToken(): bool {
|
|
|
|
|
return $this->session->exists('app_password');
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 05:20:22 -04:00
|
|
|
/**
|
2020-06-23 14:18:38 -04:00
|
|
|
* @NoSubAdminRequired
|
2016-05-19 05:20:22 -04:00
|
|
|
*
|
2019-01-22 12:01:14 -05:00
|
|
|
* @param int $id
|
|
|
|
|
* @return array|JSONResponse
|
2016-05-19 05:20:22 -04:00
|
|
|
*/
|
2024-07-25 07:14:49 -04:00
|
|
|
#[NoAdminRequired]
|
2016-05-19 05:20:22 -04:00
|
|
|
public function destroy($id) {
|
2021-05-17 06:04:21 -04:00
|
|
|
if ($this->checkAppToken()) {
|
|
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 12:01:14 -05:00
|
|
|
try {
|
|
|
|
|
$token = $this->findTokenByIdAndUser($id);
|
2019-04-03 10:00:46 -04:00
|
|
|
} catch (WipeTokenException $e) {
|
|
|
|
|
//continue as we can destroy tokens in wipe
|
|
|
|
|
$token = $e->getToken();
|
2019-01-22 12:01:14 -05:00
|
|
|
} catch (InvalidTokenException $e) {
|
|
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->tokenProvider->invalidateTokenById($this->uid, $token->getId());
|
2019-02-12 17:40:53 -05:00
|
|
|
$this->publishActivity(Provider::APP_TOKEN_DELETED, $token->getId(), ['name' => $token->getName()]);
|
2016-05-19 05:20:22 -04:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-03 09:57:06 -04:00
|
|
|
/**
|
2020-06-23 14:18:38 -04:00
|
|
|
* @NoSubAdminRequired
|
2016-08-03 09:57:06 -04:00
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @param array $scope
|
2019-01-20 14:55:02 -05:00
|
|
|
* @param string $name
|
2018-01-09 05:41:08 -05:00
|
|
|
* @return array|JSONResponse
|
2016-08-03 09:57:06 -04:00
|
|
|
*/
|
2024-07-25 07:14:49 -04:00
|
|
|
#[NoAdminRequired]
|
2019-01-20 14:55:02 -05:00
|
|
|
public function update($id, array $scope, string $name) {
|
2021-05-17 06:04:21 -04:00
|
|
|
if ($this->checkAppToken()) {
|
|
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 05:41:08 -05:00
|
|
|
try {
|
2019-01-22 12:01:14 -05:00
|
|
|
$token = $this->findTokenByIdAndUser($id);
|
2018-01-09 05:41:08 -05:00
|
|
|
} catch (InvalidTokenException $e) {
|
|
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 17:40:53 -05:00
|
|
|
$currentName = $token->getName();
|
2019-01-22 12:01:14 -05:00
|
|
|
|
2019-02-12 17:40:53 -05:00
|
|
|
if ($scope !== $token->getScopeAsArray()) {
|
2024-03-15 07:51:31 -04:00
|
|
|
$token->setScope([IToken::SCOPE_FILESYSTEM => $scope[IToken::SCOPE_FILESYSTEM]]);
|
|
|
|
|
$this->publishActivity($scope[IToken::SCOPE_FILESYSTEM] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]);
|
2019-02-12 17:40:53 -05:00
|
|
|
}
|
2019-01-20 14:55:02 -05:00
|
|
|
|
2022-03-22 05:51:54 -04:00
|
|
|
if (mb_strlen($name) > 128) {
|
|
|
|
|
$name = mb_substr($name, 0, 120) . '…';
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 17:40:53 -05:00
|
|
|
if ($token instanceof INamedToken && $name !== $currentName) {
|
2019-01-20 14:55:02 -05:00
|
|
|
$token->setName($name);
|
2019-02-12 17:40:53 -05:00
|
|
|
$this->publishActivity(Provider::APP_TOKEN_RENAMED, $token->getId(), ['name' => $currentName, 'newName' => $name]);
|
2019-01-20 14:55:02 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-03 09:57:06 -04:00
|
|
|
$this->tokenProvider->updateToken($token);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2019-01-12 11:48:27 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $subject
|
|
|
|
|
* @param int $id
|
2019-02-12 17:40:53 -05:00
|
|
|
* @param array $parameters
|
2019-01-12 11:48:27 -05:00
|
|
|
*/
|
2019-02-12 17:40:53 -05:00
|
|
|
private function publishActivity(string $subject, int $id, array $parameters = []): void {
|
2019-01-12 11:48:27 -05:00
|
|
|
$event = $this->activityManager->generateEvent();
|
|
|
|
|
$event->setApp('settings')
|
|
|
|
|
->setType('security')
|
|
|
|
|
->setAffectedUser($this->uid)
|
|
|
|
|
->setAuthor($this->uid)
|
2019-02-12 17:40:53 -05:00
|
|
|
->setSubject($subject, $parameters)
|
2019-01-12 11:48:27 -05:00
|
|
|
->setObject('app_token', $id, 'App Password');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->activityManager->publish($event);
|
|
|
|
|
} catch (BadMethodCallException $e) {
|
2021-03-01 14:53:41 -05:00
|
|
|
$this->logger->warning('could not publish activity', ['exception' => $e]);
|
2019-01-12 11:48:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-22 12:01:14 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find a token by given id and check if uid for current session belongs to this token
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return IToken
|
|
|
|
|
* @throws InvalidTokenException
|
|
|
|
|
*/
|
|
|
|
|
private function findTokenByIdAndUser(int $id): IToken {
|
2019-10-03 20:28:48 -04:00
|
|
|
try {
|
|
|
|
|
$token = $this->tokenProvider->getTokenById($id);
|
|
|
|
|
} catch (ExpiredTokenException $e) {
|
|
|
|
|
$token = $e->getToken();
|
|
|
|
|
}
|
2019-01-22 12:01:14 -05:00
|
|
|
if ($token->getUID() !== $this->uid) {
|
2024-01-08 11:47:22 -05:00
|
|
|
/** @psalm-suppress DeprecatedClass We have to throw the OC version so both OC and OCP catches catch it */
|
2024-01-04 06:20:14 -05:00
|
|
|
throw new OcInvalidTokenException('This token does not belong to you!');
|
2019-01-22 12:01:14 -05:00
|
|
|
}
|
|
|
|
|
return $token;
|
|
|
|
|
}
|
2019-04-03 10:00:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2020-06-23 14:18:38 -04:00
|
|
|
* @NoSubAdminRequired
|
2019-04-03 10:00:46 -04:00
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return JSONResponse
|
|
|
|
|
* @throws InvalidTokenException
|
2024-01-04 06:20:14 -05:00
|
|
|
* @throws ExpiredTokenException
|
2019-04-03 10:00:46 -04:00
|
|
|
*/
|
2024-07-25 07:14:49 -04:00
|
|
|
#[NoAdminRequired]
|
|
|
|
|
#[PasswordConfirmationRequired]
|
2019-04-03 10:00:46 -04:00
|
|
|
public function wipe(int $id): JSONResponse {
|
2021-05-17 06:04:21 -04:00
|
|
|
if ($this->checkAppToken()) {
|
|
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 03:52:46 -04:00
|
|
|
try {
|
|
|
|
|
$token = $this->findTokenByIdAndUser($id);
|
|
|
|
|
} catch (InvalidTokenException $e) {
|
|
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$this->remoteWipe->markTokenForWipe($token)) {
|
2019-04-03 10:00:46 -04:00
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new JSONResponse([]);
|
|
|
|
|
}
|
2016-05-18 06:03:22 -04:00
|
|
|
}
|