mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	В форму включения двухфакторной аутентификации добавлено поле для фиксации времени запроса
This commit is contained in:
		@@ -23,6 +23,8 @@ class TwoFactorAuthForm extends ApiForm {
 | 
			
		||||
 | 
			
		||||
    public $token;
 | 
			
		||||
 | 
			
		||||
    public $timestamp;
 | 
			
		||||
 | 
			
		||||
    public $password;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -38,10 +40,16 @@ class TwoFactorAuthForm extends ApiForm {
 | 
			
		||||
    public function rules() {
 | 
			
		||||
        $bothScenarios = [self::SCENARIO_ACTIVATE, self::SCENARIO_DISABLE];
 | 
			
		||||
        return [
 | 
			
		||||
            ['timestamp', 'integer', 'on' => [self::SCENARIO_ACTIVATE]],
 | 
			
		||||
            ['account', 'validateOtpDisabled', 'on' => self::SCENARIO_ACTIVATE],
 | 
			
		||||
            ['account', 'validateOtpEnabled', 'on' => self::SCENARIO_DISABLE],
 | 
			
		||||
            ['token', 'required', 'message' => E::OTP_TOKEN_REQUIRED, 'on' => $bothScenarios],
 | 
			
		||||
            ['token', TotpValidator::class, 'account' => $this->account, 'window' => 30, 'on' => $bothScenarios],
 | 
			
		||||
            ['token', TotpValidator::class, 'on' => $bothScenarios,
 | 
			
		||||
                'account' => $this->account,
 | 
			
		||||
                'timestamp' => function() {
 | 
			
		||||
                    return $this->timestamp;
 | 
			
		||||
                },
 | 
			
		||||
            ],
 | 
			
		||||
            ['password', PasswordRequiredValidator::class, 'account' => $this->account, 'on' => $bothScenarios],
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -19,9 +19,17 @@ class TotpValidator extends Validator {
 | 
			
		||||
     * @var int|null Задаёт окно, в промежуток которого будет проверяться код.
 | 
			
		||||
     * Позволяет избежать ситуации, когда пользователь ввёл код в последнюю секунду
 | 
			
		||||
     * его существования и пока шёл запрос, тот протух.
 | 
			
		||||
     * Значение задаётся в +- кодах, а не секундах.
 | 
			
		||||
     */
 | 
			
		||||
    public $window;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var int|callable|null Позволяет задать точное время, относительно которого будет
 | 
			
		||||
     * выполняться проверка. Это может быть собственно время или функция, возвращающая значение.
 | 
			
		||||
     * Если не задано, то будет использовано текущее время.
 | 
			
		||||
     */
 | 
			
		||||
    public $timestamp;
 | 
			
		||||
 | 
			
		||||
    public $skipOnEmpty = false;
 | 
			
		||||
 | 
			
		||||
    public function init() {
 | 
			
		||||
@@ -41,11 +49,23 @@ class TotpValidator extends Validator {
 | 
			
		||||
 | 
			
		||||
    protected function validateValue($value) {
 | 
			
		||||
        $totp = new TOTP(null, $this->account->otp_secret);
 | 
			
		||||
        if (!$totp->verify((string)$value, null, $this->window)) {
 | 
			
		||||
        if (!$totp->verify((string)$value, $this->getTimestamp(), $this->window)) {
 | 
			
		||||
            return [E::OTP_TOKEN_INCORRECT, []];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function getTimestamp(): ?int {
 | 
			
		||||
        if ($this->timestamp === null) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (is_callable($this->timestamp)) {
 | 
			
		||||
            return (int)call_user_func($this->timestamp);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return (int)$this->timestamp;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user