Part-DB.Part-DB-server/src/Entity/UserSystem/WebauthnKey.php

117 lines
3.3 KiB
PHP
Raw Normal View History

2022-10-04 00:08:58 +02:00
<?php
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;
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;
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;
#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
#[ORM\Table(name: 'webauthn_keys')]
class WebauthnKey extends BasePublicKeyCredentialSource implements TimeStampableInterface
2022-10-04 00:08:58 +02:00
{
use TimestampTrait;
#[ORM\Id]
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::INTEGER)]
#[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]
#[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;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
protected ?\DateTimeInterface $last_time_used = null;
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;
}
/**
* 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
/**
* 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()
);
}
}