function configure_otp_over_sms_and_email::miniorange_saml_validate_otp_submit in Google Authenticator / 2 Factor Authentication - 2FA 8.2
Same name and namespace in other branches
- 8 src/Form/configure_otp_over_sms_and_email.php \Drupal\miniorange_2fa\form\configure_otp_over_sms_and_email::miniorange_saml_validate_otp_submit()
File
- src/
Form/ configure_otp_over_sms_and_email.php, line 249
Class
Namespace
Drupal\miniorange_2fa\formCode
function miniorange_saml_validate_otp_submit(array &$form, FormStateInterface $form_state) {
$form_values = $form_state
->getValues();
$customer = new MiniorangeCustomerProfile();
$cKey = $customer
->getCustomerID();
$customerApiKey = $customer
->getAPIKey();
$otpToken = str_replace(' ', '', $form_values['miniorange_OTP']);
$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;
$phone_number = isset($form_values['miniorange_phone']) ? str_replace(' ', '', $form_values['miniorange_phone']) : NULL;
if (!is_null($phone_number) && empty($phone_number)) {
\Drupal::messenger()
->addError(t('Please enter your phone number first.'));
return;
}
if (empty($otpToken)) {
\Drupal::messenger()
->addError(t('Please enter OTP first.'));
return;
}
\Drupal::configFactory()
->getEditable('miniorange_2fa.settings')
->set('mo_phone', $phone_number)
->save();
$transactionId = \Drupal::config('miniorange_2fa.settings')
->get('mo_auth_tx_id');
$customer_config = new MiniorangeCustomerSetup($user_email, $phone_number, NULL, NULL);
$otp_validation = $customer_config
->validate_otp_token($transactionId, $otpToken, $cKey, $customerApiKey);
//$txId = $otp_validation->txId;
if ($otp_validation->status == 'FAILED') {
\Drupal::messenger()
->addError(t("Validation Failed. Please enter the correct OTP."));
return;
}
elseif ($otp_validation->status == 'SUCCESS') {
$form_state
->setRebuild();
$authTypeCode = $form_values['authTypeCode'];
$user_email = $custom_attribute[0]->miniorange_registered_email;
$customer = new MiniorangeCustomerProfile();
$miniorange_user = new MiniorangeUser($customer
->getCustomerID(), $user_email, $phone_number, NULL, $authTypeCode);
$configured_methods = MoAuthUtilities::mo_auth_get_configured_methods($user_id);
if (!in_array($authTypeCode, $configured_methods)) {
array_push($configured_methods, $authTypeCode);
}
$config_methods = implode(', ', $configured_methods);
$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') {
// Save User
$user_id = $user
->id();
$available = $utilities::check_for_userID($user_id);
$database = \Drupal::database();
if ($available == TRUE) {
$database
->update('UserAuthenticationType')
->fields([
'activated_auth_methods' => $authTypeCode,
])
->condition('uid', $user_id, '=')
->execute();
$database
->update('UserAuthenticationType')
->fields([
'configured_auth_methods' => $config_methods,
])
->condition('uid', $user_id, '=')
->execute();
}
else {
echo t("error while saving authentication method.");
exit;
}
if ($authTypeCode == AuthenticationType::$SMS_AND_EMAIL['code']) {
$message = t('OTP Over Email has been configured successfully.');
}
elseif ($authTypeCode == AuthenticationType::$SMS['code']) {
$message = t('OTP Over SMS has been configured successfully.');
}
elseif ($authTypeCode == AuthenticationType::$SMS_AND_EMAIL['code']) {
$message = t('OTP Over SMS and Email has been configured successfully.');
}
elseif ($authTypeCode == AuthenticationType::$OTP_OVER_PHONE['code']) {
$message = t('OTP Over Phone Call has been configured successfully.');
}
MoAuthUtilities::show_error_or_success_message($message, 'status');
return;
}
return;
}
$message = t('An error occurred while processing your request. Please try again.');
MoAuthUtilities::show_error_or_success_message($message, 'error');
return;
}