103 lines
3.3 KiB
PHP
103 lines
3.3 KiB
PHP
<?php
|
||
/**
|
||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||
*
|
||
* @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/>.
|
||
*
|
||
*/
|
||
|
||
namespace Friendica\Module\Security\TwoFactor;
|
||
|
||
use Friendica\App;
|
||
use Friendica\BaseModule;
|
||
use Friendica\Core\L10n;
|
||
use Friendica\Core\Renderer;
|
||
use Friendica\Core\Session\Capability\IHandleSessions;
|
||
use Friendica\Model\User;
|
||
use Friendica\Security\Authentication;
|
||
use Friendica\Security\TwoFactor\Model\RecoveryCode;
|
||
|
||
/**
|
||
* // Page 1a: Recovery code verification
|
||
*
|
||
* @package Friendica\Module\TwoFactor
|
||
*/
|
||
class Recovery extends BaseModule
|
||
{
|
||
/** @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 = [])
|
||
{
|
||
parent::__construct($l10n, $parameters);
|
||
|
||
$this->app = $app;
|
||
$this->baseUrl = $baseUrl;
|
||
$this->auth = $auth;
|
||
$this->session = $session;
|
||
}
|
||
|
||
public function post()
|
||
{
|
||
if (!local_user()) {
|
||
return;
|
||
}
|
||
|
||
if (($_POST['action'] ?? '') == 'recover') {
|
||
self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
|
||
|
||
$recovery_code = $_POST['recovery_code'] ?? '';
|
||
|
||
if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
|
||
RecoveryCode::markUsedForUser(local_user(), $recovery_code);
|
||
$this->session->set('2fa', true);
|
||
info($this->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
|
||
|
||
$this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true);
|
||
} else {
|
||
notice($this->t('Invalid code, please retry.'));
|
||
}
|
||
}
|
||
}
|
||
|
||
public function content(): string
|
||
{
|
||
if (!local_user()) {
|
||
$this->baseUrl->redirect();
|
||
}
|
||
|
||
// Already authenticated with 2FA token
|
||
if ($this->session->get('2fa')) {
|
||
$this->baseUrl->redirect();
|
||
}
|
||
|
||
return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
|
||
'$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
|
||
|
||
'$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'),
|
||
]);
|
||
}
|
||
}
|