Add two-factor authentication support for users and subusers

- Extend `users_o` and `subusers_o` with `two_factor_enabled` and `two_factor_secret` properties.
- Implement methods for managing 2FA (`isTwoFactorEnabled`, `setTwoFactorSecret`, `verify_2fa_code`) in authentication logic.
- Add 2FA handling in login flows for both users and subusers, including token generation and validation.
- Introduce `totp` class for TOTP-based authentication, including QR code generation and code verification.
- Add test cases for 2FA functionality (`TwoFactorAuthTest.php`) and coverage for login scenarios with 2FA.
- Update OpenAPI specifications to include 2FA flows (`auth/2fa/setup`, `auth/2fa/enable`, `auth/2fa/verify`, `auth/2fa/disable`).
This commit is contained in:
Jeppe Bundgaard
2026-02-23 17:00:18 +01:00
parent 827fafd46b
commit f6b526f4ef
9 changed files with 899 additions and 21 deletions
@@ -2,6 +2,7 @@
namespace classes;
use classes\totp;
use Exception;
use interfaces\authentication_i;
use objects\plate_scanners_o;
@@ -41,6 +42,28 @@ class authentication implements authentication_i
return true;
}
public function is_2fa_enabled(users_o|subusers_o $user): bool
{
return $user->isTwoFactorEnabled();
}
public function create_2fa_token(int $id, string $type): string
{
// Create a temporary 2FA token
$token = bin2hex(random_bytes(32));
(new tokens_o())->create($id, $token, $type);
return $token;
}
public function verify_2fa_code(users_o|subusers_o $user, string $code): bool
{
$secret = $user->getTwoFactorSecret();
if (!$secret) {
return false;
}
return (new totp())->verifyCode($secret, $code);
}
public function match_passwords($password, $hash): bool
{
// Compare the password with the hash