adhere use and type-hints :-)

This commit is contained in:
Philipp 2022-07-09 11:41:36 +02:00
parent b31c2f4ec1
commit e8fd495847
No known key found for this signature in database
GPG Key ID: 24A7501396EB5432
4 changed files with 22 additions and 18 deletions

View File

@ -33,6 +33,8 @@ use Friendica\Network\HTTPException\FoundException;
use Friendica\Network\HTTPException\MovedPermanentlyException;
use Friendica\Network\HTTPException\TemporaryRedirectException;
use Friendica\Security\Authentication;
use Friendica\Security\TwoFactor\Exception\TrustedBrowserNotFoundException;
use Friendica\Security\TwoFactor\Exception\TrustedBrowserPersistenceException;
use Friendica\Util\Profiler;
use Friendica\Security\TwoFactor;
use Psr\Log\LoggerInterface;
@ -92,7 +94,7 @@ class Trust extends BaseModule
if (!$this->cookie->set('2fa_cookie_hash', $trustedBrowser->cookie_hash)) {
notice($this->t('Couldn\'t save browser to Cookie.'));
};
} catch (TwoFactor\Exception\TrustedBrowserPersistenceException $exception) {
} catch (TrustedBrowserPersistenceException $exception) {
$this->logger->warning('Unexpected error when saving the trusted browser.', ['trustedBrowser' => $trustedBrowser, 'exception' => $exception]);
}
break;
@ -122,9 +124,9 @@ class Trust extends BaseModule
$this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true);
$this->baseUrl->redirect();
}
} catch (TwoFactor\Exception\TrustedBrowserNotFoundException $exception) {
} catch (TrustedBrowserNotFoundException $exception) {
$this->logger->notice('Trusted Browser of the cookie not found.', ['cookie_hash' => $this->cookie->get('trusted'), 'uid' => $this->app->getLoggedInUserId(), 'exception' => $exception]);
} catch (TwoFactor\Exception\TrustedBrowserPersistenceException $exception) {
} catch (TrustedBrowserPersistenceException $exception) {
$this->logger->warning('Unexpected persistence exception.', ['cookie_hash' => $this->cookie->get('trusted'), 'uid' => $this->app->getLoggedInUserId(), 'exception' => $exception]);
} catch (\Exception $exception) {
$this->logger->warning('Unexpected exception.', ['cookie_hash' => $this->cookie->get('trusted'), 'uid' => $this->app->getLoggedInUserId(), 'exception' => $exception]);

View File

@ -25,7 +25,7 @@ use Exception;
class TrustedBrowserNotFoundException extends \RuntimeException
{
public function __construct($message = '', Exception $previous = null)
public function __construct(string $message = '', Exception $previous = null)
{
parent::__construct($message, 404, $previous);
}

View File

@ -25,7 +25,7 @@ use Throwable;
class TrustedBrowserPersistenceException extends \RuntimeException
{
public function __construct($message = "", Throwable $previous = null)
public function __construct(string $message = "", Throwable $previous = null)
{
parent::__construct($message, 500, $previous);
}

View File

@ -23,6 +23,8 @@ namespace Friendica\Security\TwoFactor\Repository;
use Friendica\Security\TwoFactor;
use Friendica\Database\Database;
use Friendica\Security\TwoFactor\Exception\TrustedBrowserNotFoundException;
use Friendica\Security\TwoFactor\Exception\TrustedBrowserPersistenceException;
use Psr\Log\LoggerInterface;
class TrustedBrowser
@ -50,18 +52,18 @@ class TrustedBrowser
*
* @return TwoFactor\Model\TrustedBrowser|null
*
* @throws TwoFactor\Exception\TrustedBrowserPersistenceException
* @throws TwoFactor\Exception\TrustedBrowserNotFoundException
* @throws TrustedBrowserPersistenceException
* @throws TrustedBrowserNotFoundException
*/
public function selectOneByHash(string $cookie_hash): TwoFactor\Model\TrustedBrowser
{
try {
$fields = $this->db->selectFirst(self::$table_name, [], ['cookie_hash' => $cookie_hash]);
} catch (\Exception $exception) {
throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Internal server error when retrieving cookie hash \'%s\'', $cookie_hash));
throw new TrustedBrowserPersistenceException(sprintf('Internal server error when retrieving cookie hash \'%s\'', $cookie_hash));
}
if (!$this->db->isResult($fields)) {
throw new TwoFactor\Exception\TrustedBrowserNotFoundException(sprintf('Cookie hash \'%s\' not found', $cookie_hash));
throw new TrustedBrowserNotFoundException(sprintf('Cookie hash \'%s\' not found', $cookie_hash));
}
return $this->factory->createFromTableRow($fields);
@ -72,7 +74,7 @@ class TrustedBrowser
*
* @return TwoFactor\Collection\TrustedBrowsers
*
* @throws TwoFactor\Exception\TrustedBrowserPersistenceException
* @throws TrustedBrowserPersistenceException
*/
public function selectAllByUid(int $uid): TwoFactor\Collection\TrustedBrowsers
{
@ -86,7 +88,7 @@ class TrustedBrowser
return new TwoFactor\Collection\TrustedBrowsers($trustedBrowsers);
} catch (\Exception $exception) {
throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('selection for uid \'%s\' wasn\'t successful.', $uid));
throw new TrustedBrowserPersistenceException(sprintf('selection for uid \'%s\' wasn\'t successful.', $uid));
}
}
@ -95,14 +97,14 @@ class TrustedBrowser
*
* @return bool
*
* @throws TwoFactor\Exception\TrustedBrowserPersistenceException
* @throws TrustedBrowserPersistenceException
*/
public function save(TwoFactor\Model\TrustedBrowser $trustedBrowser): bool
{
try {
return $this->db->insert(self::$table_name, $trustedBrowser->toArray(), $this->db::INSERT_UPDATE);
} catch (\Exception $exception) {
throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Couldn\'t save trusted Browser with cookie_hash \'%s\'', $trustedBrowser->cookie_hash));
throw new TrustedBrowserPersistenceException(sprintf('Couldn\'t save trusted Browser with cookie_hash \'%s\'', $trustedBrowser->cookie_hash));
}
}
@ -111,14 +113,14 @@ class TrustedBrowser
*
* @return bool
*
* @throws TwoFactor\Exception\TrustedBrowserPersistenceException
* @throws TrustedBrowserPersistenceException
*/
public function remove(TwoFactor\Model\TrustedBrowser $trustedBrowser): bool
{
try {
return $this->db->delete(self::$table_name, ['cookie_hash' => $trustedBrowser->cookie_hash]);
} catch (\Exception $exception) {
throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browser with cookie hash \'%s\'', $trustedBrowser->cookie_hash));
throw new TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browser with cookie hash \'%s\'', $trustedBrowser->cookie_hash));
}
}
@ -128,14 +130,14 @@ class TrustedBrowser
*
* @return bool
*
* @throws TwoFactor\Exception\TrustedBrowserPersistenceException
* @throws TrustedBrowserPersistenceException
*/
public function removeForUser(int $local_user, string $cookie_hash): bool
{
try {
return $this->db->delete(self::$table_name, ['cookie_hash' => $cookie_hash, 'uid' => $local_user]);
} catch (\Exception $exception) {
throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browser for user \'%s\' and cookie hash \'%s\'', $local_user, $cookie_hash));
throw new TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browser for user \'%s\' and cookie hash \'%s\'', $local_user, $cookie_hash));
}
}
@ -148,7 +150,7 @@ class TrustedBrowser
try {
return $this->db->delete(self::$table_name, ['uid' => $local_user]);
} catch (\Exception $exception) {
throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browsers for user \'%s\'', $local_user));
throw new TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browsers for user \'%s\'', $local_user));
}
}
}