diff --git a/ga_login.info.yml b/ga_login.info.yml index 4a45e3b..3c6030e 100644 --- a/ga_login.info.yml +++ b/ga_login.info.yml @@ -3,5 +3,6 @@ type: module package: System description: "TFA plugins for Hmac-Based One Time Passwords & Time Based One Time Password with recovery codes as fallback." core: 8.x +core_version_requirement: ^8 || ^9 dependencies: - tfa:tfa (>=8.x-1.0-alpha4) diff --git a/src/Plugin/TfaSetup/GALoginHotpSetup.php b/src/Plugin/TfaSetup/GALoginHotpSetup.php index de9cf50..4731de7 100644 --- a/src/Plugin/TfaSetup/GALoginHotpSetup.php +++ b/src/Plugin/TfaSetup/GALoginHotpSetup.php @@ -182,7 +182,7 @@ class GALoginHotpSetup extends GALoginHotpValidation implements TfaSetupInterfac /** @var User $account */ $account = User::load($this->configuration['uid']); $prefix = $this->siteNamePrefix ? preg_replace('@[^a-z0-9-]+@','-', strtolower(\Drupal::config('system.site')->get('name'))) : $this->namePrefix; - return urlencode($prefix . '-' . $account->getUsername()); + return urlencode($prefix . '-' . $account->getAccountName()); } /** diff --git a/src/Plugin/TfaSetup/GALoginTotpSetup.php b/src/Plugin/TfaSetup/GALoginTotpSetup.php index d8589c4..42fe5c2 100644 --- a/src/Plugin/TfaSetup/GALoginTotpSetup.php +++ b/src/Plugin/TfaSetup/GALoginTotpSetup.php @@ -179,7 +179,7 @@ class GALoginTotpSetup extends GALoginTotpValidation implements TfaSetupInterfac /** @var User $account */ $account = User::load($this->configuration['uid']); $prefix = $this->siteNamePrefix ? preg_replace('@[^a-z0-9-]+@','-', strtolower(\Drupal::config('system.site')->get('name'))) : $this->namePrefix; - return urlencode($prefix . '-' . $account->getUsername()); + return urlencode($prefix . '-' . $account->getAccountName()); } /** diff --git a/src/Plugin/TfaValidation/GALoginHotpValidation.php b/src/Plugin/TfaValidation/GALoginHotpValidation.php index 6a6260c..983256c 100644 --- a/src/Plugin/TfaValidation/GALoginHotpValidation.php +++ b/src/Plugin/TfaValidation/GALoginHotpValidation.php @@ -2,9 +2,11 @@ namespace Drupal\ga_login\Plugin\TfaValidation; -use Drupal\Core\StringTranslation\StringTranslationTrait; -use ParagonIE\ConstantTime\Encoding; +use Drupal\Component\Datetime\TimeInterface; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\encrypt\EncryptionProfileManagerInterface; use Drupal\encrypt\EncryptServiceInterface; use Drupal\tfa\Plugin\TfaBasePlugin; @@ -12,6 +14,7 @@ use Drupal\tfa\Plugin\TfaValidationInterface; use Drupal\user\UserDataInterface; use Otp\GoogleAuthenticator; use Otp\Otp; +use ParagonIE\ConstantTime\Encoding; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -27,7 +30,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * isFallback = FALSE * ) */ -class GALoginHotpValidation extends TfaBasePlugin implements TfaValidationInterface { +class GALoginHotpValidation extends TfaBasePlugin implements TfaValidationInterface, ContainerFactoryPluginInterface { use StringTranslationTrait; /** @@ -72,15 +75,22 @@ class GALoginHotpValidation extends TfaBasePlugin implements TfaValidationInterf */ protected $alreadyAccepted; + /** + * The Datetime service. + * + * @var \Drupal\Component\Datetime\TimeInterface + */ + protected $time; + /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, UserDataInterface $user_data, EncryptionProfileManagerInterface $encryption_profile_manager, EncryptServiceInterface $encrypt_service) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, UserDataInterface $user_data, EncryptionProfileManagerInterface $encryption_profile_manager, EncryptServiceInterface $encrypt_service, ConfigFactoryInterface $config_factory, TimeInterface $time) { parent::__construct($configuration, $plugin_id, $plugin_definition, $user_data, $encryption_profile_manager, $encrypt_service); $this->auth = new \StdClass(); $this->auth->otp = new Otp(); $this->auth->ga = new GoogleAuthenticator(); - $plugin_settings = \Drupal::config('tfa.settings')->get('validation_plugin_settings'); + $plugin_settings = $config_factory->get('tfa.settings')->get('validation_plugin_settings'); $settings = isset($plugin_settings['tfa_hotp']) ? $plugin_settings['tfa_hotp'] : []; $settings = array_replace([ 'counter_window' => 10, @@ -94,6 +104,7 @@ class GALoginHotpValidation extends TfaBasePlugin implements TfaValidationInterf $this->namePrefix = $settings['name_prefix']; $this->issuer = $settings['issuer']; $this->alreadyAccepted = FALSE; + $this->time = $time; } /** @@ -106,7 +117,9 @@ class GALoginHotpValidation extends TfaBasePlugin implements TfaValidationInterf $plugin_definition, $container->get('user.data'), $container->get('encrypt.encryption_profile.manager'), - $container->get('encryption') + $container->get('encryption'), + $container->get('config.factory'), + $container->get('datetime.time') ); } @@ -286,7 +299,7 @@ class GALoginHotpValidation extends TfaBasePlugin implements TfaValidationInterf $record = [ 'tfa_hotp_seed' => [ 'seed' => base64_encode($encrypted), - 'created' => REQUEST_TIME, + 'created' => $this->time->getRequestTime(), ], ]; $this->setUserData('tfa', $record, $this->uid, $this->userData); diff --git a/src/Plugin/TfaValidation/GALoginTotpValidation.php b/src/Plugin/TfaValidation/GALoginTotpValidation.php index f59df22..7ba6b1c 100644 --- a/src/Plugin/TfaValidation/GALoginTotpValidation.php +++ b/src/Plugin/TfaValidation/GALoginTotpValidation.php @@ -2,6 +2,9 @@ namespace Drupal\ga_login\Plugin\TfaValidation; +use Drupal\Component\Datetime\TimeInterface; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use ParagonIE\ConstantTime\Encoding; use Drupal\Core\Form\FormStateInterface; @@ -27,7 +30,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * isFallback = FALSE * ) */ -class GALoginTotpValidation extends TfaBasePlugin implements TfaValidationInterface { +class GALoginTotpValidation extends TfaBasePlugin implements TfaValidationInterface, ContainerFactoryPluginInterface { use StringTranslationTrait; /** @@ -72,16 +75,23 @@ class GALoginTotpValidation extends TfaBasePlugin implements TfaValidationInterf */ protected $alreadyAccepted; + /** + * The Datetime service. + * + * @var \Drupal\Component\Datetime\TimeInterface + */ + protected $time; + /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, UserDataInterface $user_data, EncryptionProfileManagerInterface $encryption_profile_manager, EncryptServiceInterface $encrypt_service) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, UserDataInterface $user_data, EncryptionProfileManagerInterface $encryption_profile_manager, EncryptServiceInterface $encrypt_service, ConfigFactoryInterface $config_factory, TimeInterface $time) { parent::__construct($configuration, $plugin_id, $plugin_definition, $user_data, $encryption_profile_manager, $encrypt_service); $this->auth = new \StdClass(); $this->auth->otp = new Otp(); $this->auth->ga = new GoogleAuthenticator(); // Allow codes within tolerance range of 3 * 30 second units. - $plugin_settings = \Drupal::config('tfa.settings')->get('validation_plugin_settings'); + $plugin_settings = $config_factory->get('tfa.settings')->get('validation_plugin_settings'); $settings = isset($plugin_settings['tfa_totp']) ? $plugin_settings['tfa_totp'] : []; $settings = array_replace([ 'time_skew' => 2, @@ -94,6 +104,7 @@ class GALoginTotpValidation extends TfaBasePlugin implements TfaValidationInterf $this->namePrefix = $settings['name_prefix']; $this->issuer = $settings['issuer']; $this->alreadyAccepted = FALSE; + $this->time = $time; } /** @@ -106,7 +117,9 @@ class GALoginTotpValidation extends TfaBasePlugin implements TfaValidationInterf $plugin_definition, $container->get('user.data'), $container->get('encrypt.encryption_profile.manager'), - $container->get('encryption') + $container->get('encryption'), + $container->get('config.factory'), + $container->get('datetime.time') ); } @@ -286,7 +299,7 @@ class GALoginTotpValidation extends TfaBasePlugin implements TfaValidationInterf $record = [ 'tfa_totp_seed' => [ 'seed' => base64_encode($encrypted), - 'created' => REQUEST_TIME, + 'created' => $this->time->getRequestTime(), ], ];