| Total Complexity | 66 |
| Total Lines | 521 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Webauthn often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Webauthn, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 58 | class Webauthn |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * The attestation data formats |
||
| 62 | * @var array<string> |
||
| 63 | */ |
||
| 64 | protected array $formats = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The challenge to use |
||
| 68 | * @var ByteBuffer|null |
||
| 69 | */ |
||
| 70 | protected ?ByteBuffer $challenge = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The signature counter |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | protected int $signatureCounter = 0; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The relying party entity |
||
| 80 | * @var RelyingParty |
||
| 81 | */ |
||
| 82 | protected RelyingParty $relyingParty; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The certificates files path |
||
| 86 | * @var array<string> |
||
| 87 | */ |
||
| 88 | protected array $certificates = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The configuration instance |
||
| 92 | * @var WebauthnConfiguration |
||
| 93 | */ |
||
| 94 | protected WebauthnConfiguration $config; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Create new instance |
||
| 98 | * @param WebauthnConfiguration $config |
||
| 99 | * @param array<string> $allowedFormats |
||
| 100 | */ |
||
| 101 | public function __construct(WebauthnConfiguration $config, array $allowedFormats = []) |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Add a root certificate to verify new registrations |
||
| 123 | * @param string|array<string> $path |
||
| 124 | * @return $this |
||
| 125 | */ |
||
| 126 | public function addRootCertificate($path): self |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Return the parameters to be used for the registration |
||
| 141 | * @param string $userId |
||
| 142 | * @param string $userName |
||
| 143 | * @param string $userDisplayName |
||
| 144 | * @param string $userVerificationType |
||
| 145 | * @param bool $crossPlatformAttachment |
||
| 146 | * @param array<string> $excludeCredentialIds |
||
| 147 | * @param bool $withoutAttestation |
||
| 148 | * @return PublicKey |
||
| 149 | */ |
||
| 150 | public function getRegistrationParams( |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Return the authentication parameters |
||
| 215 | * @param string $userVerificationType |
||
| 216 | * @param array<string> $credentialIds |
||
| 217 | * @return PublicKey |
||
| 218 | */ |
||
| 219 | public function getAuthenticationParams( |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Process the user registration |
||
| 248 | * @param string $clientDataJson |
||
| 249 | * @param string $attestationObject |
||
| 250 | * @param string|ByteBuffer $challenge |
||
| 251 | * @param bool $requireUserVerification |
||
| 252 | * @param bool $requireUserPresent |
||
| 253 | * @param bool $failIfRootCertificateMismatch |
||
| 254 | * @return array<string, mixed> |
||
| 255 | */ |
||
| 256 | public function processRegistration( |
||
| 257 | string $clientDataJson, |
||
| 258 | string $attestationObject, |
||
| 259 | $challenge, |
||
| 260 | bool $requireUserVerification = false, |
||
| 261 | bool $requireUserPresent = true, |
||
| 262 | bool $failIfRootCertificateMismatch = true |
||
| 263 | ): array { |
||
| 264 | $clientDataHash = hash('sha256', $clientDataJson, true); |
||
| 265 | if (is_string($challenge)) { |
||
| 266 | $challenge = new ByteBuffer($challenge); |
||
| 267 | } |
||
| 268 | |||
| 269 | // security: https://www.w3.org/TR/webauthn/#registering-a-new-credential |
||
| 270 | try { |
||
| 271 | // 2. Let C, the client data claimed as collected during the credential creation, |
||
| 272 | // be the result of running an implementation-specific JSON parser on JSONtext. |
||
| 273 | $clientData = Json::decode($clientDataJson); |
||
| 274 | } catch (Exception $ex) { |
||
| 275 | throw new WebauthnException(sprintf('Invalid client data provided, [%s]', $ex->getMessage())); |
||
| 276 | } |
||
| 277 | |||
| 278 | // 3. Verify that the value of C.type is webauthn.create. |
||
| 279 | if (! isset($clientData->type) || $clientData->type !== 'webauthn.create') { |
||
| 280 | throw new WebauthnException('Invalid client type provided'); |
||
| 281 | } |
||
| 282 | |||
| 283 | // 4. Verify that the value of C.challenge matches the challenge that was |
||
| 284 | // sent to the authenticator in the create() call. |
||
| 285 | if ( |
||
| 286 | ! isset($clientData->challenge) || |
||
| 287 | ByteBuffer::fromBase64Url($clientData->challenge)->getBinaryString() !== $challenge->getBinaryString() |
||
| 288 | ) { |
||
| 289 | throw new WebauthnException('Invalid challenge provided'); |
||
| 290 | } |
||
| 291 | |||
| 292 | // 5. Verify that the value of C.origin matches the Relying Party's origin. |
||
| 293 | if (! isset($clientData->origin) || $this->checkOrigin($clientData->origin) === false) { |
||
| 294 | throw new WebauthnException('Invalid origin provided'); |
||
| 295 | } |
||
| 296 | |||
| 297 | $attestation = $this->createAttestationData($attestationObject); |
||
| 298 | |||
| 299 | // 9. Verify that the RP ID hash in authData is indeed the SHA-256 |
||
| 300 | // hash of the RP ID expected by the RP. |
||
| 301 | if ($attestation->validateRelyingPartyIdHash($this->relyingParty->getHashId()) === false) { |
||
| 302 | throw new WebauthnException('Invalid relying party id hash provided'); |
||
| 303 | } |
||
| 304 | |||
| 305 | // 14. Verify that attStmt is a correct attestation statement, conveying |
||
| 306 | // a valid attestation signature |
||
| 307 | if ($attestation->validateAttestation($clientDataHash) === false) { |
||
| 308 | throw new WebauthnException('Invalid certificate signature'); |
||
| 309 | } |
||
| 310 | |||
| 311 | // 15. If validation is successful, obtain a list of acceptable trust anchors |
||
| 312 | $isRootValid = count($this->certificates) > 0 |
||
| 313 | ? $attestation->validateRootCertificate($this->certificates) |
||
| 314 | : false; |
||
| 315 | |||
| 316 | if ($failIfRootCertificateMismatch && count($this->certificates) > 0 && $isRootValid === false) { |
||
| 317 | throw new WebauthnException('Invalid root certificate'); |
||
| 318 | } |
||
| 319 | |||
| 320 | // 10. Verify that the User Present bit of the flags in authData is set. |
||
| 321 | $userPresent = $attestation->getAuthenticatorData()->isUserPresent(); |
||
| 322 | if ($requireUserPresent && $userPresent === false) { |
||
| 323 | throw new WebauthnException('User is not present during authentication'); |
||
| 324 | } |
||
| 325 | |||
| 326 | // 11. If user verification is required for this registration, verify |
||
| 327 | // that the User Verified bit of the flags in authData is set. |
||
| 328 | $userVerified = $attestation->getAuthenticatorData()->isUserVerified(); |
||
| 329 | if ($requireUserVerification && $userVerified === false) { |
||
| 330 | throw new WebauthnException('User is not verified during authentication'); |
||
| 331 | } |
||
| 332 | |||
| 333 | $signCount = $attestation->getAuthenticatorData()->getSignatureCount(); |
||
| 334 | if ($signCount > 0) { |
||
| 335 | $this->signatureCounter = $signCount; |
||
| 336 | } |
||
| 337 | |||
| 338 | // Prepare data to store for future logins |
||
| 339 | $data = [ |
||
| 340 | 'rp_id' => $this->relyingParty->getId(), |
||
| 341 | 'attestation_format' => $attestation->getFormatName(), |
||
| 342 | 'credential_id' => bin2hex($attestation->getAuthenticatorData()->getCredentialId()), |
||
| 343 | 'credential_public_key' => $attestation->getAuthenticatorData()->getPublicKeyPEM(), |
||
| 344 | 'cert_chain' => $attestation->getCertificateChain(), |
||
| 345 | 'cert' => $attestation->getCertificatePem(), |
||
| 346 | 'cert_issuer' => $attestation->getCertificateIssuer(), |
||
| 347 | 'cert_subject' => $attestation->getCertificateSubject(), |
||
| 348 | 'is_root_cert_valid' => $isRootValid, |
||
| 349 | 'signature_counter' => $this->signatureCounter, |
||
| 350 | 'aaguid' => bin2hex($attestation->getAuthenticatorData()->getAaguid()), |
||
| 351 | 'is_user_present' => $userPresent, |
||
| 352 | 'is_user_verified' => $userVerified, |
||
| 353 | ]; |
||
| 354 | |||
| 355 | |||
| 356 | return $data; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Process the user authentication |
||
| 361 | * @param string $clientDataJson |
||
| 362 | * @param string $authenticatorData |
||
| 363 | * @param string $signature |
||
| 364 | * @param string $credentialPublicKey |
||
| 365 | * @param ByteBuffer|string $challenge |
||
| 366 | * @param int|null $previousSignatureCount |
||
| 367 | * @param bool $requireUserVerification |
||
| 368 | * @param bool $requireUserPresent |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public function processAuthentication( |
||
| 372 | string $clientDataJson, |
||
| 373 | string $authenticatorData, |
||
| 374 | string $signature, |
||
| 375 | string $credentialPublicKey, |
||
| 376 | $challenge, |
||
| 377 | ?int $previousSignatureCount = null, |
||
| 378 | bool $requireUserVerification = false, |
||
| 379 | bool $requireUserPresent = true |
||
| 380 | ): bool { |
||
| 381 | if (is_string($challenge)) { |
||
| 382 | $challenge = new ByteBuffer($challenge); |
||
| 383 | } |
||
| 384 | $clientDataHash = hash('sha256', $clientDataJson, true); |
||
| 385 | $authenticator = $this->createAuthenticatorData($authenticatorData); |
||
| 386 | try { |
||
| 387 | // 5. Let JSON text be the result of running UTF-8 decode on the value of cData. |
||
| 388 | $clientData = Json::decode($clientDataJson); |
||
| 389 | } catch (Exception $ex) { |
||
| 390 | throw new WebauthnException(sprintf('Invalid client data provided, [%s]', $ex->getMessage())); |
||
| 391 | } |
||
| 392 | |||
| 393 | // https://www.w3.org/TR/webauthn/#verifying-assertion |
||
| 394 | |||
| 395 | // 1. If the allowCredentials option was given when this authentication ceremony was initiated, |
||
| 396 | // verify that credential.id identifies one of the public key credentials |
||
| 397 | // that were listed in allowCredentials. |
||
| 398 | // -> TO BE VERIFIED BY IMPLEMENTATION |
||
| 399 | |||
| 400 | // 2. If credential.response.userHandle is present, verify that the user identified |
||
| 401 | // by this value is the owner of the public key credential identified by credential.id. |
||
| 402 | // -> TO BE VERIFIED BY IMPLEMENTATION |
||
| 403 | |||
| 404 | // 3. Using credential’s id attribute (or the corresponding rawId, if base64url encoding is |
||
| 405 | // inappropriate for your use case), |
||
| 406 | // look up the corresponding credential public key. |
||
| 407 | // -> TO BE LOOKED UP BY IMPLEMENTATION |
||
| 408 | |||
| 409 | // 7. Verify that the value of C.type is the string webauthn.get. |
||
| 410 | if (! isset($clientData->type) || $clientData->type !== 'webauthn.get') { |
||
| 411 | throw new WebauthnException('Invalid client type provided'); |
||
| 412 | } |
||
| 413 | |||
| 414 | // 8. Verify that the value of C.challenge matches the challenge that was sent to the |
||
| 415 | // authenticator in the PublicKeyCredentialRequestOptions passed to the get() call. |
||
| 416 | if ( |
||
| 417 | ! isset($clientData->challenge) || |
||
| 418 | ByteBuffer::fromBase64Url($clientData->challenge)->getBinaryString() !== $challenge->getBinaryString() |
||
| 419 | ) { |
||
| 420 | throw new WebauthnException('Invalid challenge provided'); |
||
| 421 | } |
||
| 422 | |||
| 423 | // 9. Verify that the value of C.origin matches the Relying Party's origin. |
||
| 424 | if (! isset($clientData->origin) || $this->checkOrigin($clientData->origin) === false) { |
||
| 425 | throw new WebauthnException('Invalid origin provided'); |
||
| 426 | } |
||
| 427 | |||
| 428 | // 11. Verify that the rpIdHash in authData is the SHA-256 hash |
||
| 429 | // of the RP ID expected by the Relying Party. |
||
| 430 | if ($authenticator->getRelyingPartyIdHash() !== $this->relyingParty->getHashId()) { |
||
| 431 | throw new WebauthnException('Invalid relying party id hash provided'); |
||
| 432 | } |
||
| 433 | |||
| 434 | // 12. Verify that the User Present bit of the flags in authData is set |
||
| 435 | if ($requireUserPresent && $authenticator->isUserPresent() === false) { |
||
| 436 | throw new WebauthnException('User is not present during authentication'); |
||
| 437 | } |
||
| 438 | |||
| 439 | // 13. If user verification is required for this assertion, verify that |
||
| 440 | // the User Verified bit of the flags in authData is set. |
||
| 441 | if ($requireUserVerification && $authenticator->isUserVerified() === false) { |
||
| 442 | throw new WebauthnException('User is not verified during authentication'); |
||
| 443 | } |
||
| 444 | |||
| 445 | // 14. Verify the values of the client extension outputs |
||
| 446 | // TODO (extensions not implemented) |
||
| 447 | |||
| 448 | // 16. Using the credential public key looked up in step 3, verify |
||
| 449 | // that sig is a valid signature over the binary |
||
| 450 | // concatenation of authData and hash. |
||
| 451 | $dataToVerify = ''; |
||
| 452 | $dataToVerify .= $authenticatorData; |
||
| 453 | $dataToVerify .= $clientDataHash; |
||
| 454 | |||
| 455 | $publicKey = openssl_pkey_get_public($credentialPublicKey); |
||
| 456 | if ($publicKey === false) { |
||
| 457 | throw new WebauthnException('Invalid public key provided'); |
||
| 458 | } |
||
| 459 | |||
| 460 | if ( |
||
| 461 | openssl_verify( |
||
| 462 | $dataToVerify, |
||
| 463 | $signature, |
||
| 464 | $publicKey, |
||
| 465 | OPENSSL_ALGO_SHA256 |
||
| 466 | ) !== 1 |
||
| 467 | ) { |
||
| 468 | throw new WebauthnException('Invalid signature provided'); |
||
| 469 | } |
||
| 470 | |||
| 471 | $signatureCount = $authenticator->getSignatureCount(); |
||
| 472 | if ($signatureCount !== 0) { |
||
| 473 | $this->signatureCounter = $signatureCount; |
||
| 474 | } |
||
| 475 | |||
| 476 | // 17. If either of the signature counter value authData.signCount or |
||
| 477 | // previous signature count is non-zero, and if authData.signCount |
||
| 478 | // less than or equal to previous signature count, it's a signal |
||
| 479 | // that the authenticator may be cloned |
||
| 480 | if ($previousSignatureCount !== null) { |
||
| 481 | if ($signatureCount !== 0 || $previousSignatureCount !== 0) { |
||
| 482 | if ($previousSignatureCount >= $signatureCount) { |
||
| 483 | throw new WebauthnException('Invalid signature counter provided'); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } |
||
| 487 | |||
| 488 | return true; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Return the challenge |
||
| 493 | * @return ByteBuffer|null |
||
| 494 | */ |
||
| 495 | public function getChallenge(): ?ByteBuffer |
||
| 498 | } |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * Create the attestation data instance |
||
| 503 | * @param string $attestationObject |
||
| 504 | * @return AttestationData |
||
| 505 | */ |
||
| 506 | protected function createAttestationData(string $attestationObject): AttestationData |
||
| 507 | { |
||
| 508 | return new AttestationData($attestationObject, $this->formats); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Create the authenticator data instance |
||
| 513 | * @param string $authenticatorData |
||
| 514 | * @return AuthenticatorData |
||
| 515 | */ |
||
| 516 | protected function createAuthenticatorData(string $authenticatorData): AuthenticatorData |
||
| 517 | { |
||
| 518 | return new AuthenticatorData($authenticatorData); |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Check the given origin |
||
| 523 | * @param string $origin |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | protected function checkOrigin(string $origin): bool |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Create the challenge if not yet created |
||
| 546 | * @return ByteBuffer |
||
| 547 | */ |
||
| 548 | protected function createChallenge(): ByteBuffer |
||
| 549 | { |
||
| 550 | if ($this->challenge === null) { |
||
| 551 | $length = $this->config->get('challenge_length'); |
||
| 552 | $this->challenge = ByteBuffer::randomBuffer($length); |
||
| 553 | } |
||
| 554 | |||
| 555 | return $this->challenge; |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Normalize the formats |
||
| 560 | * @param array<string> $formats |
||
| 561 | * @return array<string> |
||
| 562 | */ |
||
| 563 | protected function normalizeFormats(array $formats): array |
||
| 579 | } |
||
| 580 | } |
||
| 581 |