2fa by email and by sms
This commit is contained in:
61
src/User/Service/TwoFactorEmailCodeGeneratorService.php
Normal file
61
src/User/Service/TwoFactorEmailCodeGeneratorService.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\TwoFA\Manager;
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Factory\MailFactory;
|
||||
|
||||
use Yii;
|
||||
|
||||
class TwoFactorEmailCodeGeneratorService implements ServiceInterface
|
||||
{
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* TwoFactorEmailCodeGeneratorService constructor.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$user = $this->user;
|
||||
if (!$user->auth_tf_key) {
|
||||
$user->auth_tf_key = (new Manager())->generateSecretKey();
|
||||
$user->updateAttributes(['auth_tf_key']);
|
||||
}
|
||||
// generate key
|
||||
$code = random_int(0, 999999);
|
||||
$code = str_pad($code, 6, 0, STR_PAD_LEFT);
|
||||
// send email
|
||||
$mailService = MailFactory::makeTwoFactorCodeMailerService($user, $code);
|
||||
$mailService->run();
|
||||
|
||||
// put key in session
|
||||
Yii::$app->session->set("email_code_time", date('Y-m-d H:i:s'));
|
||||
Yii::$app->session->set("email_code", $code);
|
||||
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
79
src/User/Service/TwoFactorSmsCodeGeneratorService.php
Normal file
79
src/User/Service/TwoFactorSmsCodeGeneratorService.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\TwoFA\Manager;
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Factory\MailFactory;
|
||||
use yii\di\Instance;
|
||||
use yetopen\smssender\SmsSenderInterface;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
use Yii;
|
||||
|
||||
class TwoFactorSmsCodeGeneratorService implements ServiceInterface
|
||||
{
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* @var Type
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* @var SmsSenderInterface
|
||||
*/
|
||||
protected $smsSender;
|
||||
|
||||
/**
|
||||
* TwoFactorSmsCodeGeneratorService constructor.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->type = 'sms';
|
||||
$module = Yii::$app->getModule('user');
|
||||
$validators = $module->twoFactorAuthenticationValidators;
|
||||
$smsSender = ArrayHelper::getValue($validators,'sms'.'.smsSender');
|
||||
$this->smsSender = Instance::ensure($smsSender, SmsSenderInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// generate key
|
||||
$code = random_int(0, 999999);
|
||||
$code = str_pad($code, 6, 0, STR_PAD_LEFT);
|
||||
// get the mobile phone of the user
|
||||
$user = $this->user;
|
||||
$mobilePhone=$user->getAuthTfMobilePhone();
|
||||
|
||||
if( !(null===$mobilePhone) && $mobilePhone!='' ){
|
||||
// send sms
|
||||
$this->smsSender->send($mobilePhone, $code);
|
||||
// put key in session
|
||||
Yii::$app->session->set("sms_code_time", date('Y-m-d H:i:s'));
|
||||
Yii::$app->session->set("sms_code", $code);
|
||||
}
|
||||
|
||||
return $code;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user