2022-10-04 00:08:58 +02:00
|
|
|
<?php
|
2023-06-11 18:59:07 +02:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2022-10-04 00:08:58 +02:00
|
|
|
namespace App\Entity\UserSystem;
|
|
|
|
|
2023-11-27 17:53:35 +01:00
|
|
|
use App\Entity\Contracts\TimeStampableInterface;
|
2023-06-11 14:55:06 +02:00
|
|
|
use Doctrine\DBAL\Types\Types;
|
2022-10-04 00:08:58 +02:00
|
|
|
use App\Entity\Base\TimestampTrait;
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2024-03-06 19:46:11 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\Length;
|
2023-06-18 00:00:58 +02:00
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
2022-10-04 00:08:58 +02:00
|
|
|
use Webauthn\PublicKeyCredentialSource as BasePublicKeyCredentialSource;
|
|
|
|
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Entity]
|
|
|
|
#[ORM\HasLifecycleCallbacks]
|
|
|
|
#[ORM\Table(name: 'webauthn_keys')]
|
2023-11-27 17:53:35 +01:00
|
|
|
class WebauthnKey extends BasePublicKeyCredentialSource implements TimeStampableInterface
|
2022-10-04 00:08:58 +02:00
|
|
|
{
|
|
|
|
use TimestampTrait;
|
|
|
|
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Id]
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Column(type: Types::INTEGER)]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\GeneratedValue]
|
2022-10-04 00:08:58 +02:00
|
|
|
protected int $id;
|
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Column(type: Types::STRING)]
|
2023-06-18 00:00:58 +02:00
|
|
|
#[NotBlank]
|
2024-03-06 19:46:11 +01:00
|
|
|
#[Length(max: 255)]
|
2023-06-18 00:00:58 +02:00
|
|
|
protected string $name = '';
|
2022-10-04 00:08:58 +02:00
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'webauthn_keys')]
|
2022-10-04 00:08:58 +02:00
|
|
|
protected ?User $user = null;
|
|
|
|
|
2024-04-28 00:31:38 +02:00
|
|
|
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
|
|
|
|
protected ?\DateTimeInterface $last_time_used = null;
|
2024-04-28 17:50:19 +02:00
|
|
|
|
2022-10-04 00:08:58 +02:00
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setName(string $name): WebauthnKey
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUser(): ?User
|
|
|
|
{
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUser(?User $user): WebauthnKey
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2024-04-28 00:31:38 +02:00
|
|
|
/**
|
|
|
|
* Retrieve the last time when the key was used.
|
|
|
|
* @return \DateTimeInterface|null
|
|
|
|
*/
|
|
|
|
public function getLastTimeUsed(): ?\DateTimeInterface
|
|
|
|
{
|
|
|
|
return $this->last_time_used;
|
|
|
|
}
|
2022-10-04 00:08:58 +02:00
|
|
|
|
2024-04-28 00:31:38 +02:00
|
|
|
/**
|
|
|
|
* Update the last time when the key was used.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function updateLastTimeUsed(): void
|
|
|
|
{
|
|
|
|
$this->last_time_used = new \DateTimeImmutable('now');
|
|
|
|
}
|
2022-10-04 00:08:58 +02:00
|
|
|
|
|
|
|
public static function fromRegistration(BasePublicKeyCredentialSource $registration): self
|
|
|
|
{
|
2022-10-05 21:59:42 +02:00
|
|
|
return new self(
|
2022-10-04 00:08:58 +02:00
|
|
|
$registration->getPublicKeyCredentialId(),
|
|
|
|
$registration->getType(),
|
|
|
|
$registration->getTransports(),
|
|
|
|
$registration->getAttestationType(),
|
|
|
|
$registration->getTrustPath(),
|
|
|
|
$registration->getAaguid(),
|
|
|
|
$registration->getCredentialPublicKey(),
|
|
|
|
$registration->getUserHandle(),
|
|
|
|
$registration->getCounter(),
|
|
|
|
$registration->getOtherUI()
|
|
|
|
);
|
|
|
|
}
|
2023-06-11 18:59:07 +02:00
|
|
|
}
|