MoAuthSetupTwoFactor.php in Google Authenticator / 2 Factor Authentication - 2FA 8
Same filename and directory in other branches
Contains Setup Two-Factor page for miniOrange 2FA Login Module.
Namespace
Drupal\miniorange_2fa\FormFile
src/Form/MoAuthSetupTwoFactor.phpView source
<?php
/**
* @file
* Contains Setup Two-Factor page for miniOrange 2FA Login Module.
*/
namespace Drupal\miniorange_2fa\Form;
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormBase;
use Drupal\miniorange_2fa\MiniorangeUser;
use Drupal\miniorange_2fa\UsersAPIHandler;
use Drupal\miniorange_2fa\MoAuthUtilities;
use Drupal\miniorange_2fa\AuthenticationType;
use Drupal\miniorange_2fa\MiniorangeCustomerProfile;
/**
* Showing Setup Two-Factor page.
*/
class MoAuthSetupTwoFactor extends FormBase {
public function getFormId() {
return 'miniorange_2fa_setup_two_factor';
}
public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
//used in test_otp_over_email, test_otp_over_sms and test_otp_over_sms_and_email forms
\Drupal::configFactory()
->getEditable('miniorange_2fa.settings')
->set('txId_Value', 'EMPTY_VALUE')
->save();
global $base_url;
$success_error_message = \Drupal::config('miniorange_2fa.settings')
->get('mo_auth_2fa_Success/Error message');
$success_error_status = \Drupal::config('miniorange_2fa.settings')
->get('mo_auth_2fa_Success/Error status');
if ($success_error_message != NULL && $success_error_status != NULL) {
\Drupal::messenger()
->addMessage(t($success_error_message), $success_error_status);
\Drupal::configFactory()
->getEditable('miniorange_2fa.settings')
->set('mo_auth_2fa_Success/Error message', NULL)
->save();
\Drupal::configFactory()
->getEditable('miniorange_2fa.settings')
->set('mo_auth_2fa_Success/Error status', NULL)
->save();
}
$form['markup_top_2'] = array(
'#markup' => '<div class="mo_saml_table_layout_1"><div class="mo_saml_table_layout mo_saml_container">',
);
$disabled = False;
if (!MoAuthUtilities::isCustomerRegistered()) {
$form['header'] = array(
'#markup' => '<div class="mo_saml_register_message"><p>You need to <a href="' . $base_url . '/admin/config/people/miniorange_2fa/customer_setup">Register/Login</a> with miniOrange before using this module.</p></div><br>',
);
$disabled = True;
}
$form['markup_library'] = array(
'#attached' => array(
'library' => array(
"miniorange_2fa/miniorange_2fa.admin",
"miniorange_2fa/miniorange_2fa.license",
),
),
);
global $base_url;
$user = User::load(\Drupal::currentUser()
->id());
$user_id = $user
->id();
$utilities = new MoAuthUtilities();
$custom_attribute = $utilities::get_users_custom_attribute($user_id);
if (!is_null($custom_attribute) && !empty($custom_attribute)) {
$user_email = $custom_attribute[0]->miniorange_registered_email;
}
else {
$user_email = NULL;
}
$customer = new MiniorangeCustomerProfile();
$user_api_handler = new UsersAPIHandler($customer
->getCustomerID(), $customer
->getAPIKey());
$miniorange_user = new MiniorangeUser($customer
->getCustomerID(), $user_email, '', '', '');
$response = $user_api_handler
->get($miniorange_user);
if (!is_null($response)) {
$users_active_method = AuthenticationType::getAuthType($response->authType);
}
else {
$users_active_method = NULL;
}
$configured_methods = MoAuthUtilities::mo_auth_get_configured_methods($user_id);
$form['header_top']['#markup'] = '<div class="mo2f-setup-header">
<span class="mo2f-setup-header-top-left">Setup Two-Factor</span>
<span class="mo2f-setup-header-top-center">Active Method - <b>' . strtoupper($users_active_method['name']) . '</b></span>
</div>
<div>
<div class="mo2f-info">Select any Two-Factor of your choice below and complete its setup.</div>
</div>
<div></div>';
$form['header_methods']['#markup'] = '<div class="mo2f-setup-methods-info-wrap">
<div class="mo2f-setup-methods-info-left"><span class="mo2f-color-icon mo2f-active-method"></span>- Active Method</div>
<div class="mo2f-setup-methods-info-center"><span class="mo2f-color-icon mo2f-configured-method"></span>- Configured Method</div>
<div class="mo2f-setup-methods-info-right"><span class="mo2f-color-icon mo2f-unconfigured-method"></span>- Unconfigured Method</div>
</div>';
$emailVerificationOption = $this
->mo_auth_create_auth_type(AuthenticationType::$EMAIL_VERIFICATION, $configured_methods, $users_active_method['code'], FALSE, $base_url);
$kbaAuth = $this
->mo_auth_create_auth_type(AuthenticationType::$KBA, $configured_methods, $users_active_method['code'], TRUE, $base_url);
$googleAuthenticatorOption = $this
->mo_auth_create_auth_type(AuthenticationType::$GOOGLE_AUTHENTICATOR, $configured_methods, $users_active_method['code'], TRUE, $base_url);
$qrCodeAuthenticatorOption = $this
->mo_auth_create_auth_type(AuthenticationType::$QR_CODE, $configured_methods, $users_active_method['code'], TRUE, $base_url);
$softTokenOption = $this
->mo_auth_create_auth_type(AuthenticationType::$SOFT_TOKEN, $configured_methods, $users_active_method['code'], TRUE, $base_url);
$pushNotificationsOption = $this
->mo_auth_create_auth_type(AuthenticationType::$PUSH_NOTIFICATIONS, $configured_methods, $users_active_method['code'], TRUE, $base_url);
$OTPOverSMSOption = $this
->mo_auth_create_auth_type(AuthenticationType::$SMS, $configured_methods, $users_active_method['code'], TRUE, $base_url);
$OTPOverSMSandEMAILOption = $this
->mo_auth_create_auth_type(AuthenticationType::$SMS_AND_EMAIL, $configured_methods, $users_active_method['code'], TRUE, $base_url);
$OTPOverEMAILOption = $this
->mo_auth_create_auth_type(AuthenticationType::$OTP_OVER_EMAIL, $configured_methods, $users_active_method['code'], TRUE, $base_url);
$OTPOverPHONEOption = $this
->mo_auth_create_auth_type(AuthenticationType::$OTP_OVER_PHONE, $configured_methods, $users_active_method['code'], TRUE, $base_url);
$options = array(
AuthenticationType::$EMAIL_VERIFICATION['code'] => $emailVerificationOption,
AuthenticationType::$GOOGLE_AUTHENTICATOR['code'] => $googleAuthenticatorOption,
AuthenticationType::$SMS['code'] => $OTPOverSMSOption,
AuthenticationType::$OTP_OVER_EMAIL['code'] => $OTPOverEMAILOption,
AuthenticationType::$SMS_AND_EMAIL['code'] => $OTPOverSMSandEMAILOption,
AuthenticationType::$KBA['code'] => $kbaAuth,
AuthenticationType::$PUSH_NOTIFICATIONS['code'] => $pushNotificationsOption,
AuthenticationType::$QR_CODE['code'] => $qrCodeAuthenticatorOption,
AuthenticationType::$SOFT_TOKEN['code'] => $softTokenOption,
AuthenticationType::$OTP_OVER_PHONE['code'] => $OTPOverPHONEOption,
);
$form['mo_auth_method'] = array(
'#type' => 'radios',
'#options' => $options,
'#default_value' => $users_active_method['code'],
'#disabled' => $disabled,
);
$form['Submit_setup_two_factor_form'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#disabled' => $disabled,
'#suffix' => '</div>',
);
MoAuthUtilities::AddsupportTab($form, $form_state);
return $form;
}
function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$user_obj = User::load(\Drupal::currentUser()
->id());
$user_id = $user_obj
->id();
$configured_methods = MoAuthUtilities::mo_auth_get_configured_methods($user_id);
$utilities = new MoAuthUtilities();
$custom_attribute = $utilities::get_users_custom_attribute($user_id);
$user_email = $custom_attribute[0]->miniorange_registered_email;
$form_state
->setRebuild();
$input = $form_state
->getUserInput();
$authType = $input['mo_auth_method'];
$database = \Drupal::database();
$database
->update('UserAuthenticationType')
->fields([
'activated_auth_methods' => $authType,
])
->condition('miniorange_registered_email', $user_email, '=')
->execute();
if (in_array($authType, $configured_methods)) {
$utilities = new MoAuthUtilities();
$custom_attribute = $utilities::get_users_custom_attribute($user_id);
$user_email = $custom_attribute[0]->miniorange_registered_email;
$customer = new MiniorangeCustomerProfile();
$miniorange_user = new MiniorangeUser($customer
->getCustomerID(), $user_email, NULL, NULL, $authType);
$user_api_handler = new UsersAPIHandler($customer
->getCustomerID(), $customer
->getAPIKey());
$response = $user_api_handler
->update($miniorange_user);
if ($response->status == 'SUCCESS') {
\Drupal::messenger()
->addMessage(t('Authentication method updated successfully.'), 'status');
}
else {
\Drupal::messenger()
->addMessage(t('An error occured while updating the authentication type. Please try again.'), 'error');
return;
}
}
else {
\Drupal::messenger()
->addMessage(t('Please configure this authentication method first to enable it.'), 'error');
return;
}
}
function mo_auth_create_auth_type($authType, $configuredMethods, $active_method, $reconfigure_allowed, $base_url) {
$label_title = 'Supported in ' . implode(', ', $authType['supported-for']);
$supported_for_icon_class = '';
if (in_array('Landline', $authType['supported-for'])) {
$supported_for_icon_class = 'mo2f-landline';
}
elseif (in_array('Feature Phones', $authType['supported-for'])) {
$supported_for_icon_class = 'mo2f-smartphone-feature-phone';
}
elseif (in_array('Laptops', $authType['supported-for'])) {
$supported_for_icon_class = 'mo2f-laptop';
}
elseif (in_array('Smartphones', $authType['supported-for'])) {
$supported_for_icon_class = 'mo2f-smartphone';
}
elseif (in_array('Laptops Phones', $authType['supported-for'])) {
$supported_for_icon_class = 'mo2f-smartphone-feature-phone-laptop';
}
$config_type_class = 'mo2f-unconfigured-method';
if ($authType['code'] == $active_method) {
$config_type_class = 'mo2f-active-method';
}
elseif (in_array($authType['code'], $configuredMethods)) {
$config_type_class = 'mo2f-configured-method';
}
$authTypeID = $authType['id'];
$Configure = "{$base_url}/admin/config/people/miniorange_2fa/setup/user/configure/{$authTypeID}";
$Test = "{$base_url}/admin/config/people/miniorange_2fa/setup/user/test/{$authTypeID}";
$Re_Configure = "{$base_url}/admin/config/people/miniorange_2fa/setup/user/configure/{$authTypeID}";
if (!MoAuthUtilities::isCustomerRegistered()) {
$Configure = "{$base_url}/admin/config/people/miniorange_2fa/customer_setup";
$Test = "{$base_url}/admin/config/people/miniorange_2fa/customer_setup";
$Re_Configure = "{$base_url}/admin/config/people/miniorange_2fa/customer_setup";
}
$configured = 'false';
$classes = $supported_for_icon_class . ' ' . $config_type_class;
$test = '<a href="' . $Configure . '">Configure</a>';
if (!empty($configuredMethods) && in_array($authType['code'], $configuredMethods)) {
$test = '<a href="' . $Test . '">Test</a>';
if ($reconfigure_allowed === TRUE) {
$test .= ' | <a href="' . $Re_Configure . '">Re-configure</a>';
}
$configured = 'true';
}
$html = '<span>
<span class="mo2f-method" data-id="' . $authType['id'] . '" data-configured="' . $configured . '">' . $authType['name'] . '</span>
<p>' . $authType['description'] . '</p>
<div class="' . $classes . '" title="' . $label_title . '">' . $test . '</div>
<div><br><hr><hr><br></div>
</span>';
return $html;
}
}
Classes
Name | Description |
---|---|
MoAuthSetupTwoFactor | Showing Setup Two-Factor page. |