Skip to content

Commit

Permalink
Clean up the test code for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
forevermatt committed May 30, 2024
1 parent 6e49e09 commit 3f8d3a3
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions application/features/bootstrap/AuthenticationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,46 @@ public function userHasAValidWebauthnMfaMethod($username)
Assert::notEmpty($user, 'Unable to find user ' . $username);

$creationResult = Mfa::create($user->id, Mfa::TYPE_WEBAUTHN);
$mfa = Mfa::findOne(['id' => $creationResult['id']]);

$publicKey = $creationResult['data']['publicKey'];
$rpId = $publicKey['rp']['id'];
$mfa = Mfa::findOne(['id' => $creationResult['id']]);
Assert::notEmpty($mfa, sprintf(
"Unable to find MFA after creation, response was: \n%s",
json_encode($creationResult, JSON_PRETTY_PRINT)
));

$u2fSimResponse = $this->simulateU2fDevice($publicKey['challenge'], $rpId, $user, $mfa);

$mfaVerifyResult = $mfa->verify(
$u2fSimResponse,
$rpId,
'registration'
);
Assert::true($mfaVerifyResult, 'Failed to verify the WebAuthn MFA');
}

/**
* Simulate the browser interactions for registering a U2F/WebAuthn device.
*
* @param $challenge
* @param $rpId
* @param User|null $user
* @param Mfa|null $mfa
* @return array|mixed
*/
public function simulateU2fDevice($challenge, $rpId, User $user, Mfa $mfa)
{
$this->cleanRequestBody();
$this->setRequestBody('challenge', $publicKey['challenge']);
$this->setRequestBody('challenge', $challenge);
$this->setRequestBody('relying_party_id', $rpId);
$this->callU2fSimulator('/u2f/registration', 'created', $user, $mfa->external_uuid);
$u2fSimResponse = $this->getResponseBody();

if (isset($u2fSimResponse['clientExtensionResults']) && empty($u2fSimResponse['clientExtensionResults'])) {
// Force JSON-encoding to treat this as an empty object, not an empty array.
$u2fSimResponse['clientExtensionResults'] = new stdClass();
}

$mfaVerifyResult = $mfa->verify(
$u2fSimResponse,
$rpId,
'registration'
);
Assert::true($mfaVerifyResult, 'Failed to verify the WebAuthn MFA');
return $u2fSimResponse;
}

/**
Expand Down

0 comments on commit 3f8d3a3

Please sign in to comment.