2019-05-13 05:36:09 +00:00
|
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
|
/**
|
2021-03-29 06:40:20 +00:00
|
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 14:45:36 +00: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 05:36:09 +00:00
|
|
|
|
|
2019-12-27 21:19:28 +00:00
|
|
|
|
namespace Friendica\Module\Security\TwoFactor;
|
2019-05-13 05:36:09 +00:00
|
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
|
|
|
|
use Friendica\Core\Renderer;
|
|
|
|
|
use Friendica\Core\Session;
|
2019-12-15 21:34:11 +00:00
|
|
|
|
use Friendica\DI;
|
2021-01-19 03:53:06 +00:00
|
|
|
|
use Friendica\Security\TwoFactor\Model\RecoveryCode;
|
2019-05-13 05:36:09 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* // Page 1a: Recovery code verification
|
|
|
|
|
*
|
|
|
|
|
* @package Friendica\Module\TwoFactor
|
|
|
|
|
*/
|
|
|
|
|
class Recovery extends BaseModule
|
|
|
|
|
{
|
2019-11-05 21:48:54 +00:00
|
|
|
|
public static function init(array $parameters = [])
|
2019-05-13 05:36:09 +00:00
|
|
|
|
{
|
|
|
|
|
if (!local_user()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 21:48:54 +00:00
|
|
|
|
public static function post(array $parameters = [])
|
2019-05-13 05:36:09 +00:00
|
|
|
|
{
|
|
|
|
|
if (!local_user()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 13:20:32 +00:00
|
|
|
|
if (($_POST['action'] ?? '') == 'recover') {
|
2019-05-13 05:36:09 +00:00
|
|
|
|
self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
|
|
|
|
|
|
2019-12-15 21:34:11 +00:00
|
|
|
|
$a = DI::app();
|
2019-05-13 05:36:09 +00:00
|
|
|
|
|
2019-10-15 13:20:32 +00:00
|
|
|
|
$recovery_code = $_POST['recovery_code'] ?? '';
|
2019-05-13 05:36:09 +00:00
|
|
|
|
|
2019-07-22 11:41:01 +00:00
|
|
|
|
if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
|
|
|
|
|
RecoveryCode::markUsedForUser(local_user(), $recovery_code);
|
2019-05-13 05:36:09 +00:00
|
|
|
|
Session::set('2fa', true);
|
2020-09-07 10:17:42 +00:00
|
|
|
|
info(DI::l10n()->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
|
2019-05-13 05:36:09 +00:00
|
|
|
|
|
2019-12-15 22:28:01 +00:00
|
|
|
|
DI::auth()->setForUser($a, $a->user, true, true);
|
2019-05-13 05:36:09 +00:00
|
|
|
|
} else {
|
2020-01-18 19:52:34 +00:00
|
|
|
|
notice(DI::l10n()->t('Invalid code, please retry.'));
|
2019-05-13 05:36:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 21:48:54 +00:00
|
|
|
|
public static function content(array $parameters = [])
|
2019-05-13 05:36:09 +00:00
|
|
|
|
{
|
|
|
|
|
if (!local_user()) {
|
2019-12-15 23:28:31 +00:00
|
|
|
|
DI::baseUrl()->redirect();
|
2019-05-13 05:36:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Already authenticated with 2FA token
|
|
|
|
|
if (Session::get('2fa')) {
|
2019-12-15 23:28:31 +00:00
|
|
|
|
DI::baseUrl()->redirect();
|
2019-05-13 05:36:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
|
|
|
|
|
'$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
|
2019-05-13 17:31:08 +00:00
|
|
|
|
|
2020-01-18 19:52:34 +00:00
|
|
|
|
'$title' => DI::l10n()->t('Two-factor recovery'),
|
|
|
|
|
'$message' => DI::l10n()->t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
|
|
|
|
|
'$recovery_message' => DI::l10n()->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
|
|
|
|
|
'$recovery_code' => ['recovery_code', DI::l10n()->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
|
|
|
|
|
'$recovery_label' => DI::l10n()->t('Submit recovery code and complete login'),
|
2019-05-13 05:36:09 +00:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|