Skip to content

Commit 6615843

Browse files
authored
feat: use Credential repository as class (#516)
1 parent ba9048d commit 6615843

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

src/Models/WebauthnKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class WebauthnKey extends Model
1919
/**
2020
* The attributes that aren't mass assignable.
2121
*
22-
* @var string[]|bool
22+
* @var array<string>
2323
*/
2424
protected $guarded = ['id'];
2525

src/Services/Webauthn.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,12 @@ public static function enabled(User $user): bool
180180

181181
/**
182182
* Test if the user can register a new key.
183+
*
184+
* @psalm-suppress PossiblyUnusedParam
183185
*/
184186
public static function canRegister(User $user): bool
185187
{
186-
return static::webauthnEnabled() && (! static::enabled($user) || static::check());
188+
return static::webauthnEnabled();
187189
}
188190

189191
/**

src/Services/Webauthn/CreationOptionsFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function __construct(
2727
Config $config,
2828
protected PublicKeyCredentialRpEntity $publicKeyCredentialRpEntity,
2929
protected AuthenticatorSelectionCriteria $authenticatorSelectionCriteria,
30-
protected CoseAlgorithmManager $algorithmManager
30+
protected CoseAlgorithmManager $algorithmManager,
31+
protected CredentialRepository $repository
3132
) {
3233
parent::__construct($request, $cache, $config);
3334
$this->attestationConveyance = $config->get('webauthn.attestation_conveyance', 'none');
@@ -82,6 +83,6 @@ private function createCredentialParameters(): array
8283
*/
8384
protected function getExcludedCredentials(User $user): array
8485
{
85-
return CredentialRepository::getRegisteredKeys($user);
86+
return $this->repository->getRegisteredKeys($user);
8687
}
8788
}

src/Services/Webauthn/CredentialRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CredentialRepository
1515
*
1616
* @return Collection<array-key,PublicKeyCredentialSource>
1717
*/
18-
protected static function getAllRegisteredKeys(int|string $userId): Collection
18+
protected function getAllRegisteredKeys(int|string $userId): Collection
1919
{
2020
return (Webauthn::model())::where('user_id', $userId)
2121
->get()
@@ -28,9 +28,9 @@ protected static function getAllRegisteredKeys(int|string $userId): Collection
2828
*
2929
* @return array<array-key,PublicKeyCredentialDescriptor>
3030
*/
31-
public static function getRegisteredKeys(User $user): array
31+
public function getRegisteredKeys(User $user): array
3232
{
33-
return static::getAllRegisteredKeys($user->getAuthIdentifier())
33+
return $this->getAllRegisteredKeys($user->getAuthIdentifier())
3434
->map
3535
->getPublicKeyCredentialDescriptor()
3636
->toArray();

src/Services/Webauthn/RequestOptionsFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public function __construct(
2121
Request $request,
2222
Cache $cache,
2323
Config $config,
24-
protected PublicKeyCredentialRpEntity $publicKeyCredentialRpEntity
24+
protected PublicKeyCredentialRpEntity $publicKeyCredentialRpEntity,
25+
protected CredentialRepository $repository
2526
) {
2627
parent::__construct($request, $cache, $config);
2728
$this->userVerification = self::getUserVerification($config);
@@ -60,7 +61,7 @@ private static function getUserVerification(Config $config): ?string
6061
*/
6162
private function getAllowedCredentials(?User $user): array
6263
{
63-
return $user !== null ? CredentialRepository::getRegisteredKeys($user) : [];
64+
return $user !== null ? $this->repository->getRegisteredKeys($user) : [];
6465
}
6566

6667
/**

tests/Unit/Services/Webauthn/CredentialRepositoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function it_returns_an_empty_array_when_no_keys_are_registered()
1919

2020
$this->assertEmpty(WebauthnKey::all());
2121

22-
$this->assertEquals([], CredentialRepository::getRegisteredKeys($user));
22+
$this->assertEquals([], (new CredentialRepository)->getRegisteredKeys($user));
2323
}
2424

2525
#[Test]
@@ -34,7 +34,7 @@ public function it_returns_an_array_with_the_keys()
3434
'credentialId' => '1',
3535
]);
3636

37-
$keys = CredentialRepository::getRegisteredKeys($user);
37+
$keys = (new CredentialRepository)->getRegisteredKeys($user);
3838
$this->assertCount(1, $keys);
3939
$this->assertEquals('{"type":"public-key","id":"1","transports":[]}', json_encode($keys[0], JSON_THROW_ON_ERROR));
4040
}

0 commit comments

Comments
 (0)