WebAuthn初步接好

This commit is contained in:
2022-08-08 02:07:27 +08:00
parent 50934228ef
commit 30b38e3f4b
16 changed files with 1329 additions and 5 deletions

View File

@ -0,0 +1,21 @@
<?php
namespace App\Models\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Webauthn\TrustPath\TrustPathLoader;
class TrustPath implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes): ?\Webauthn\TrustPath\TrustPath
{
return $value !== null
? TrustPathLoader::loadTrustPath(json_decode($value, true))
: null;
}
public function set($model, string $key, $value, array $attributes)
{
return json_encode($value);
}
}

24
app/Models/Casts/Uuid.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace App\Models\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Ramsey\Uuid\Uuid as UuidConvert;
use Ramsey\Uuid\UuidInterface;
class Uuid implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes): ?UuidInterface
{
if ($value !== null && UuidConvert::isValid($value)) {
return UuidConvert::fromString($value);
}
return null;
}
public function set($model, string $key, $value, array $attributes): ?string
{
return (string) $value;
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Models\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use function Safe\base64_decode;
class WebAuthnBase64 implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes): ?string
{
return $value !== null ? base64_decode($value) : null;
}
public function set($model, string $key, $value, array $attributes)
{
return $value !== null ? base64_encode($value) : null;
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\Models;
use App\Models\Casts\TrustPath;
use App\Models\Casts\Uuid;
use App\Models\Casts\WebAuthnBase64;
use Illuminate\Database\Eloquent\Model;
use Webauthn\PublicKeyCredentialSource;
class WebauthnCredential extends Model
{
protected $guarded = ['id'];
protected $visible = [
'id',
'name',
'type',
'attachment_type',
'transports',
'type_free',
'last_used_at',
'created_at',
'updated_at',
];
protected $casts = [
'aaguid' => Uuid::class,
'counter' => 'integer',
'credential_id' => WebAuthnBase64::class,
'credential_public_key' => WebAuthnBase64::class,
'transports' => 'array',
'trust_path' => TrustPath::class,
'last_used_at' => 'immutable_datetime',
];
public function getPublicKeyCredentialSourceAttribute(): PublicKeyCredentialSource
{
return new PublicKeyCredentialSource(
$this->credential_id,
$this->type,
$this->transports,
$this->attestation_type,
$this->trust_path,
$this->aaguid ?? \Ramsey\Uuid\Uuid::uuid4(),
$this->credential_public_key,
(string) $this->user_id,
$this->counter,
);
}
public function setPublicKeyCredentialSourceAttribute(PublicKeyCredentialSource $source): void
{
$this->credential_id = $source->getPublicKeyCredentialId();
$this->type = $source->getType();
$this->transports = $source->getTransports();
$this->attestation_type = $source->getAttestationType();
$this->trust_path = $source->getTrustPath();
$this->aaguid = $source->getAaguid();
$this->credential_public_key = $source->getCredentialPublicKey();
$this->counter = $source->getCounter();
$this->user_id = $source->getUserHandle();
}
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class, "user_id", "id");
}
}