2021-08-08 16:23:02 +00:00
|
|
|
<?php
|
|
|
|
|
2021-08-08 17:48:35 +00:00
|
|
|
namespace Pterodactyl\Repositories\SecurityKeys;
|
2021-08-08 16:23:02 +00:00
|
|
|
|
2021-08-08 17:48:35 +00:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2021-08-08 16:23:02 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Container\Container;
|
|
|
|
use Webauthn\PublicKeyCredentialSource;
|
|
|
|
use Webauthn\PublicKeyCredentialUserEntity;
|
2021-08-08 17:48:35 +00:00
|
|
|
use Pterodactyl\Models\SecurityKey;
|
2021-08-08 16:23:02 +00:00
|
|
|
use Webauthn\PublicKeyCredentialSourceRepository as PublicKeyRepositoryInterface;
|
|
|
|
|
|
|
|
class PublicKeyCredentialSourceRepository implements PublicKeyRepositoryInterface
|
|
|
|
{
|
|
|
|
protected User $user;
|
|
|
|
|
|
|
|
public function __construct(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a single hardware security token for a user by uzing the credential ID.
|
|
|
|
*/
|
|
|
|
public function findOneByCredentialId(string $id): ?PublicKeyCredentialSource
|
|
|
|
{
|
2021-08-08 17:48:35 +00:00
|
|
|
/** @var \Pterodactyl\Models\SecurityKey $key */
|
|
|
|
$key = $this->user->securityKeys()
|
2021-08-08 16:23:02 +00:00
|
|
|
->where('public_key_id', $id)
|
|
|
|
->first();
|
|
|
|
|
2021-08-08 18:24:11 +00:00
|
|
|
return $key ? $key->public_key_credential_source : null;
|
2021-08-08 16:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all of the hardware tokens that exist for the user using the given
|
|
|
|
* entity handle.
|
|
|
|
*/
|
|
|
|
public function findAllForUserEntity(PublicKeyCredentialUserEntity $entity): array
|
|
|
|
{
|
2021-08-08 17:48:35 +00:00
|
|
|
$results = $this->user->securityKeys()
|
2021-08-08 16:23:02 +00:00
|
|
|
->where('user_handle', $entity->getId())
|
|
|
|
->get();
|
|
|
|
|
2021-08-08 17:48:35 +00:00
|
|
|
return $results->map(function (SecurityKey $key) {
|
2021-08-08 18:24:11 +00:00
|
|
|
return $key->public_key_credential_source;
|
2021-08-08 16:23:02 +00:00
|
|
|
})->values()->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a credential to the database and link it with the user.
|
2021-08-08 17:48:35 +00:00
|
|
|
*
|
|
|
|
* @throws \Throwable
|
2021-08-08 16:23:02 +00:00
|
|
|
*/
|
|
|
|
public function saveCredentialSource(PublicKeyCredentialSource $source): void
|
|
|
|
{
|
2021-08-08 18:24:11 +00:00
|
|
|
$key = $this->user->securityKeys()->make();
|
|
|
|
|
|
|
|
$key->forceFill([
|
|
|
|
'uuid' => Uuid::uuid4(),
|
2021-08-08 17:48:35 +00:00
|
|
|
'user_id' => $this->user->id,
|
2021-08-08 18:24:11 +00:00
|
|
|
'public_key_id' => $source->getPublicKeyCredentialId(),
|
|
|
|
'public_key' => $source->getCredentialPublicKey(),
|
|
|
|
'aaguid' => $source->getAaguid(),
|
2021-08-08 17:48:35 +00:00
|
|
|
'type' => $source->getType(),
|
|
|
|
'transports' => $source->getTransports(),
|
|
|
|
'attestation_type' => $source->getAttestationType(),
|
2021-08-08 18:24:11 +00:00
|
|
|
'trust_path' => $source->getTrustPath(),
|
2021-08-08 17:48:35 +00:00
|
|
|
'user_handle' => $source->getUserHandle(),
|
|
|
|
'counter' => $source->getCounter(),
|
|
|
|
'other_ui' => $source->getOtherUI(),
|
|
|
|
]);
|
2021-08-08 18:24:11 +00:00
|
|
|
|
|
|
|
$key->saveOrFail();
|
2021-08-08 16:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new instance of the repository with the provided user attached.
|
|
|
|
*/
|
|
|
|
public static function factory(User $user): self
|
|
|
|
{
|
|
|
|
return Container::getInstance()->make(static::class, ['user' => $user]);
|
|
|
|
}
|
|
|
|
}
|