Intending to build support for single-use backup codes. Started refactoring to move trait dependencies internally, so that classes only have to specify their own direct dependencies.
29 lines
704 B
PHP
29 lines
704 B
PHP
<?php
|
|
|
|
namespace App\Trait;
|
|
|
|
use App\ConfigBag;
|
|
use OTPHP\Factory;
|
|
use OTPHP\TOTPInterface;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
trait GetTotpTrait {
|
|
protected readonly ConfigBag $config;
|
|
|
|
#[Required]
|
|
public function setConfig(ConfigBag $config): void {
|
|
$this->config = $config;
|
|
}
|
|
|
|
protected function getTotp(): TOTPInterface {
|
|
$otp = Factory::loadFromProvisioningUri(
|
|
$this->config->totpUri(), $this->config->clock()
|
|
);
|
|
if ($otp instanceof TOTPInterface) {
|
|
return $otp;
|
|
}
|
|
throw new HttpException(500, 'Internal Server Exception');
|
|
}
|
|
}
|