]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/TwoFactor/Recovery.php
Allow colon in password
[friendica.git] / src / Module / Security / TwoFactor / Recovery.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Security\TwoFactor;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\DI;
30 use Friendica\Model\User;
31 use Friendica\Module\Response;
32 use Friendica\Security\Authentication;
33 use Friendica\Security\TwoFactor\Model\RecoveryCode;
34 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
36
37 /**
38  * // Page 1a: Recovery code verification
39  *
40  * @package Friendica\Module\TwoFactor
41  */
42 class Recovery extends BaseModule
43 {
44         /** @var IHandleUserSessions */
45         protected $session;
46         /** @var App */
47         protected $app;
48         /** @var Authentication */
49         protected $auth;
50
51         public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Authentication $auth, IHandleUserSessions $session, array $server, array $parameters = [])
52         {
53                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
54
55                 $this->app     = $app;
56                 $this->auth    = $auth;
57                 $this->session = $session;
58         }
59
60         protected function post(array $request = [])
61         {
62                 if (!$this->session->getLocalUserId()) {
63                         return;
64                 }
65
66                 if (($_POST['action'] ?? '') == 'recover') {
67                         self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
68
69                         $recovery_code = $_POST['recovery_code'] ?? '';
70
71                         if (RecoveryCode::existsForUser($this->session->getLocalUserId(), $recovery_code)) {
72                                 RecoveryCode::markUsedForUser($this->session->getLocalUserId(), $recovery_code);
73                                 $this->session->set('2fa', true);
74                                 DI::sysmsg()->addInfo($this->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser($this->session->getLocalUserId())));
75
76                                 $this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true);
77
78                                 $this->baseUrl->redirect($this->session->pop('return_path', ''));
79                         } else {
80                                 DI::sysmsg()->addNotice($this->t('Invalid code, please retry.'));
81                         }
82                 }
83         }
84
85         protected function content(array $request = []): string
86         {
87                 if (!$this->session->getLocalUserId()) {
88                         $this->baseUrl->redirect();
89                 }
90
91                 // Already authenticated with 2FA token
92                 if ($this->session->get('2fa')) {
93                         $this->baseUrl->redirect();
94                 }
95
96                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
97                         '$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
98
99                         '$title'            => $this->t('Two-factor recovery'),
100                         '$message'          => $this->t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
101                         '$recovery_message' => $this->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
102                         '$recovery_code'    => ['recovery_code', $this->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
103                         '$recovery_label'   => $this->t('Submit recovery code and complete login'),
104                 ]);
105         }
106 }