ValidateMfaBehavior::events()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Multi-factor authentication for Yii2 projects
4
 *
5
 * @link      https://github.com/hiqdev/yii2-mfa
6
 * @package   yii2-mfa
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\mfa\behaviors;
12
13
use hiqdev\yii2\mfa\base\MfaIdentityInterface;
14
use hiqdev\yii2\mfa\exceptions\AuthenticationException;
15
use hiqdev\yii2\mfa\Module;
16
use Yii;
17
use yii\web\User;
18
use yii\web\UserEvent;
19
20
class ValidateMfaBehavior extends \yii\base\Behavior
21
{
22
    public function events()
23
    {
24
        return [
25
            User::EVENT_BEFORE_LOGIN => 'beforeLogin',
26
        ];
27
    }
28
29
    /**
30
     * @param UserEvent $event
31
     */
32
    public function beforeLogin(UserEvent $event)
33
    {
34
        /** @var Module $module */
35
        $module = Yii::$app->getModule('mfa');
36
37
        /** @var MfaIdentityInterface $identity */
38
        $identity = $event->identity;
39
        $module->setHalfUser($identity);
40
41
        try {
42
            $module->validateIps($identity);
43
            $module->validateTotp($identity);
44
        } catch (AuthenticationException $e) {
45
            if ($event->cookieBased) {
46
                $event->isValid = false;
47
            } else {
48
                $e->redirect();
49
            }
50
        }
51
    }
52
}
53