Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3) - Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung - Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü - Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person - Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“ - Benutzerverwaltung im Admin, Zugang vorläufig über is_admin - vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/ - PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung - VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
111 lines
3.3 KiB
PHP
111 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Laravel\Passkeys\Actions;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use Laravel\Passkeys\Contracts\PasskeyUser;
|
|
use Laravel\Passkeys\Events\PasskeyRegistered;
|
|
use Laravel\Passkeys\Exceptions\InvalidPasskeyException;
|
|
use Laravel\Passkeys\Passkey;
|
|
use Laravel\Passkeys\Passkeys;
|
|
use Laravel\Passkeys\Support\WebAuthn;
|
|
use ParagonIE\ConstantTime\Base64UrlSafe;
|
|
use RuntimeException;
|
|
use Webauthn\AuthenticatorAttestationResponse;
|
|
use Webauthn\CredentialRecord;
|
|
use Webauthn\PublicKeyCredential;
|
|
use Webauthn\PublicKeyCredentialCreationOptions;
|
|
|
|
class StorePasskey
|
|
{
|
|
/**
|
|
* Validate and store a passkey for the user.
|
|
*
|
|
* @throws InvalidPasskeyException
|
|
*/
|
|
public function __invoke(
|
|
Authenticatable $user,
|
|
string $name,
|
|
PublicKeyCredential $credential,
|
|
PublicKeyCredentialCreationOptions $options
|
|
): Passkey {
|
|
if (! $user instanceof PasskeyUser) {
|
|
throw new RuntimeException('User model must implement the PasskeyUser contract.');
|
|
}
|
|
|
|
$response = $this->getResponse($credential);
|
|
|
|
$source = $this->validate($response, $options);
|
|
|
|
$this->ensureCredentialIsUnique($source);
|
|
|
|
$passkey = $this->createPasskey($user, $name, $source);
|
|
|
|
PasskeyRegistered::dispatch($user, $passkey);
|
|
|
|
return $passkey;
|
|
}
|
|
|
|
/**
|
|
* Get the authenticator attestation response from the credential.
|
|
*
|
|
* @throws InvalidPasskeyException
|
|
*/
|
|
protected function getResponse(PublicKeyCredential $credential): AuthenticatorAttestationResponse
|
|
{
|
|
if (! $credential->response instanceof AuthenticatorAttestationResponse) {
|
|
throw InvalidPasskeyException::make('Unable to register passkey. Please try again.');
|
|
}
|
|
|
|
return $credential->response;
|
|
}
|
|
|
|
/**
|
|
* Validate the credential and return the source.
|
|
*/
|
|
protected function validate(
|
|
AuthenticatorAttestationResponse $response,
|
|
PublicKeyCredentialCreationOptions $options
|
|
): CredentialRecord {
|
|
return WebAuthn::attestationValidator()->check(
|
|
authenticatorAttestationResponse: $response,
|
|
publicKeyCredentialCreationOptions: $options,
|
|
host: Passkeys::relyingPartyId(),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Ensure the credential is not already registered.
|
|
*
|
|
* @throws InvalidPasskeyException
|
|
*/
|
|
protected function ensureCredentialIsUnique(CredentialRecord $source): void
|
|
{
|
|
$credentialId = Base64UrlSafe::encodeUnpadded($source->publicKeyCredentialId);
|
|
|
|
$exists = Passkeys::passkeyModel()::where('credential_id', $credentialId)->exists();
|
|
|
|
if ($exists) {
|
|
throw InvalidPasskeyException::make('Unable to register this passkey.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create the passkey record for the user.
|
|
*/
|
|
public function createPasskey(
|
|
PasskeyUser $user,
|
|
string $name,
|
|
CredentialRecord $source
|
|
): Passkey {
|
|
$credentialId = Base64UrlSafe::encodeUnpadded($source->publicKeyCredentialId);
|
|
|
|
return $user->passkeys()->create([
|
|
'name' => $name,
|
|
'credential_id' => $credentialId,
|
|
'credential' => json_decode(WebAuthn::toJson($source), true, flags: JSON_THROW_ON_ERROR),
|
|
]);
|
|
}
|
|
}
|