From: Hypolite Petovan Date: Mon, 22 Jul 2019 11:41:01 +0000 (-0400) Subject: Move recovery code model to sub-folder X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a149d6ec4448ee6c38f2b6ee4c2e907cdf20d5b3;p=friendica.git Move recovery code model to sub-folder --- diff --git a/src/Model/TwoFactor/RecoveryCode.php b/src/Model/TwoFactor/RecoveryCode.php new file mode 100644 index 0000000000..676b538378 --- /dev/null +++ b/src/Model/TwoFactor/RecoveryCode.php @@ -0,0 +1,125 @@ + $uid, 'used' => null]); + } + + /** + * Checks the provided code is available to use for login by the provided user + * + * @param int $uid User ID + * @param string $code + * @return bool + * @throws \Exception + */ + public static function existsForUser($uid, $code) + { + return DBA::exists('2fa_recovery_codes', ['uid' => $uid, 'code' => $code, 'used' => null]); + } + + /** + * Returns a complete list of all recovery codes for the provided user, including the used status + * + * @param int $uid User ID + * @return array + * @throws \Exception + */ + public static function getListForUser($uid) + { + $codesStmt = DBA::select('2fa_recovery_codes', ['code', 'used'], ['uid' => $uid]); + + return DBA::toArray($codesStmt); + } + + /** + * Marks the provided code as used for the provided user. + * Returns false if the code doesn't exist for the user or it has been used already. + * + * @param int $uid User ID + * @param string $code + * @return bool + * @throws \Exception + */ + public static function markUsedForUser($uid, $code) + { + DBA::update('2fa_recovery_codes', ['used' => DateTimeFormat::utcNow()], ['uid' => $uid, 'code' => $code, 'used' => null]); + + return DBA::affectedRows() > 0; + } + + /** + * Generates a fresh set of recovery codes for the provided user. + * Generates 12 codes constituted of 2 blocks of 6 characters separated by a dash. + * + * @param int $uid User ID + * @throws \Exception + */ + public static function generateForUser($uid) + { + $Random = (new Random())->pattern('[a-z0-9]'); + + $RecoveryGenerator = new Recovery($Random); + + $codes = $RecoveryGenerator + ->setCount(12) + ->setBlocks(2) + ->setChars(6) + ->lowercase(true) + ->toArray(); + + $generated = DateTimeFormat::utcNow(); + foreach ($codes as $code) { + DBA::insert('2fa_recovery_codes', [ + 'uid' => $uid, + 'code' => $code, + 'generated' => $generated + ]); + } + } + + /** + * Deletes all the recovery codes for the provided user. + * + * @param int $uid User ID + * @throws \Exception + */ + public static function deleteForUser($uid) + { + DBA::delete('2fa_recovery_codes', ['uid' => $uid]); + } + + /** + * Replaces the existing recovery codes for the provided user by a freshly generated set. + * + * @param int $uid User ID + * @throws \Exception + */ + public static function regenerateForUser($uid) + { + self::deleteForUser($uid); + self::generateForUser($uid); + } +} diff --git a/src/Model/TwoFactorRecoveryCode.php b/src/Model/TwoFactorRecoveryCode.php deleted file mode 100644 index 74dead32d9..0000000000 --- a/src/Model/TwoFactorRecoveryCode.php +++ /dev/null @@ -1,125 +0,0 @@ - $uid, 'used' => null]); - } - - /** - * Checks the provided code is available to use for login by the provided user - * - * @param int $uid User ID - * @param string $code - * @return bool - * @throws \Exception - */ - public static function existsForUser($uid, $code) - { - return DBA::exists('2fa_recovery_codes', ['uid' => $uid, 'code' => $code, 'used' => null]); - } - - /** - * Returns a complete list of all recovery codes for the provided user, including the used status - * - * @param int $uid User ID - * @return array - * @throws \Exception - */ - public static function getListForUser($uid) - { - $codesStmt = DBA::select('2fa_recovery_codes', ['code', 'used'], ['uid' => $uid]); - - return DBA::toArray($codesStmt); - } - - /** - * Marks the provided code as used for the provided user. - * Returns false if the code doesn't exist for the user or it has been used already. - * - * @param int $uid User ID - * @param string $code - * @return bool - * @throws \Exception - */ - public static function markUsedForUser($uid, $code) - { - DBA::update('2fa_recovery_codes', ['used' => DateTimeFormat::utcNow()], ['uid' => $uid, 'code' => $code, 'used' => null]); - - return DBA::affectedRows() > 0; - } - - /** - * Generates a fresh set of recovery codes for the provided user. - * Generates 12 codes constituted of 2 blocks of 6 characters separated by a dash. - * - * @param int $uid User ID - * @throws \Exception - */ - public static function generateForUser($uid) - { - $Random = (new Random())->pattern('[a-z0-9]'); - - $RecoveryGenerator = new Recovery($Random); - - $codes = $RecoveryGenerator - ->setCount(12) - ->setBlocks(2) - ->setChars(6) - ->lowercase(true) - ->toArray(); - - $generated = DateTimeFormat::utcNow(); - foreach ($codes as $code) { - DBA::insert('2fa_recovery_codes', [ - 'uid' => $uid, - 'code' => $code, - 'generated' => $generated - ]); - } - } - - /** - * Deletes all the recovery codes for the provided user. - * - * @param int $uid User ID - * @throws \Exception - */ - public static function deleteForUser($uid) - { - DBA::delete('2fa_recovery_codes', ['uid' => $uid]); - } - - /** - * Replaces the existing recovery codes for the provided user by a freshly generated set. - * - * @param int $uid User ID - * @throws \Exception - */ - public static function regenerateForUser($uid) - { - self::deleteForUser($uid); - self::generateForUser($uid); - } -} diff --git a/src/Module/Settings/TwoFactor/Index.php b/src/Module/Settings/TwoFactor/Index.php index 9f02692706..0ae271a359 100644 --- a/src/Module/Settings/TwoFactor/Index.php +++ b/src/Module/Settings/TwoFactor/Index.php @@ -8,7 +8,7 @@ use Friendica\Core\L10n; use Friendica\Core\PConfig; use Friendica\Core\Renderer; use Friendica\Core\Session; -use Friendica\Model\TwoFactorRecoveryCode; +use Friendica\Model\TwoFactor\RecoveryCode; use Friendica\Model\User; use Friendica\Module\BaseSettingsModule; use Friendica\Module\Login; @@ -42,7 +42,7 @@ class Index extends BaseSettingsModule break; case 'disable': if ($has_secret) { - TwoFactorRecoveryCode::deleteForUser(local_user()); + RecoveryCode::deleteForUser(local_user()); PConfig::delete(local_user(), '2fa', 'secret'); PConfig::delete(local_user(), '2fa', 'verified'); Session::remove('2fa'); @@ -94,7 +94,7 @@ class Index extends BaseSettingsModule '$recovery_codes_title' => L10n::t('Recovery codes'), '$recovery_codes_remaining' => L10n::t('Remaining valid codes'), - '$recovery_codes_count' => TwoFactorRecoveryCode::countValidForUser(local_user()), + '$recovery_codes_count' => RecoveryCode::countValidForUser(local_user()), '$recovery_codes_message' => L10n::t('

These one-use codes can replace an authenticator app code in case you have lost access to it.

'), '$action_title' => L10n::t('Actions'), diff --git a/src/Module/Settings/TwoFactor/Recovery.php b/src/Module/Settings/TwoFactor/Recovery.php index a2d08cda86..6937fa503f 100644 --- a/src/Module/Settings/TwoFactor/Recovery.php +++ b/src/Module/Settings/TwoFactor/Recovery.php @@ -7,7 +7,7 @@ namespace Friendica\Module\Settings\TwoFactor; use Friendica\Core\L10n; use Friendica\Core\PConfig; use Friendica\Core\Renderer; -use Friendica\Model\TwoFactorRecoveryCode; +use Friendica\Model\TwoFactor\RecoveryCode; use Friendica\Module\BaseSettingsModule; use Friendica\Module\Login; @@ -46,7 +46,7 @@ class Recovery extends BaseSettingsModule self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery'); if ($_POST['action'] == 'regenerate') { - TwoFactorRecoveryCode::regenerateForUser(local_user()); + RecoveryCode::regenerateForUser(local_user()); notice(L10n::t('New recovery codes successfully generated.')); self::getApp()->internalRedirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password')); } @@ -61,11 +61,11 @@ class Recovery extends BaseSettingsModule parent::content(); - if (!TwoFactorRecoveryCode::countValidForUser(local_user())) { - TwoFactorRecoveryCode::generateForUser(local_user()); + if (!RecoveryCode::countValidForUser(local_user())) { + RecoveryCode::generateForUser(local_user()); } - $recoveryCodes = TwoFactorRecoveryCode::getListForUser(local_user()); + $recoveryCodes = RecoveryCode::getListForUser(local_user()); $verified = PConfig::get(local_user(), '2fa', 'verified'); diff --git a/src/Module/TwoFactor/Recovery.php b/src/Module/TwoFactor/Recovery.php index 4952d220b7..60f443c35f 100644 --- a/src/Module/TwoFactor/Recovery.php +++ b/src/Module/TwoFactor/Recovery.php @@ -6,7 +6,7 @@ use Friendica\BaseModule; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Session; -use Friendica\Model\TwoFactorRecoveryCode; +use Friendica\Model\TwoFactor\RecoveryCode; /** * // Page 1a: Recovery code verification @@ -35,10 +35,10 @@ class Recovery extends BaseModule $recovery_code = defaults($_POST, 'recovery_code', ''); - if (TwoFactorRecoveryCode::existsForUser(local_user(), $recovery_code)) { - TwoFactorRecoveryCode::markUsedForUser(local_user(), $recovery_code); + if (RecoveryCode::existsForUser(local_user(), $recovery_code)) { + RecoveryCode::markUsedForUser(local_user(), $recovery_code); Session::set('2fa', true); - notice(L10n::t('Remaining recovery codes: %d', TwoFactorRecoveryCode::countValidForUser(local_user()))); + notice(L10n::t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user()))); // Resume normal login workflow Session::setAuthenticatedForUser($a, $a->user, true, true);