]> git.mxchange.org Git - friendica.git/commitdiff
Move recovery code model to sub-folder
authorHypolite Petovan <hypolite@mrpetovan.com>
Mon, 22 Jul 2019 11:41:01 +0000 (07:41 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 22 Jul 2019 11:41:01 +0000 (07:41 -0400)
src/Model/TwoFactor/RecoveryCode.php [new file with mode: 0644]
src/Model/TwoFactorRecoveryCode.php [deleted file]
src/Module/Settings/TwoFactor/Index.php
src/Module/Settings/TwoFactor/Recovery.php
src/Module/TwoFactor/Recovery.php

diff --git a/src/Model/TwoFactor/RecoveryCode.php b/src/Model/TwoFactor/RecoveryCode.php
new file mode 100644 (file)
index 0000000..676b538
--- /dev/null
@@ -0,0 +1,125 @@
+<?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);
+       }
+}
diff --git a/src/Model/TwoFactorRecoveryCode.php b/src/Model/TwoFactorRecoveryCode.php
deleted file mode 100644 (file)
index 74dead3..0000000
+++ /dev/null
@@ -1,125 +0,0 @@
-<?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);
-       }
-}
index 9f02692706a0c72e3343702a731bf914236237f5..0ae271a359abe5a9586e23a3f0aef7117f0c2c04 100644 (file)
@@ -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('<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'),
index a2d08cda86cb4def517a817d7a32d4007248de43..6937fa503f4eec65a0bc3bfe92cb8fff668d84c5 100644 (file)
@@ -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');
                
index 4952d220b7ac755bd7210aed3e57aa4422c7e091..60f443c35ffe88bc54cf0658c52625c8b394f40d 100644 (file)
@@ -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);