2022-06-20 19:52:31 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\Objects\DatabaseEntity;
|
2022-06-20 19:52:31 +02:00
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\SQL;
|
2022-11-27 12:33:27 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Attribute\ExtendingEnum;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
|
2023-01-16 21:47:23 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Transient;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Visibility;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Objects\TwoFactor\KeyBasedTwoFactorToken;
|
|
|
|
use Core\Objects\TwoFactor\TimeBasedTwoFactorToken;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
|
2022-06-20 19:52:31 +02:00
|
|
|
|
|
|
|
abstract class TwoFactorToken extends DatabaseEntity {
|
|
|
|
|
2022-11-27 12:33:27 +01:00
|
|
|
const TWO_FACTOR_TOKEN_TYPES = [
|
|
|
|
"totp" => TimeBasedTwoFactorToken::class,
|
|
|
|
"fido" => KeyBasedTwoFactorToken::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
#[ExtendingEnum(self::TWO_FACTOR_TOKEN_TYPES)] private string $type;
|
2022-06-20 19:52:31 +02:00
|
|
|
private bool $confirmed;
|
2023-01-16 21:47:23 +01:00
|
|
|
|
|
|
|
#[Transient]
|
2022-06-20 19:52:31 +02:00
|
|
|
private bool $authenticated;
|
2023-01-16 21:47:23 +01:00
|
|
|
|
|
|
|
#[MaxLength(512)]
|
|
|
|
#[Visibility(Visibility::NONE)]
|
|
|
|
private ?string $data;
|
2022-06-20 19:52:31 +02:00
|
|
|
|
|
|
|
public function __construct(string $type, ?int $id = null, bool $confirmed = false) {
|
|
|
|
parent::__construct($id);
|
|
|
|
$this->id = $id;
|
|
|
|
$this->type = $type;
|
|
|
|
$this->confirmed = $confirmed;
|
|
|
|
$this->authenticated = $_SESSION["2faAuthenticated"] ?? false;
|
2022-11-27 15:58:44 +01:00
|
|
|
$this->data = null;
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract function getData(): string;
|
|
|
|
protected abstract function readData(string $data);
|
|
|
|
|
|
|
|
public function preInsert(array &$row) {
|
|
|
|
$row["data"] = $this->getData();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postFetch(SQL $sql, array $row) {
|
|
|
|
parent::postFetch($sql, $row);
|
2023-01-16 21:47:23 +01:00
|
|
|
$this->authenticated = $_SESSION["2faAuthenticated"] ?? false;
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->readData($row["data"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authenticate() {
|
|
|
|
$this->authenticated = true;
|
|
|
|
$_SESSION["2faAuthenticated"] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType(): string {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isConfirmed(): bool {
|
|
|
|
return $this->confirmed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isAuthenticated(): bool {
|
|
|
|
return $this->authenticated;
|
|
|
|
}
|
2022-11-27 15:58:44 +01:00
|
|
|
|
|
|
|
public function confirm(SQL $sql): bool {
|
|
|
|
$this->confirmed = true;
|
2023-01-16 22:11:49 +01:00
|
|
|
return $this->save($sql, ["confirmed"]) !== false;
|
2022-11-27 15:58:44 +01:00
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
|
|
|
public function jsonSerialize(?array $propertyNames = null): array {
|
|
|
|
$jsonData = parent::jsonSerialize($propertyNames);
|
|
|
|
|
|
|
|
if ($propertyNames === null || in_array("authenticated", $propertyNames)) {
|
|
|
|
$jsonData["authenticated"] = $this->authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $jsonData;
|
|
|
|
}
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|