src/Services/GoogleAuthenticatorService.php line 98

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Services;
  3. use Sonata\GoogleAuthenticator\GoogleAuthenticator;
  4. use EADPlataforma\Services\SchoolEntityManager;
  5. use EADPlataforma\Services\ConfigurationService;
  6. use EADPlataforma\Entity\User;
  7. /**
  8.  * GoogleAuthenticatorService
  9.  */
  10. class GoogleAuthenticatorService
  11. {
  12.     /**
  13.      * @var string
  14.      */
  15.     protected $eadDomain;
  16.     /**
  17.      * @var SchoolEntityManager
  18.      */
  19.     protected $em;
  20.     /**
  21.      * @var ConfigurationService
  22.      */
  23.     protected $configuration;
  24.     /**
  25.      * @var FileService
  26.      */
  27.     protected $fileService;
  28.     /**
  29.      * @var Sonata\GoogleAuthenticator\GoogleAuthenticator
  30.      */
  31.     protected $googleAuthenticator;
  32.     /**
  33.      * @var string
  34.      */
  35.     protected $email;
  36.     /**
  37.      * @var string
  38.      */
  39.     protected $secretKey;
  40.     /**
  41.      * @var string
  42.      */
  43.     protected $code;
  44.     /**
  45.      * Constructor
  46.      *
  47.      * @param SchoolEntityManager $em
  48.      * @param ConfigurationService $configurationService
  49.      */
  50.     public function __construct(
  51.         SchoolEntityManager $em
  52.         ConfigurationService $configurationService
  53.     )
  54.     {
  55.         $this->em $em;
  56.         $this->configuration $configurationService;
  57.         $this->googleAuthenticator = new \Google\Authenticator\GoogleAuthenticator();
  58.         $this->client $this->configuration->getClient();
  59.         if($this->client){
  60.             $this->eadDomain $this->client->getDomainPrimary();
  61.         }
  62.     }
  63.     public function setEmail(string $email): self
  64.     {
  65.         $this->email $email;
  66.         return $this;
  67.     }
  68.     public function setSecretKey(string $secretKey): self
  69.     {
  70.         $this->secretKey $secretKey;
  71.         return $this;
  72.     }
  73.     public function setCode(string $code): self
  74.     {
  75.         $this->code $code;
  76.         return $this;
  77.     }
  78.     public function generateQrCodeAuthentication(): array
  79.     {
  80.         $generateSecretKey $this->googleAuthenticator->generateSecret();
  81.         $domain str_replace('.eadplataforma.app'''$this->eadDomain);
  82.         $domain str_replace('.eadplataforma.com'''$domain);
  83.         $domain ucfirst($domain);
  84.         $qrCode $this->googleAuthenticator->getURL($domain$this->email$generateSecretKey);
  85.         $dataAuthentication = [
  86.             "secretKey" => $generateSecretKey,
  87.             "qrCode" => $qrCode
  88.         ];
  89.         return $dataAuthentication;
  90.     }
  91.     public function checkCodeAuthentication(): bool
  92.     {
  93.         $checkCode $this->googleAuthenticator->checkCode($this->secretKey$this->code2);
  94.         if($checkCode){
  95.             return true;
  96.         }
  97.         return false;
  98.     }
  99. }