2021-01-19 23:44:19 -05:00
< ? php
2022-01-02 10:49:50 +01:00
/**
* @ copyright Copyright ( C ) 2010 - 2022 , 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 />.
*
*/
2021-01-19 23:44:19 -05:00
namespace Friendica\Module\Settings\TwoFactor ;
2021-11-20 15:38:03 +01:00
use Friendica\App ;
2021-11-19 20:18:48 +01:00
use Friendica\Core\L10n ;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues ;
2021-01-19 23:44:19 -05:00
use Friendica\Core\Renderer ;
use Friendica\Module\BaseSettings ;
2021-11-21 20:06:36 +01:00
use Friendica\Module\Response ;
2021-01-19 23:44:19 -05:00
use Friendica\Security\TwoFactor ;
2021-11-21 22:51:21 -05:00
use Friendica\Util\DateTimeFormat ;
2021-11-20 15:38:03 +01:00
use Friendica\Util\Profiler ;
2021-01-19 23:44:19 -05:00
use Friendica\Util\Temporal ;
2021-11-20 15:38:03 +01:00
use Psr\Log\LoggerInterface ;
2021-01-19 23:44:19 -05:00
use UAParser\Parser ;
/**
* Manages users ' two - factor trusted browsers in the 2 fa_trusted_browsers table
*/
class Trusted extends BaseSettings
{
2021-11-19 20:18:48 +01:00
/** @var IManagePersonalConfigValues */
protected $pConfig ;
/** @var TwoFactor\Repository\TrustedBrowser */
protected $trustedBrowserRepo ;
2021-11-21 20:06:36 +01:00
public function __construct ( L10n $l10n , App\BaseURL $baseUrl , App\Arguments $args , LoggerInterface $logger , Profiler $profiler , Response $response , IManagePersonalConfigValues $pConfig , TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo , array $server , array $parameters = [])
2021-01-19 23:44:19 -05:00
{
2021-11-21 20:06:36 +01:00
parent :: __construct ( $l10n , $baseUrl , $args , $logger , $profiler , $response , $server , $parameters );
2021-11-19 20:18:48 +01:00
$this -> pConfig = $pConfig ;
$this -> trustedBrowserRepo = $trustedBrowserRepo ;
2021-01-19 23:44:19 -05:00
if ( ! local_user ()) {
return ;
}
2021-11-19 20:18:48 +01:00
$verified = $this -> pConfig -> get ( local_user (), '2fa' , 'verified' );
2021-01-19 23:44:19 -05:00
if ( ! $verified ) {
2021-11-19 20:18:48 +01:00
$this -> baseUrl -> redirect ( 'settings/2fa' );
2021-01-19 23:44:19 -05:00
}
if ( ! self :: checkFormSecurityToken ( 'settings_2fa_password' , 't' )) {
2021-11-19 20:18:48 +01:00
notice ( $this -> t ( 'Please enter your password to access this page.' ));
$this -> baseUrl -> redirect ( 'settings/2fa' );
2021-01-19 23:44:19 -05:00
}
}
2021-11-28 13:44:42 +01:00
protected function post ( array $request = [])
2021-01-19 23:44:19 -05:00
{
if ( ! local_user ()) {
return ;
}
if ( ! empty ( $_POST [ 'action' ])) {
self :: checkFormSecurityTokenRedirectOnError ( 'settings/2fa/trusted' , 'settings_2fa_trusted' );
switch ( $_POST [ 'action' ]) {
case 'remove_all' :
2021-11-19 20:18:48 +01:00
$this -> trustedBrowserRepo -> removeAllForUser ( local_user ());
info ( $this -> t ( 'Trusted browsers successfully removed.' ));
$this -> baseUrl -> redirect ( 'settings/2fa/trusted?t=' . self :: getFormSecurityToken ( 'settings_2fa_password' ));
2021-01-19 23:44:19 -05:00
break ;
}
}
if ( ! empty ( $_POST [ 'remove_id' ])) {
self :: checkFormSecurityTokenRedirectOnError ( 'settings/2fa/trusted' , 'settings_2fa_trusted' );
2021-11-19 20:18:48 +01:00
if ( $this -> trustedBrowserRepo -> removeForUser ( local_user (), $_POST [ 'remove_id' ])) {
info ( $this -> t ( 'Trusted browser successfully removed.' ));
2021-01-19 23:44:19 -05:00
}
2021-11-19 20:18:48 +01:00
$this -> baseUrl -> redirect ( 'settings/2fa/trusted?t=' . self :: getFormSecurityToken ( 'settings_2fa_password' ));
2021-01-19 23:44:19 -05:00
}
}
2021-11-20 15:38:03 +01:00
protected function content ( array $request = []) : string
2021-01-19 23:44:19 -05:00
{
2021-11-14 20:46:25 +01:00
parent :: content ();
2021-01-19 23:44:19 -05:00
2021-11-19 20:18:48 +01:00
$trustedBrowsers = $this -> trustedBrowserRepo -> selectAllByUid ( local_user ());
2021-01-19 23:44:19 -05:00
$parser = Parser :: create ();
$trustedBrowserDisplay = array_map ( function ( TwoFactor\Model\TrustedBrowser $trustedBrowser ) use ( $parser ) {
$dates = [
2022-01-22 21:10:20 -05:00
'created_ago' => Temporal :: getRelativeDate ( $trustedBrowser -> created ),
'created_utc' => DateTimeFormat :: utc ( $trustedBrowser -> created , 'c' ),
'created_local' => DateTimeFormat :: local ( $trustedBrowser -> created , 'r' ),
'last_used_ago' => Temporal :: getRelativeDate ( $trustedBrowser -> last_used ),
'last_used_utc' => $trustedBrowser -> last_used ? DateTimeFormat :: utc ( $trustedBrowser -> last_used , 'c' ) : '' ,
'last_used_local' => $trustedBrowser -> last_used ? DateTimeFormat :: local ( $trustedBrowser -> last_used , 'r' ) : '' ,
2021-01-19 23:44:19 -05:00
];
$result = $parser -> parse ( $trustedBrowser -> user_agent );
$uaData = [
'os' => $result -> os -> family ,
'device' => $result -> device -> family ,
'browser' => $result -> ua -> family ,
];
return $trustedBrowser -> toArray () + $dates + $uaData ;
}, $trustedBrowsers -> getArrayCopy ());
return Renderer :: replaceMacros ( Renderer :: getMarkupTemplate ( 'settings/twofactor/trusted_browsers.tpl' ), [
'$form_security_token' => self :: getFormSecurityToken ( 'settings_2fa_trusted' ),
'$password_security_token' => self :: getFormSecurityToken ( 'settings_2fa_password' ),
2021-11-19 20:18:48 +01:00
'$title' => $this -> t ( 'Two-factor Trusted Browsers' ),
'$message' => $this -> t ( 'Trusted browsers are individual browsers you chose to skip two-factor authentication to access Friendica. Please use this feature sparingly, as it can negate the benefit of two-factor authentication.' ),
'$device_label' => $this -> t ( 'Device' ),
'$os_label' => $this -> t ( 'OS' ),
'$browser_label' => $this -> t ( 'Browser' ),
'$created_label' => $this -> t ( 'Trusted' ),
'$last_used_label' => $this -> t ( 'Last Use' ),
'$remove_label' => $this -> t ( 'Remove' ),
'$remove_all_label' => $this -> t ( 'Remove All' ),
2021-01-19 23:44:19 -05:00
'$trusted_browsers' => $trustedBrowserDisplay ,
]);
}
}