/**
* @param AcmeService $acme
* @param KeyPair $keyPair
* @param $domain
* @return \Generator
* @throws AcmeException
* @throws \Exception
* @throws \Throwable
*/
private function solveChallenge(AcmeService $acme, KeyPair $keyPair, $domain)
{
list($location, $challenges) = (yield $acme->requestChallenges($domain));
$goodChallenges = $this->findSuitableCombination($challenges);
if (empty($goodChallenges)) {
throw new AcmeException("Couldn't find any combination of challenges which this client can solve!");
}
$challenge = $challenges->challenges[reset($goodChallenges)];
$token = $challenge->token;
if (!preg_match("#^[a-zA-Z0-9-_]+\$#", $token)) {
throw new AcmeException("Protocol violation: Invalid Token!");
}
$payload = $acme->generateHttp01Payload($keyPair, $token);
$challengeStore = $this->getChallengeStorage();
try {
$challengeStore->put($token, $payload);
(yield $acme->verifyHttp01Challenge($domain, $token, $payload));
(yield $acme->answerChallenge($challenge->uri, $payload));
(yield $acme->pollForChallenge($location));
$challengeStore->delete($token);
} catch (\Exception $e) {
// no finally because generators...
$challengeStore->delete($token);
throw $e;
} catch (\Throwable $e) {
// no finally because generators...
$challengeStore->delete($token);
throw $e;
}
}