@@ -56,249 +56,249 @@ |
||
| 56 | 56 | |
| 57 | 57 | class AuthSettingsController extends Controller { |
| 58 | 58 | |
| 59 | - /** @var IProvider */ |
|
| 60 | - private $tokenProvider; |
|
| 61 | - |
|
| 62 | - /** @var ISession */ |
|
| 63 | - private $session; |
|
| 64 | - |
|
| 65 | - /** IUserSession */ |
|
| 66 | - private $userSession; |
|
| 67 | - |
|
| 68 | - /** @var string */ |
|
| 69 | - private $uid; |
|
| 70 | - |
|
| 71 | - /** @var ISecureRandom */ |
|
| 72 | - private $random; |
|
| 73 | - |
|
| 74 | - /** @var IManager */ |
|
| 75 | - private $activityManager; |
|
| 76 | - |
|
| 77 | - /** @var RemoteWipe */ |
|
| 78 | - private $remoteWipe; |
|
| 79 | - |
|
| 80 | - /** @var ILogger */ |
|
| 81 | - private $logger; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @param string $appName |
|
| 85 | - * @param IRequest $request |
|
| 86 | - * @param IProvider $tokenProvider |
|
| 87 | - * @param ISession $session |
|
| 88 | - * @param ISecureRandom $random |
|
| 89 | - * @param string|null $userId |
|
| 90 | - * @param IUserSession $userSession |
|
| 91 | - * @param IManager $activityManager |
|
| 92 | - * @param RemoteWipe $remoteWipe |
|
| 93 | - * @param ILogger $logger |
|
| 94 | - */ |
|
| 95 | - public function __construct(string $appName, |
|
| 96 | - IRequest $request, |
|
| 97 | - IProvider $tokenProvider, |
|
| 98 | - ISession $session, |
|
| 99 | - ISecureRandom $random, |
|
| 100 | - ?string $userId, |
|
| 101 | - IUserSession $userSession, |
|
| 102 | - IManager $activityManager, |
|
| 103 | - RemoteWipe $remoteWipe, |
|
| 104 | - ILogger $logger) { |
|
| 105 | - parent::__construct($appName, $request); |
|
| 106 | - $this->tokenProvider = $tokenProvider; |
|
| 107 | - $this->uid = $userId; |
|
| 108 | - $this->userSession = $userSession; |
|
| 109 | - $this->session = $session; |
|
| 110 | - $this->random = $random; |
|
| 111 | - $this->activityManager = $activityManager; |
|
| 112 | - $this->remoteWipe = $remoteWipe; |
|
| 113 | - $this->logger = $logger; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @NoAdminRequired |
|
| 118 | - * @NoSubadminRequired |
|
| 119 | - * @PasswordConfirmationRequired |
|
| 120 | - * |
|
| 121 | - * @param string $name |
|
| 122 | - * @return JSONResponse |
|
| 123 | - */ |
|
| 124 | - public function create($name) { |
|
| 125 | - try { |
|
| 126 | - $sessionId = $this->session->getId(); |
|
| 127 | - } catch (SessionNotAvailableException $ex) { |
|
| 128 | - return $this->getServiceNotAvailableResponse(); |
|
| 129 | - } |
|
| 130 | - if ($this->userSession->getImpersonatingUserID() !== null) |
|
| 131 | - { |
|
| 132 | - return $this->getServiceNotAvailableResponse(); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - try { |
|
| 136 | - $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
| 137 | - $loginName = $sessionToken->getLoginName(); |
|
| 138 | - try { |
|
| 139 | - $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
| 140 | - } catch (PasswordlessTokenException $ex) { |
|
| 141 | - $password = null; |
|
| 142 | - } |
|
| 143 | - } catch (InvalidTokenException $ex) { |
|
| 144 | - return $this->getServiceNotAvailableResponse(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - $token = $this->generateRandomDeviceToken(); |
|
| 148 | - $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN); |
|
| 149 | - $tokenData = $deviceToken->jsonSerialize(); |
|
| 150 | - $tokenData['canDelete'] = true; |
|
| 151 | - $tokenData['canRename'] = true; |
|
| 152 | - |
|
| 153 | - $this->publishActivity(Provider::APP_TOKEN_CREATED, $deviceToken->getId(), ['name' => $deviceToken->getName()]); |
|
| 154 | - |
|
| 155 | - return new JSONResponse([ |
|
| 156 | - 'token' => $token, |
|
| 157 | - 'loginName' => $loginName, |
|
| 158 | - 'deviceToken' => $tokenData, |
|
| 159 | - ]); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @return JSONResponse |
|
| 164 | - */ |
|
| 165 | - private function getServiceNotAvailableResponse() { |
|
| 166 | - $resp = new JSONResponse(); |
|
| 167 | - $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); |
|
| 168 | - return $resp; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Return a 25 digit device password |
|
| 173 | - * |
|
| 174 | - * Example: AbCdE-fGhJk-MnPqR-sTwXy-23456 |
|
| 175 | - * |
|
| 176 | - * @return string |
|
| 177 | - */ |
|
| 178 | - private function generateRandomDeviceToken() { |
|
| 179 | - $groups = []; |
|
| 180 | - for ($i = 0; $i < 5; $i++) { |
|
| 181 | - $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 182 | - } |
|
| 183 | - return implode('-', $groups); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * @NoAdminRequired |
|
| 188 | - * @NoSubadminRequired |
|
| 189 | - * |
|
| 190 | - * @param int $id |
|
| 191 | - * @return array|JSONResponse |
|
| 192 | - */ |
|
| 193 | - public function destroy($id) { |
|
| 194 | - try { |
|
| 195 | - $token = $this->findTokenByIdAndUser($id); |
|
| 196 | - } catch (WipeTokenException $e) { |
|
| 197 | - //continue as we can destroy tokens in wipe |
|
| 198 | - $token = $e->getToken(); |
|
| 199 | - } catch (InvalidTokenException $e) { |
|
| 200 | - return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - $this->tokenProvider->invalidateTokenById($this->uid, $token->getId()); |
|
| 204 | - $this->publishActivity(Provider::APP_TOKEN_DELETED, $token->getId(), ['name' => $token->getName()]); |
|
| 205 | - return []; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @NoAdminRequired |
|
| 210 | - * @NoSubadminRequired |
|
| 211 | - * |
|
| 212 | - * @param int $id |
|
| 213 | - * @param array $scope |
|
| 214 | - * @param string $name |
|
| 215 | - * @return array|JSONResponse |
|
| 216 | - */ |
|
| 217 | - public function update($id, array $scope, string $name) { |
|
| 218 | - try { |
|
| 219 | - $token = $this->findTokenByIdAndUser($id); |
|
| 220 | - } catch (InvalidTokenException $e) { |
|
| 221 | - return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $currentName = $token->getName(); |
|
| 225 | - |
|
| 226 | - if ($scope !== $token->getScopeAsArray()) { |
|
| 227 | - $token->setScope(['filesystem' => $scope['filesystem']]); |
|
| 228 | - $this->publishActivity($scope['filesystem'] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - if ($token instanceof INamedToken && $name !== $currentName) { |
|
| 232 | - $token->setName($name); |
|
| 233 | - $this->publishActivity(Provider::APP_TOKEN_RENAMED, $token->getId(), ['name' => $currentName, 'newName' => $name]); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - $this->tokenProvider->updateToken($token); |
|
| 237 | - return []; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * @param string $subject |
|
| 242 | - * @param int $id |
|
| 243 | - * @param array $parameters |
|
| 244 | - */ |
|
| 245 | - private function publishActivity(string $subject, int $id, array $parameters = []): void { |
|
| 246 | - $event = $this->activityManager->generateEvent(); |
|
| 247 | - $event->setApp('settings') |
|
| 248 | - ->setType('security') |
|
| 249 | - ->setAffectedUser($this->uid) |
|
| 250 | - ->setAuthor($this->uid) |
|
| 251 | - ->setSubject($subject, $parameters) |
|
| 252 | - ->setObject('app_token', $id, 'App Password'); |
|
| 253 | - |
|
| 254 | - try { |
|
| 255 | - $this->activityManager->publish($event); |
|
| 256 | - } catch (BadMethodCallException $e) { |
|
| 257 | - $this->logger->warning('could not publish activity'); |
|
| 258 | - $this->logger->logException($e); |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Find a token by given id and check if uid for current session belongs to this token |
|
| 264 | - * |
|
| 265 | - * @param int $id |
|
| 266 | - * @return IToken |
|
| 267 | - * @throws InvalidTokenException |
|
| 268 | - */ |
|
| 269 | - private function findTokenByIdAndUser(int $id): IToken { |
|
| 270 | - try { |
|
| 271 | - $token = $this->tokenProvider->getTokenById($id); |
|
| 272 | - } catch (ExpiredTokenException $e) { |
|
| 273 | - $token = $e->getToken(); |
|
| 274 | - } |
|
| 275 | - if ($token->getUID() !== $this->uid) { |
|
| 276 | - throw new InvalidTokenException('This token does not belong to you!'); |
|
| 277 | - } |
|
| 278 | - return $token; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * @NoAdminRequired |
|
| 283 | - * @NoSubadminRequired |
|
| 284 | - * @PasswordConfirmationRequired |
|
| 285 | - * |
|
| 286 | - * @param int $id |
|
| 287 | - * @return JSONResponse |
|
| 288 | - * @throws InvalidTokenException |
|
| 289 | - * @throws \OC\Authentication\Exceptions\ExpiredTokenException |
|
| 290 | - */ |
|
| 291 | - public function wipe(int $id): JSONResponse { |
|
| 292 | - try { |
|
| 293 | - $token = $this->findTokenByIdAndUser($id); |
|
| 294 | - } catch (InvalidTokenException $e) { |
|
| 295 | - return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - if (!$this->remoteWipe->markTokenForWipe($token)) { |
|
| 299 | - return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - return new JSONResponse([]); |
|
| 303 | - } |
|
| 59 | + /** @var IProvider */ |
|
| 60 | + private $tokenProvider; |
|
| 61 | + |
|
| 62 | + /** @var ISession */ |
|
| 63 | + private $session; |
|
| 64 | + |
|
| 65 | + /** IUserSession */ |
|
| 66 | + private $userSession; |
|
| 67 | + |
|
| 68 | + /** @var string */ |
|
| 69 | + private $uid; |
|
| 70 | + |
|
| 71 | + /** @var ISecureRandom */ |
|
| 72 | + private $random; |
|
| 73 | + |
|
| 74 | + /** @var IManager */ |
|
| 75 | + private $activityManager; |
|
| 76 | + |
|
| 77 | + /** @var RemoteWipe */ |
|
| 78 | + private $remoteWipe; |
|
| 79 | + |
|
| 80 | + /** @var ILogger */ |
|
| 81 | + private $logger; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @param string $appName |
|
| 85 | + * @param IRequest $request |
|
| 86 | + * @param IProvider $tokenProvider |
|
| 87 | + * @param ISession $session |
|
| 88 | + * @param ISecureRandom $random |
|
| 89 | + * @param string|null $userId |
|
| 90 | + * @param IUserSession $userSession |
|
| 91 | + * @param IManager $activityManager |
|
| 92 | + * @param RemoteWipe $remoteWipe |
|
| 93 | + * @param ILogger $logger |
|
| 94 | + */ |
|
| 95 | + public function __construct(string $appName, |
|
| 96 | + IRequest $request, |
|
| 97 | + IProvider $tokenProvider, |
|
| 98 | + ISession $session, |
|
| 99 | + ISecureRandom $random, |
|
| 100 | + ?string $userId, |
|
| 101 | + IUserSession $userSession, |
|
| 102 | + IManager $activityManager, |
|
| 103 | + RemoteWipe $remoteWipe, |
|
| 104 | + ILogger $logger) { |
|
| 105 | + parent::__construct($appName, $request); |
|
| 106 | + $this->tokenProvider = $tokenProvider; |
|
| 107 | + $this->uid = $userId; |
|
| 108 | + $this->userSession = $userSession; |
|
| 109 | + $this->session = $session; |
|
| 110 | + $this->random = $random; |
|
| 111 | + $this->activityManager = $activityManager; |
|
| 112 | + $this->remoteWipe = $remoteWipe; |
|
| 113 | + $this->logger = $logger; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @NoAdminRequired |
|
| 118 | + * @NoSubadminRequired |
|
| 119 | + * @PasswordConfirmationRequired |
|
| 120 | + * |
|
| 121 | + * @param string $name |
|
| 122 | + * @return JSONResponse |
|
| 123 | + */ |
|
| 124 | + public function create($name) { |
|
| 125 | + try { |
|
| 126 | + $sessionId = $this->session->getId(); |
|
| 127 | + } catch (SessionNotAvailableException $ex) { |
|
| 128 | + return $this->getServiceNotAvailableResponse(); |
|
| 129 | + } |
|
| 130 | + if ($this->userSession->getImpersonatingUserID() !== null) |
|
| 131 | + { |
|
| 132 | + return $this->getServiceNotAvailableResponse(); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + try { |
|
| 136 | + $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
| 137 | + $loginName = $sessionToken->getLoginName(); |
|
| 138 | + try { |
|
| 139 | + $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
| 140 | + } catch (PasswordlessTokenException $ex) { |
|
| 141 | + $password = null; |
|
| 142 | + } |
|
| 143 | + } catch (InvalidTokenException $ex) { |
|
| 144 | + return $this->getServiceNotAvailableResponse(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + $token = $this->generateRandomDeviceToken(); |
|
| 148 | + $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN); |
|
| 149 | + $tokenData = $deviceToken->jsonSerialize(); |
|
| 150 | + $tokenData['canDelete'] = true; |
|
| 151 | + $tokenData['canRename'] = true; |
|
| 152 | + |
|
| 153 | + $this->publishActivity(Provider::APP_TOKEN_CREATED, $deviceToken->getId(), ['name' => $deviceToken->getName()]); |
|
| 154 | + |
|
| 155 | + return new JSONResponse([ |
|
| 156 | + 'token' => $token, |
|
| 157 | + 'loginName' => $loginName, |
|
| 158 | + 'deviceToken' => $tokenData, |
|
| 159 | + ]); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @return JSONResponse |
|
| 164 | + */ |
|
| 165 | + private function getServiceNotAvailableResponse() { |
|
| 166 | + $resp = new JSONResponse(); |
|
| 167 | + $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); |
|
| 168 | + return $resp; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Return a 25 digit device password |
|
| 173 | + * |
|
| 174 | + * Example: AbCdE-fGhJk-MnPqR-sTwXy-23456 |
|
| 175 | + * |
|
| 176 | + * @return string |
|
| 177 | + */ |
|
| 178 | + private function generateRandomDeviceToken() { |
|
| 179 | + $groups = []; |
|
| 180 | + for ($i = 0; $i < 5; $i++) { |
|
| 181 | + $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 182 | + } |
|
| 183 | + return implode('-', $groups); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * @NoAdminRequired |
|
| 188 | + * @NoSubadminRequired |
|
| 189 | + * |
|
| 190 | + * @param int $id |
|
| 191 | + * @return array|JSONResponse |
|
| 192 | + */ |
|
| 193 | + public function destroy($id) { |
|
| 194 | + try { |
|
| 195 | + $token = $this->findTokenByIdAndUser($id); |
|
| 196 | + } catch (WipeTokenException $e) { |
|
| 197 | + //continue as we can destroy tokens in wipe |
|
| 198 | + $token = $e->getToken(); |
|
| 199 | + } catch (InvalidTokenException $e) { |
|
| 200 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + $this->tokenProvider->invalidateTokenById($this->uid, $token->getId()); |
|
| 204 | + $this->publishActivity(Provider::APP_TOKEN_DELETED, $token->getId(), ['name' => $token->getName()]); |
|
| 205 | + return []; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @NoAdminRequired |
|
| 210 | + * @NoSubadminRequired |
|
| 211 | + * |
|
| 212 | + * @param int $id |
|
| 213 | + * @param array $scope |
|
| 214 | + * @param string $name |
|
| 215 | + * @return array|JSONResponse |
|
| 216 | + */ |
|
| 217 | + public function update($id, array $scope, string $name) { |
|
| 218 | + try { |
|
| 219 | + $token = $this->findTokenByIdAndUser($id); |
|
| 220 | + } catch (InvalidTokenException $e) { |
|
| 221 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $currentName = $token->getName(); |
|
| 225 | + |
|
| 226 | + if ($scope !== $token->getScopeAsArray()) { |
|
| 227 | + $token->setScope(['filesystem' => $scope['filesystem']]); |
|
| 228 | + $this->publishActivity($scope['filesystem'] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + if ($token instanceof INamedToken && $name !== $currentName) { |
|
| 232 | + $token->setName($name); |
|
| 233 | + $this->publishActivity(Provider::APP_TOKEN_RENAMED, $token->getId(), ['name' => $currentName, 'newName' => $name]); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + $this->tokenProvider->updateToken($token); |
|
| 237 | + return []; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * @param string $subject |
|
| 242 | + * @param int $id |
|
| 243 | + * @param array $parameters |
|
| 244 | + */ |
|
| 245 | + private function publishActivity(string $subject, int $id, array $parameters = []): void { |
|
| 246 | + $event = $this->activityManager->generateEvent(); |
|
| 247 | + $event->setApp('settings') |
|
| 248 | + ->setType('security') |
|
| 249 | + ->setAffectedUser($this->uid) |
|
| 250 | + ->setAuthor($this->uid) |
|
| 251 | + ->setSubject($subject, $parameters) |
|
| 252 | + ->setObject('app_token', $id, 'App Password'); |
|
| 253 | + |
|
| 254 | + try { |
|
| 255 | + $this->activityManager->publish($event); |
|
| 256 | + } catch (BadMethodCallException $e) { |
|
| 257 | + $this->logger->warning('could not publish activity'); |
|
| 258 | + $this->logger->logException($e); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Find a token by given id and check if uid for current session belongs to this token |
|
| 264 | + * |
|
| 265 | + * @param int $id |
|
| 266 | + * @return IToken |
|
| 267 | + * @throws InvalidTokenException |
|
| 268 | + */ |
|
| 269 | + private function findTokenByIdAndUser(int $id): IToken { |
|
| 270 | + try { |
|
| 271 | + $token = $this->tokenProvider->getTokenById($id); |
|
| 272 | + } catch (ExpiredTokenException $e) { |
|
| 273 | + $token = $e->getToken(); |
|
| 274 | + } |
|
| 275 | + if ($token->getUID() !== $this->uid) { |
|
| 276 | + throw new InvalidTokenException('This token does not belong to you!'); |
|
| 277 | + } |
|
| 278 | + return $token; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * @NoAdminRequired |
|
| 283 | + * @NoSubadminRequired |
|
| 284 | + * @PasswordConfirmationRequired |
|
| 285 | + * |
|
| 286 | + * @param int $id |
|
| 287 | + * @return JSONResponse |
|
| 288 | + * @throws InvalidTokenException |
|
| 289 | + * @throws \OC\Authentication\Exceptions\ExpiredTokenException |
|
| 290 | + */ |
|
| 291 | + public function wipe(int $id): JSONResponse { |
|
| 292 | + try { |
|
| 293 | + $token = $this->findTokenByIdAndUser($id); |
|
| 294 | + } catch (InvalidTokenException $e) { |
|
| 295 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + if (!$this->remoteWipe->markTokenForWipe($token)) { |
|
| 299 | + return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + return new JSONResponse([]); |
|
| 303 | + } |
|
| 304 | 304 | } |
@@ -39,118 +39,118 @@ |
||
| 39 | 39 | |
| 40 | 40 | class RemoteWipe { |
| 41 | 41 | |
| 42 | - /** @var IProvider */ |
|
| 43 | - private $tokenProvider; |
|
| 44 | - |
|
| 45 | - /** @var IEventDispatcher */ |
|
| 46 | - private $eventDispatcher; |
|
| 47 | - |
|
| 48 | - /** @var ILogger */ |
|
| 49 | - private $logger; |
|
| 50 | - |
|
| 51 | - public function __construct(IProvider $tokenProvider, |
|
| 52 | - IEventDispatcher $eventDispatcher, |
|
| 53 | - ILogger $logger) { |
|
| 54 | - $this->tokenProvider = $tokenProvider; |
|
| 55 | - $this->eventDispatcher = $eventDispatcher; |
|
| 56 | - $this->logger = $logger; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param IToken $token |
|
| 61 | - * @return bool |
|
| 62 | - * |
|
| 63 | - * @throws InvalidTokenException |
|
| 64 | - * @throws WipeTokenException |
|
| 65 | - */ |
|
| 66 | - public function markTokenForWipe(IToken $token): bool { |
|
| 67 | - if (!$token instanceof IWipeableToken) { |
|
| 68 | - return false; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $token->wipe(); |
|
| 72 | - $this->tokenProvider->updateToken($token); |
|
| 73 | - |
|
| 74 | - return true; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param IUser $user |
|
| 79 | - * |
|
| 80 | - * @return bool true if any tokens have been marked for remote wipe |
|
| 81 | - */ |
|
| 82 | - public function markAllTokensForWipe(IUser $user): bool { |
|
| 83 | - $tokens = $this->tokenProvider->getTokenByUser($user->getUID()); |
|
| 84 | - |
|
| 85 | - /** @var IWipeableToken[] $wipeable */ |
|
| 86 | - $wipeable = array_filter($tokens, function (IToken $token) { |
|
| 87 | - return $token instanceof IWipeableToken; |
|
| 88 | - }); |
|
| 89 | - |
|
| 90 | - if (empty($wipeable)) { |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - foreach ($wipeable as $token) { |
|
| 95 | - $token->wipe(); |
|
| 96 | - $this->tokenProvider->updateToken($token); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return true; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param string $token |
|
| 104 | - * |
|
| 105 | - * @return bool whether wiping was started |
|
| 106 | - * @throws InvalidTokenException |
|
| 107 | - * |
|
| 108 | - */ |
|
| 109 | - public function start(string $token): bool { |
|
| 110 | - try { |
|
| 111 | - $this->tokenProvider->getToken($token); |
|
| 112 | - |
|
| 113 | - // We expect a WipedTokenException here. If we reach this point this |
|
| 114 | - // is an ordinary token |
|
| 115 | - return false; |
|
| 116 | - } catch (WipeTokenException $e) { |
|
| 117 | - // Expected -> continue below |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $dbToken = $e->getToken(); |
|
| 121 | - |
|
| 122 | - $this->logger->info("user " . $dbToken->getUID() . " started a remote wipe"); |
|
| 123 | - |
|
| 124 | - $this->eventDispatcher->dispatch(RemoteWipeStarted::class, new RemoteWipeStarted($dbToken)); |
|
| 125 | - |
|
| 126 | - return true; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param string $token |
|
| 131 | - * |
|
| 132 | - * @return bool whether wiping could be finished |
|
| 133 | - * @throws InvalidTokenException |
|
| 134 | - */ |
|
| 135 | - public function finish(string $token): bool { |
|
| 136 | - try { |
|
| 137 | - $this->tokenProvider->getToken($token); |
|
| 138 | - |
|
| 139 | - // We expect a WipedTokenException here. If we reach this point this |
|
| 140 | - // is an ordinary token |
|
| 141 | - return false; |
|
| 142 | - } catch (WipeTokenException $e) { |
|
| 143 | - // Expected -> continue below |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $dbToken = $e->getToken(); |
|
| 147 | - |
|
| 148 | - $this->tokenProvider->invalidateToken($token); |
|
| 149 | - |
|
| 150 | - $this->logger->info("user " . $dbToken->getUID() . " finished a remote wipe"); |
|
| 151 | - $this->eventDispatcher->dispatch(RemoteWipeFinished::class, new RemoteWipeFinished($dbToken)); |
|
| 152 | - |
|
| 153 | - return true; |
|
| 154 | - } |
|
| 42 | + /** @var IProvider */ |
|
| 43 | + private $tokenProvider; |
|
| 44 | + |
|
| 45 | + /** @var IEventDispatcher */ |
|
| 46 | + private $eventDispatcher; |
|
| 47 | + |
|
| 48 | + /** @var ILogger */ |
|
| 49 | + private $logger; |
|
| 50 | + |
|
| 51 | + public function __construct(IProvider $tokenProvider, |
|
| 52 | + IEventDispatcher $eventDispatcher, |
|
| 53 | + ILogger $logger) { |
|
| 54 | + $this->tokenProvider = $tokenProvider; |
|
| 55 | + $this->eventDispatcher = $eventDispatcher; |
|
| 56 | + $this->logger = $logger; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param IToken $token |
|
| 61 | + * @return bool |
|
| 62 | + * |
|
| 63 | + * @throws InvalidTokenException |
|
| 64 | + * @throws WipeTokenException |
|
| 65 | + */ |
|
| 66 | + public function markTokenForWipe(IToken $token): bool { |
|
| 67 | + if (!$token instanceof IWipeableToken) { |
|
| 68 | + return false; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $token->wipe(); |
|
| 72 | + $this->tokenProvider->updateToken($token); |
|
| 73 | + |
|
| 74 | + return true; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param IUser $user |
|
| 79 | + * |
|
| 80 | + * @return bool true if any tokens have been marked for remote wipe |
|
| 81 | + */ |
|
| 82 | + public function markAllTokensForWipe(IUser $user): bool { |
|
| 83 | + $tokens = $this->tokenProvider->getTokenByUser($user->getUID()); |
|
| 84 | + |
|
| 85 | + /** @var IWipeableToken[] $wipeable */ |
|
| 86 | + $wipeable = array_filter($tokens, function (IToken $token) { |
|
| 87 | + return $token instanceof IWipeableToken; |
|
| 88 | + }); |
|
| 89 | + |
|
| 90 | + if (empty($wipeable)) { |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + foreach ($wipeable as $token) { |
|
| 95 | + $token->wipe(); |
|
| 96 | + $this->tokenProvider->updateToken($token); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return true; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param string $token |
|
| 104 | + * |
|
| 105 | + * @return bool whether wiping was started |
|
| 106 | + * @throws InvalidTokenException |
|
| 107 | + * |
|
| 108 | + */ |
|
| 109 | + public function start(string $token): bool { |
|
| 110 | + try { |
|
| 111 | + $this->tokenProvider->getToken($token); |
|
| 112 | + |
|
| 113 | + // We expect a WipedTokenException here. If we reach this point this |
|
| 114 | + // is an ordinary token |
|
| 115 | + return false; |
|
| 116 | + } catch (WipeTokenException $e) { |
|
| 117 | + // Expected -> continue below |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $dbToken = $e->getToken(); |
|
| 121 | + |
|
| 122 | + $this->logger->info("user " . $dbToken->getUID() . " started a remote wipe"); |
|
| 123 | + |
|
| 124 | + $this->eventDispatcher->dispatch(RemoteWipeStarted::class, new RemoteWipeStarted($dbToken)); |
|
| 125 | + |
|
| 126 | + return true; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param string $token |
|
| 131 | + * |
|
| 132 | + * @return bool whether wiping could be finished |
|
| 133 | + * @throws InvalidTokenException |
|
| 134 | + */ |
|
| 135 | + public function finish(string $token): bool { |
|
| 136 | + try { |
|
| 137 | + $this->tokenProvider->getToken($token); |
|
| 138 | + |
|
| 139 | + // We expect a WipedTokenException here. If we reach this point this |
|
| 140 | + // is an ordinary token |
|
| 141 | + return false; |
|
| 142 | + } catch (WipeTokenException $e) { |
|
| 143 | + // Expected -> continue below |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + $dbToken = $e->getToken(); |
|
| 147 | + |
|
| 148 | + $this->tokenProvider->invalidateToken($token); |
|
| 149 | + |
|
| 150 | + $this->logger->info("user " . $dbToken->getUID() . " finished a remote wipe"); |
|
| 151 | + $this->eventDispatcher->dispatch(RemoteWipeFinished::class, new RemoteWipeFinished($dbToken)); |
|
| 152 | + |
|
| 153 | + return true; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | 156 | } |