You are here

function ga_login_validate_gacode in Google Authenticator login 7

Form element validation handler for gacode field.

Note that #required is validated by _form_validate() already.

1 string reference to 'ga_login_validate_gacode'
ga_login_element_info in ./ga_login.module
Implements hook_element_info().

File

./ga_login.module, line 445
Main ga_login module.

Code

function ga_login_validate_gacode(&$element, &$form_state) {
  $code = $element['#value'];
  if ($code === '') {
    return;
  }
  $name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];

  // Ensure the input exactly 6 digits.
  if (strlen($code) != 6 || !ctype_digit($code)) {
    form_error($element, t('%name has to be exactly 6 digits.', array(
      '%name' => $name,
    )));
    return;
  }

  // Load the associated user account.
  $account = NULL;
  if (is_null($element['#uid'])) {
    global $user;

    // Make sure we have a user, if not bail out.
    if ($user->uid === 0) {
      return;
    }
    $account = user_load($user->uid);
  }
  else {
    $account = user_load($element['#uid']);
  }
  $ga = _ga_login_get_class();
  $username = _ga_login_username($account);
  if ($ga
    ->hasToken($username) && !$element['#_new_ga_code']) {
    $keyok = $ga
      ->authenticateUser($username, $code);
    if (!$keyok) {
      form_error($element, t('%name has an invalid code.', array(
        '%name' => $name,
      )));
    }
  }
}