/** * Request and check a domain. * * @param string $domain * * @throws \Exception */ public function challengeDomain($domain) { Assert::stringNotEmpty($domain, 'challengeDomain::$domain expected a non-empty string. Got: %s'); $challenge = $this->client->requestChallenge($domain); $challengeEvent = new ChallengeEvent($challenge); $this->dispatcher->dispatch(AcmePhpBundleEvents::CHALLENGE_REQUESTED, $challengeEvent); try { $this->client->checkChallenge($challenge); $this->dispatcher->dispatch(AcmePhpBundleEvents::CHALLENGE_ACCEPTED, $challengeEvent); } catch (\Exception $e) { $this->dispatcher->dispatch(AcmePhpBundleEvents::CHALLENGE_REJECTED, $challengeEvent); throw $e; } finally { $this->dispatcher->dispatch(AcmePhpBundleEvents::CHALLENGE_CHECKED, $challengeEvent); } }
/** * Request a new certificate for the given configuration. * * @param DomainConfiguration $configuration * * @return Certificate */ public function requestCertificate(DomainConfiguration $configuration) { $domains = array_merge([$configuration->getDomain()], (array) $configuration->getCSR()->getSubjectAlternativeNames()); $challengedDomains = []; if ($this->certificateRepository->hasCertificate($configuration)) { $metadata = $this->certificateRepository->loadCertificate($configuration); $challengedDomains = array_merge([$configuration->getDomain()], $metadata->getSubjectAlternativeNames()); } $unchallengedDomains = array_values(array_diff($domains, $challengedDomains)); if (count($unchallengedDomains)) { $this->challenger->challengeDomains($unchallengedDomains); } $domain = $configuration->getDomain(); $domainKeyPair = $this->keyPairFactory->createKeyPairProvider($domain)->getOrCreateKeyPair(); $certificate = $this->client->requestCertificate($domain, $domainKeyPair, $configuration->getCSR()); $this->dispatcher->dispatch(AcmePhpBundleEvents::CERTIFICATE_REQUESTED, new CertificateEvent($configuration, $certificate, $domainKeyPair)); $this->logger->notice('Certificate for domain {domain} requested', ['domain' => $configuration->getDomain()]); return $certificate; }
/** * @expectedException \Exception */ public function test challengeDomain triggers rejected event() { $dummyDomain = uniqid(); $dummyChallenge = $this->prophesize(Challenge::class)->reveal(); $this->mockClient->requestChallenge($dummyDomain)->shouldBeCalled()->willReturn($dummyChallenge); $this->mockClient->checkChallenge($dummyChallenge)->shouldBeCalled()->willThrow(new \Exception()); $this->mockDispatcher->dispatch('acme_php.challenge.requested', Argument::that(function ($item) use($dummyChallenge) { $this->assertInstanceOf(ChallengeEvent::class, $item); $this->assertSame($dummyChallenge, $item->getChallenge()); return true; }))->shouldBeCalled(); $this->mockDispatcher->dispatch('acme_php.challenge.accepted', Argument::any())->shouldNotBeCalled(); $this->mockDispatcher->dispatch('acme_php.challenge.rejected', Argument::that(function ($item) use($dummyChallenge) { $this->assertInstanceOf(ChallengeEvent::class, $item); $this->assertSame($dummyChallenge, $item->getChallenge()); return true; }))->shouldBeCalled(); $this->mockDispatcher->dispatch('acme_php.challenge.checked', Argument::that(function ($item) use($dummyChallenge) { $this->assertInstanceOf(ChallengeEvent::class, $item); $this->assertSame($dummyChallenge, $item->getChallenge()); return true; }))->shouldBeCalled(); $this->service->challengeDomain($dummyDomain); }
public function test requestCertificate trigger event() { $dummyDomain = uniqid(); $dummyCsr = $this->prophesize(CSR::class)->reveal(); $dummyDomainKeyPair = $this->prophesize(KeyPair::class)->reveal(); $dummyCertificate = $this->prophesize(Certificate::class)->reveal(); $dummyMetadata = new CertificateMetadata($dummyDomain, null, true, null, [$dummyDomain]); $configuration = new DomainConfiguration($dummyDomain, $dummyCsr); $this->mockCertificateRepository->hasCertificate($configuration)->willReturn(true); $this->mockCertificateRepository->loadCertificate($configuration)->willReturn($dummyMetadata); $mockKeyPairProvider = $this->prophesize(KeyPairProvider::class); $this->mockKeyPairFactory->createKeyPairProvider($dummyDomain)->shouldBeCalled()->willReturn($mockKeyPairProvider); $mockKeyPairProvider->getOrCreateKeyPair()->shouldBeCalled()->willReturn($dummyDomainKeyPair); $this->mockClient->requestCertificate($dummyDomain, $dummyDomainKeyPair, $dummyCsr)->shouldBeCalled()->willReturn($dummyCertificate); $this->mockDispatcher->dispatch('acme_php.certificate.requested', Argument::that(function ($item) use($dummyCertificate) { return $item instanceof CertificateEvent && $item->getCertificate() === $dummyCertificate; }))->shouldBeCalled(); $this->service->requestCertificate($configuration); }