2021-01-19 04:32:28 +00:00
< ? php
2021-10-22 07:29:41 +00:00
/**
2023-01-01 14:36:24 +00:00
* @ copyright Copyright ( C ) 2010 - 2023 , the Friendica project
2021-10-22 07:29:41 +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 />.
*
*/
2021-01-19 04:32:28 +00:00
namespace Friendica\Security\TwoFactor\Repository ;
2022-07-07 19:33:41 +00:00
use Friendica\Security\TwoFactor ;
2021-01-19 04:32:28 +00:00
use Friendica\Database\Database ;
2022-07-09 09:41:36 +00:00
use Friendica\Security\TwoFactor\Exception\TrustedBrowserNotFoundException ;
use Friendica\Security\TwoFactor\Exception\TrustedBrowserPersistenceException ;
2021-01-19 04:32:28 +00:00
use Psr\Log\LoggerInterface ;
class TrustedBrowser
{
/** @var Database */
protected $db ;
/** @var LoggerInterface */
protected $logger ;
2022-07-07 19:33:41 +00:00
/** @var TwoFactor\Factory\TrustedBrowser */
2021-01-19 04:32:28 +00:00
protected $factory ;
protected static $table_name = '2fa_trusted_browser' ;
2022-07-07 19:33:41 +00:00
public function __construct ( Database $database , LoggerInterface $logger , TwoFactor\Factory\TrustedBrowser $factory = null )
2021-01-19 04:32:28 +00:00
{
2022-07-07 19:33:41 +00:00
$this -> db = $database ;
$this -> logger = $logger ;
$this -> factory = $factory ? ? new TwoFactor\Factory\TrustedBrowser ( $logger );
2021-01-19 04:32:28 +00:00
}
/**
* @ param string $cookie_hash
2022-07-07 19:33:41 +00:00
*
* @ return TwoFactor\Model\TrustedBrowser | null
*
2022-07-09 09:41:36 +00:00
* @ throws TrustedBrowserPersistenceException
* @ throws TrustedBrowserNotFoundException
2021-01-19 04:32:28 +00:00
*/
2022-07-07 19:33:41 +00:00
public function selectOneByHash ( string $cookie_hash ) : TwoFactor\Model\TrustedBrowser
2021-01-19 04:32:28 +00:00
{
2022-07-07 19:33:41 +00:00
try {
$fields = $this -> db -> selectFirst ( self :: $table_name , [], [ 'cookie_hash' => $cookie_hash ]);
} catch ( \Exception $exception ) {
2022-07-09 09:41:36 +00:00
throw new TrustedBrowserPersistenceException ( sprintf ( 'Internal server error when retrieving cookie hash \'%s\'' , $cookie_hash ));
2022-07-07 19:33:41 +00:00
}
2021-01-19 04:32:28 +00:00
if ( ! $this -> db -> isResult ( $fields )) {
2022-07-09 09:41:36 +00:00
throw new TrustedBrowserNotFoundException ( sprintf ( 'Cookie hash \'%s\' not found' , $cookie_hash ));
2021-01-19 04:32:28 +00:00
}
return $this -> factory -> createFromTableRow ( $fields );
}
2022-07-07 19:33:41 +00:00
/**
* @ param int $uid
*
* @ return TwoFactor\Collection\TrustedBrowsers
*
2022-07-09 09:41:36 +00:00
* @ throws TrustedBrowserPersistenceException
2022-07-07 19:33:41 +00:00
*/
public function selectAllByUid ( int $uid ) : TwoFactor\Collection\TrustedBrowsers
2021-01-19 04:32:28 +00:00
{
2022-07-07 19:33:41 +00:00
try {
$rows = $this -> db -> selectToArray ( self :: $table_name , [], [ 'uid' => $uid ]);
2021-01-19 04:32:28 +00:00
2022-07-07 19:33:41 +00:00
$trustedBrowsers = [];
foreach ( $rows as $fields ) {
$trustedBrowsers [] = $this -> factory -> createFromTableRow ( $fields );
}
return new TwoFactor\Collection\TrustedBrowsers ( $trustedBrowsers );
2021-01-19 04:32:28 +00:00
2022-07-07 19:33:41 +00:00
} catch ( \Exception $exception ) {
2022-07-09 09:41:36 +00:00
throw new TrustedBrowserPersistenceException ( sprintf ( 'selection for uid \'%s\' wasn\'t successful.' , $uid ));
2022-07-07 19:33:41 +00:00
}
2021-01-19 04:32:28 +00:00
}
/**
2022-07-07 19:33:41 +00:00
* @ param TwoFactor\Model\TrustedBrowser $trustedBrowser
*
2021-01-19 04:32:28 +00:00
* @ return bool
2022-07-07 19:33:41 +00:00
*
2022-07-09 09:41:36 +00:00
* @ throws TrustedBrowserPersistenceException
2021-01-19 04:32:28 +00:00
*/
2022-07-07 19:33:41 +00:00
public function save ( TwoFactor\Model\TrustedBrowser $trustedBrowser ) : bool
2021-01-19 04:32:28 +00:00
{
2022-07-07 19:33:41 +00:00
try {
return $this -> db -> insert ( self :: $table_name , $trustedBrowser -> toArray (), $this -> db :: INSERT_UPDATE );
} catch ( \Exception $exception ) {
2022-07-09 09:41:36 +00:00
throw new TrustedBrowserPersistenceException ( sprintf ( 'Couldn\'t save trusted Browser with cookie_hash \'%s\'' , $trustedBrowser -> cookie_hash ));
2022-07-07 19:33:41 +00:00
}
2021-01-19 04:32:28 +00:00
}
/**
2022-07-07 19:33:41 +00:00
* @ param TwoFactor\Model\TrustedBrowser $trustedBrowser
*
2021-01-19 04:32:28 +00:00
* @ return bool
2022-07-07 19:33:41 +00:00
*
2022-07-09 09:41:36 +00:00
* @ throws TrustedBrowserPersistenceException
2021-01-19 04:32:28 +00:00
*/
2022-07-07 19:33:41 +00:00
public function remove ( TwoFactor\Model\TrustedBrowser $trustedBrowser ) : bool
2021-01-19 04:32:28 +00:00
{
2022-07-07 19:33:41 +00:00
try {
return $this -> db -> delete ( self :: $table_name , [ 'cookie_hash' => $trustedBrowser -> cookie_hash ]);
} catch ( \Exception $exception ) {
2022-07-09 09:41:36 +00:00
throw new TrustedBrowserPersistenceException ( sprintf ( 'Couldn\'t delete trusted Browser with cookie hash \'%s\'' , $trustedBrowser -> cookie_hash ));
2022-07-07 19:33:41 +00:00
}
2021-01-19 04:32:28 +00:00
}
/**
* @ param int $local_user
* @ param string $cookie_hash
2022-07-07 19:33:41 +00:00
*
2021-01-19 04:32:28 +00:00
* @ return bool
2022-07-07 19:33:41 +00:00
*
2022-07-09 09:41:36 +00:00
* @ throws TrustedBrowserPersistenceException
2021-01-19 04:32:28 +00:00
*/
public function removeForUser ( int $local_user , string $cookie_hash ) : bool
{
2022-07-07 19:33:41 +00:00
try {
return $this -> db -> delete ( self :: $table_name , [ 'cookie_hash' => $cookie_hash , 'uid' => $local_user ]);
} catch ( \Exception $exception ) {
2022-07-09 09:41:36 +00:00
throw new TrustedBrowserPersistenceException ( sprintf ( 'Couldn\'t delete trusted Browser for user \'%s\' and cookie hash \'%s\'' , $local_user , $cookie_hash ));
2022-07-07 19:33:41 +00:00
}
2021-01-19 04:32:28 +00:00
}
/**
* @ param int $local_user
* @ return bool
*/
public function removeAllForUser ( int $local_user ) : bool
{
2022-07-07 19:33:41 +00:00
try {
return $this -> db -> delete ( self :: $table_name , [ 'uid' => $local_user ]);
} catch ( \Exception $exception ) {
2022-07-09 09:41:36 +00:00
throw new TrustedBrowserPersistenceException ( sprintf ( 'Couldn\'t delete trusted Browsers for user \'%s\'' , $local_user ));
2022-07-07 19:33:41 +00:00
}
2021-01-19 04:32:28 +00:00
}
}