Skip to content

Commit 6f4a1dc

Browse files
authored
feat: support event dispatch from webauthn-lib (#506)
1 parent af7cdb1 commit 6f4a1dc

File tree

9 files changed

+134
-84
lines changed

9 files changed

+134
-84
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,14 @@ Then use this new custom rate limiter in your `webauthn.limiters.login` configur
363363

364364
Events are dispatched by LaravelWebauthn:
365365

366+
* `\Illuminate\Auth\Events\Failed` on a failed login check.
366367
* `\LaravelWebauthn\Events\WebauthnLogin` on login with Webauthn check.
367368
* `\LaravelWebauthn\Events\WebauthnLoginData` on preparing authentication data challenge.
368-
* `\Illuminate\Auth\Events\Failed` on a failed login check.
369369
* `\LaravelWebauthn\Events\WebauthnRegister` on registering a new key.
370370
* `\LaravelWebauthn\Events\WebauthnRegisterData` on preparing register data challenge.
371371
* `\LaravelWebauthn\Events\WebauthnRegisterFailed` on failing registering a new key.
372+
* `\LaravelWebauthn\Events\WebauthnAuthenticate` when a key has been authenticated.
373+
372374

373375
## View response
374376

config/webauthn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266
| See https://www.w3.org/TR/webauthn/#enum-userVerificationRequirement
267267
|
268268
| Supported: "required", "preferred", "discouraged".
269-
| Forced to "required" when userless is true.
269+
| This should be set to "required" when userless is true.
270270
|
271271
*/
272272

@@ -283,7 +283,7 @@
283283
| See https://www.w3.org/TR/webauthn/#enum-residentKeyRequirement
284284
|
285285
| Supported: "null", "required", "preferred", "discouraged".
286-
| Forced to "required" when userless is true.
286+
| This should be set to "required" when userless is true.
287287
|
288288
*/
289289

src/Events/EventDispatcher.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace LaravelWebauthn\Events;
4+
5+
use Illuminate\Contracts\Events\Dispatcher;
6+
use Psr\EventDispatcher\EventDispatcherInterface;
7+
8+
final class EventDispatcher implements EventDispatcherInterface
9+
{
10+
/**
11+
* Create a new event dispatcher instance.
12+
*/
13+
public function __construct(
14+
private readonly Dispatcher $dispatcher,
15+
) {}
16+
17+
/**
18+
* Dispatch the given event.
19+
*
20+
* @return object
21+
*/
22+
#[\Override]
23+
public function dispatch(object $event)
24+
{
25+
$this->dispatcher->dispatch($event);
26+
27+
return $event;
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace LaravelWebauthn\Events;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
/**
10+
* @psalm-suppress PossiblyUnusedProperty
11+
*/
12+
class WebauthnAuthenticate
13+
{
14+
use Dispatchable, SerializesModels;
15+
16+
/**
17+
* Create a new event instance.
18+
*
19+
* @param \Illuminate\Database\Eloquent\Model $webauthnKey The WebauthnKey used to authenticate.
20+
*/
21+
public function __construct(
22+
public Model $webauthnKey,
23+
) {}
24+
}

src/Http/Controllers/ConfirmableKeyController.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,11 @@
1313

1414
class ConfirmableKeyController extends Controller
1515
{
16-
/**
17-
* The guard implementation.
18-
*
19-
* @var \Illuminate\Contracts\Auth\StatefulGuard
20-
*/
21-
protected $guard;
22-
2316
/**
2417
* Create a new controller instance.
2518
*/
26-
public function __construct(StatefulGuard $guard)
27-
{
28-
$this->guard = $guard;
29-
}
19+
public function __construct(
20+
protected StatefulGuard $guard) {}
3021

3122
/**
3223
* Confirm the user's key.

src/Models/WebauthnKey.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class WebauthnKey extends Model
3939
'aaguid',
4040
'credentialPublicKey',
4141
'counter',
42-
'timestamp',
4342
];
4443

4544
/**

src/Services/Webauthn/CredentialAssertionValidator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Auth\Authenticatable as User;
66
use Illuminate\Contracts\Cache\Repository as Cache;
77
use Illuminate\Http\Request;
8+
use LaravelWebauthn\Events\WebauthnAuthenticate;
89
use LaravelWebauthn\Exceptions\ResponseMismatchException;
910
use LaravelWebauthn\Services\Webauthn;
1011
use ParagonIE\ConstantTime\Base64UrlSafe;
@@ -36,15 +37,19 @@ public function __invoke(?User $user, array $data): bool
3637
$content = json_encode($data, flags: JSON_THROW_ON_ERROR);
3738
$publicKeyCredential = $this->loader->deserialize($content, PublicKeyCredential::class, 'json');
3839

40+
$webauthnKey = $this->getKey($user, $publicKeyCredential);
41+
3942
// Check the response against the request
4043
$this->validator->check(
41-
$this->getCredentialSource($user, $publicKeyCredential),
44+
$webauthnKey->publicKeyCredentialSource,
4245
$this->getResponse($publicKeyCredential),
4346
$this->pullPublicKey($user),
4447
$this->request->host(),
4548
optional($user)->getAuthIdentifier()
4649
);
4750

51+
WebauthnAuthenticate::dispatch($webauthnKey);
52+
4853
return true;
4954
}
5055

@@ -87,7 +92,7 @@ protected function getResponse(PublicKeyCredential $publicKeyCredential): Authen
8792
/**
8893
* Get credential source from user and public key.
8994
*/
90-
protected function getCredentialSource(?User $user, PublicKeyCredential $publicKeyCredential)
95+
protected function getKey(?User $user, PublicKeyCredential $publicKeyCredential)
9196
{
9297
$credentialId = $publicKeyCredential->rawId;
9398

@@ -97,7 +102,6 @@ protected function getCredentialSource(?User $user, PublicKeyCredential $publicK
97102
)->where(
98103
fn ($query) => $user !== null ? $query->where('user_id', $user->getAuthIdentifier()) : $query
99104
)
100-
->firstOrFail()
101-
->publicKeyCredentialSource;
105+
->firstOrFail();
102106
}
103107
}

src/WebauthnAuthenticatable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace LaravelWebauthn;
44

55
use Illuminate\Database\Eloquent\Relations\HasMany;
6-
use LaravelWebauthn\Models\WebauthnKey;
6+
use LaravelWebauthn\Facades\Webauthn;
77

88
/**
99
* Trait to add Webauthn authenticatable to a user model.
@@ -17,6 +17,6 @@ trait WebauthnAuthenticatable
1717
*/
1818
public function webauthnKeys(): HasMany
1919
{
20-
return $this->hasMany(WebauthnKey::class);
20+
return $this->hasMany(Webauthn::model());
2121
}
2222
}

0 commit comments

Comments
 (0)