Skip to content

Commit

Permalink
fix: fix CredentialAssertionValidator (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Sep 6, 2023
1 parent 3bcbe53 commit 7ea4c1d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
23 changes: 19 additions & 4 deletions src/Services/Webauthn/CredentialAssertionValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Http\Request;
use LaravelWebauthn\Exceptions\ResponseMismatchException;
use Psr\Http\Message\ServerRequestInterface;
use LaravelWebauthn\Services\Webauthn;
use ParagonIE\ConstantTime\Base64UrlSafe;
use Webauthn\AuthenticatorAssertionResponse;
use Webauthn\AuthenticatorAssertionResponseValidator;
use Webauthn\PublicKeyCredential;
Expand All @@ -18,7 +19,6 @@ class CredentialAssertionValidator extends CredentialValidator
public function __construct(
Request $request,
Cache $cache,
protected ServerRequestInterface $serverRequest,
protected PublicKeyCredentialLoader $loader,
protected AuthenticatorAssertionResponseValidator $validator
) {
Expand All @@ -37,10 +37,10 @@ public function __invoke(User $user, array $data): bool

// Check the response against the request
$this->validator->check(
$publicKeyCredential->getRawId(),
$this->getCredentialSource($user, $publicKeyCredential),
$this->getResponse($publicKeyCredential),
$this->pullPublicKey($user),
$this->serverRequest,
$this->request->host(),
$user->getAuthIdentifier()
);

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

return $response;
}

/**
* Get credential source from user and public key.
*/
protected function getCredentialSource(User $user, PublicKeyCredential $publicKeyCredential)
{
$credentialId = $publicKeyCredential->getRawId();

return (Webauthn::model())::where('user_id', $user->getAuthIdentifier())
->where(fn ($query) => $query->where('credentialId', Base64UrlSafe::encode($credentialId))
->orWhere('credentialId', Base64UrlSafe::encodeUnpadded($credentialId))
)
->firstOrFail()
->publicKeyCredentialSource;
}
}
4 changes: 1 addition & 3 deletions src/Services/Webauthn/CredentialAttestationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Http\Request;
use LaravelWebauthn\Exceptions\ResponseMismatchException;
use Psr\Http\Message\ServerRequestInterface;
use Webauthn\AuthenticatorAttestationResponse;
use Webauthn\AuthenticatorAttestationResponseValidator;
use Webauthn\PublicKeyCredential;
Expand All @@ -19,7 +18,6 @@ class CredentialAttestationValidator extends CredentialValidator
public function __construct(
Request $request,
Cache $cache,
protected ServerRequestInterface $serverRequest,
protected PublicKeyCredentialLoader $loader,
protected AuthenticatorAttestationResponseValidator $validator
) {
Expand All @@ -40,7 +38,7 @@ public function __invoke(User $user, array $data): PublicKeyCredentialSource
return $this->validator->check(
$this->getResponse($publicKeyCredential),
$this->pullPublicKey($user),
$this->serverRequest
$this->request->host()
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Services/WebauthnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function test_wrong_do_authenticate()
$user = $this->signIn();
factory(WebauthnKey::class)->create([
'user_id' => $user->getAuthIdentifier(),
'credentialId' => '0',
]);

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

0 comments on commit 7ea4c1d

Please sign in to comment.