2019-05-13 01:36:09 -04:00
|
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
|
/**
|
2022-01-02 08:27:47 +01:00
|
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
|
*
|
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2019-05-13 01:36:09 -04:00
|
|
|
|
|
2019-12-27 22:19:28 +01:00
|
|
|
|
namespace Friendica\Module\Security\TwoFactor;
|
2019-05-13 01:36:09 -04:00
|
|
|
|
|
2021-11-19 20:18:48 +01:00
|
|
|
|
use Friendica\App;
|
2019-05-13 01:36:09 -04:00
|
|
|
|
use Friendica\BaseModule;
|
2021-11-19 20:18:48 +01:00
|
|
|
|
use Friendica\Core\L10n;
|
2019-05-13 01:36:09 -04:00
|
|
|
|
use Friendica\Core\Renderer;
|
2021-11-19 20:18:48 +01:00
|
|
|
|
use Friendica\Core\Session\Capability\IHandleSessions;
|
2021-08-08 19:30:21 +00:00
|
|
|
|
use Friendica\Model\User;
|
2021-11-21 20:06:36 +01:00
|
|
|
|
use Friendica\Module\Response;
|
2021-11-19 20:18:48 +01:00
|
|
|
|
use Friendica\Security\Authentication;
|
2021-01-18 22:53:06 -05:00
|
|
|
|
use Friendica\Security\TwoFactor\Model\RecoveryCode;
|
2021-11-20 15:38:03 +01:00
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2019-05-13 01:36:09 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* // Page 1a: Recovery code verification
|
|
|
|
|
*
|
|
|
|
|
* @package Friendica\Module\TwoFactor
|
|
|
|
|
*/
|
|
|
|
|
class Recovery extends BaseModule
|
|
|
|
|
{
|
2021-11-19 20:18:48 +01:00
|
|
|
|
/** @var IHandleSessions */
|
|
|
|
|
protected $session;
|
|
|
|
|
/** @var App */
|
|
|
|
|
protected $app;
|
|
|
|
|
/** @var Authentication */
|
|
|
|
|
protected $auth;
|
|
|
|
|
|
2021-11-21 20:06:36 +01:00
|
|
|
|
public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Authentication $auth, IHandleSessions $session, array $server, array $parameters = [])
|
2019-05-13 01:36:09 -04:00
|
|
|
|
{
|
2021-11-21 20:06:36 +01:00
|
|
|
|
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
|
2021-11-19 20:18:48 +01:00
|
|
|
|
|
|
|
|
|
$this->app = $app;
|
|
|
|
|
$this->auth = $auth;
|
|
|
|
|
$this->session = $session;
|
2019-05-13 01:36:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 13:44:42 +01:00
|
|
|
|
protected function post(array $request = [])
|
2019-05-13 01:36:09 -04:00
|
|
|
|
{
|
|
|
|
|
if (!local_user()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 09:20:32 -04:00
|
|
|
|
if (($_POST['action'] ?? '') == 'recover') {
|
2019-05-13 01:36:09 -04:00
|
|
|
|
self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
|
|
|
|
|
|
2019-10-15 09:20:32 -04:00
|
|
|
|
$recovery_code = $_POST['recovery_code'] ?? '';
|
2019-05-13 01:36:09 -04:00
|
|
|
|
|
2019-07-22 07:41:01 -04:00
|
|
|
|
if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
|
|
|
|
|
RecoveryCode::markUsedForUser(local_user(), $recovery_code);
|
2021-11-19 20:18:48 +01:00
|
|
|
|
$this->session->set('2fa', true);
|
|
|
|
|
info($this->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
|
2019-05-13 01:36:09 -04:00
|
|
|
|
|
2021-11-19 20:18:48 +01:00
|
|
|
|
$this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true);
|
2022-08-01 11:38:54 -04:00
|
|
|
|
|
|
|
|
|
$this->baseUrl->redirect($this->session->pop('return_path', ''));
|
2019-05-13 01:36:09 -04:00
|
|
|
|
} else {
|
2021-11-19 20:18:48 +01:00
|
|
|
|
notice($this->t('Invalid code, please retry.'));
|
2019-05-13 01:36:09 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-20 15:38:03 +01:00
|
|
|
|
protected function content(array $request = []): string
|
2019-05-13 01:36:09 -04:00
|
|
|
|
{
|
|
|
|
|
if (!local_user()) {
|
2021-11-19 20:18:48 +01:00
|
|
|
|
$this->baseUrl->redirect();
|
2019-05-13 01:36:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Already authenticated with 2FA token
|
2021-11-19 20:18:48 +01:00
|
|
|
|
if ($this->session->get('2fa')) {
|
|
|
|
|
$this->baseUrl->redirect();
|
2019-05-13 01:36:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
|
|
|
|
|
'$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
|
2019-05-13 13:31:08 -04:00
|
|
|
|
|
2021-11-19 20:18:48 +01:00
|
|
|
|
'$title' => $this->t('Two-factor recovery'),
|
|
|
|
|
'$message' => $this->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->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
|
|
|
|
|
'$recovery_code' => ['recovery_code', $this->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
|
|
|
|
|
'$recovery_label' => $this->t('Submit recovery code and complete login'),
|
2019-05-13 01:36:09 -04:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|