2019-05-13 01:36:09 -04:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Friendica\Module\TwoFactor;
|
|
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
2019-12-08 22:45:34 +01:00
|
|
|
|
use Friendica\App\Authentication;
|
2019-05-13 01:36:09 -04:00
|
|
|
|
use Friendica\Core\L10n;
|
|
|
|
|
use Friendica\Core\Renderer;
|
|
|
|
|
use Friendica\Core\Session;
|
2019-07-22 07:41:01 -04:00
|
|
|
|
use Friendica\Model\TwoFactor\RecoveryCode;
|
2019-05-13 01:36:09 -04: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 01:36:09 -04:00
|
|
|
|
{
|
|
|
|
|
if (!local_user()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 21:48:54 +00:00
|
|
|
|
public static function post(array $parameters = [])
|
2019-05-13 01:36:09 -04:00
|
|
|
|
{
|
|
|
|
|
if (!local_user()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 09:20:32 -04:00
|
|
|
|
if (($_POST['action'] ?? '') == 'recover') {
|
2019-05-13 01:36:09 -04:00
|
|
|
|
self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
|
|
|
|
|
|
|
|
|
|
$a = self::getApp();
|
|
|
|
|
|
2019-10-15 09:20:32 -04:00
|
|
|
|
$recovery_code = $_POST['recovery_code'] ?? '';
|
2019-05-13 01:36:09 -04:00
|
|
|
|
|
2019-07-22 07:41:01 -04:00
|
|
|
|
if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
|
|
|
|
|
RecoveryCode::markUsedForUser(local_user(), $recovery_code);
|
2019-05-13 01:36:09 -04:00
|
|
|
|
Session::set('2fa', true);
|
2019-07-22 07:41:01 -04:00
|
|
|
|
notice(L10n::t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
|
2019-05-13 01:36:09 -04:00
|
|
|
|
|
|
|
|
|
// Resume normal login workflow
|
2019-12-03 22:29:37 +01:00
|
|
|
|
/** @var Authentication $authentication */
|
|
|
|
|
$authentication = self::getClass(Authentication::class);
|
|
|
|
|
$authentication->setForUser($a, $a->user, true, true);
|
2019-05-13 01:36:09 -04:00
|
|
|
|
} else {
|
|
|
|
|
notice(L10n::t('Invalid code, please retry.'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 21:48:54 +00:00
|
|
|
|
public static function content(array $parameters = [])
|
2019-05-13 01:36:09 -04:00
|
|
|
|
{
|
|
|
|
|
if (!local_user()) {
|
|
|
|
|
self::getApp()->internalRedirect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Already authenticated with 2FA token
|
|
|
|
|
if (Session::get('2fa')) {
|
|
|
|
|
self::getApp()->internalRedirect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
|
|
|
|
|
'$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
|
2019-05-13 13:31:08 -04:00
|
|
|
|
|
|
|
|
|
'$title' => L10n::t('Two-factor recovery'),
|
|
|
|
|
'$message' => L10n::t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
|
2019-05-13 01:36:09 -04:00
|
|
|
|
'$recovery_message' => L10n::t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
|
2019-05-13 13:31:08 -04:00
|
|
|
|
'$recovery_code' => ['recovery_code', L10n::t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
|
|
|
|
|
'$recovery_label' => L10n::t('Submit recovery code and complete login'),
|
2019-05-13 01:36:09 -04:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|