You are here

function mo_auth_configure_google_authenticator_submit in Google Authenticator / 2 Factor Authentication - 2FA 7

File

./configure_google_authenticator.inc, line 77

Code

function mo_auth_configure_google_authenticator_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
  $secret = $form_state['input']['secret'];
  $methodToConfigure = $form_state['input']['methodToConfigure'];
  global $base_url, $user;
  $user = user_load($user->uid);
  $user_email = $user->miniorange_registered_email[LANGUAGE_NONE][0]['value'];
  $otpToken = $form_state['input']['mo_auth_googleauth_token'];
  if (empty($otpToken)) {
    drupal_set_message(t('The passcode field is required. Please try again.'), 'warning');
    return;
  }
  $customer = new MiniorangeCustomerProfile();
  $miniorange_user = new MiniorangeUser($customer
    ->getCustomerID(), $user_email, NULL, NULL, AuthenticationType::$GOOGLE_AUTHENTICATOR['code']);
  $auth_api_handler = new AuthenticationAPIHandler($customer
    ->getCustomerID(), $customer
    ->getAPIKey());
  $response = $auth_api_handler
    ->register($miniorange_user, AuthenticationType::$GOOGLE_AUTHENTICATOR['code'], $secret, $otpToken, NULL);

  // Clear all the messages
  drupal_get_messages();

  // read API response
  if ($response->status == 'SUCCESS') {
    drupal_set_message(t(''), 'status');
    $configured_methods = mo_auth_get_configured_methods($user->uid);

    /**
     * Delete all the configured TOTP methods as only one can be used at a time
     */
    $configured_methods = array_values(array_diff($configured_methods, array(
      'MICROSOFT AUTHENTICATOR',
      'GOOGLE AUTHENTICATOR',
      'AUTHY AUTHENTICATOR',
      'LASTPASS AUTHENTICATOR',
    )));
    array_push($configured_methods, $methodToConfigure);
    $user->configured_auth_methods[LANGUAGE_NONE] = array();
    foreach ($configured_methods as $value) {
      array_push($user->configured_auth_methods[LANGUAGE_NONE], array(
        'value' => $value,
      ));
    }
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $user_api_handler
      ->update($miniorange_user);
    if ($response->status == 'SUCCESS') {
      user_save($user);
      drupal_set_message(t($methodToConfigure . ' configured successfully.'), 'status');
      drupal_goto('admin/config/people/mo_auth/setup');
    }
  }
  elseif ($response->status == 'FAILED') {
    form_set_error('form', t('The passcode you have entered is incorrect. Please try again.'));

    // $form_state['redirect'] = FALSE;
    // drupal_goto('admin/config/people/mo_auth/setup-twofactor');
  }
  else {
    form_set_error('form', t('An error occured while processing your request. Please try again.'));

    // drupal_goto('admin/config/people/mo_auth/setup-twofactor');
  }
}