reverted: --- b/modules/includes/tfa_email.inc +++ /dev/null @@ -1,224 +0,0 @@ -code = $context['validate_context']['code']; - } - - $this->account = $account; - $this->siteEmailAddress = variable_get('site_mail', ini_get('sendmail_from')); - $this->codeLength = 6; - $this->language = user_preferred_language($account); - - $this->userEmailAddress = $this->getUserEmailAddress(); - } - - /** - * - */ - public function begin() { - if (!$this->code) { - $this->code = $this->generate(); - if (!$this->sendCode($this->code)) { - $this->showErrorMessage(); - } - } - } - - public function getForm(array $form, array &$form_state) { - $form['code'] = array( - '#type' => 'textfield', - '#title' => t('Verification Code'), - '#required' => TRUE, - '#description' => t('Enter @length-character code sent to your email account (@email).', array('@length' => $this->codeLength, '@email' => $this->userEmailAddress)), - '#attributes' => array('autocomplete' => array('off')) - ); - $form['actions']['#type'] = 'actions'; - // @todo optionally report on when code was sent/delivered. - $form['actions']['login'] = array( - '#type' => 'submit', - '#value' => t('Verify'), - ); - $form['actions']['resend'] = array( - '#type' => 'submit', - '#value' => t('Resend'), - '#submit' => array('tfa_form_submit'), - '#limit_validation_errors' => array(), - ); - - return $form; - } - - public function validateForm(array $form, array &$form_state) { - // If operation is resend then do not attempt to validate code. - if ($form_state['values']['op'] === $form_state['values']['resend']) { - return TRUE; - } - elseif (!parent::validate($form_state['values']['code'])) { - $this->errorMessages['code'] = t('Invalid code.'); - return FALSE; - } - else { - return TRUE; - } - } - - public function showErrorMessage() { - $email_error_message = variable_get('tfa_email_error_message', t('An error occured while attempting to email your authentication code. If you continue to experience difficulties, please contact us.')); - drupal_set_message($email_error_message, 'error'); - - // clear the message that is automatically generated by core. - // We don't want to tell the user "Contact the site administrator..." - $unwanted_core_message = t('Unable to send e-mail. Contact the site administrator if the problem persists.'); - $error_messages = drupal_get_messages('error', TRUE); - while (($key = array_search($unwanted_core_message, $error_messages['error'])) !== FALSE) { - unset($error_messages['error'][$key]); - } - // restore any queued error messages - foreach ($error_messages['error'] as $key => $message) { - drupal_set_message($message, 'error'); - } - // Take the user back to the login page to view the error message. - drupal_goto('user'); - } - - public function submitForm(array $form, array &$form_state) { - // Resend code if pushed. - if ($form_state['values']['op'] === $form_state['values']['resend']) { - $this->code = $this->generate(); - if (!$this->sendCode($this->code)) { - $this->showErrorMessage(); - } - else { - drupal_set_message(t('Code resent')); - } - return FALSE; - } - else { - return parent::submitForm($form, $form_state); - } - } - - /** - * Return context for this plugin. - * - * @return array - */ - public function getPluginContext() { - return array( - 'code' => $this->code, - ); - } - - protected function generate() { - $characters = '0123456789'; - $string = ''; - $max = strlen($characters) - 1; - for ($p = 0; $p < $this->codeLength; $p++) { - $string .= $characters[mt_rand(0, $max)]; - } - return $string; - } - - protected function getUserEmailAddress() { - $email = $this->account->mail; - if (!empty($this->context['user_email_address'])) { - $email = $this->context['user_email_address']; - } - // Allow other modules to update the email address - drupal_alter('tfa_email_get_user_email_address', $this->account, $email); - return $email; - } - - /** - * Send the code via the drupal_mail function. - * - * @param string $code - * The 2-factor authentication code that the challenged user must provide. - * @return bool - * Whether or not an error was detected while sending the message. - */ - protected function sendCode($code) { - $to = $this->userEmailAddress; - $key = 'tfa_email_send_code'; - $params = array( - 'site_name' => variable_get('site_name', 'Your login'), - 'authentication_code' => $code, - 'langcode' => $this->language->language, - ); - - $message_result = drupal_mail('tfa_email', $key, $to, $this->language, $params); - - return ($message_result['result'] == TRUE); - } -} - -/** - * Class TfaBasicEmailSetup - */ -class TfaBasicEmailSetup extends TfaBasicEmail implements TfaSetupPluginInterface { - - public function __construct(array $context, $user_email_address) { - parent::__construct($context, $user_email_address); - } - - public function begin() { - if (empty($this->code)) { - $this->code = $this->generate(); - if (!$this->sendCode($this->code)) { - // @todo decide on error text - $this->errorMessages[''] = t('Unable to deliver code to that email address.'); - } - } - } - - /** - * @copydoc TfaSetupPluginInterface::getSetupForm() - */ - public function getSetupForm(array $form, array &$form_state) { - $form['email_code'] = array( - '#type' => 'textfield', - '#title' => t('Verification Code'), - '#required' => TRUE, - '#description' => t('Enter @length-character code sent to your email account.', array('@length' => $this->codeLength)), - ); - $form['actions']['verify'] = array( - '#type' => 'submit', - '#value' => t('Verify and save'), - ); - - return $form; - } - - /** - * @copydoc TfaSetupPluginInterface::validateSetupForm() - */ - public function validateSetupForm(array $form, array &$form_state) { - if (!$this->validate($form_state['values']['email_code'])) { - $this->errorMessages['email_code'] = t('Invalid code. Please try again.'); - return FALSE; - } - else { - return TRUE; - } - } - - /** - * @copydoc TfaSetupPluginInterface::submitSetupForm() - */ - public function submitSetupForm(array $form, array &$form_state) { - // No submission handling required. - return TRUE; - } - -} reverted: --- b/modules/tfa_email.info +++ /dev/null @@ -1,13 +0,0 @@ -name = "Two-Factor Email Authentication" -description = "TFA plugin for sending one-time use code via email" -core = 7.x - -dependencies[] = tfa -dependencies[] = tfa_basic - -files[] = includes/tfa_email.inc - -version = "7.x-1.0" -core = "7.x" -project = "tfa_email" -datestamp = "1437427929" reverted: --- b/modules/tfa_email.module +++ /dev/null @@ -1,46 +0,0 @@ - array( - 'class' => 'TfaBasicEmail', - 'name' => 'Email', - ), - ); -} - -/** - * Create TfaBasicEmail plugin. - * - * @param array $context - * @return TfaBasicEmail - */ -function tfa_basic_email_create($context) { - $account = user_load($context['uid']); - // Allow other modules to alter the variables passed to the class constructor. - module_invoke_all('tfa_basic_email_create_alter', $context, $account); - return new TfaBasicEmail($context, $account); -} - -/** - * Implements hook_mail() - * @see TfaBasicEmail::sendCode() - */ -function tfa_email_mail($key, &$message, $params) { - $options = array( - 'langcode' => $message['language']->language, - ); - - $subject_tpl = variable_get('tfa_basic_email_subject_text', '!sitename verification code'); - $body_tpl = variable_get('tfa_basic_email_message_text', 'Verification code: !code'); - - switch ($key) { - case 'tfa_email_send_code': - $message['subject'] = t($subject_tpl, array('!sitename' => $params['site_name']), $options); - $message['body'][] = t($body_tpl, array('!code' => $params['authentication_code']), $options); - break; - } -} only in patch2: unchanged: --- /dev/null +++ b/modules/tfa_email/includes/tfa_email.inc @@ -0,0 +1,224 @@ +code = $context['validate_context']['code']; + } + + $this->account = $account; + $this->siteEmailAddress = variable_get('site_mail', ini_get('sendmail_from')); + $this->codeLength = 6; + $this->language = user_preferred_language($account); + + $this->userEmailAddress = $this->getUserEmailAddress(); + } + + /** + * + */ + public function begin() { + if (!$this->code) { + $this->code = $this->generate(); + if (!$this->sendCode($this->code)) { + $this->showErrorMessage(); + } + } + } + + public function getForm(array $form, array &$form_state) { + $form['code'] = array( + '#type' => 'textfield', + '#title' => t('Verification Code'), + '#required' => TRUE, + '#description' => t('Enter @length-character code sent to your email account (@email).', array('@length' => $this->codeLength, '@email' => $this->userEmailAddress)), + '#attributes' => array('autocomplete' => array('off')) + ); + $form['actions']['#type'] = 'actions'; + // @todo optionally report on when code was sent/delivered. + $form['actions']['login'] = array( + '#type' => 'submit', + '#value' => t('Verify'), + ); + $form['actions']['resend'] = array( + '#type' => 'submit', + '#value' => t('Resend'), + '#submit' => array('tfa_form_submit'), + '#limit_validation_errors' => array(), + ); + + return $form; + } + + public function validateForm(array $form, array &$form_state) { + // If operation is resend then do not attempt to validate code. + if ($form_state['values']['op'] === $form_state['values']['resend']) { + return TRUE; + } + elseif (!parent::validate($form_state['values']['code'])) { + $this->errorMessages['code'] = t('Invalid code.'); + return FALSE; + } + else { + return TRUE; + } + } + + public function showErrorMessage() { + $email_error_message = variable_get('tfa_email_error_message', t('An error occured while attempting to email your authentication code. If you continue to experience difficulties, please contact us.')); + drupal_set_message($email_error_message, 'error'); + + // clear the message that is automatically generated by core. + // We don't want to tell the user "Contact the site administrator..." + $unwanted_core_message = t('Unable to send e-mail. Contact the site administrator if the problem persists.'); + $error_messages = drupal_get_messages('error', TRUE); + while (($key = array_search($unwanted_core_message, $error_messages['error'])) !== FALSE) { + unset($error_messages['error'][$key]); + } + // restore any queued error messages + foreach ($error_messages['error'] as $key => $message) { + drupal_set_message($message, 'error'); + } + // Take the user back to the login page to view the error message. + drupal_goto('user'); + } + + public function submitForm(array $form, array &$form_state) { + // Resend code if pushed. + if ($form_state['values']['op'] === $form_state['values']['resend']) { + $this->code = $this->generate(); + if (!$this->sendCode($this->code)) { + $this->showErrorMessage(); + } + else { + drupal_set_message(t('Code resent')); + } + return FALSE; + } + else { + return parent::submitForm($form, $form_state); + } + } + + /** + * Return context for this plugin. + * + * @return array + */ + public function getPluginContext() { + return array( + 'code' => $this->code, + ); + } + + protected function generate() { + $characters = '0123456789'; + $string = ''; + $max = strlen($characters) - 1; + for ($p = 0; $p < $this->codeLength; $p++) { + $string .= $characters[mt_rand(0, $max)]; + } + return $string; + } + + protected function getUserEmailAddress() { + $email = $this->account->mail; + if (!empty($this->context['user_email_address'])) { + $email = $this->context['user_email_address']; + } + // Allow other modules to update the email address + drupal_alter('tfa_email_get_user_email_address', $this->account, $email); + return $email; + } + + /** + * Send the code via the drupal_mail function. + * + * @param string $code + * The 2-factor authentication code that the challenged user must provide. + * @return bool + * Whether or not an error was detected while sending the message. + */ + protected function sendCode($code) { + $to = $this->userEmailAddress; + $key = 'tfa_email_send_code'; + $params = array( + 'site_name' => variable_get('site_name', 'Your login'), + 'authentication_code' => $code, + 'langcode' => $this->language->language, + ); + + $message_result = drupal_mail('tfa_email', $key, $to, $this->language, $params); + + return ($message_result['result'] == TRUE); + } +} + +/** + * Class TfaBasicEmailSetup + */ +class TfaBasicEmailSetup extends TfaBasicEmail implements TfaSetupPluginInterface { + + public function __construct(array $context, $user_email_address) { + parent::__construct($context, $user_email_address); + } + + public function begin() { + if (empty($this->code)) { + $this->code = $this->generate(); + if (!$this->sendCode($this->code)) { + // @todo decide on error text + $this->errorMessages[''] = t('Unable to deliver code to that email address.'); + } + } + } + + /** + * @copydoc TfaSetupPluginInterface::getSetupForm() + */ + public function getSetupForm(array $form, array &$form_state) { + $form['email_code'] = array( + '#type' => 'textfield', + '#title' => t('Verification Code'), + '#required' => TRUE, + '#description' => t('Enter @length-character code sent to your email account.', array('@length' => $this->codeLength)), + ); + $form['actions']['verify'] = array( + '#type' => 'submit', + '#value' => t('Verify and save'), + ); + + return $form; + } + + /** + * @copydoc TfaSetupPluginInterface::validateSetupForm() + */ + public function validateSetupForm(array $form, array &$form_state) { + if (!$this->validate($form_state['values']['email_code'])) { + $this->errorMessages['email_code'] = t('Invalid code. Please try again.'); + return FALSE; + } + else { + return TRUE; + } + } + + /** + * @copydoc TfaSetupPluginInterface::submitSetupForm() + */ + public function submitSetupForm(array $form, array &$form_state) { + // No submission handling required. + return TRUE; + } + +} only in patch2: unchanged: --- /dev/null +++ b/modules/tfa_email/tfa_email.info @@ -0,0 +1,13 @@ +name = "Two-Factor Email Authentication" +description = "TFA plugin for sending one-time use code via email" +core = 7.x + +dependencies[] = tfa +dependencies[] = tfa_basic + +files[] = includes/tfa_email.inc + +version = "7.x-1.0" +core = "7.x" +project = "tfa_email" +datestamp = "1437427929" only in patch2: unchanged: --- /dev/null +++ b/modules/tfa_email/tfa_email.module @@ -0,0 +1,46 @@ + array( + 'class' => 'TfaBasicEmail', + 'name' => 'Email', + ), + ); +} + +/** + * Create TfaBasicEmail plugin. + * + * @param array $context + * @return TfaBasicEmail + */ +function tfa_basic_email_create($context) { + $account = user_load($context['uid']); + // Allow other modules to alter the variables passed to the class constructor. + module_invoke_all('tfa_basic_email_create_alter', $context, $account); + return new TfaBasicEmail($context, $account); +} + +/** + * Implements hook_mail() + * @see TfaBasicEmail::sendCode() + */ +function tfa_email_mail($key, &$message, $params) { + $options = array( + 'langcode' => $message['language']->language, + ); + + $subject_tpl = variable_get('tfa_basic_email_subject_text', '!sitename verification code'); + $body_tpl = variable_get('tfa_basic_email_message_text', 'Verification code: !code'); + + switch ($key) { + case 'tfa_email_send_code': + $message['subject'] = t($subject_tpl, array('!sitename' => $params['site_name']), $options); + $message['body'][] = t($body_tpl, array('!code' => $params['authentication_code']), $options); + break; + } +}