test_otp_over_sms_and_email.php in Google Authenticator / 2 Factor Authentication - 2FA 8
Same filename and directory in other branches
OTP Over SMS and Email(test) functions.
Namespace
Drupal\miniorange_2fa\FormFile
src/Form/test_otp_over_sms_and_email.phpView source
<?php
namespace Drupal\miniorange_2fa\Form;
/**
* @file
* OTP Over SMS and Email(test) 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 testing OTP Over SMS and Email.
*/
class test_otp_over_sms_and_email extends FormBase {
public function getFormId() {
return 'miniorange_otp_over_sms_and_email';
}
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' => array(
"miniorange_2fa/miniorange_2fa.admin",
"miniorange_2fa/miniorange_2fa.license",
),
),
);
global $base_url;
$url = $base_url . '/admin/config/people/miniorange_2fa/setup_twofactor';
$user = User::load(\Drupal::currentUser()
->id());
$user_id = $user
->id();
$custom_attribute = MoAuthUtilities::get_users_custom_attribute($user_id);
$user_email = $custom_attribute[0]->miniorange_registered_email;
$user_phone = \Drupal::config('miniorange_2fa.settings')
->get('mo_phone');
$authTypeCode = AuthenticationType::$SMS_AND_EMAIL['code'];
$messageHeader = 'An OTP has been sent to <strong>' . $user_phone . '</strong> and <strong>' . $user_email . '</strong>. Please enter it here to complete the test.';
$txId_Value = \Drupal::config('miniorange_2fa.settings')
->get('txId_Value');
if ($txId_Value == 'EMPTY_VALUE') {
$customer = new MiniorangeCustomerProfile();
$miniorange_user = new MiniorangeUser($customer
->getCustomerID(), NULL, $user_phone, NULL, $authTypeCode, $user_email);
$auth_api_handler = new AuthenticationAPIHandler($customer
->getCustomerID(), $customer
->getAPIKey());
$response = $auth_api_handler
->challenge($miniorange_user);
if (isset($response->status) && $response->status != 'SUCCESS') {
$message = 'An error occured while sending passcode. <em>(' . $response->message . ')</em>';
MoAuthUtilities::show_error_or_success_message($message, 'error');
}
else {
\Drupal::messenger()
->addMessage(t($messageHeader), 'status');
}
//Store txId
\Drupal::configFactory()
->getEditable('miniorange_2fa.settings')
->set('txId_Value', $response->txId)
->save();
}
$form['header']['#markup'] = '<div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Test OTP Over SMS and Email</div></div>';
$form['mo_auth_otpoversms_and_email_token'] = array(
'#type' => 'textfield',
'#maxlength' => 6,
'#attributes' => array(
'placeholder' => t('Enter passcode.'),
'class' => array(
'mo2f-textbox',
'mo2f-textbox-otp',
),
'style' => 'width:200px',
'autofocus' => 'true',
),
);
$form['actions_form_submit'] = array(
'#type' => 'submit',
'#value' => t('Verify'),
);
$form['actions_form_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><br><br><br><br></div>',
);
MoAuthUtilities::AddsupportTab($form, $form_state);
return $form;
}
/**
* Form submit handler for otp over sms validation.
*/
public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
// Clear all the messages
\Drupal::messenger()
->deleteAll();
$user = User::load(\Drupal::currentUser()
->id());
$user_id = $user
->id();
$custom_attribute = MoAuthUtilities::get_users_custom_attribute($user_id);
$user_email = $custom_attribute[0]->miniorange_registered_email;
$txId = \Drupal::config('miniorange_2fa.settings')
->get('txId_Value');
$token = $form['mo_auth_otpoversms_and_email_token']['#value'];
if (empty($token)) {
\Drupal::messenger()
->addMessage(t('Please enter passcode first.'), 'error');
return;
}
$authTypeCode = AuthenticationType::$SMS['code'];
$user_phone = \Drupal::config('miniorange_2fa.settings')
->get('mo_phone');
$customer = new MiniorangeCustomerProfile();
$miniorange_user = new MiniorangeUser($customer
->getCustomerID(), NULL, $user_phone, $authTypeCode, $user_email);
$auth_api_handler = new AuthenticationAPIHandler($customer
->getCustomerID(), $customer
->getAPIKey());
$response = $auth_api_handler
->validate($miniorange_user, $txId, $token);
// 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_otp_over_sms_and_email | Menu callback for testing OTP Over SMS and Email. |