test_google_authenticator.php in Google Authenticator / 2 Factor Authentication - 2FA 8
Same filename and directory in other branches
Email verification functions.
Namespace
Drupal\miniorange_2fa\FormFile
src/Form/test_google_authenticator.phpView source
<?php
namespace Drupal\miniorange_2fa\Form;
/**
* @file
* Email verification functions.
*/
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormBase;
use Drupal\miniorange_2fa\MiniorangeUser;
use Drupal\miniorange_2fa\MoAuthUtilities;
use Drupal\miniorange_2fa\AuthenticationType;
use Drupal\miniorange_2fa\AuthenticationAPIHandler;
use Drupal\miniorange_2fa\MiniorangeCustomerProfile;
/**
* Menu callback for email verification.
*/
class test_google_authenticator extends FormBase {
public function getFormId() {
return 'miniorange_googleAuthenticator';
}
public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
$form['markup_top_2'] = array(
'#markup' => '<div class="mo_saml_table_layout_1"><div class="mo_saml_table_layout mo_saml_container">',
);
$form['markup_library'] = array(
'#attached' => array(
'library' => 'miniorange_2fa/miniorange_2fa.admin',
'library' => 'miniorange_2fa/miniorange_2fa.license',
),
);
global $base_url;
$url = $base_url . '/admin/config/people/miniorange_2fa/setup_twofactor';
// To check which method (Soft Token, Google Authenticator') is being tested by user
$query_param = \Drupal::service('path.current')
->getPath();
$url_parts = explode('/', $query_param);
if (in_array("soft-token", $url_parts)) {
$authTypeCode = AuthenticationType::$SOFT_TOKEN['code'];
$messageHeader = '<div class="mo2f-setup-header-top-left">Test Soft Token</div></div><div class="mo2f-info">Please enter the passcode generated on your miniOrange Authenticator app.</div>';
}
else {
$authTypeCode = AuthenticationType::$GOOGLE_AUTHENTICATOR['code'];
$messageHeader = '<div class="mo2f-setup-header-top-left">Test Google Authenticator</div></div><div class="mo2f-info">Please enter the passcode generated on your Google Authenticator app.</div>';
}
$form['header']['#markup'] = t('<div class="mo2f-setup-header">' . $messageHeader);
$form['mo_auth_googleauth_token'] = array(
'#type' => 'textfield',
'#maxlength' => 6,
'#title' => 'Passcode: ',
'#attributes' => array(
'placeholder' => t('Enter passcode.'),
'class' => array(
'mo2f-textbox',
'mo2f-textbox-otp',
),
'style' => 'width:200px',
'autofocus' => TRUE,
),
'#suffix' => '<br>',
);
$form['authTypeCode'] = array(
'#type' => 'hidden',
'#value' => $authTypeCode,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Verify'),
);
$form['actions']['cancel'] = array(
'#markup' => '<span> </span><a href="' . $url . '"> Cancel Test</a>',
);
$form['main_layout_div_end'] = array(
'#markup' => '<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>',
);
MoAuthUtilities::AddsupportTab($form, $form_state);
return $form;
}
/**
* Form submit handler for email verify.
*/
public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$form_state
->setRebuild();
$input = $form_state
->getUserInput();
$user = User::load(\Drupal::currentUser()
->id());
$user_id = $user
->id();
$utilities = new MoAuthUtilities();
$custom_attribute = $utilities::get_users_custom_attribute($user_id);
$user_email = $custom_attribute[0]->miniorange_registered_email;
$token = $input['mo_auth_googleauth_token'];
if (empty($token)) {
\Drupal::messenger()
->addMessage(t('Please enter the passcode.'), 'error');
return;
}
$authTypeCode = $input['authTypeCode'];
$customer = new MiniorangeCustomerProfile();
$miniorange_user = new MiniorangeUser($customer
->getCustomerID(), $user_email, NULL, NULL, $authTypeCode);
$auth_api_handler = new AuthenticationAPIHandler($customer
->getCustomerID(), $customer
->getAPIKey());
$response = $auth_api_handler
->validate($miniorange_user, '', $token);
// Clear all the messages
\Drupal::messenger()
->deleteAll();
// read API response
if ($response->status == 'SUCCESS') {
$message = 'You have successfully completed the test.';
MoAuthUtilities::show_error_or_success_message($message, 'status');
}
elseif ($response->status == 'FAILED') {
\Drupal::messenger()
->addMessage(t('The passcode you have entered is incorrect. Please try again.'), 'error');
return;
}
else {
$message = 'An error occured while processing your request. Please try again.';
MoAuthUtilities::show_error_or_success_message($message, 'error');
}
}
}
Classes
Name | Description |
---|---|
test_google_authenticator | Menu callback for email verification. |