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,15 @@
namespace Friendica\Module\Security;
use Friendica\App\BaseURL;
use Friendica\BaseModule;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\System;
use Friendica\DI;
use Friendica\Model\Profile;
use Friendica\Model\User\Cookie;
use Friendica\Security\TwoFactor;
/**
@ -33,33 +37,55 @@ use Friendica\Security\TwoFactor;
*/
class Logout extends BaseModule
{
/** @var ICanCache */
protected $cache;
/** @var Cookie */
protected $cookie;
/** @var IHandleSessions */
protected $session;
/** @var BaseURL */
protected $baseUrl;
/** @var TwoFactor\Repository\TrustedBrowser */
protected $trustedBrowserRepo;
public function __construct(TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, ICanCache $cache, Cookie $cookie, IHandleSessions $session, BaseURL $baseUrl, L10n $l10n, array $parameters = [])
{
parent::__construct($l10n, $parameters);
$this->cache = $cache;
$this->cookie = $cookie;
$this->session = $session;
$this->baseUrl = $baseUrl;
$this->trustedBrowserRepo = $trustedBrowserRepo;
}
/**
* Process logout requests
*/
public function init()
public function rawContent()
{
$visitor_home = null;
if (remote_user()) {
$visitor_home = Profile::getMyURL();
DI::cache()->delete('zrlInit:' . $visitor_home);
$this->cache->delete('zrlInit:' . $visitor_home);
}
Hook::callAll("logging_out");
// Remove this trusted browser as it won't be able to be used ever again after the cookie is cleared
if (DI::cookie()->get('trusted')) {
$trustedBrowserRepository = new TwoFactor\Repository\TrustedBrowser(DI::dba(), DI::logger());
$trustedBrowserRepository->removeForUser(local_user(), DI::cookie()->get('trusted'));
if ($this->cookie->get('trusted')) {
$this->trustedBrowserRepo->removeForUser(local_user(), $this->cookie->get('trusted'));
}
DI::cookie()->clear();
DI::session()->clear();
$this->cookie->clear();
$this->session->clear();
if ($visitor_home) {
System::externalRedirect($visitor_home);
} else {
info(DI::l10n()->t('Logged out.'));
DI::baseUrl()->redirect();
info($this->l10n->t('Logged out.'));
$this->baseUrl->redirect();
}
}
}

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'),
]);
}
}