mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-11 22:14:25 +02:00
Remove DI and superglobals dependency from two-factor settings modules
This commit is contained in:
parent
309844e6d8
commit
4a90394c38
5 changed files with 147 additions and 115 deletions
|
@ -21,75 +21,95 @@
|
|||
|
||||
namespace Friendica\Module\Settings\TwoFactor;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
|
||||
use Friendica\Core\Renderer;
|
||||
use Friendica\DI;
|
||||
use Friendica\Core\Session\Capability\IHandleUserSessions;
|
||||
use Friendica\Module\Response;
|
||||
use Friendica\Navigation\SystemMessages;
|
||||
use Friendica\Network\HTTPException\FoundException;
|
||||
use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
|
||||
use Friendica\Security\TwoFactor\Model\RecoveryCode;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Module\BaseSettings;
|
||||
use Friendica\Module\Security\Login;
|
||||
use Friendica\Util\Profiler;
|
||||
use PragmaRX\Google2FA\Google2FA;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Index extends BaseSettings
|
||||
{
|
||||
/** @var IManagePersonalConfigValues */
|
||||
protected $pConfig;
|
||||
/** @var SystemMessages */
|
||||
protected $systemMessages;
|
||||
|
||||
public function __construct(SystemMessages $systemMessages, IManagePersonalConfigValues $pConfig, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
|
||||
{
|
||||
parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
|
||||
|
||||
$this->pConfig = $pConfig;
|
||||
$this->systemMessages = $systemMessages;
|
||||
}
|
||||
|
||||
protected function post(array $request = [])
|
||||
{
|
||||
if (!DI::userSession()->getLocalUserId()) {
|
||||
if (!$this->session->getLocalUserId()) {
|
||||
return;
|
||||
}
|
||||
|
||||
self::checkFormSecurityTokenRedirectOnError('settings/2fa', 'settings_2fa');
|
||||
|
||||
try {
|
||||
User::getIdFromPasswordAuthentication(DI::userSession()->getLocalUserId(), $_POST['password'] ?? '');
|
||||
User::getIdFromPasswordAuthentication($this->session->getLocalUserId(), $request['password'] ?? '');
|
||||
|
||||
$has_secret = (bool)DI::pConfig()->get(DI::userSession()->getLocalUserId(), '2fa', 'secret');
|
||||
$verified = DI::pConfig()->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
|
||||
$has_secret = (bool)$this->pConfig->get($this->session->getLocalUserId(), '2fa', 'secret');
|
||||
$verified = $this->pConfig->get($this->session->getLocalUserId(), '2fa', 'verified');
|
||||
|
||||
switch ($_POST['action'] ?? '') {
|
||||
switch ($request['action'] ?? '') {
|
||||
case 'enable':
|
||||
if (!$has_secret && !$verified) {
|
||||
$Google2FA = new Google2FA();
|
||||
|
||||
DI::pConfig()->set(DI::userSession()->getLocalUserId(), '2fa', 'secret', $Google2FA->generateSecretKey(32));
|
||||
$this->pConfig->set($this->session->getLocalUserId(), '2fa', 'secret', $Google2FA->generateSecretKey(32));
|
||||
|
||||
DI::baseUrl()
|
||||
$this->baseUrl
|
||||
->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
|
||||
}
|
||||
break;
|
||||
case 'disable':
|
||||
if ($has_secret) {
|
||||
RecoveryCode::deleteForUser(DI::userSession()->getLocalUserId());
|
||||
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), '2fa', 'secret');
|
||||
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), '2fa', 'verified');
|
||||
DI::session()->remove('2fa');
|
||||
RecoveryCode::deleteForUser($this->session->getLocalUserId());
|
||||
$this->pConfig->delete($this->session->getLocalUserId(), '2fa', 'secret');
|
||||
$this->pConfig->delete($this->session->getLocalUserId(), '2fa', 'verified');
|
||||
$this->session->remove('2fa');
|
||||
|
||||
DI::sysmsg()->addInfo(DI::l10n()->t('Two-factor authentication successfully disabled.'));
|
||||
DI::baseUrl()->redirect('settings/2fa');
|
||||
$this->systemMessages->addInfo($this->t('Two-factor authentication successfully disabled.'));
|
||||
$this->baseUrl->redirect('settings/2fa');
|
||||
}
|
||||
break;
|
||||
case 'recovery':
|
||||
if ($has_secret) {
|
||||
DI::baseUrl()
|
||||
$this->baseUrl
|
||||
->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
|
||||
}
|
||||
break;
|
||||
case 'app_specific':
|
||||
if ($has_secret) {
|
||||
DI::baseUrl()
|
||||
$this->baseUrl
|
||||
->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
|
||||
}
|
||||
break;
|
||||
case 'trusted':
|
||||
if ($has_secret) {
|
||||
DI::baseUrl()
|
||||
$this->baseUrl
|
||||
->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
|
||||
}
|
||||
break;
|
||||
case 'configure':
|
||||
if (!$verified) {
|
||||
DI::baseUrl()
|
||||
$this->baseUrl
|
||||
->redirect('settings/2fa/verify?t=' . self::getFormSecurityToken('settings_2fa_password'));
|
||||
}
|
||||
break;
|
||||
|
@ -97,53 +117,53 @@ class Index extends BaseSettings
|
|||
} catch (FoundException $exception) {
|
||||
// Nothing to do here
|
||||
} catch (\Exception $e) {
|
||||
DI::sysmsg()->addNotice(DI::l10n()->t($e->getMessage()));
|
||||
$this->systemMessages->addNotice($this->t($e->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
protected function content(array $request = []): string
|
||||
{
|
||||
if (!DI::userSession()->getLocalUserId()) {
|
||||
if (!$this->session->getLocalUserId()) {
|
||||
return Login::form('settings/2fa');
|
||||
}
|
||||
|
||||
parent::content();
|
||||
|
||||
$has_secret = (bool) DI::pConfig()->get(DI::userSession()->getLocalUserId(), '2fa', 'secret');
|
||||
$verified = DI::pConfig()->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
|
||||
$has_secret = (bool) $this->pConfig->get($this->session->getLocalUserId(), '2fa', 'secret');
|
||||
$verified = $this->pConfig->get($this->session->getLocalUserId(), '2fa', 'verified');
|
||||
|
||||
return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/index.tpl'), [
|
||||
'$form_security_token' => self::getFormSecurityToken('settings_2fa'),
|
||||
'$title' => DI::l10n()->t('Two-factor authentication'),
|
||||
'$help_label' => DI::l10n()->t('Help'),
|
||||
'$status_title' => DI::l10n()->t('Status'),
|
||||
'$message' => DI::l10n()->t('<p>Use an application on a mobile device to get two-factor authentication codes when prompted on login.</p>'),
|
||||
'$title' => $this->t('Two-factor authentication'),
|
||||
'$help_label' => $this->t('Help'),
|
||||
'$status_title' => $this->t('Status'),
|
||||
'$message' => $this->t('<p>Use an application on a mobile device to get two-factor authentication codes when prompted on login.</p>'),
|
||||
'$has_secret' => $has_secret,
|
||||
'$verified' => $verified,
|
||||
|
||||
'$auth_app_label' => DI::l10n()->t('Authenticator app'),
|
||||
'$app_status' => $has_secret ? $verified ? DI::l10n()->t('Configured') : DI::l10n()->t('Not Configured') : DI::l10n()->t('Disabled'),
|
||||
'$not_configured_message' => DI::l10n()->t('<p>You haven\'t finished configuring your authenticator app.</p>'),
|
||||
'$configured_message' => DI::l10n()->t('<p>Your authenticator app is correctly configured.</p>'),
|
||||
'$auth_app_label' => $this->t('Authenticator app'),
|
||||
'$app_status' => $has_secret ? $verified ? $this->t('Configured') : $this->t('Not Configured') : $this->t('Disabled'),
|
||||
'$not_configured_message' => $this->t('<p>You haven\'t finished configuring your authenticator app.</p>'),
|
||||
'$configured_message' => $this->t('<p>Your authenticator app is correctly configured.</p>'),
|
||||
|
||||
'$recovery_codes_title' => DI::l10n()->t('Recovery codes'),
|
||||
'$recovery_codes_remaining' => DI::l10n()->t('Remaining valid codes'),
|
||||
'$recovery_codes_count' => RecoveryCode::countValidForUser(DI::userSession()->getLocalUserId()),
|
||||
'$recovery_codes_message' => DI::l10n()->t('<p>These one-use codes can replace an authenticator app code in case you have lost access to it.</p>'),
|
||||
'$recovery_codes_title' => $this->t('Recovery codes'),
|
||||
'$recovery_codes_remaining' => $this->t('Remaining valid codes'),
|
||||
'$recovery_codes_count' => RecoveryCode::countValidForUser($this->session->getLocalUserId()),
|
||||
'$recovery_codes_message' => $this->t('<p>These one-use codes can replace an authenticator app code in case you have lost access to it.</p>'),
|
||||
|
||||
'$app_specific_passwords_title' => DI::l10n()->t('App-specific passwords'),
|
||||
'$app_specific_passwords_remaining' => DI::l10n()->t('Generated app-specific passwords'),
|
||||
'$app_specific_passwords_count' => AppSpecificPassword::countForUser(DI::userSession()->getLocalUserId()),
|
||||
'$app_specific_passwords_message' => DI::l10n()->t('<p>These randomly generated passwords allow you to authenticate on apps not supporting two-factor authentication.</p>'),
|
||||
'$app_specific_passwords_title' => $this->t('App-specific passwords'),
|
||||
'$app_specific_passwords_remaining' => $this->t('Generated app-specific passwords'),
|
||||
'$app_specific_passwords_count' => AppSpecificPassword::countForUser($this->session->getLocalUserId()),
|
||||
'$app_specific_passwords_message' => $this->t('<p>These randomly generated passwords allow you to authenticate on apps not supporting two-factor authentication.</p>'),
|
||||
|
||||
'$action_title' => DI::l10n()->t('Actions'),
|
||||
'$password' => ['password', DI::l10n()->t('Current password:'), '', DI::l10n()->t('You need to provide your current password to change two-factor authentication settings.'), DI::l10n()->t('Required'), 'autofocus'],
|
||||
'$enable_label' => DI::l10n()->t('Enable two-factor authentication'),
|
||||
'$disable_label' => DI::l10n()->t('Disable two-factor authentication'),
|
||||
'$recovery_codes_label' => DI::l10n()->t('Show recovery codes'),
|
||||
'$app_specific_passwords_label' => DI::l10n()->t('Manage app-specific passwords'),
|
||||
'$trusted_browsers_label' => DI::l10n()->t('Manage trusted browsers'),
|
||||
'$configure_label' => DI::l10n()->t('Finish app configuration'),
|
||||
'$action_title' => $this->t('Actions'),
|
||||
'$password' => ['password', $this->t('Current password:'), '', $this->t('You need to provide your current password to change two-factor authentication settings.'), $this->t('Required'), 'autofocus'],
|
||||
'$enable_label' => $this->t('Enable two-factor authentication'),
|
||||
'$disable_label' => $this->t('Disable two-factor authentication'),
|
||||
'$recovery_codes_label' => $this->t('Show recovery codes'),
|
||||
'$app_specific_passwords_label' => $this->t('Manage app-specific passwords'),
|
||||
'$trusted_browsers_label' => $this->t('Manage trusted browsers'),
|
||||
'$configure_label' => $this->t('Finish app configuration'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue