Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Feb 28, 2025
1 parent 6cd0be6 commit 1701d1c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
4 changes: 3 additions & 1 deletion src/Services/Webauthn/CreationOptionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Http\Request;
use LaravelWebauthn\Facades\Webauthn;
use Symfony\Component\Serializer\SerializerInterface;
use Webauthn\AuthenticatorSelectionCriteria;
use Webauthn\PublicKeyCredentialCreationOptions;
use Webauthn\PublicKeyCredentialDescriptor;
Expand All @@ -27,6 +28,7 @@ public function __construct(
Cache $cache,
Config $config,
CredentialRepository $repository,
protected SerializerInterface $loader,
protected PublicKeyCredentialRpEntity $publicKeyCredentialRpEntity,
protected AuthenticatorSelectionCriteria $authenticatorSelectionCriteria,
protected CoseAlgorithmManager $algorithmManager
Expand All @@ -51,7 +53,7 @@ public function __invoke(User $user): PublicKeyCredentialCreationOptions
$this->timeout
);

$value = json_encode($publicKey, flags: JSON_THROW_ON_ERROR);
$value = $this->loader->serialize($publicKey, 'json');

$this->cache->put($this->cacheKey($user), $value, $this->timeout);

Expand Down
4 changes: 3 additions & 1 deletion src/Services/Webauthn/RequestOptionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Http\Request;
use Symfony\Component\Serializer\SerializerInterface;
use Webauthn\PublicKeyCredentialDescriptor;
use Webauthn\PublicKeyCredentialRequestOptions;
use Webauthn\PublicKeyCredentialRpEntity;
Expand All @@ -22,6 +23,7 @@ public function __construct(
Cache $cache,
Config $config,
CredentialRepository $repository,
protected SerializerInterface $loader,
protected PublicKeyCredentialRpEntity $publicKeyCredentialRpEntity
) {
parent::__construct($request, $cache, $config, $repository);
Expand All @@ -41,7 +43,7 @@ public function __invoke(?User $user): PublicKeyCredentialRequestOptions
$this->timeout
);

$value = json_encode($publicKey, flags: JSON_THROW_ON_ERROR);
$value = $this->loader->serialize($publicKey, 'json');

$this->cache->put($this->cacheKey($user), $value, $this->timeout);

Expand Down
26 changes: 10 additions & 16 deletions src/WebauthnServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ protected function bindWebAuthnPackage(): void
$this->app->bind(
PackedAttestationStatementSupport::class,
fn ($app) => new PackedAttestationStatementSupport(
$app[CoseAlgorithmManager::class]
algorithmManager: $app[CoseAlgorithmManager::class]
)
);
$this->app->bind(
AttestationStatementSupportManager::class,
fn ($app) => tap(new AttestationStatementSupportManager(), function ($manager) use ($app) {
fn ($app) => tap(new AttestationStatementSupportManager, function ($manager) use ($app) {
// https://www.w3.org/TR/webauthn/#sctn-none-attestation
$manager->add($app[NoneAttestationStatementSupport::class]);

Expand Down Expand Up @@ -167,8 +167,7 @@ protected function bindWebAuthnPackage(): void
$this->app->bind(
AuthenticatorAttestationResponseValidator::class,
fn ($app) => tap(new AuthenticatorAttestationResponseValidator(
attestationStatementSupportManager: $app[AttestationStatementSupportManager::class],
extensionOutputCheckerHandler: $app[ExtensionOutputCheckerHandler::class]
ceremonyStepManager: ($app[CeremonyStepManagerFactory::class])->creationCeremony(),
), fn (AuthenticatorAttestationResponseValidator $responseValidator) => $responseValidator->setLogger($app['webauthn.log'])
)
);
Expand All @@ -183,29 +182,24 @@ protected function bindWebAuthnPackage(): void
$this->app->bind(
AuthenticatorAssertionResponseValidator::class,
fn ($app) => tap((new AuthenticatorAssertionResponseValidator(
null,
null,
$app[ExtensionOutputCheckerHandler::class],
$app[CoseAlgorithmManager::class],
null,
($app[CeremonyStepManagerFactory::class])->requestCeremony()
ceremonyStepManager: ($app[CeremonyStepManagerFactory::class])->requestCeremony()
)), fn (AuthenticatorAssertionResponseValidator $responseValidator) => $responseValidator->setLogger($app['webauthn.log'])
)
);
$this->app->bind(
AuthenticatorSelectionCriteria::class,
fn ($app) => new AuthenticatorSelectionCriteria(
$app['config']->get('webauthn.attachment_mode', 'null'),
$app['config']->get('webauthn.user_verification', 'preferred'),
$app['config']->get('webauthn.userless')
authenticatorAttachment: $app['config']->get('webauthn.attachment_mode', 'null'),
userVerification: $app['config']->get('webauthn.user_verification', 'preferred'),
residentKey: $app['config']->get('webauthn.userless')
)
);
$this->app->bind(
PublicKeyCredentialRpEntity::class,
fn ($app) => new PublicKeyCredentialRpEntity(
$app['config']->get('app.name', 'Laravel'),
$app->make('request')->host(),
$app['config']->get('webauthn.icon')
name: $app['config']->get('app.name', 'Laravel'),
id: $app->make('request')->host(),
icon: $app['config']->get('webauthn.icon')
)
);
$this->app->bind(
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Http/Controllers/AuthenticateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function it_auth_get()
$response = $this->get('/webauthn/auth', ['accept' => 'application/json']);

$response->assertStatus(200);
$this->assertEquals('Y2hhbGxlbmdl', $response->json('publicKey.challenge'));
$this->assertEquals('challenge', $response->json('publicKey.challenge'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Http/Controllers/WebauthnControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function it_register_get_data()
$response = $this->post('/webauthn/keys/options', [], ['accept' => 'application/json']);

$response->assertStatus(200);
$this->assertEquals('Y2hhbGxlbmdl', $response->json('publicKey.challenge'));
$this->assertEquals('challenge', $response->json('publicKey.challenge'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Services/Webauthn/CredentialRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public function it_returns_an_array_with_the_keys()

$keys = CredentialRepository::getRegisteredKeys($user);
$this->assertCount(1, $keys);
$this->assertEquals('{"type":"public-key","id":"MQ"}', json_encode($keys[0], JSON_THROW_ON_ERROR));
$this->assertEquals('{"type":"public-key","id":"1","transports":[]}', json_encode($keys[0], JSON_THROW_ON_ERROR));
}
}
40 changes: 20 additions & 20 deletions tests/Unit/Services/WebauthnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function test_get_register_data()

$this->assertInstanceOf(\Webauthn\PublicKeyCredentialCreationOptions::class, $publicKey);

$this->assertNotNull($publicKey->getChallenge());
$this->assertEquals(32, strlen($publicKey->getChallenge()));
$this->assertNotNull($publicKey->challenge);
$this->assertEquals(32, strlen($publicKey->challenge));

$this->assertInstanceOf(\Webauthn\PublicKeyCredentialUserEntity::class, $publicKey->getUser());
$this->assertEquals($user->getAuthIdentifier(), $publicKey->getUser()->getId());
$this->assertEquals($user->email, $publicKey->getUser()->getDisplayName());
$this->assertInstanceOf(\Webauthn\PublicKeyCredentialUserEntity::class, $publicKey->user);
$this->assertEquals($user->getAuthIdentifier(), $publicKey->user->id);
$this->assertEquals($user->email, $publicKey->user->displayName);
}

/**
Expand All @@ -65,7 +65,7 @@ public function test_do_register_data()
'type' => 'public-key',
'transports' => '[]',
'attestationType' => 'none',
'trustPath' => '{"type":"Webauthn\\\\TrustPath\\\\EmptyTrustPath"}',
'trustPath' => '{}',
'aaguid' => '30303030-3030-3030-3030-303030303030',
'credentialPublicKey' => 'omExZXZhbHVlYTMm',
'counter' => '1',
Expand All @@ -86,13 +86,13 @@ public function test_get_authenticate_data()

$this->assertInstanceOf(\Webauthn\PublicKeyCredentialRequestOptions::class, $publicKey);

$this->assertNotNull($publicKey->getChallenge());
$this->assertEquals(32, strlen($publicKey->getChallenge()));
$this->assertNotNull($publicKey->challenge);
$this->assertEquals(32, strlen($publicKey->challenge));

$this->assertEquals('preferred', $publicKey->getUserVerification());
$this->assertEquals('localhost', $publicKey->getRpId());
$this->assertEquals(60000, $publicKey->getTimeout());
$this->assertCount(0, $publicKey->getExtensions());
$this->assertEquals('preferred', $publicKey->userVerification);
$this->assertEquals('localhost', $publicKey->rpId);
$this->assertEquals(60000, $publicKey->timeout);
$this->assertCount(0, $publicKey->extensions);
}

/**
Expand Down Expand Up @@ -124,7 +124,7 @@ private function getAttestationData($publicKey)
'response' => [
'clientDataJSON' => Base64UrlSafe::encodeUnpadded(json_encode([
'type' => 'webauthn.create',
'challenge' => Base64UrlSafe::encodeUnpadded($publicKey->getChallenge()),
'challenge' => Base64UrlSafe::encodeUnpadded($publicKey->challenge),
'origin' => 'https://localhost',
'tokenBinding' => [
'status' => 'supported',
Expand Down Expand Up @@ -192,7 +192,7 @@ public function test_enabled()
*/
public function test_aaguid_null()
{
$webauthnKey = new WebauthnKey();
$webauthnKey = new WebauthnKey;
$webauthnKey->aaguid = null;

$this->assertNull($webauthnKey->getAttributeValue('aaguid'));
Expand All @@ -204,7 +204,7 @@ public function test_aaguid_null()
*/
public function test_aaguid_empty()
{
$webauthnKey = new WebauthnKey();
$webauthnKey = new WebauthnKey;
$webauthnKey->aaguid = '';

$this->assertEquals('', $webauthnKey->getAttributeValue('aaguid'));
Expand All @@ -216,7 +216,7 @@ public function test_aaguid_empty()
*/
public function test_aaguid_string()
{
$webauthnKey = new WebauthnKey();
$webauthnKey = new WebauthnKey;
$webauthnKey->aaguid = '38195f59-0e5b-4ebf-be46-75664177eeee';

$this->assertEquals('38195f59-0e5b-4ebf-be46-75664177eeee', $webauthnKey->getAttributeValue('aaguid'));
Expand All @@ -227,9 +227,9 @@ public function test_aaguid_string()
/**
* @test
*/
public function test_aaguid_Uuid()
public function test_aaguid_uuid()
{
$webauthnKey = new WebauthnKey();
$webauthnKey = new WebauthnKey;
$webauthnKey->aaguid = Uuid::fromString('38195f59-0e5b-4ebf-be46-75664177eeee');

$this->assertEquals('38195f59-0e5b-4ebf-be46-75664177eeee', $webauthnKey->getAttributeValue('aaguid'));
Expand All @@ -252,7 +252,7 @@ public function it_creates_model()
[],
'attestationType',
new \Webauthn\TrustPath\EmptyTrustPath,
new NilUuid(),
new NilUuid,
'credentialPublicKey',
$user->id,
0
Expand All @@ -278,7 +278,7 @@ public function it_creates_model_anyway()
[],
'attestationType',
new \Webauthn\TrustPath\EmptyTrustPath,
new NilUuid(),
new NilUuid,
'credentialPublicKey',
$user->id,
0
Expand Down

0 comments on commit 1701d1c

Please sign in to comment.