Module   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 85
ccs 0
cts 65
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A removeHalfUser() 0 5 1
A validateIps() 0 16 3
A setTotp() 0 4 1
A getTotp() 0 9 2
A sessionSet() 0 4 1
A sessionGet() 0 4 1
A sessionRemove() 0 4 1
A setHalfUser() 0 5 1
A getHalfUser() 0 7 1
A validateTotp() 0 11 3
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;
12
13
use hiqdev\yii2\mfa\base\MfaIdentityInterface;
14
use hiqdev\yii2\mfa\base\Totp;
15
use hiqdev\yii2\mfa\exceptions\IpNotAllowedException;
16
use hiqdev\yii2\mfa\exceptions\TotpVerificationFailedException;
17
use Yii;
18
use yii\di\Instance;
19
use yii\validators\IpValidator;
20
21
/**
22
 * Multi-factor authentication module.
23
 *
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class Module extends \yii\base\Module
27
{
28
    public $paramPrefix = 'MFA-';
29
30
    protected $_totp;
31
32
    public function setTotp($value)
33
    {
34
        $this->_totp = $value;
35
    }
36
37
    public function getTotp()
38
    {
39
        if (!is_object($this->_totp)) {
40
            $this->_totp = Instance::ensure($this->_totp, Totp::class);
41
            $this->_totp->module = $this;
42
        }
43
44
        return $this->_totp;
45
    }
46
47
    public function sessionSet($name, $value)
48
    {
49
        Yii::$app->session->set($this->paramPrefix . $name, $value);
50
    }
51
52
    public function sessionGet($name)
53
    {
54
        return Yii::$app->session->get($this->paramPrefix . $name);
55
    }
56
57
    public function sessionRemove($name)
58
    {
59
        return Yii::$app->session->remove($this->paramPrefix . $name);
60
    }
61
62
    public function setHalfUser(MfaIdentityInterface $value)
63
    {
64
        $this->sessionSet('half-user-id', $value->getId());
65
        $this->sessionSet('totp-tmp-secret', $value->getTotpSecret());
66
    }
67
68
    public function getHalfUser(): ?MfaIdentityInterface
69
    {
70
        $id = $this->sessionGet('half-user-id');
71
        $class = Yii::$app->user->identityClass;
72
73
        return $class::findIdentity($id);
74
    }
75
76
    public function removeHalfUser()
77
    {
78
        $this->sessionRemove('half-user-id');
79
        $this->sessionRemove('totp-tmp-secret');
80
    }
81
82
    public function validateIps(MfaIdentityInterface $identity)
83
    {
84
        if (empty($identity->getAllowedIps())) {
85
            return;
86
        }
87
        $ips = array_filter($identity->getAllowedIps());
88
        $validator = new IpValidator([
89
            'ipv6' => false,
90
            'ranges' => $ips,
91
        ]);
92
        if ($validator->validate(Yii::$app->request->getUserIP())) {
93
            return;
94
        }
95
96
        throw new IpNotAllowedException();
97
    }
98
99
    public function validateTotp(MfaIdentityInterface $identity)
100
    {
101
        if (empty($identity->getTotpSecret())) {
102
            return;
103
        }
104
        if ($this->getTotp()->getIsVerified()) {
105
            return;
106
        }
107
108
        throw new TotpVerificationFailedException();
109
    }
110
}
111