You are here

protected function TfaRecoveryCode::validate in Two-factor Authentication (TFA) 8

Validate code.

Note, plugins overriding validate() should be sure to set isValid property correctly or else also override submitForm().

Parameters

string $code: Code to be validated.

Return value

bool Whether code is valid.

Overrides TfaBasePlugin::validate

2 calls to TfaRecoveryCode::validate()
TfaRecoveryCode::validateForm in src/Plugin/TfaValidation/TfaRecoveryCode.php
Validate form.
TfaRecoveryCode::validateRequest in src/Plugin/TfaValidation/TfaRecoveryCode.php
Simple validate for web services.

File

src/Plugin/TfaValidation/TfaRecoveryCode.php, line 232

Class

TfaRecoveryCode
Recovery validation class for performing recovery codes validation.

Namespace

Drupal\tfa\Plugin\TfaValidation

Code

protected function validate($code) {
  $this->isValid = FALSE;

  // Get codes and compare.
  $codes = $this
    ->getCodes();
  if (empty($codes)) {
    $this->errorMessages['recovery_code'] = $this
      ->t('You have no unused codes available.');
    return FALSE;
  }

  // Remove empty spaces.
  $code = str_replace(' ', '', $code);
  foreach ($codes as $id => $stored) {

    // Remove spaces from stored code.
    if (hash_equals(trim(str_replace(' ', '', $stored)), $code)) {
      $this->isValid = TRUE;
      unset($codes[$id]);
      $this
        ->storeCodes($codes);
      return $this->isValid;
    }
  }
  $this->errorMessages['recovery_code'] = $this
    ->t('Invalid recovery code.');
  return $this->isValid;
}