2019-05-13 01:38:15 -04:00
< ? php
2020-02-09 09:45:36 -05:00
/**
2022-01-02 02:27:47 -05:00
* @ copyright Copyright ( C ) 2010 - 2022 , the Friendica project
2020-02-09 09:45:36 -05: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 01:38:15 -04:00
namespace Friendica\Module\Settings\TwoFactor ;
2021-11-20 09:38:03 -05:00
use Friendica\App ;
2021-11-19 14:18:48 -05:00
use Friendica\Core\L10n ;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues ;
2019-05-13 01:38:15 -04:00
use Friendica\Core\Renderer ;
2021-11-21 14:06:36 -05:00
use Friendica\Module\Response ;
2021-01-18 22:53:06 -05:00
use Friendica\Security\TwoFactor\Model\RecoveryCode ;
2020-01-22 23:14:14 -05:00
use Friendica\Module\BaseSettings ;
2019-12-27 16:19:28 -05:00
use Friendica\Module\Security\Login ;
2021-11-20 09:38:03 -05:00
use Friendica\Util\Profiler ;
use Psr\Log\LoggerInterface ;
2019-05-13 01:38:15 -04:00
/**
* // Page 3: 2FA enabled but not verified, show recovery codes
*
* @ package Friendica\Module\TwoFactor
*/
2020-01-22 23:14:14 -05:00
class Recovery extends BaseSettings
2019-05-13 01:38:15 -04:00
{
2021-11-19 14:18:48 -05:00
/** @var IManagePersonalConfigValues */
protected $pConfig ;
2021-11-21 14:06:36 -05:00
public function __construct ( L10n $l10n , App\BaseURL $baseUrl , App\Arguments $args , LoggerInterface $logger , Profiler $profiler , Response $response , IManagePersonalConfigValues $pConfig , array $server , array $parameters = [])
2019-05-13 01:38:15 -04:00
{
2021-11-21 14:06:36 -05:00
parent :: __construct ( $l10n , $baseUrl , $args , $logger , $profiler , $response , $server , $parameters );
2021-11-19 14:18:48 -05:00
$this -> pConfig = $pConfig ;
2019-05-13 01:38:15 -04:00
if ( ! local_user ()) {
return ;
}
2021-11-19 14:18:48 -05:00
$secret = $this -> pConfig -> get ( local_user (), '2fa' , 'secret' );
2019-05-13 01:38:15 -04:00
if ( ! $secret ) {
2021-11-19 14:18:48 -05:00
$this -> baseUrl -> redirect ( 'settings/2fa' );
2019-05-13 01:38:15 -04:00
}
if ( ! self :: checkFormSecurityToken ( 'settings_2fa_password' , 't' )) {
2021-11-19 14:18:48 -05:00
notice ( $this -> t ( 'Please enter your password to access this page.' ));
$this -> baseUrl -> redirect ( 'settings/2fa' );
2019-05-13 01:38:15 -04:00
}
}
2021-11-28 07:44:42 -05:00
protected function post ( array $request = [])
2019-05-13 01:38:15 -04:00
{
if ( ! local_user ()) {
return ;
}
if ( ! empty ( $_POST [ 'action' ])) {
self :: checkFormSecurityTokenRedirectOnError ( 'settings/2fa/recovery' , 'settings_2fa_recovery' );
if ( $_POST [ 'action' ] == 'regenerate' ) {
2019-07-22 07:41:01 -04:00
RecoveryCode :: regenerateForUser ( local_user ());
2021-11-19 14:18:48 -05:00
info ( $this -> t ( 'New recovery codes successfully generated.' ));
$this -> baseUrl -> redirect ( 'settings/2fa/recovery?t=' . self :: getFormSecurityToken ( 'settings_2fa_password' ));
2019-05-13 01:38:15 -04:00
}
}
}
2021-11-20 09:38:03 -05:00
protected function content ( array $request = []) : string
2019-05-13 01:38:15 -04:00
{
if ( ! local_user ()) {
return Login :: form ( 'settings/2fa/recovery' );
}
2021-11-14 14:46:25 -05:00
parent :: content ();
2019-05-13 01:38:15 -04:00
2019-07-22 07:41:01 -04:00
if ( ! RecoveryCode :: countValidForUser ( local_user ())) {
RecoveryCode :: generateForUser ( local_user ());
2019-05-13 01:38:15 -04:00
}
2019-07-22 07:41:01 -04:00
$recoveryCodes = RecoveryCode :: getListForUser ( local_user ());
2019-05-13 01:38:15 -04:00
2021-11-19 14:18:48 -05:00
$verified = $this -> pConfig -> get ( local_user (), '2fa' , 'verified' );
2019-05-13 01:38:15 -04:00
return Renderer :: replaceMacros ( Renderer :: getMarkupTemplate ( 'settings/twofactor/recovery.tpl' ), [
2019-05-13 13:31:08 -04:00
'$form_security_token' => self :: getFormSecurityToken ( 'settings_2fa_recovery' ),
2019-05-13 01:38:15 -04:00
'$password_security_token' => self :: getFormSecurityToken ( 'settings_2fa_password' ),
2019-05-13 13:31:08 -04:00
2021-11-19 14:18:48 -05:00
'$title' => $this -> t ( 'Two-factor recovery codes' ),
'$help_label' => $this -> t ( 'Help' ),
'$message' => $this -> t ( '<p>Recovery codes can be used to access your account in the event you lose access to your device and cannot receive two-factor authentication codes.</p><p><strong>Put these in a safe spot!</strong> If you lose your device and don’ t have the recovery codes you will lose access to your account.</p>' ),
2019-05-13 13:31:08 -04:00
'$recovery_codes' => $recoveryCodes ,
2021-11-19 14:18:48 -05:00
'$regenerate_message' => $this -> t ( 'When you generate new recovery codes, you must copy the new codes. Your old codes won’ t work anymore.' ),
'$regenerate_label' => $this -> t ( 'Generate new recovery codes' ),
2019-05-13 13:31:08 -04:00
'$verified' => $verified ,
2021-11-19 14:18:48 -05:00
'$verify_label' => $this -> t ( 'Next: Verification' ),
2019-05-13 01:38:15 -04:00
]);
}
}