@@ -35,41 +35,41 @@ |
||
| 35 | 35 | * @template-extends QBMapper<BackupCode> |
| 36 | 36 | */ |
| 37 | 37 | class BackupCodeMapper extends QBMapper { |
| 38 | - public function __construct(IDBConnection $db) { |
|
| 39 | - parent::__construct($db, 'twofactor_backupcodes'); |
|
| 40 | - } |
|
| 38 | + public function __construct(IDBConnection $db) { |
|
| 39 | + parent::__construct($db, 'twofactor_backupcodes'); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @param IUser $user |
|
| 44 | - * @return BackupCode[] |
|
| 45 | - */ |
|
| 46 | - public function getBackupCodes(IUser $user): array { |
|
| 47 | - /* @var IQueryBuilder $qb */ |
|
| 48 | - $qb = $this->db->getQueryBuilder(); |
|
| 42 | + /** |
|
| 43 | + * @param IUser $user |
|
| 44 | + * @return BackupCode[] |
|
| 45 | + */ |
|
| 46 | + public function getBackupCodes(IUser $user): array { |
|
| 47 | + /* @var IQueryBuilder $qb */ |
|
| 48 | + $qb = $this->db->getQueryBuilder(); |
|
| 49 | 49 | |
| 50 | - $qb->select('id', 'user_id', 'code', 'used') |
|
| 51 | - ->from('twofactor_backupcodes') |
|
| 52 | - ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID()))); |
|
| 50 | + $qb->select('id', 'user_id', 'code', 'used') |
|
| 51 | + ->from('twofactor_backupcodes') |
|
| 52 | + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID()))); |
|
| 53 | 53 | |
| 54 | - return self::findEntities($qb); |
|
| 55 | - } |
|
| 54 | + return self::findEntities($qb); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @param IUser $user |
|
| 59 | - */ |
|
| 60 | - public function deleteCodes(IUser $user): void { |
|
| 61 | - $this->deleteCodesByUserId($user->getUID()); |
|
| 62 | - } |
|
| 57 | + /** |
|
| 58 | + * @param IUser $user |
|
| 59 | + */ |
|
| 60 | + public function deleteCodes(IUser $user): void { |
|
| 61 | + $this->deleteCodesByUserId($user->getUID()); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * @param string $uid |
|
| 66 | - */ |
|
| 67 | - public function deleteCodesByUserId(string $uid): void { |
|
| 68 | - /* @var IQueryBuilder $qb */ |
|
| 69 | - $qb = $this->db->getQueryBuilder(); |
|
| 64 | + /** |
|
| 65 | + * @param string $uid |
|
| 66 | + */ |
|
| 67 | + public function deleteCodesByUserId(string $uid): void { |
|
| 68 | + /* @var IQueryBuilder $qb */ |
|
| 69 | + $qb = $this->db->getQueryBuilder(); |
|
| 70 | 70 | |
| 71 | - $qb->delete('twofactor_backupcodes') |
|
| 72 | - ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid))); |
|
| 73 | - $qb->execute(); |
|
| 74 | - } |
|
| 71 | + $qb->delete('twofactor_backupcodes') |
|
| 72 | + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid))); |
|
| 73 | + $qb->execute(); |
|
| 74 | + } |
|
| 75 | 75 | } |
@@ -36,27 +36,27 @@ |
||
| 36 | 36 | |
| 37 | 37 | class Version1002Date20170919123342 extends SimpleMigrationStep { |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param IOutput $output |
|
| 41 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 42 | - * @param array $options |
|
| 43 | - * @return null|ISchemaWrapper |
|
| 44 | - * @since 13.0.0 |
|
| 45 | - */ |
|
| 46 | - public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 47 | - /** @var ISchemaWrapper $schema */ |
|
| 48 | - $schema = $schemaClosure(); |
|
| 49 | - |
|
| 50 | - $table = $schema->getTable('twofactor_backupcodes'); |
|
| 51 | - $column = $table->getColumn('user_id'); |
|
| 52 | - $column->setDefault(''); |
|
| 53 | - |
|
| 54 | - $column = $table->getColumn('used'); |
|
| 55 | - if ($column->getType()->getName() !== Types::SMALLINT) { |
|
| 56 | - $column->setType(Type::getType(Types::SMALLINT)); |
|
| 57 | - $column->setOptions(['length' => 6]); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $schema; |
|
| 61 | - } |
|
| 39 | + /** |
|
| 40 | + * @param IOutput $output |
|
| 41 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 42 | + * @param array $options |
|
| 43 | + * @return null|ISchemaWrapper |
|
| 44 | + * @since 13.0.0 |
|
| 45 | + */ |
|
| 46 | + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 47 | + /** @var ISchemaWrapper $schema */ |
|
| 48 | + $schema = $schemaClosure(); |
|
| 49 | + |
|
| 50 | + $table = $schema->getTable('twofactor_backupcodes'); |
|
| 51 | + $column = $table->getColumn('user_id'); |
|
| 52 | + $column->setDefault(''); |
|
| 53 | + |
|
| 54 | + $column = $table->getColumn('used'); |
|
| 55 | + if ($column->getType()->getName() !== Types::SMALLINT) { |
|
| 56 | + $column->setType(Type::getType(Types::SMALLINT)); |
|
| 57 | + $column->setOptions(['length' => 6]); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $schema; |
|
| 61 | + } |
|
| 62 | 62 | } |
@@ -35,103 +35,103 @@ |
||
| 35 | 35 | use OCP\Security\ISecureRandom; |
| 36 | 36 | |
| 37 | 37 | class BackupCodeStorage { |
| 38 | - private static $CODE_LENGTH = 16; |
|
| 39 | - |
|
| 40 | - /** @var BackupCodeMapper */ |
|
| 41 | - private $mapper; |
|
| 42 | - |
|
| 43 | - /** @var IHasher */ |
|
| 44 | - private $hasher; |
|
| 45 | - |
|
| 46 | - /** @var ISecureRandom */ |
|
| 47 | - private $random; |
|
| 48 | - |
|
| 49 | - /** @var IEventDispatcher */ |
|
| 50 | - private $eventDispatcher; |
|
| 51 | - |
|
| 52 | - public function __construct(BackupCodeMapper $mapper, |
|
| 53 | - ISecureRandom $random, |
|
| 54 | - IHasher $hasher, |
|
| 55 | - IEventDispatcher $eventDispatcher) { |
|
| 56 | - $this->mapper = $mapper; |
|
| 57 | - $this->hasher = $hasher; |
|
| 58 | - $this->random = $random; |
|
| 59 | - $this->eventDispatcher = $eventDispatcher; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param IUser $user |
|
| 64 | - * @param int $number |
|
| 65 | - * @return string[] |
|
| 66 | - */ |
|
| 67 | - public function createCodes(IUser $user, int $number = 10): array { |
|
| 68 | - $result = []; |
|
| 69 | - |
|
| 70 | - // Delete existing ones |
|
| 71 | - $this->mapper->deleteCodes($user); |
|
| 72 | - |
|
| 73 | - $uid = $user->getUID(); |
|
| 74 | - foreach (range(1, min([$number, 20])) as $i) { |
|
| 75 | - $code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 76 | - |
|
| 77 | - $dbCode = new BackupCode(); |
|
| 78 | - $dbCode->setUserId($uid); |
|
| 79 | - $dbCode->setCode($this->hasher->hash($code)); |
|
| 80 | - $dbCode->setUsed(0); |
|
| 81 | - $this->mapper->insert($dbCode); |
|
| 82 | - |
|
| 83 | - $result[] = $code; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $this->eventDispatcher->dispatchTyped(new CodesGenerated($user)); |
|
| 87 | - |
|
| 88 | - return $result; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param IUser $user |
|
| 93 | - * @return bool |
|
| 94 | - */ |
|
| 95 | - public function hasBackupCodes(IUser $user): bool { |
|
| 96 | - $codes = $this->mapper->getBackupCodes($user); |
|
| 97 | - return count($codes) > 0; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @param IUser $user |
|
| 102 | - * @return array |
|
| 103 | - */ |
|
| 104 | - public function getBackupCodesState(IUser $user): array { |
|
| 105 | - $codes = $this->mapper->getBackupCodes($user); |
|
| 106 | - $total = count($codes); |
|
| 107 | - $used = 0; |
|
| 108 | - array_walk($codes, function (BackupCode $code) use (&$used) { |
|
| 109 | - if (1 === (int)$code->getUsed()) { |
|
| 110 | - $used++; |
|
| 111 | - } |
|
| 112 | - }); |
|
| 113 | - return [ |
|
| 114 | - 'enabled' => $total > 0, |
|
| 115 | - 'total' => $total, |
|
| 116 | - 'used' => $used, |
|
| 117 | - ]; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @param IUser $user |
|
| 122 | - * @param string $code |
|
| 123 | - * @return bool |
|
| 124 | - */ |
|
| 125 | - public function validateCode(IUser $user, string $code): bool { |
|
| 126 | - $dbCodes = $this->mapper->getBackupCodes($user); |
|
| 127 | - |
|
| 128 | - foreach ($dbCodes as $dbCode) { |
|
| 129 | - if (0 === (int)$dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) { |
|
| 130 | - $dbCode->setUsed(1); |
|
| 131 | - $this->mapper->update($dbCode); |
|
| 132 | - return true; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 38 | + private static $CODE_LENGTH = 16; |
|
| 39 | + |
|
| 40 | + /** @var BackupCodeMapper */ |
|
| 41 | + private $mapper; |
|
| 42 | + |
|
| 43 | + /** @var IHasher */ |
|
| 44 | + private $hasher; |
|
| 45 | + |
|
| 46 | + /** @var ISecureRandom */ |
|
| 47 | + private $random; |
|
| 48 | + |
|
| 49 | + /** @var IEventDispatcher */ |
|
| 50 | + private $eventDispatcher; |
|
| 51 | + |
|
| 52 | + public function __construct(BackupCodeMapper $mapper, |
|
| 53 | + ISecureRandom $random, |
|
| 54 | + IHasher $hasher, |
|
| 55 | + IEventDispatcher $eventDispatcher) { |
|
| 56 | + $this->mapper = $mapper; |
|
| 57 | + $this->hasher = $hasher; |
|
| 58 | + $this->random = $random; |
|
| 59 | + $this->eventDispatcher = $eventDispatcher; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param IUser $user |
|
| 64 | + * @param int $number |
|
| 65 | + * @return string[] |
|
| 66 | + */ |
|
| 67 | + public function createCodes(IUser $user, int $number = 10): array { |
|
| 68 | + $result = []; |
|
| 69 | + |
|
| 70 | + // Delete existing ones |
|
| 71 | + $this->mapper->deleteCodes($user); |
|
| 72 | + |
|
| 73 | + $uid = $user->getUID(); |
|
| 74 | + foreach (range(1, min([$number, 20])) as $i) { |
|
| 75 | + $code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 76 | + |
|
| 77 | + $dbCode = new BackupCode(); |
|
| 78 | + $dbCode->setUserId($uid); |
|
| 79 | + $dbCode->setCode($this->hasher->hash($code)); |
|
| 80 | + $dbCode->setUsed(0); |
|
| 81 | + $this->mapper->insert($dbCode); |
|
| 82 | + |
|
| 83 | + $result[] = $code; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $this->eventDispatcher->dispatchTyped(new CodesGenerated($user)); |
|
| 87 | + |
|
| 88 | + return $result; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param IUser $user |
|
| 93 | + * @return bool |
|
| 94 | + */ |
|
| 95 | + public function hasBackupCodes(IUser $user): bool { |
|
| 96 | + $codes = $this->mapper->getBackupCodes($user); |
|
| 97 | + return count($codes) > 0; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @param IUser $user |
|
| 102 | + * @return array |
|
| 103 | + */ |
|
| 104 | + public function getBackupCodesState(IUser $user): array { |
|
| 105 | + $codes = $this->mapper->getBackupCodes($user); |
|
| 106 | + $total = count($codes); |
|
| 107 | + $used = 0; |
|
| 108 | + array_walk($codes, function (BackupCode $code) use (&$used) { |
|
| 109 | + if (1 === (int)$code->getUsed()) { |
|
| 110 | + $used++; |
|
| 111 | + } |
|
| 112 | + }); |
|
| 113 | + return [ |
|
| 114 | + 'enabled' => $total > 0, |
|
| 115 | + 'total' => $total, |
|
| 116 | + 'used' => $used, |
|
| 117 | + ]; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @param IUser $user |
|
| 122 | + * @param string $code |
|
| 123 | + * @return bool |
|
| 124 | + */ |
|
| 125 | + public function validateCode(IUser $user, string $code): bool { |
|
| 126 | + $dbCodes = $this->mapper->getBackupCodes($user); |
|
| 127 | + |
|
| 128 | + foreach ($dbCodes as $dbCode) { |
|
| 129 | + if (0 === (int)$dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) { |
|
| 130 | + $dbCode->setUsed(1); |
|
| 131 | + $this->mapper->update($dbCode); |
|
| 132 | + return true; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | 137 | } |
@@ -33,36 +33,36 @@ |
||
| 33 | 33 | |
| 34 | 34 | class SettingsController extends Controller { |
| 35 | 35 | |
| 36 | - /** @var BackupCodeStorage */ |
|
| 37 | - private $storage; |
|
| 36 | + /** @var BackupCodeStorage */ |
|
| 37 | + private $storage; |
|
| 38 | 38 | |
| 39 | - /** @var IUserSession */ |
|
| 40 | - private $userSession; |
|
| 39 | + /** @var IUserSession */ |
|
| 40 | + private $userSession; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @param string $appName |
|
| 44 | - * @param IRequest $request |
|
| 45 | - * @param BackupCodeStorage $storage |
|
| 46 | - * @param IUserSession $userSession |
|
| 47 | - */ |
|
| 48 | - public function __construct($appName, IRequest $request, BackupCodeStorage $storage, IUserSession $userSession) { |
|
| 49 | - parent::__construct($appName, $request); |
|
| 50 | - $this->userSession = $userSession; |
|
| 51 | - $this->storage = $storage; |
|
| 52 | - } |
|
| 42 | + /** |
|
| 43 | + * @param string $appName |
|
| 44 | + * @param IRequest $request |
|
| 45 | + * @param BackupCodeStorage $storage |
|
| 46 | + * @param IUserSession $userSession |
|
| 47 | + */ |
|
| 48 | + public function __construct($appName, IRequest $request, BackupCodeStorage $storage, IUserSession $userSession) { |
|
| 49 | + parent::__construct($appName, $request); |
|
| 50 | + $this->userSession = $userSession; |
|
| 51 | + $this->storage = $storage; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @NoAdminRequired |
|
| 56 | - * @PasswordConfirmationRequired |
|
| 57 | - * |
|
| 58 | - * @return JSONResponse |
|
| 59 | - */ |
|
| 60 | - public function createCodes(): JSONResponse { |
|
| 61 | - $user = $this->userSession->getUser(); |
|
| 62 | - $codes = $this->storage->createCodes($user); |
|
| 63 | - return new JSONResponse([ |
|
| 64 | - 'codes' => $codes, |
|
| 65 | - 'state' => $this->storage->getBackupCodesState($user), |
|
| 66 | - ]); |
|
| 67 | - } |
|
| 54 | + /** |
|
| 55 | + * @NoAdminRequired |
|
| 56 | + * @PasswordConfirmationRequired |
|
| 57 | + * |
|
| 58 | + * @return JSONResponse |
|
| 59 | + */ |
|
| 60 | + public function createCodes(): JSONResponse { |
|
| 61 | + $user = $this->userSession->getUser(); |
|
| 62 | + $codes = $this->storage->createCodes($user); |
|
| 63 | + return new JSONResponse([ |
|
| 64 | + 'codes' => $codes, |
|
| 65 | + 'state' => $this->storage->getBackupCodesState($user), |
|
| 66 | + ]); |
|
| 67 | + } |
|
| 68 | 68 | } |
@@ -36,46 +36,46 @@ |
||
| 36 | 36 | |
| 37 | 37 | class Provider implements IProvider { |
| 38 | 38 | |
| 39 | - /** @var L10nFactory */ |
|
| 40 | - private $l10n; |
|
| 39 | + /** @var L10nFactory */ |
|
| 40 | + private $l10n; |
|
| 41 | 41 | |
| 42 | - /** @var IURLGenerator */ |
|
| 43 | - private $urlGenerator; |
|
| 42 | + /** @var IURLGenerator */ |
|
| 43 | + private $urlGenerator; |
|
| 44 | 44 | |
| 45 | - /** @var IManager */ |
|
| 46 | - private $activityManager; |
|
| 45 | + /** @var IManager */ |
|
| 46 | + private $activityManager; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @param L10nFactory $l10n |
|
| 50 | - * @param IURLGenerator $urlGenerator |
|
| 51 | - * @param IManager $activityManager |
|
| 52 | - */ |
|
| 53 | - public function __construct(L10nFactory $l10n, IURLGenerator $urlGenerator, IManager $activityManager) { |
|
| 54 | - $this->urlGenerator = $urlGenerator; |
|
| 55 | - $this->activityManager = $activityManager; |
|
| 56 | - $this->l10n = $l10n; |
|
| 57 | - } |
|
| 48 | + /** |
|
| 49 | + * @param L10nFactory $l10n |
|
| 50 | + * @param IURLGenerator $urlGenerator |
|
| 51 | + * @param IManager $activityManager |
|
| 52 | + */ |
|
| 53 | + public function __construct(L10nFactory $l10n, IURLGenerator $urlGenerator, IManager $activityManager) { |
|
| 54 | + $this->urlGenerator = $urlGenerator; |
|
| 55 | + $this->activityManager = $activityManager; |
|
| 56 | + $this->l10n = $l10n; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent { |
|
| 60 | - if ($event->getApp() !== 'twofactor_backupcodes') { |
|
| 61 | - throw new InvalidArgumentException(); |
|
| 62 | - } |
|
| 59 | + public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent { |
|
| 60 | + if ($event->getApp() !== 'twofactor_backupcodes') { |
|
| 61 | + throw new InvalidArgumentException(); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - $l = $this->l10n->get('twofactor_backupcodes', $language); |
|
| 64 | + $l = $this->l10n->get('twofactor_backupcodes', $language); |
|
| 65 | 65 | |
| 66 | - switch ($event->getSubject()) { |
|
| 67 | - case 'codes_generated': |
|
| 68 | - $event->setParsedSubject($l->t('You created two-factor backup codes for your account')); |
|
| 66 | + switch ($event->getSubject()) { |
|
| 67 | + case 'codes_generated': |
|
| 68 | + $event->setParsedSubject($l->t('You created two-factor backup codes for your account')); |
|
| 69 | 69 | |
| 70 | - if ($this->activityManager->getRequirePNG()) { |
|
| 71 | - $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png'))); |
|
| 72 | - } else { |
|
| 73 | - $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg'))); |
|
| 74 | - } |
|
| 75 | - break; |
|
| 76 | - default: |
|
| 77 | - throw new InvalidArgumentException(); |
|
| 78 | - } |
|
| 79 | - return $event; |
|
| 80 | - } |
|
| 70 | + if ($this->activityManager->getRequirePNG()) { |
|
| 71 | + $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png'))); |
|
| 72 | + } else { |
|
| 73 | + $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg'))); |
|
| 74 | + } |
|
| 75 | + break; |
|
| 76 | + default: |
|
| 77 | + throw new InvalidArgumentException(); |
|
| 78 | + } |
|
| 79 | + return $event; |
|
| 80 | + } |
|
| 81 | 81 | } |