3 namespace Friendica\Model\TwoFactor;
5 use Friendica\Database\DBA;
6 use Friendica\Util\DateTimeFormat;
7 use PragmaRX\Random\Random;
8 use PragmaRX\Recovery\Recovery;
11 * Manages users' two-factor recovery codes in the 2fa_recovery_codes table
13 * @package Friendica\Model
18 * Returns the number of code the provided users can still use to replace a TOTP code
20 * @param int $uid User ID
24 public static function countValidForUser($uid)
26 return DBA::count('2fa_recovery_codes', ['uid' => $uid, 'used' => null]);
30 * Checks the provided code is available to use for login by the provided user
32 * @param int $uid User ID
37 public static function existsForUser($uid, $code)
39 return DBA::exists('2fa_recovery_codes', ['uid' => $uid, 'code' => $code, 'used' => null]);
43 * Returns a complete list of all recovery codes for the provided user, including the used status
45 * @param int $uid User ID
49 public static function getListForUser($uid)
51 $codesStmt = DBA::select('2fa_recovery_codes', ['code', 'used'], ['uid' => $uid]);
53 return DBA::toArray($codesStmt);
57 * Marks the provided code as used for the provided user.
58 * Returns false if the code doesn't exist for the user or it has been used already.
60 * @param int $uid User ID
65 public static function markUsedForUser($uid, $code)
67 DBA::update('2fa_recovery_codes', ['used' => DateTimeFormat::utcNow()], ['uid' => $uid, 'code' => $code, 'used' => null]);
69 return DBA::affectedRows() > 0;
73 * Generates a fresh set of recovery codes for the provided user.
74 * Generates 12 codes constituted of 2 blocks of 6 characters separated by a dash.
76 * @param int $uid User ID
79 public static function generateForUser($uid)
81 $Random = (new Random())->pattern('[a-z0-9]');
83 $RecoveryGenerator = new Recovery($Random);
85 $codes = $RecoveryGenerator
92 $generated = DateTimeFormat::utcNow();
93 foreach ($codes as $code) {
94 DBA::insert('2fa_recovery_codes', [
97 'generated' => $generated
103 * Deletes all the recovery codes for the provided user.
105 * @param int $uid User ID
108 public static function deleteForUser($uid)
110 DBA::delete('2fa_recovery_codes', ['uid' => $uid]);
114 * Replaces the existing recovery codes for the provided user by a freshly generated set.
116 * @param int $uid User ID
119 public static function regenerateForUser($uid)
121 self::deleteForUser($uid);
122 self::generateForUser($uid);