2016-08-29 20:19:44 +03:00
|
|
|
<?php
|
2021-04-14 18:05:19 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
/**
|
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-08-29 20:19:44 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-08-29 20:19:44 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-10-03 22:47:33 +03:00
|
|
|
namespace OCA\TwoFactorBackupCodes\Service;
|
2016-08-29 20:19:44 +03:00
|
|
|
|
2016-10-03 22:47:33 +03:00
|
|
|
use OCA\TwoFactorBackupCodes\Db\BackupCode;
|
|
|
|
use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper;
|
2018-07-31 11:54:00 +03:00
|
|
|
use OCA\TwoFactorBackupCodes\Event\CodesGenerated;
|
2019-05-28 20:46:36 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2016-08-29 20:19:44 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\Security\IHasher;
|
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
|
|
|
|
class BackupCodeStorage {
|
2017-01-16 13:24:03 +03:00
|
|
|
private static $CODE_LENGTH = 16;
|
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
/** @var BackupCodeMapper */
|
|
|
|
private $mapper;
|
|
|
|
|
|
|
|
/** @var IHasher */
|
|
|
|
private $hasher;
|
|
|
|
|
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $random;
|
|
|
|
|
2019-05-28 20:46:36 +03:00
|
|
|
/** @var IEventDispatcher */
|
2018-07-31 11:54:00 +03:00
|
|
|
private $eventDispatcher;
|
2016-12-13 12:47:48 +03:00
|
|
|
|
2018-07-31 11:54:00 +03:00
|
|
|
public function __construct(BackupCodeMapper $mapper,
|
|
|
|
ISecureRandom $random,
|
|
|
|
IHasher $hasher,
|
2019-05-28 20:46:36 +03:00
|
|
|
IEventDispatcher $eventDispatcher) {
|
2016-08-29 20:19:44 +03:00
|
|
|
$this->mapper = $mapper;
|
|
|
|
$this->hasher = $hasher;
|
|
|
|
$this->random = $random;
|
2018-07-31 11:54:00 +03:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2016-08-29 20:19:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
2021-04-14 18:05:19 +03:00
|
|
|
* @param int $number
|
2016-08-29 20:19:44 +03:00
|
|
|
* @return string[]
|
|
|
|
*/
|
2021-04-14 18:05:19 +03:00
|
|
|
public function createCodes(IUser $user, int $number = 10): array {
|
2016-08-29 20:19:44 +03:00
|
|
|
$result = [];
|
|
|
|
|
|
|
|
// Delete existing ones
|
|
|
|
$this->mapper->deleteCodes($user);
|
|
|
|
|
|
|
|
$uid = $user->getUID();
|
|
|
|
foreach (range(1, min([$number, 20])) as $i) {
|
2019-10-21 22:51:17 +03:00
|
|
|
$code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_HUMAN_READABLE);
|
2016-08-29 20:19:44 +03:00
|
|
|
|
|
|
|
$dbCode = new BackupCode();
|
|
|
|
$dbCode->setUserId($uid);
|
|
|
|
$dbCode->setCode($this->hasher->hash($code));
|
|
|
|
$dbCode->setUsed(0);
|
|
|
|
$this->mapper->insert($dbCode);
|
|
|
|
|
2018-01-26 00:36:03 +03:00
|
|
|
$result[] = $code;
|
2016-08-29 20:19:44 +03:00
|
|
|
}
|
|
|
|
|
2020-08-18 17:25:19 +03:00
|
|
|
$this->eventDispatcher->dispatchTyped(new CodesGenerated($user));
|
2016-12-13 12:47:48 +03:00
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-04-14 18:05:19 +03:00
|
|
|
public function hasBackupCodes(IUser $user): bool {
|
2016-08-29 20:19:44 +03:00
|
|
|
$codes = $this->mapper->getBackupCodes($user);
|
|
|
|
return count($codes) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-04-14 18:05:19 +03:00
|
|
|
public function getBackupCodesState(IUser $user): array {
|
2016-08-29 20:19:44 +03:00
|
|
|
$codes = $this->mapper->getBackupCodes($user);
|
|
|
|
$total = count($codes);
|
|
|
|
$used = 0;
|
|
|
|
array_walk($codes, function (BackupCode $code) use (&$used) {
|
2018-07-31 11:54:00 +03:00
|
|
|
if (1 === (int)$code->getUsed()) {
|
2016-08-29 20:19:44 +03:00
|
|
|
$used++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return [
|
|
|
|
'enabled' => $total > 0,
|
|
|
|
'total' => $total,
|
|
|
|
'used' => $used,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @param string $code
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-04-14 18:05:19 +03:00
|
|
|
public function validateCode(IUser $user, string $code): bool {
|
2016-08-29 20:19:44 +03:00
|
|
|
$dbCodes = $this->mapper->getBackupCodes($user);
|
|
|
|
|
|
|
|
foreach ($dbCodes as $dbCode) {
|
2018-07-31 11:54:00 +03:00
|
|
|
if (0 === (int)$dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) {
|
2016-08-29 20:19:44 +03:00
|
|
|
$dbCode->setUsed(1);
|
|
|
|
$this->mapper->update($dbCode);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|