Skip to content

Commit 0545a45

Browse files
[4.x] Adds custom validation callback (#112)
* Should fix tests. * Fixes static analysis * Fixes tests. * Should fix static analysis
1 parent 4179306 commit 0545a45

5 files changed

Lines changed: 17 additions & 4 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
},
4747
"require-dev": {
4848
"ext-sodium": "*",
49-
"orchestra/testbench": "9.*|10.*"
49+
"orchestra/testbench": "^9.16|10.*"
5050
},
5151
"suggest": {
5252
"paragonie/sodium_compat": "To enable EdDSA 25519 keys from authenticators, if `ext-sodium` is unavailable."

src/Auth/WebAuthnUserProvider.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laragear\WebAuthn\Auth;
44

5+
use Closure;
56
use Illuminate\Auth\EloquentUserProvider;
67
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
78
use Illuminate\Contracts\Database\Eloquent\Builder;
@@ -15,6 +16,7 @@
1516
use function class_implements;
1617
use function config;
1718
use function in_array;
19+
use function is_bool;
1820
use function logger;
1921

2022
/**
@@ -24,6 +26,13 @@
2426
*/
2527
class WebAuthnUserProvider extends EloquentUserProvider
2628
{
29+
/**
30+
* Custom callback to validate the user credentials.
31+
*
32+
* @var (\Closure(\Illuminate\Contracts\Auth\Authenticatable|\Laragear\WebAuthn\Contracts\WebAuthnAuthenticatable, array):bool|null)|null
33+
*/
34+
public static ?Closure $validateUsing;
35+
2736
/**
2837
* Create a new database user provider.
2938
*/
@@ -87,6 +96,10 @@ protected function isSignedChallenge(array $credentials): bool
8796
*/
8897
public function validateCredentials($user, array $credentials): bool
8998
{
99+
if (isset(static::$validateUsing) && is_bool($result = (static::$validateUsing)($user, $credentials))) { // @phpstan-ignore-line
100+
return $result;
101+
}
102+
90103
if ($user instanceof WebAuthnAuthenticatable && $this->isSignedChallenge($credentials)) {
91104
return $this->validateWebAuthn($user, $credentials);
92105
}

src/CborDecoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static function decode(ByteBuffer|string $encoded): ByteBuffer|array|bool
103103
*
104104
* @throws \Laragear\WebAuthn\Exceptions\DataException
105105
*/
106-
public static function decodePortion(ByteBuffer|string $bufOrBin, int $startOffset, ?int &$endOffset = null): ByteBuffer|array|bool|float|int|string|null // @phpstan-ignore-line
106+
public static function decodePortion(ByteBuffer|string $bufOrBin, int $startOffset, ?int &$endOffset = null): ByteBuffer|array|bool|float|int|string|null
107107
{
108108
$buf = $bufOrBin instanceof ByteBuffer ? $bufOrBin : new ByteBuffer($bufOrBin);
109109

src/Models/WebAuthnCredential.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected function rpId(): Attribute
207207
*/
208208
public static function migration(): CustomMigration
209209
{
210-
return new CustomMigration(new static, function (Blueprint $table): void { // @phpstan-ignore-line
210+
return new CustomMigration(new static, function (Blueprint $table): void {
211211
// Here we set the PublicKeyCredential ID generated by the authenticator as string.
212212
// This way it's easier and faster for the database to find the right credential
213213
// on the Assertion procedure as the device returns which credential it used.

tests/Auth/EloquentWebAuthnProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Laragear\WebAuthn\Assertion\Validator\AssertionValidator;
1010
use Laragear\WebAuthn\Auth\WebAuthnUserProvider;
1111
use Laragear\WebAuthn\Exceptions\AssertionException;
12-
use Laragear\WebAuthn\Repositories\Eloquent\WebAuthnCredential;
12+
use Laragear\WebAuthn\Models\WebAuthnCredential;
1313
use Mockery;
1414
use Psr\Log\LoggerInterface;
1515
use Ramsey\Uuid\Uuid;

0 commit comments

Comments
 (0)