@@ -34,102 +34,102 @@ |
||
| 34 | 34 | use OCP\Security\ISecureRandom; |
| 35 | 35 | |
| 36 | 36 | class BackupCodeStorage { |
| 37 | - private static $CODE_LENGTH = 16; |
|
| 38 | - |
|
| 39 | - /** @var BackupCodeMapper */ |
|
| 40 | - private $mapper; |
|
| 41 | - |
|
| 42 | - /** @var IHasher */ |
|
| 43 | - private $hasher; |
|
| 44 | - |
|
| 45 | - /** @var ISecureRandom */ |
|
| 46 | - private $random; |
|
| 47 | - |
|
| 48 | - /** @var IEventDispatcher */ |
|
| 49 | - private $eventDispatcher; |
|
| 50 | - |
|
| 51 | - public function __construct(BackupCodeMapper $mapper, |
|
| 52 | - ISecureRandom $random, |
|
| 53 | - IHasher $hasher, |
|
| 54 | - IEventDispatcher $eventDispatcher) { |
|
| 55 | - $this->mapper = $mapper; |
|
| 56 | - $this->hasher = $hasher; |
|
| 57 | - $this->random = $random; |
|
| 58 | - $this->eventDispatcher = $eventDispatcher; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param IUser $user |
|
| 63 | - * @return string[] |
|
| 64 | - */ |
|
| 65 | - public function createCodes(IUser $user, $number = 10) { |
|
| 66 | - $result = []; |
|
| 67 | - |
|
| 68 | - // Delete existing ones |
|
| 69 | - $this->mapper->deleteCodes($user); |
|
| 70 | - |
|
| 71 | - $uid = $user->getUID(); |
|
| 72 | - foreach (range(1, min([$number, 20])) as $i) { |
|
| 73 | - $code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 74 | - |
|
| 75 | - $dbCode = new BackupCode(); |
|
| 76 | - $dbCode->setUserId($uid); |
|
| 77 | - $dbCode->setCode($this->hasher->hash($code)); |
|
| 78 | - $dbCode->setUsed(0); |
|
| 79 | - $this->mapper->insert($dbCode); |
|
| 80 | - |
|
| 81 | - $result[] = $code; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $this->eventDispatcher->dispatchTyped(new CodesGenerated($user)); |
|
| 85 | - |
|
| 86 | - return $result; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param IUser $user |
|
| 91 | - * @return bool |
|
| 92 | - */ |
|
| 93 | - public function hasBackupCodes(IUser $user) { |
|
| 94 | - $codes = $this->mapper->getBackupCodes($user); |
|
| 95 | - return count($codes) > 0; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param IUser $user |
|
| 100 | - * @return array |
|
| 101 | - */ |
|
| 102 | - public function getBackupCodesState(IUser $user) { |
|
| 103 | - $codes = $this->mapper->getBackupCodes($user); |
|
| 104 | - $total = count($codes); |
|
| 105 | - $used = 0; |
|
| 106 | - array_walk($codes, function (BackupCode $code) use (&$used) { |
|
| 107 | - if (1 === (int)$code->getUsed()) { |
|
| 108 | - $used++; |
|
| 109 | - } |
|
| 110 | - }); |
|
| 111 | - return [ |
|
| 112 | - 'enabled' => $total > 0, |
|
| 113 | - 'total' => $total, |
|
| 114 | - 'used' => $used, |
|
| 115 | - ]; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param IUser $user |
|
| 120 | - * @param string $code |
|
| 121 | - * @return bool |
|
| 122 | - */ |
|
| 123 | - public function validateCode(IUser $user, $code) { |
|
| 124 | - $dbCodes = $this->mapper->getBackupCodes($user); |
|
| 125 | - |
|
| 126 | - foreach ($dbCodes as $dbCode) { |
|
| 127 | - if (0 === (int)$dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) { |
|
| 128 | - $dbCode->setUsed(1); |
|
| 129 | - $this->mapper->update($dbCode); |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - return false; |
|
| 134 | - } |
|
| 37 | + private static $CODE_LENGTH = 16; |
|
| 38 | + |
|
| 39 | + /** @var BackupCodeMapper */ |
|
| 40 | + private $mapper; |
|
| 41 | + |
|
| 42 | + /** @var IHasher */ |
|
| 43 | + private $hasher; |
|
| 44 | + |
|
| 45 | + /** @var ISecureRandom */ |
|
| 46 | + private $random; |
|
| 47 | + |
|
| 48 | + /** @var IEventDispatcher */ |
|
| 49 | + private $eventDispatcher; |
|
| 50 | + |
|
| 51 | + public function __construct(BackupCodeMapper $mapper, |
|
| 52 | + ISecureRandom $random, |
|
| 53 | + IHasher $hasher, |
|
| 54 | + IEventDispatcher $eventDispatcher) { |
|
| 55 | + $this->mapper = $mapper; |
|
| 56 | + $this->hasher = $hasher; |
|
| 57 | + $this->random = $random; |
|
| 58 | + $this->eventDispatcher = $eventDispatcher; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param IUser $user |
|
| 63 | + * @return string[] |
|
| 64 | + */ |
|
| 65 | + public function createCodes(IUser $user, $number = 10) { |
|
| 66 | + $result = []; |
|
| 67 | + |
|
| 68 | + // Delete existing ones |
|
| 69 | + $this->mapper->deleteCodes($user); |
|
| 70 | + |
|
| 71 | + $uid = $user->getUID(); |
|
| 72 | + foreach (range(1, min([$number, 20])) as $i) { |
|
| 73 | + $code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 74 | + |
|
| 75 | + $dbCode = new BackupCode(); |
|
| 76 | + $dbCode->setUserId($uid); |
|
| 77 | + $dbCode->setCode($this->hasher->hash($code)); |
|
| 78 | + $dbCode->setUsed(0); |
|
| 79 | + $this->mapper->insert($dbCode); |
|
| 80 | + |
|
| 81 | + $result[] = $code; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $this->eventDispatcher->dispatchTyped(new CodesGenerated($user)); |
|
| 85 | + |
|
| 86 | + return $result; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param IUser $user |
|
| 91 | + * @return bool |
|
| 92 | + */ |
|
| 93 | + public function hasBackupCodes(IUser $user) { |
|
| 94 | + $codes = $this->mapper->getBackupCodes($user); |
|
| 95 | + return count($codes) > 0; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param IUser $user |
|
| 100 | + * @return array |
|
| 101 | + */ |
|
| 102 | + public function getBackupCodesState(IUser $user) { |
|
| 103 | + $codes = $this->mapper->getBackupCodes($user); |
|
| 104 | + $total = count($codes); |
|
| 105 | + $used = 0; |
|
| 106 | + array_walk($codes, function (BackupCode $code) use (&$used) { |
|
| 107 | + if (1 === (int)$code->getUsed()) { |
|
| 108 | + $used++; |
|
| 109 | + } |
|
| 110 | + }); |
|
| 111 | + return [ |
|
| 112 | + 'enabled' => $total > 0, |
|
| 113 | + 'total' => $total, |
|
| 114 | + 'used' => $used, |
|
| 115 | + ]; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param IUser $user |
|
| 120 | + * @param string $code |
|
| 121 | + * @return bool |
|
| 122 | + */ |
|
| 123 | + public function validateCode(IUser $user, $code) { |
|
| 124 | + $dbCodes = $this->mapper->getBackupCodes($user); |
|
| 125 | + |
|
| 126 | + foreach ($dbCodes as $dbCode) { |
|
| 127 | + if (0 === (int)$dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) { |
|
| 128 | + $dbCode->setUsed(1); |
|
| 129 | + $this->mapper->update($dbCode); |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + return false; |
|
| 134 | + } |
|
| 135 | 135 | } |
@@ -49,7 +49,7 @@ |
||
| 49 | 49 | |
| 50 | 50 | // Load Viewer scripts |
| 51 | 51 | if (class_exists(LoadViewer::class)) { |
| 52 | - $eventDispatcher->dispatchTyped(new LoadViewer()); |
|
| 52 | + $eventDispatcher->dispatchTyped(new LoadViewer()); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | $tmpl->printPage(); |