--- /dev/null
+<?php
+
+namespace Friendica\Model\TwoFactor;
+
+use Friendica\BaseObject;
+use Friendica\Database\DBA;
+use Friendica\Util\DateTimeFormat;
+use PragmaRX\Random\Random;
+use PragmaRX\Recovery\Recovery;
+
+/**
+ * Manages users' two-factor recovery codes in the 2fa_recovery_codes table
+ *
+ * @package Friendica\Model
+ */
+class RecoveryCode extends BaseObject
+{
+ /**
+ * Returns the number of code the provided users can still use to replace a TOTP code
+ *
+ * @param int $uid User ID
+ * @return int
+ * @throws \Exception
+ */
+ public static function countValidForUser($uid)
+ {
+ return DBA::count('2fa_recovery_codes', ['uid' => $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);
+ }
+}
+++ /dev/null
-<?php
-
-namespace Friendica\Model;
-
-use Friendica\BaseObject;
-use Friendica\Database\DBA;
-use Friendica\Util\DateTimeFormat;
-use PragmaRX\Random\Random;
-use PragmaRX\Recovery\Recovery;
-
-/**
- * Manages users' two-factor recovery codes in the 2fa_recovery_codes table
- *
- * @package Friendica\Model
- */
-class TwoFactorRecoveryCode extends BaseObject
-{
- /**
- * Returns the number of code the provided users can still use to replace a TOTP code
- *
- * @param int $uid User ID
- * @return int
- * @throws \Exception
- */
- public static function countValidForUser($uid)
- {
- return DBA::count('2fa_recovery_codes', ['uid' => $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);
- }
-}
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;
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');
'$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('<p>These one-use codes can replace an authenticator app code in case you have lost access to it.</p>'),
'$action_title' => L10n::t('Actions'),
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;
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'));
}
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');
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
$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);