2017-01-21 04:24:30 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace api\tests\unit\validators;
|
2017-01-21 04:24:30 +05:30
|
|
|
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
2017-01-21 04:24:30 +05:30
|
|
|
use api\validators\TotpValidator;
|
|
|
|
use common\helpers\Error as E;
|
|
|
|
use common\models\Account;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\_support\ProtectedCaller;
|
2019-02-23 04:41:57 +05:30
|
|
|
use OTPHP\TOTP;
|
2017-01-21 04:24:30 +05:30
|
|
|
|
|
|
|
class TotpValidatorTest extends TestCase {
|
|
|
|
use ProtectedCaller;
|
|
|
|
|
|
|
|
public function testValidateValue() {
|
|
|
|
$account = new Account();
|
2017-08-08 22:48:44 +05:30
|
|
|
$account->otp_secret = 'AAAA';
|
|
|
|
$controlTotp = TOTP::create($account->otp_secret);
|
2017-01-21 04:24:30 +05:30
|
|
|
|
|
|
|
$validator = new TotpValidator(['account' => $account]);
|
|
|
|
|
|
|
|
$result = $this->callProtected($validator, 'validateValue', 123456);
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame([E::TOTP_INCORRECT, []], $result);
|
2017-01-21 04:24:30 +05:30
|
|
|
|
|
|
|
$result = $this->callProtected($validator, 'validateValue', $controlTotp->now());
|
|
|
|
$this->assertNull($result);
|
|
|
|
|
|
|
|
$result = $this->callProtected($validator, 'validateValue', $controlTotp->at(time() - 31));
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame([E::TOTP_INCORRECT, []], $result);
|
2017-01-21 04:24:30 +05:30
|
|
|
|
2017-02-22 04:19:24 +05:30
|
|
|
$validator->window = 2;
|
2017-01-21 04:24:30 +05:30
|
|
|
$result = $this->callProtected($validator, 'validateValue', $controlTotp->at(time() - 31));
|
|
|
|
$this->assertNull($result);
|
2017-02-22 04:19:24 +05:30
|
|
|
|
|
|
|
$at = time() - 400;
|
|
|
|
$validator->timestamp = $at;
|
|
|
|
$result = $this->callProtected($validator, 'validateValue', $controlTotp->now());
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame([E::TOTP_INCORRECT, []], $result);
|
2017-02-22 04:19:24 +05:30
|
|
|
|
|
|
|
$result = $this->callProtected($validator, 'validateValue', $controlTotp->at($at));
|
|
|
|
$this->assertNull($result);
|
|
|
|
|
2017-02-23 04:48:25 +05:30
|
|
|
$at = function() {
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
$validator->timestamp = $at;
|
|
|
|
$result = $this->callProtected($validator, 'validateValue', $controlTotp->now());
|
|
|
|
$this->assertNull($result);
|
|
|
|
|
2017-02-22 04:19:24 +05:30
|
|
|
$at = function() {
|
|
|
|
return time() - 700;
|
|
|
|
};
|
|
|
|
$validator->timestamp = $at;
|
|
|
|
$result = $this->callProtected($validator, 'validateValue', $controlTotp->at($at()));
|
|
|
|
$this->assertNull($result);
|
2017-01-21 04:24:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|