src/Cms/DomainBundle/Controller/LetsEncryptController.php line 31

Open in your IDE?
  1. <?php
  2. namespace Cms\DomainBundle\Controller;
  3. use Cms\CoreBundle\Util\Controller;
  4. use Cms\DomainBundle\Entity\Domain;
  5. use Cms\DomainBundle\Entity\SslCertificates\LetsEncryptSslCertificate;
  6. use Cms\DomainBundle\Service\Managers\SslCertificateManagers\LetsEncryptSslCertificateManager;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. /**
  11.  * Class LetsEncryptController
  12.  * @package Cms\DomainBundle\Controller
  13.  */
  14. final class LetsEncryptController extends Controller
  15. {
  16.     /**
  17.      * @param Request $request
  18.      * @param string $token
  19.      * @return Response
  20.      * @throws NotFoundHttpException
  21.      */
  22.     public function verifyChallengeAction(Request $request$token)
  23.     {
  24.         // get the hostname of the request
  25.         $host $request->getHost();
  26.         // obtain the domain for this host
  27.         $domain $this->getEntityManager()->getRepository(Domain::class)->findOneByHost($host);
  28.         // if we did not match, treat as a 404
  29.         if (empty($domain)) {
  30.             throw new NotFoundHttpException();
  31.         }
  32.         // need a lets encrypt certificate
  33.         $certificate $this->getLetsEncryptSslCertificateManager()
  34.             ->getPendingCertificate($domain);
  35.         if ( ! $certificate instanceof LetsEncryptSslCertificate) {
  36.             throw new NotFoundHttpException();
  37.         }
  38.         // make sure we are expecting a verification
  39.         if ($certificate->getState()->getChallenge() !== LetsEncryptSslCertificateManager::CHALLENGES__HTTP_01) {
  40.             throw new NotFoundHttpException();
  41.         }
  42.         // now match the tokens
  43.         if ($certificate->getState()->getToken() !== $token) {
  44.             throw new NotFoundHttpException();
  45.         }
  46.         // all is good, we can return the payload
  47.         return Response::create(
  48.             $certificate->getState()->getPayload(),
  49.             200,
  50.             array(
  51.                 'Content-Type' => 'text/plain',
  52.             )
  53.         );
  54.     }
  55.     /**
  56.      * @return LetsEncryptSslCertificateManager|object
  57.      */
  58.     private function getLetsEncryptSslCertificateManager(): LetsEncryptSslCertificateManager
  59.     {
  60.         return $this->get(__METHOD__);
  61.     }
  62. }