Loading src/Plugin/TfaSetup/TfaDuoSetup.php +9 −5 Changes for src/Plugin/TfaSetup/TfaDuoSetup.php: 9 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\tfa_duo\Plugin\TfaSetup; use Drupal\Component\Utility\HTML; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; use Drupal\Core\Messenger\MessengerTrait; use Drupal\Core\Url; use Drupal\encrypt\EncryptServiceInterface; use Drupal\encrypt\EncryptionProfileManagerInterface; Loading Loading @@ -35,11 +36,12 @@ use Duo\Web; * ) */ class TfaDuoSetup extends TfaBasePlugin implements TfaSetupInterface { use MessengerTrait; /** * Object containing the external validation library. * * @var Dup\Web * @var \Duo\Web */ protected $duo; Loading Loading @@ -82,7 +84,7 @@ class TfaDuoSetup extends TfaBasePlugin implements TfaSetupInterface { public function getSetupForm(array $form, FormStateInterface $form_state) { // @TODO Let drupal know that you have setup 2fa with duo somehow. $key = $this->keyRepository->getKey($this->settings['duo_key'])->getKeyValues(); $sign_request = $this->duo->signRequest($key['duo_integration'], $key['duo_secret'], $key['duo_application'], \Drupal::currentUser()->getUsername()); $sign_request = $this->duo->signRequest($key['duo_integration'], $key['duo_secret'], $key['duo_application'], \Drupal::currentUser()->getDisplayName()); if ($this->responseErrors($sign_request)) { return $form; } Loading Loading @@ -179,17 +181,19 @@ class TfaDuoSetup extends TfaBasePlugin implements TfaSetupInterface { * * @param string $response * The response string from Duo. * @param boolean $verbose * @param bool $verbose * Whether or not to log the error message. * * @return boolean Whether or not there were errors. * @return bool * Whether or not there were errors. */ public function responseErrors($response, $verbose = TRUE) { list($response_code, $message) = explode('|', $response); $has_errors = ($response_code == 'ERR'); if ($has_errors && $verbose) { drupal_set_message(HTML::escape($message), 'error'); $this->messenger()->addError(HTML::escape($message)); } return $has_errors; } } src/Plugin/TfaValidation/TfaDuoValidation.php +14 −10 Changes for src/Plugin/TfaValidation/TfaDuoValidation.php: 14 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -5,15 +5,14 @@ namespace Drupal\tfa_duo\Plugin\TfaValidation; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\HTML; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerTrait; use Drupal\encrypt\EncryptionProfileManagerInterface; use Drupal\encrypt\EncryptServiceInterface; use Drupal\tfa\Plugin\TfaBasePlugin; use Drupal\tfa\Plugin\TfaValidationInterface; use Drupal\user\UserDataInterface; use Duo\Web; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\key\KeyRepositoryInterface; /** * Duo validation class for performing Duo validation. Loading @@ -26,11 +25,12 @@ use Drupal\key\KeyRepositoryInterface; * ) */ class TfaDuoValidation extends TfaBasePlugin implements TfaValidationInterface { use MessengerTrait; /** * Object containing the external validation library. * * @var Dup\Web * @var \Duo\Web */ protected $duo; Loading Loading @@ -152,13 +152,13 @@ class TfaDuoValidation extends TfaBasePlugin implements TfaValidationInterface { $sig_resp = trim($_POST['sig_response']); $action = trim($_POST['reset']); if (!$sig_resp) { drupal_set_message("The required information wasn't received. Please try again.", 'error'); $this->messenger()->addError("The required information wasn't received. Please try again."); return FALSE; } $name = $this->duo->verifyResponse($ikey, $skey, $akey, $sig_resp); $this->responseErrors($name); if (!$name || !user_load_by_name($name)) { drupal_set_message("Secondary authentication credentials were invalid, or the user cannot be loaded.", 'error'); $this->messenger()->addError("Secondary authentication credentials were invalid, or the user cannot be loaded."); return FALSE; } } Loading @@ -179,14 +179,16 @@ class TfaDuoValidation extends TfaBasePlugin implements TfaValidationInterface { // @TODO Do we need error checking here? \Drupal::logger('user')->info('User %name has authenticated with Duo.', ['%name' => $user->getDisplayName()]); user_login_finalize($user); $redirect = new RedirectResponse(\Drupal::url('user.page')); /** @var \Drupal\Core\Routing\UrlGenerator $generator */ $generator = \Drupal::service('url_generator'); $redirect = new RedirectResponse($generator->generateFromRoute('user.page')); // Handle user reset requests (UNTESTED) if (isset($_POST['reset']) && trim($_POST['reset']) == TRUE) { $token = Crypt::randomBytesBase64(55); $_SESSION['pass_reset_' . $user->id()] = $token; $timestamp = REQUEST_TIME; $redirect = new RedirectResponse(\Drupal::url('user.reset'), [ $redirect = new RedirectResponse($generator->generateFromRoute('user.reset'), [ 'uid' => $user->id(), 'timestamp' => $timestamp, 'hash' => user_pass_rehash($user, $timestamp), Loading @@ -201,17 +203,19 @@ class TfaDuoValidation extends TfaBasePlugin implements TfaValidationInterface { * * @param string $response * The response string from Duo. * @param boolean $verbose * @param bool $verbose * Whether or not to log the error message. * * @return boolean Whether or not there were errors. * @return bool * Whether or not there were errors. */ public function responseErrors($response, $verbose = TRUE) { list($response_code, $message) = explode('|', $response); $has_errors = ($response_code == 'ERR'); if ($has_errors && $verbose) { drupal_set_message(HTML::escape($message), 'error'); $this->messenger()->addError(HTML::escape($message)); } return $has_errors; } } tests/src/Functional/TfaDuoTest.php +1 −1 Changes for tests/src/Functional/TfaDuoTest.php: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -73,7 +73,7 @@ class TfaDuoTest extends TfaTestBase { // Verify a normal user is prompted with the tfa page. $user = $this->drupalCreateUser(); $edit = ['name' => $user->getUserName(), 'pass' => $user->passRaw]; $edit = ['name' => $user->getAccountName(), 'pass' => $user->passRaw]; $this->drupalPostForm('user/login', $edit, t('Log in')); $assert_session->addressEquals('tfa/' . $user->id() . '/' . $this->getLoginHash($user)); } Loading Loading
src/Plugin/TfaSetup/TfaDuoSetup.php +9 −5 Changes for src/Plugin/TfaSetup/TfaDuoSetup.php: 9 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\tfa_duo\Plugin\TfaSetup; use Drupal\Component\Utility\HTML; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; use Drupal\Core\Messenger\MessengerTrait; use Drupal\Core\Url; use Drupal\encrypt\EncryptServiceInterface; use Drupal\encrypt\EncryptionProfileManagerInterface; Loading Loading @@ -35,11 +36,12 @@ use Duo\Web; * ) */ class TfaDuoSetup extends TfaBasePlugin implements TfaSetupInterface { use MessengerTrait; /** * Object containing the external validation library. * * @var Dup\Web * @var \Duo\Web */ protected $duo; Loading Loading @@ -82,7 +84,7 @@ class TfaDuoSetup extends TfaBasePlugin implements TfaSetupInterface { public function getSetupForm(array $form, FormStateInterface $form_state) { // @TODO Let drupal know that you have setup 2fa with duo somehow. $key = $this->keyRepository->getKey($this->settings['duo_key'])->getKeyValues(); $sign_request = $this->duo->signRequest($key['duo_integration'], $key['duo_secret'], $key['duo_application'], \Drupal::currentUser()->getUsername()); $sign_request = $this->duo->signRequest($key['duo_integration'], $key['duo_secret'], $key['duo_application'], \Drupal::currentUser()->getDisplayName()); if ($this->responseErrors($sign_request)) { return $form; } Loading Loading @@ -179,17 +181,19 @@ class TfaDuoSetup extends TfaBasePlugin implements TfaSetupInterface { * * @param string $response * The response string from Duo. * @param boolean $verbose * @param bool $verbose * Whether or not to log the error message. * * @return boolean Whether or not there were errors. * @return bool * Whether or not there were errors. */ public function responseErrors($response, $verbose = TRUE) { list($response_code, $message) = explode('|', $response); $has_errors = ($response_code == 'ERR'); if ($has_errors && $verbose) { drupal_set_message(HTML::escape($message), 'error'); $this->messenger()->addError(HTML::escape($message)); } return $has_errors; } }
src/Plugin/TfaValidation/TfaDuoValidation.php +14 −10 Changes for src/Plugin/TfaValidation/TfaDuoValidation.php: 14 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -5,15 +5,14 @@ namespace Drupal\tfa_duo\Plugin\TfaValidation; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\HTML; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerTrait; use Drupal\encrypt\EncryptionProfileManagerInterface; use Drupal\encrypt\EncryptServiceInterface; use Drupal\tfa\Plugin\TfaBasePlugin; use Drupal\tfa\Plugin\TfaValidationInterface; use Drupal\user\UserDataInterface; use Duo\Web; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\key\KeyRepositoryInterface; /** * Duo validation class for performing Duo validation. Loading @@ -26,11 +25,12 @@ use Drupal\key\KeyRepositoryInterface; * ) */ class TfaDuoValidation extends TfaBasePlugin implements TfaValidationInterface { use MessengerTrait; /** * Object containing the external validation library. * * @var Dup\Web * @var \Duo\Web */ protected $duo; Loading Loading @@ -152,13 +152,13 @@ class TfaDuoValidation extends TfaBasePlugin implements TfaValidationInterface { $sig_resp = trim($_POST['sig_response']); $action = trim($_POST['reset']); if (!$sig_resp) { drupal_set_message("The required information wasn't received. Please try again.", 'error'); $this->messenger()->addError("The required information wasn't received. Please try again."); return FALSE; } $name = $this->duo->verifyResponse($ikey, $skey, $akey, $sig_resp); $this->responseErrors($name); if (!$name || !user_load_by_name($name)) { drupal_set_message("Secondary authentication credentials were invalid, or the user cannot be loaded.", 'error'); $this->messenger()->addError("Secondary authentication credentials were invalid, or the user cannot be loaded."); return FALSE; } } Loading @@ -179,14 +179,16 @@ class TfaDuoValidation extends TfaBasePlugin implements TfaValidationInterface { // @TODO Do we need error checking here? \Drupal::logger('user')->info('User %name has authenticated with Duo.', ['%name' => $user->getDisplayName()]); user_login_finalize($user); $redirect = new RedirectResponse(\Drupal::url('user.page')); /** @var \Drupal\Core\Routing\UrlGenerator $generator */ $generator = \Drupal::service('url_generator'); $redirect = new RedirectResponse($generator->generateFromRoute('user.page')); // Handle user reset requests (UNTESTED) if (isset($_POST['reset']) && trim($_POST['reset']) == TRUE) { $token = Crypt::randomBytesBase64(55); $_SESSION['pass_reset_' . $user->id()] = $token; $timestamp = REQUEST_TIME; $redirect = new RedirectResponse(\Drupal::url('user.reset'), [ $redirect = new RedirectResponse($generator->generateFromRoute('user.reset'), [ 'uid' => $user->id(), 'timestamp' => $timestamp, 'hash' => user_pass_rehash($user, $timestamp), Loading @@ -201,17 +203,19 @@ class TfaDuoValidation extends TfaBasePlugin implements TfaValidationInterface { * * @param string $response * The response string from Duo. * @param boolean $verbose * @param bool $verbose * Whether or not to log the error message. * * @return boolean Whether or not there were errors. * @return bool * Whether or not there were errors. */ public function responseErrors($response, $verbose = TRUE) { list($response_code, $message) = explode('|', $response); $has_errors = ($response_code == 'ERR'); if ($has_errors && $verbose) { drupal_set_message(HTML::escape($message), 'error'); $this->messenger()->addError(HTML::escape($message)); } return $has_errors; } }
tests/src/Functional/TfaDuoTest.php +1 −1 Changes for tests/src/Functional/TfaDuoTest.php: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -73,7 +73,7 @@ class TfaDuoTest extends TfaTestBase { // Verify a normal user is prompted with the tfa page. $user = $this->drupalCreateUser(); $edit = ['name' => $user->getUserName(), 'pass' => $user->passRaw]; $edit = ['name' => $user->getAccountName(), 'pass' => $user->passRaw]; $this->drupalPostForm('user/login', $edit, t('Log in')); $assert_session->addressEquals('tfa/' . $user->id() . '/' . $this->getLoginHash($user)); } Loading