1 | <?php |
||
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() |
||
81 | |||
82 | public function validateIps(MfaIdentityInterface $identity) |
||
98 | |||
99 | public function validateTotp(MfaIdentityInterface $identity) |
||
100 | { |
||
101 | if (empty($identity->getTotpSecret())) { |
||
102 | return; |
||
103 | } |
||
104 | if ($this->getTotp()->getIsVerified()) { |
||
105 | return; |
||
110 | } |
||
111 |