3 * @copyright Copyright (C) 2010-2021, 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\Security\Authentication;
31 use Friendica\Security\TwoFactor\Model\RecoveryCode;
34 * // Page 1a: Recovery code verification
36 * @package Friendica\Module\TwoFactor
38 class Recovery extends BaseModule
40 /** @var IHandleSessions */
44 /** @var App\BaseURL */
46 /** @var Authentication */
49 public function __construct(App $app, App\BaseURL $baseUrl, Authentication $auth, IHandleSessions $session, L10n $l10n, array $parameters = [])
51 parent::__construct($l10n, $parameters);
54 $this->baseUrl = $baseUrl;
56 $this->session = $session;
59 public function post()
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 public function content(): 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'),