web-base/Core/Objects/DatabaseEntity/TwoFactorToken.class.php

66 lines
1.7 KiB
PHP
Raw Normal View History

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;
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;
private bool $authenticated;
2022-11-27 15:58:44 +01:00
#[MaxLength(512)] 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);
$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;
return $this->save($sql) !== false;
}
2022-06-20 19:52:31 +02:00
}