Skip to content

Commit

Permalink
added ValidateMfaBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Dec 23, 2016
1 parent 3df540d commit f54246d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
14 changes: 2 additions & 12 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use hiqdev\yii2\mfa\base\Totp;
use Yii;
use yii\base\Event;
use yii\di\Instance;
use yii\helpers\StringHelper;
use yii\validators\IpValidator;
Expand Down Expand Up @@ -75,16 +74,7 @@ public function removeHalfUser()
$this->sessionRemove('halfUser');
}

public static function onBeforeLogin(Event $event)
{
$module = Yii::$app->getModule('mfa');
$identity = $event->identity;
$module->setHalfUser($identity);
$module->validateIps($identity);
$module->validateTotp($identity);
}

protected function validateIps(IdentityInterface $identity)
public function validateIps(IdentityInterface $identity)
{
if (empty($identity->allowed_ips)) {
return;
Expand All @@ -102,7 +92,7 @@ protected function validateIps(IdentityInterface $identity)
Yii::$app->end();
}

protected function validateTotp(IdentityInterface $identity)
public function validateTotp(IdentityInterface $identity)
{
if (empty($identity->totp_secret)) {
return;
Expand Down
35 changes: 35 additions & 0 deletions src/behaviors/ValidateMfaBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Yii2 module providing multi-factor authentication
*
* @link https://github.com/hiqdev/yii2-mfa
* @package yii2-mfa
* @license BSD-3-Clause
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\yii2\mfa\behaviors;

use Yii;
use yii\base\Event;
use yii\web\User;

class ValidateMfaBehavior extends \yii\base\Behavior
{
public function events()
{
return [
User::EVENT_BEFORE_LOGIN => 'beforeLogin',
];
}

public function beforeLogin(Event $event)
{
$module = Yii::$app->getModule('mfa');
$identity = $event->identity;
$module->setHalfUser($identity);
$module->validateIps($identity);
$module->validateTotp($identity);
}
}
2 changes: 1 addition & 1 deletion src/config/hisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
],
'user' => [
'on beforeLogin' => [\hiqdev\yii2\mfa\Module::class, 'onBeforeLogin'],
'as validateMfa' => \hiqdev\yii2\mfa\behaviors\ValidateMfaBehavior::class,
],
'themeManager' => [
'pathMap' => [
Expand Down

0 comments on commit f54246d

Please sign in to comment.