<?php
namespace EADPlataforma\Services;
use Sonata\GoogleAuthenticator\GoogleAuthenticator;
use EADPlataforma\Services\SchoolEntityManager;
use EADPlataforma\Services\ConfigurationService;
use EADPlataforma\Entity\User;
/**
* GoogleAuthenticatorService
*/
class GoogleAuthenticatorService
{
/**
* @var string
*/
protected $eadDomain;
/**
* @var SchoolEntityManager
*/
protected $em;
/**
* @var ConfigurationService
*/
protected $configuration;
/**
* @var FileService
*/
protected $fileService;
/**
* @var Sonata\GoogleAuthenticator\GoogleAuthenticator
*/
protected $googleAuthenticator;
/**
* @var string
*/
protected $email;
/**
* @var string
*/
protected $secretKey;
/**
* @var string
*/
protected $code;
/**
* Constructor
*
* @param SchoolEntityManager $em
* @param ConfigurationService $configurationService
*/
public function __construct(
SchoolEntityManager $em,
ConfigurationService $configurationService
)
{
$this->em = $em;
$this->configuration = $configurationService;
$this->googleAuthenticator = new \Google\Authenticator\GoogleAuthenticator();
$this->client = $this->configuration->getClient();
if($this->client){
$this->eadDomain = $this->client->getDomainPrimary();
}
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function setSecretKey(string $secretKey): self
{
$this->secretKey = $secretKey;
return $this;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function generateQrCodeAuthentication(): array
{
$generateSecretKey = $this->googleAuthenticator->generateSecret();
$domain = str_replace('.eadplataforma.app', '', $this->eadDomain);
$domain = str_replace('.eadplataforma.com', '', $domain);
$domain = ucfirst($domain);
$qrCode = $this->googleAuthenticator->getURL($domain, $this->email, $generateSecretKey);
$dataAuthentication = [
"secretKey" => $generateSecretKey,
"qrCode" => $qrCode
];
return $dataAuthentication;
}
public function checkCodeAuthentication(): bool
{
$checkCode = $this->googleAuthenticator->checkCode($this->secretKey, $this->code, 2);
if($checkCode){
return true;
}
return false;
}
}