/** * @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; } }
private function solveChallenge(AcmeService $acme, KeyPair $keyPair, $domain, $path) { 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); $this->climate->whisper(" Providing payload at http://{$domain}/.well-known/acme-challenge/{$token}"); $challengeStore = new ChallengeStore($path); try { (yield $challengeStore->put($token, $payload, isset($user) ? $user : null)); (yield $acme->verifyHttp01Challenge($domain, $token, $payload)); (yield $acme->answerChallenge($challenge->uri, $payload)); (yield $acme->pollForChallenge($location)); $this->climate->comment(" {$domain} is now authorized."); (yield $challengeStore->delete($token)); } catch (Exception $e) { // no finally because generators... (yield $challengeStore->delete($token)); throw $e; } catch (Throwable $e) { // no finally because generators... (yield $challengeStore->delete($token)); throw $e; } }