1
0
Fork 0

Add trusted browser classes

- Added some tests
This commit is contained in:
Hypolite Petovan 2021-01-18 23:32:28 -05:00
commit 72bb3bce34
7 changed files with 356 additions and 0 deletions

View file

@ -0,0 +1,10 @@
<?php
namespace Friendica\Security\TwoFactor\Collection;
use Friendica\BaseCollection;
class TrustedBrowsers extends BaseCollection
{
}

View file

@ -0,0 +1,33 @@
<?php
namespace Friendica\Security\TwoFactor\Factory;
use Friendica\BaseFactory;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
class TrustedBrowser extends BaseFactory
{
public function createForUserWithUserAgent($uid, $userAgent): \Friendica\Security\TwoFactor\Model\TrustedBrowser
{
$trustedHash = Strings::getRandomHex();
return new \Friendica\Security\TwoFactor\Model\TrustedBrowser(
$trustedHash,
$uid,
$userAgent,
DateTimeFormat::utcNow()
);
}
public function createFromTableRow(array $row): \Friendica\Security\TwoFactor\Model\TrustedBrowser
{
return new \Friendica\Security\TwoFactor\Model\TrustedBrowser(
$row['cookie_hash'],
$row['uid'],
$row['user_agent'],
$row['created'],
$row['last_used']
);
}
}

View file

@ -0,0 +1,51 @@
<?php
namespace Friendica\Security\TwoFactor\Model;
use Friendica\BaseEntity;
use Friendica\Util\DateTimeFormat;
/**
* Class TrustedBrowser
*
*
* @property-read $cookie_hash
* @property-read $uid
* @property-read $user_agent
* @property-read $created
* @property-read $last_used
* @package Friendica\Model\TwoFactor
*/
class TrustedBrowser extends BaseEntity
{
protected $cookie_hash;
protected $uid;
protected $user_agent;
protected $created;
protected $last_used;
/**
* Please do not use this constructor directly, instead use one of the method of the TrustedBroser factory.
*
* @see \Friendica\Security\TwoFactor\Factory\TrustedBrowser
*
* @param string $cookie_hash
* @param int $uid
* @param string $user_agent
* @param string $created
* @param string|null $last_used
*/
public function __construct(string $cookie_hash, int $uid, string $user_agent, string $created, string $last_used = null)
{
$this->cookie_hash = $cookie_hash;
$this->uid = $uid;
$this->user_agent = $user_agent;
$this->created = $created;
$this->last_used = $last_used;
}
public function recordUse()
{
$this->last_used = DateTimeFormat::utcNow();
}
}

View file

@ -0,0 +1,98 @@
<?php
namespace Friendica\Security\TwoFactor\Repository;
use Friendica\Security\TwoFactor\Model;
use Friendica\Security\TwoFactor\Collection\TrustedBrowsers;
use Friendica\Database\Database;
use Friendica\Network\HTTPException\NotFoundException;
use Psr\Log\LoggerInterface;
class TrustedBrowser
{
/** @var Database */
protected $db;
/** @var LoggerInterface */
protected $logger;
/** @var \Friendica\Security\TwoFactor\Factory\TrustedBrowser */
protected $factory;
protected static $table_name = '2fa_trusted_browser';
public function __construct(Database $database, LoggerInterface $logger, \Friendica\Security\TwoFactor\Factory\TrustedBrowser $factory = null)
{
$this->db = $database;
$this->logger = $logger;
$this->factory = $factory ?? new \Friendica\Security\TwoFactor\Factory\TrustedBrowser($logger);
}
/**
* @param string $cookie_hash
* @return Model\TrustedBrowser|null
* @throws \Exception
*/
public function selectOneByHash(string $cookie_hash): Model\TrustedBrowser
{
$fields = $this->db->selectFirst(self::$table_name, [], ['cookie_hash' => $cookie_hash]);
if (!$this->db->isResult($fields)) {
throw new NotFoundException('');
}
return $this->factory->createFromTableRow($fields);
}
public function selectAllByUid(int $uid): TrustedBrowsers
{
$rows = $this->db->selectToArray(self::$table_name, [], ['uid' => $uid]);
$trustedBrowsers = [];
foreach ($rows as $fields) {
$trustedBrowsers[] = $this->factory->createFromTableRow($fields);
}
return new TrustedBrowsers($trustedBrowsers);
}
/**
* @param Model\TrustedBrowser $trustedBrowser
* @return bool
* @throws \Exception
*/
public function save(Model\TrustedBrowser $trustedBrowser): bool
{
return $this->db->insert(self::$table_name, $trustedBrowser->toArray(), $this->db::INSERT_UPDATE);
}
/**
* @param Model\TrustedBrowser $trustedBrowser
* @return bool
* @throws \Exception
*/
public function remove(Model\TrustedBrowser $trustedBrowser): bool
{
return $this->db->delete(self::$table_name, ['cookie_hash' => $trustedBrowser->cookie_hash]);
}
/**
* @param int $local_user
* @param string $cookie_hash
* @return bool
* @throws \Exception
*/
public function removeForUser(int $local_user, string $cookie_hash): bool
{
return $this->db->delete(self::$table_name, ['cookie_hash' => $cookie_hash,'uid' => $local_user]);
}
/**
* @param int $local_user
* @return bool
* @throws \Exception
*/
public function removeAllForUser(int $local_user): bool
{
return $this->db->delete(self::$table_name, ['uid' => $local_user]);
}
}