You are here

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

Same name in this branch
  1. 7 configure_otp_over_sms.inc \miniorange_saml_validate_otp_submit()
  2. 7 configure_otp_over_sms_and_email.inc \miniorange_saml_validate_otp_submit()
2 string references to 'miniorange_saml_validate_otp_submit'
mo_auth_configure_otp_over_sms in ./configure_otp_over_sms.inc
mo_auth_configure_otp_over_sms_and_email in ./configure_otp_over_sms_and_email.inc

File

./configure_otp_over_sms.inc, line 142

Code

function miniorange_saml_validate_otp_submit($form, &$form_state) {
  $otpToken = trim($form['miniorange_OTP']['#value']);
  $phone_number = isset($form['miniorange_phone']['#value']) ? $form['miniorange_phone']['#value'] : '';
  if (empty($otpToken)) {
    drupal_set_message(t('The <b>OTP</b> field is required.'), 'error');
    return;
  }
  if (isset($form['miniorange_phone']['#value']) && empty($phone_number)) {
    drupal_set_message(t('Please enter phone number with country code(+1).'), 'warning');
    return;
  }
  global $base_url, $user;
  $customer = new MiniorangeCustomerProfile();
  $cKey = $customer
    ->getCustomerID();
  $customerApiKey = $customer
    ->getAPIKey();
  $user = user_load($user->uid);
  $user_email = $user->miniorange_registered_email[LANGUAGE_NONE][0]['value'];
  variable_set('mo_phone', $phone_number);
  $transactionId = variable_get('mo_auth_tx_id');
  $customer_config = new MiniorangeCustomerSetup($user_email, $phone_number, NULL, NULL);
  $response = $customer_config
    ->validate_otp_token($transactionId, $otpToken, $cKey, $customerApiKey);
  $otp_validation = json_decode($response);
  if ($otp_validation->status == 'FAILED') {
    drupal_set_message(t("Validation Failed. Please enter the correct OTP."), 'error');
    return;
  }
  elseif ($otp_validation->status == 'SUCCESS') {
    $form_state['rebuild'] = TRUE;
    $methodToConfigure = $form['mo_authTypeCode']['#value'];
    $user_email = $user->miniorange_registered_email[LANGUAGE_NONE][0]['value'];
    $authTypeCode = AuthenticationType::$SMS['code'];
    $success_Message = 'OTP Over SMS has been configured successfully.';
    if ($methodToConfigure === 'EMAIL') {
      $authTypeCode = AuthenticationType::$OTP_OVER_EMAIL['code'];
      $success_Message = 'OTP Over Email has been configured successfully.';
    }
    elseif ($methodToConfigure === 'PHONE VERIFICATION') {
      $authTypeCode = AuthenticationType::$OTP_OVER_PHONE['code'];
      $success_Message = 'OTP Over Phone Call has been configured successfully.';
    }

    //global $base_url, $user;
    $user = user_load($user->uid);
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), $user_email, $phone_number, NULL, $authTypeCode);
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $configured_methods = mo_auth_get_configured_methods($user->uid);
    if (!in_array($authTypeCode, $configured_methods)) {
      array_push($user->configured_auth_methods[LANGUAGE_NONE], array(
        'value' => $authTypeCode,
      ));
    }
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());

    // Updating the authentication method for the user
    $miniorange_user
      ->setAuthType($authTypeCode);
    $response = $user_api_handler
      ->update($miniorange_user);
    if ($response->status == 'SUCCESS') {
      user_save($user);
      drupal_set_message(t($success_Message), 'status');
      drupal_goto('admin/config/people/mo_auth/setup');
      return;
    }
    drupal_set_message(t('An error occured while processing your request. Please try again.'), 'error');
    watchdog('miniorange_2fa', $response->message);
    drupal_goto('admin/config/people/mo_auth/setup');
  }
}