You are here

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

1 string reference to 'mo_auth_configure_google_authenticator'
mo_auth_menu in ./mo_auth.module
Implements hook_menu().

File

./configure_google_authenticator.inc, line 5

Code

function mo_auth_configure_google_authenticator($form, &$form_state) {
  global $base_url, $user;
  $user = user_load($user->uid);

  /**
   * To check which method (Google Authenticator, Microsoft Authenticator, Authy Authenticator') is being configured by user
   */
  $url_parts = MoAuthUtilities::mo_auth_get_url_parts();
  if (in_array("google-authenticator", $url_parts)) {
    $App_Name = 'Google';
    $method_to_configure = AuthenticationType::$GOOGLE_AUTHENTICATOR['code'];
  }
  elseif (in_array("microsoft-authenticator", $url_parts)) {
    $App_Name = 'Microsoft';
    $method_to_configure = AuthenticationType::$MICROSOFT_AUTHENTICATOR['code'];
  }
  elseif (in_array("authy-authenticator", $url_parts)) {
    $App_Name = 'Authy';
    $method_to_configure = AuthenticationType::$AUTHY_AUTHENTICATOR['code'];
  }
  elseif (in_array("lastpass-authenticator", $url_parts)) {
    $App_Name = 'LastPass';
    $method_to_configure = AuthenticationType::$LASTPASS_AUTHENTICATOR['code'];
  }
  if (array_key_exists('secret', $form_state['input']) === FALSE) {
    $user_email = $user->miniorange_registered_email[LANGUAGE_NONE][0]['value'];
    $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
      ->getGoogleAuthSecret($miniorange_user);
    $qrCode = $response->qrCodeData;
    $secret = $response->secret;
  }
  else {
    $secret = $form_state['input']['secret'];
    $qrCode = $form_state['input']['qrCode'];
  }
  $form['header']['#markup'] = '<div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Configure ' . $App_Name . ' Authenticator</div></div>' . mo_auth_create_form($base_url, $qrCode, $secret, $App_Name);
  $form['mo_auth_googleauth_token'] = array(
    '#type' => 'textfield',
    '#attributes' => array(
      'placeholder' => t('Enter  passcode.'),
      'class' => array(
        'mo2f-textbox',
        'mo2f-textbox-otp',
      ),
      'style' => 'width:200px',
      'autofocus' => 'true',
    ),
  );
  $form['secret'] = array(
    '#type' => 'hidden',
    '#value' => $secret,
  );
  $form['mo_scan_qr_code_google_authenticator']['methodToConfigure'] = array(
    '#type' => 'hidden',
    '#value' => $method_to_configure,
  );
  $form['qrCode'] = array(
    '#type' => 'hidden',
    '#value' => $qrCode,
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Verify and Save'),
  );
  $form['actions']['cancel'] = array(
    '#markup' => l(t('Cancel'), 'admin/config/people/mo_auth/setup'),
  );
  return $form;
}