Totp::getWorker()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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\base;
12
13
class Totp extends \yii\base\BaseObject
14
{
15
    public $workerClass;
16
17
    public $issuer;
18
19
    public $digits = 6;
20
21
    public $period = 30;
22
23
    public $algorithm = 'sha1';
24
25
    public $qrcodeProvider;
26
27
    public $rngProvider;
28
29
    public $tmpSecretTimeout = 3600;
30
31
    public $module;
32
33
    protected $_worker;
34
35
    protected $_secret;
36
37
    protected $_isVerified;
38
39
    public function getWorker()
40
    {
41
        if ($this->_worker === null) {
42
            $class = $this->workerClass;
43
            $this->_worker = new $class(
44
                $this->issuer, $this->digits, $this->period, $this->algorithm,
45
                $this->qrcodeProvider, $this->rngProvider
46
            );
47
        }
48
49
        return $this->_worker;
50
    }
51
52
    public function __call($name, $args)
53
    {
54
        return call_user_func_array([$this->getWorker(), $name], $args);
55
    }
56
57
    public function removeSecret()
58
    {
59
        $this->module->sessionRemove('totp-tmp-secret');
60
    }
61
62
    public function getSecret()
63
    {
64
        if ($this->_secret === null) {
65
            $expires = $this->module->sessionGet('totp-tmp-expires') ?: 0;
66
            if (time() < $expires) {
67
                $this->_secret = $this->module->sessionGet('totp-tmp-secret');
68
            }
69
        }
70
        if ($this->_secret === null) {
71
            $this->_secret = $this->createSecret();
72
            $this->module->sessionSet('totp-tmp-secret', $this->_secret);
73
            $this->module->sessionSet('totp-tmp-expires', time() + $this->tmpSecretTimeout);
74
        }
75
76
        return $this->_secret;
77
    }
78
79
    public function getIsVerified()
80
    {
81
        if ($this->_isVerified === null) {
82
            $this->_isVerified = $this->module->sessionGet('totp-verified');
83
        }
84
85
        return $this->_isVerified;
86
    }
87
88
    public function setIsVerified($value)
89
    {
90
        $this->_isVerified = $value;
91
        $this->module->sessionSet('totp-verified', $value);
92
    }
93
}
94