Skip to content

Commit 51ae014

Browse files
committed
Add missing tests
1 parent c53a496 commit 51ae014

File tree

13 files changed

+1381
-47
lines changed

13 files changed

+1381
-47
lines changed

src/Attestation/AttestationData.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,13 @@ public function __construct(string $binary, array $allowedFormats)
8585
throw new WebauthnException('Invalid attestation format provided (attStmt not available)');
8686
}
8787

88-
if (! array_key_exists('authData', $enc) || ! $enc['authData'] instanceof ByteBuffer) {
89-
throw new WebauthnException('Invalid attestation format provided (authData not available)');
90-
}
91-
9288
$this->formatName = $enc['fmt'];
93-
$this->authenticatorData = new AuthenticatorData($enc['authData']->getBinaryString());
9489

95-
if (! in_array($this->formatName, $allowedFormats)) {
96-
throw new WebauthnException(sprintf(
97-
'Invalid attestation format [%s], allowed [%s]',
98-
$this->formatName,
99-
implode(', ', $allowedFormats)
100-
));
101-
}
90+
// Set attestation data
91+
$this->setAuthenticatorData($enc);
10292

10393
// Create attestation format based on the provided format name
104-
$this->createAttestationFormat($enc);
94+
$this->createAttestationFormat($enc, $allowedFormats);
10595
}
10696

10797
/**
@@ -238,13 +228,36 @@ protected function getCertificateInfo(string $type): string
238228
return $result;
239229
}
240230

231+
/**
232+
* Set the authenticator data
233+
* @param array<string|int, mixed> $enc
234+
* @return void
235+
*/
236+
protected function setAuthenticatorData(array $enc): void
237+
{
238+
if (! array_key_exists('authData', $enc) || ! $enc['authData'] instanceof ByteBuffer) {
239+
throw new WebauthnException('Invalid attestation format provided (authData not available)');
240+
}
241+
242+
$this->authenticatorData = new AuthenticatorData($enc['authData']->getBinaryString());
243+
}
244+
241245
/**
242246
* Create the attestation format
243-
* @param array<string, mixed> $enc the encoded data
247+
* @param array<string|int, mixed> $enc the encoded data
248+
* @param array<string> $allowedFormats the allowed format
244249
* @return void
245250
*/
246-
protected function createAttestationFormat(array $enc): void
251+
protected function createAttestationFormat(array $enc, array $allowedFormats): void
247252
{
253+
if (! in_array($this->formatName, $allowedFormats)) {
254+
throw new WebauthnException(sprintf(
255+
'Invalid attestation format [%s], allowed [%s]',
256+
$this->formatName,
257+
implode(', ', $allowedFormats)
258+
));
259+
}
260+
248261
switch ($this->formatName) {
249262
case KeyFormat::FIDO_U2FA:
250263
$this->format = new FidoU2F($enc, $this->authenticatorData);

src/Attestation/Format/FidoU2F.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function __construct(
100100
}
101101

102102
if (! $attestationStatement['x5c'][0] instanceof ByteBuffer) {
103-
throw new WebauthnException('Invalid X5C certificate');
103+
throw new WebauthnException('Invalid X5C certificate must be Byte Buffer)');
104104
}
105105

106106
$this->signature = $attestationStatement['sig']->getBinaryString();
@@ -114,7 +114,7 @@ public function getCertificatePem(): string
114114
{
115115
$pem = '-----BEGIN CERTIFICATE-----' . "\n";
116116
$pem .= chunk_split(base64_encode($this->x5c), 64, "\n");
117-
$pem = '-----END CERTIFICATE-----' . "\n";
117+
$pem .= '-----END CERTIFICATE-----' . "\n";
118118

119119
return $pem;
120120
}

src/Attestation/Format/Packed.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ public function __construct(
7878
! array_key_exists('alg', $attestationStatement) ||
7979
$this->getCoseAlgorithm($attestationStatement['alg']) === null
8080
) {
81-
throw new WebauthnException(sprintf(
82-
'Unsupported algorithm [%d]',
83-
$attestationStatement['alg']
84-
));
81+
throw new WebauthnException('Unsupported algorithm or not provided');
8582
}
8683

8784
if (

src/Entity/CredentialPublicKey.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,8 @@ public function __construct(string $binaryData, int $offset, int &$endOffset)
125125
$this->kty = $enc[self::COSE_KTY];
126126
$this->alg = $enc[self::COSE_ALG];
127127

128-
switch ($this->alg) {
129-
case self::EC2_ES256:
130-
$this->createES256($enc);
131-
break;
132-
case self::RSA_RS256:
133-
$this->createRSA256($enc);
134-
break;
135-
}
128+
// Update properties
129+
$this->create($enc);
136130
}
137131

138132
/**
@@ -207,6 +201,23 @@ public function jsonSerialize()
207201
return get_object_vars($this);
208202
}
209203

204+
/**
205+
* Update properties based on the given data received
206+
* @param array<string, mixed> $enc
207+
* @return void
208+
*/
209+
protected function create(array $enc): void
210+
{
211+
switch ($this->alg) {
212+
case self::EC2_ES256:
213+
$this->createES256($enc);
214+
break;
215+
case self::RSA_RS256:
216+
$this->createRSA256($enc);
217+
break;
218+
}
219+
}
220+
210221
/**
211222
* Create for ES256
212223
* @param array<string|int, mixed> $enc

src/Webauthn.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,21 +329,21 @@ public function processRegistration(
329329
$this->signatureCounter = $signCount;
330330
}
331331

332-
// prepare data to store for future logins
332+
// Prepare data to store for future logins
333333
$data = [
334334
'rp_id' => $this->relyingParty->getId(),
335335
'attestation_format' => $attestation->getFormatName(),
336336
'credential_id' => bin2hex($attestation->getAuthenticatorData()->getCredentialId()),
337337
'credential_public_key' => $attestation->getAuthenticatorData()->getPublicKeyPEM(),
338-
'certificate_chain' => $attestation->getCertificateChain(),
339-
'certificate' => $attestation->getCertificatePem(),
340-
'certificate_issuer' => $attestation->getCertificateIssuer(),
341-
'certificate_subject' => $attestation->getCertificateSubject(),
342-
'root_certificate_valid' => $isRootValid,
338+
'cert_chain' => $attestation->getCertificateChain(),
339+
'cert' => $attestation->getCertificatePem(),
340+
'cert_issuer' => $attestation->getCertificateIssuer(),
341+
'cert_subject' => $attestation->getCertificateSubject(),
342+
'is_root_cert_valid' => $isRootValid,
343343
'signature_counter' => $this->signatureCounter,
344344
'aaguid' => bin2hex($attestation->getAuthenticatorData()->getAaguid()),
345-
'user_present' => $userPresent,
346-
'user_verified' => $userVerified,
345+
'is_user_present' => $userPresent,
346+
'is_user_verified' => $userVerified,
347347
];
348348

349349

0 commit comments

Comments
 (0)