2016-08-29 20:19:44 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-10-03 22:47:33 +03:00
|
|
|
namespace OCA\TwoFactorBackupCodes\Service;
|
2016-08-29 20:19:44 +03:00
|
|
|
|
2016-12-19 14:01:32 +03:00
|
|
|
use BadMethodCallException;
|
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;
|
2016-12-13 12:47:48 +03:00
|
|
|
use OCP\Activity\IManager;
|
2019-05-28 20:46:36 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2016-12-13 17:15:14 +03:00
|
|
|
use OCP\ILogger;
|
2016-08-29 20:19:44 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\Security\IHasher;
|
|
|
|
use OCP\Security\ISecureRandom;
|
2018-07-31 11:54:00 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2016-08-29 20:19:44 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function createCodes(IUser $user, $number = 10) {
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
// Delete existing ones
|
|
|
|
$this->mapper->deleteCodes($user);
|
|
|
|
|
|
|
|
$uid = $user->getUID();
|
|
|
|
foreach (range(1, min([$number, 20])) as $i) {
|
2017-01-16 15:14:15 +03:00
|
|
|
$code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
|
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
|
|
|
}
|
|
|
|
|
2018-07-31 11:54:00 +03:00
|
|
|
$this->eventDispatcher->dispatch(CodesGenerated::class, 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
|
|
|
|
*/
|
|
|
|
public function hasBackupCodes(IUser $user) {
|
|
|
|
$codes = $this->mapper->getBackupCodes($user);
|
|
|
|
return count($codes) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getBackupCodesState(IUser $user) {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function validateCode(IUser $user, $code) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|