3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Security\TwoFactor;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleSessions;
29 use Friendica\Model\User;
30 use Friendica\Module\Response;
31 use Friendica\Security\Authentication;
32 use Friendica\Security\TwoFactor\Model\RecoveryCode;
33 use Friendica\Util\Profiler;
34 use Psr\Log\LoggerInterface;
37 * // Page 1a: Recovery code verification
39 * @package Friendica\Module\TwoFactor
41 class Recovery extends BaseModule
43 /** @var IHandleSessions */
47 /** @var Authentication */
50 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 = [])
52 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
56 $this->session = $session;
59 protected function post(array $request = [])
65 if (($_POST['action'] ?? '') == 'recover') {
66 self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
68 $recovery_code = $_POST['recovery_code'] ?? '';
70 if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
71 RecoveryCode::markUsedForUser(local_user(), $recovery_code);
72 $this->session->set('2fa', true);
73 info($this->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
75 $this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true);
77 notice($this->t('Invalid code, please retry.'));
82 protected function content(array $request = []): string
85 $this->baseUrl->redirect();
88 // Already authenticated with 2FA token
89 if ($this->session->get('2fa')) {
90 $this->baseUrl->redirect();
93 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
94 '$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
96 '$title' => $this->t('Two-factor recovery'),
97 '$message' => $this->t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
98 '$recovery_message' => $this->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
99 '$recovery_code' => ['recovery_code', $this->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
100 '$recovery_label' => $this->t('Submit recovery code and complete login'),