Skip to content

Commit 7ea4c1d

Browse files
authored
fix: fix CredentialAssertionValidator (#447)
1 parent 3bcbe53 commit 7ea4c1d

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/Services/Webauthn/CredentialAssertionValidator.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
use Illuminate\Contracts\Cache\Repository as Cache;
77
use Illuminate\Http\Request;
88
use LaravelWebauthn\Exceptions\ResponseMismatchException;
9-
use Psr\Http\Message\ServerRequestInterface;
9+
use LaravelWebauthn\Services\Webauthn;
10+
use ParagonIE\ConstantTime\Base64UrlSafe;
1011
use Webauthn\AuthenticatorAssertionResponse;
1112
use Webauthn\AuthenticatorAssertionResponseValidator;
1213
use Webauthn\PublicKeyCredential;
@@ -18,7 +19,6 @@ class CredentialAssertionValidator extends CredentialValidator
1819
public function __construct(
1920
Request $request,
2021
Cache $cache,
21-
protected ServerRequestInterface $serverRequest,
2222
protected PublicKeyCredentialLoader $loader,
2323
protected AuthenticatorAssertionResponseValidator $validator
2424
) {
@@ -37,10 +37,10 @@ public function __invoke(User $user, array $data): bool
3737

3838
// Check the response against the request
3939
$this->validator->check(
40-
$publicKeyCredential->getRawId(),
40+
$this->getCredentialSource($user, $publicKeyCredential),
4141
$this->getResponse($publicKeyCredential),
4242
$this->pullPublicKey($user),
43-
$this->serverRequest,
43+
$this->request->host(),
4444
$user->getAuthIdentifier()
4545
);
4646

@@ -74,4 +74,19 @@ protected function getResponse(PublicKeyCredential $publicKeyCredential): Authen
7474

7575
return $response;
7676
}
77+
78+
/**
79+
* Get credential source from user and public key.
80+
*/
81+
protected function getCredentialSource(User $user, PublicKeyCredential $publicKeyCredential)
82+
{
83+
$credentialId = $publicKeyCredential->getRawId();
84+
85+
return (Webauthn::model())::where('user_id', $user->getAuthIdentifier())
86+
->where(fn ($query) => $query->where('credentialId', Base64UrlSafe::encode($credentialId))
87+
->orWhere('credentialId', Base64UrlSafe::encodeUnpadded($credentialId))
88+
)
89+
->firstOrFail()
90+
->publicKeyCredentialSource;
91+
}
7792
}

src/Services/Webauthn/CredentialAttestationValidator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Contracts\Cache\Repository as Cache;
77
use Illuminate\Http\Request;
88
use LaravelWebauthn\Exceptions\ResponseMismatchException;
9-
use Psr\Http\Message\ServerRequestInterface;
109
use Webauthn\AuthenticatorAttestationResponse;
1110
use Webauthn\AuthenticatorAttestationResponseValidator;
1211
use Webauthn\PublicKeyCredential;
@@ -19,7 +18,6 @@ class CredentialAttestationValidator extends CredentialValidator
1918
public function __construct(
2019
Request $request,
2120
Cache $cache,
22-
protected ServerRequestInterface $serverRequest,
2321
protected PublicKeyCredentialLoader $loader,
2422
protected AuthenticatorAttestationResponseValidator $validator
2523
) {
@@ -40,7 +38,7 @@ public function __invoke(User $user, array $data): PublicKeyCredentialSource
4038
return $this->validator->check(
4139
$this->getResponse($publicKeyCredential),
4240
$this->pullPublicKey($user),
43-
$this->serverRequest
41+
$this->request->host()
4442
);
4543
}
4644

tests/Unit/Services/WebauthnTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function test_wrong_do_authenticate()
102102
$user = $this->signIn();
103103
factory(WebauthnKey::class)->create([
104104
'user_id' => $user->getAuthIdentifier(),
105+
'credentialId' => '0',
105106
]);
106107

107108
$publicKey = $this->app[PrepareAssertionData::class]($user);

0 commit comments

Comments
 (0)