Close #15 added two factor authentication

This commit is contained in:
Antonio Ramirez
2017-09-21 17:48:01 +02:00
parent 5ee4c91e03
commit 308b6a0b2c
15 changed files with 1596 additions and 737 deletions

View File

@ -15,7 +15,6 @@ use Da\User\Contracts\AuthClientInterface;
use Da\User\Event\FormEvent;
use Da\User\Event\UserEvent;
use Da\User\Form\LoginForm;
use Da\User\Model\User;
use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Service\SocialNetworkAccountConnectService;
use Da\User\Service\SocialNetworkAuthenticateService;
@ -38,10 +37,10 @@ class SecurityController extends Controller
/**
* SecurityController constructor.
*
* @param string $id
* @param Module $module
* @param string $id
* @param Module $module
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery
* @param array $config
* @param array $config
*/
public function __construct(
$id,
@ -64,7 +63,7 @@ class SecurityController extends Controller
'rules' => [
[
'allow' => true,
'actions' => ['login', 'auth', 'blocked'],
'actions' => ['login', 'confirm', 'auth', 'blocked'],
'roles' => ['?'],
],
[
@ -113,16 +112,27 @@ class SecurityController extends Controller
/** @var LoginForm $form */
$form = $this->make(LoginForm::class);
/** @var FormEvent $event */
$event = $this->make(FormEvent::class, [$form]);
if (Yii::$app->request->isAjax && $form->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($form);
}
if ($form->load(Yii::$app->request->post())) {
if ($this->module->enableTwoFactorAuthentication && $form->validate()) {
if ($form->getUser()->auth_tf_enabled) {
Yii::$app->session->set('credentials', ['login' => $form->login, 'pwd' => $form->password]);
return $this->redirect(['confirm']);
}
}
$this->trigger(FormEvent::EVENT_BEFORE_LOGIN, $event);
if ($form->login()) {
$form->getUser()->updateAttributes(['last_login_at' => time()]);
@ -142,6 +152,59 @@ class SecurityController extends Controller
);
}
public function actionConfirm()
{
if (!Yii::$app->user->getIsGuest()) {
return $this->goHome();
}
if (!Yii::$app->session->has('credentials')) {
return $this->redirect(['login']);
}
$credentials = Yii::$app->session->get('credentials');
/** @var LoginForm $form */
$form = $this->make(LoginForm::class);
$form->login = $credentials['login'];
$form->password = $credentials['pwd'];
$form->setScenario('2fa');
/** @var FormEvent $event */
$event = $this->make(FormEvent::class, [$form]);
if (Yii::$app->request->isAjax && $form->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($form);
}
if ($form->load(Yii::$app->request->post())) {
$this->trigger(FormEvent::EVENT_BEFORE_LOGIN, $event);
if ($form->login()) {
Yii::$app->session->set('credentials', null);
$form->getUser()->updateAttributes(['last_login_at' => time()]);
$this->trigger(FormEvent::EVENT_AFTER_LOGIN, $event);
return $this->goBack();
}
}
return $this->render(
'confirm',
[
'model' => $form,
'module' => $this->module,
]
);
}
public function actionLogout()
{
$event = $this->make(UserEvent::class, [Yii::$app->getUser()->getIdentity()]);