Replace Module::init() with Constructors

This commit is contained in:
Philipp 2021-11-17 21:32:57 +01:00
parent de6bb280b1
commit ce578a7745
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
33 changed files with 882 additions and 600 deletions

View file

@ -21,11 +21,13 @@
namespace Friendica\Module\Security\TwoFactor;
use Friendica\App;
use Friendica\BaseModule;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\DI;
use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Model\User;
use Friendica\Security\Authentication;
use Friendica\Security\TwoFactor\Model\RecoveryCode;
/**
@ -35,11 +37,23 @@ use Friendica\Security\TwoFactor\Model\RecoveryCode;
*/
class Recovery extends BaseModule
{
public function init()
/** @var IHandleSessions */
protected $session;
/** @var App */
protected $app;
/** @var App\BaseURL */
protected $baseUrl;
/** @var Authentication */
protected $auth;
public function __construct(App $app, App\BaseURL $baseUrl, Authentication $auth, IHandleSessions $session, L10n $l10n, array $parameters = [])
{
if (!local_user()) {
return;
}
parent::__construct($l10n, $parameters);
$this->app = $app;
$this->baseUrl = $baseUrl;
$this->auth = $auth;
$this->session = $session;
}
public function post()
@ -51,18 +65,16 @@ class Recovery extends BaseModule
if (($_POST['action'] ?? '') == 'recover') {
self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
$a = DI::app();
$recovery_code = $_POST['recovery_code'] ?? '';
if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
RecoveryCode::markUsedForUser(local_user(), $recovery_code);
Session::set('2fa', true);
info(DI::l10n()->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
$this->session->set('2fa', true);
info($this->l10n->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
DI::auth()->setForUser($a, User::getById($a->getLoggedInUserId()), true, true);
$this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true);
} else {
notice(DI::l10n()->t('Invalid code, please retry.'));
notice($this->l10n->t('Invalid code, please retry.'));
}
}
}
@ -70,22 +82,22 @@ class Recovery extends BaseModule
public function content(): string
{
if (!local_user()) {
DI::baseUrl()->redirect();
$this->baseUrl->redirect();
}
// Already authenticated with 2FA token
if (Session::get('2fa')) {
DI::baseUrl()->redirect();
if ($this->session->get('2fa')) {
$this->baseUrl->redirect();
}
return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
'$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
'$title' => DI::l10n()->t('Two-factor recovery'),
'$message' => DI::l10n()->t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
'$recovery_message' => DI::l10n()->t('Dont have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
'$recovery_code' => ['recovery_code', DI::l10n()->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
'$recovery_label' => DI::l10n()->t('Submit recovery code and complete login'),
'$title' => $this->l10n->t('Two-factor recovery'),
'$message' => $this->l10n->t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
'$recovery_message' => $this->l10n->t('Dont have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
'$recovery_code' => ['recovery_code', $this->l10n->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
'$recovery_label' => $this->l10n->t('Submit recovery code and complete login'),
]);
}
}