]> git.mxchange.org Git - friendica.git/commitdiff
Add Two-factor recovery code model
authorHypolite Petovan <hypolite@mrpetovan.com>
Mon, 13 May 2019 05:34:40 +0000 (01:34 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 13 May 2019 05:52:00 +0000 (01:52 -0400)
- [DBA] Add NULL value handling in condition array

src/Database/DBA.php
src/Model/TwoFactorRecoveryCode.php [new file with mode: 0644]

index bbf134e8ad8dc5b9df75a1a639828a7a67c67a05..228565f940cc7dd32684808af8c0c23b0783e58e 100644 (file)
@@ -1484,6 +1484,8 @@ class DBA
                                                $new_values = array_merge($new_values, array_values($value));
                                                $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
                                                $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
+                                       } elseif (is_null($value)) {
+                                               $condition_string .= "`" . $field . "` IS NULL";
                                        } else {
                                                $new_values[$field] = $value;
                                                $condition_string .= "`" . $field . "` = ?";
diff --git a/src/Model/TwoFactorRecoveryCode.php b/src/Model/TwoFactorRecoveryCode.php
new file mode 100644 (file)
index 0000000..82740d3
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+
+namespace Friendica\Model;
+
+use Friendica\BaseObject;
+use Friendica\Database\DBA;
+use Friendica\Util\DateTimeFormat;
+use PragmaRX\Random\Random;
+use PragmaRX\Recovery\Recovery;
+
+class TwoFactorRecoveryCode extends BaseObject
+{
+       public static function countValidForUser($uid)
+       {
+               return DBA::count('2fa_recovery_codes', ['uid' => $uid, 'used' => null]);
+       }
+
+       public static function existsForUser($uid, $code)
+       {
+               return DBA::exists('2fa_recovery_codes', ['uid' => $uid, 'code' => $code, 'used' => null]);
+       }
+
+       public static function getListForUser($uid)
+       {
+               $codesStmt = DBA::select('2fa_recovery_codes', ['code', 'used'], ['uid' => $uid]);
+
+               return DBA::toArray($codesStmt);
+       }
+
+       public static function markUsedForUser($uid, $code)
+       {
+               DBA::update('2fa_recovery_codes', ['used' => DateTimeFormat::utcNow()], ['uid' => $uid, 'code' => $code]);
+
+               return DBA::affectedRows() > 0;
+       }
+
+       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
+                       ]);
+               }
+       }
+
+       public static function deleteForUser($uid)
+       {
+               DBA::delete('2fa_recovery_codes', ['uid' => $uid]);
+       }
+
+       public static function regenerateForUser($uid)
+       {
+               self::deleteForUser($uid);
+               self::generateForUser($uid);
+       }
+}