3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Security\TwoFactor\Repository;
24 use Friendica\Security\TwoFactor;
25 use Friendica\Database\Database;
26 use Friendica\Security\TwoFactor\Exception\TrustedBrowserNotFoundException;
27 use Friendica\Security\TwoFactor\Exception\TrustedBrowserPersistenceException;
28 use Psr\Log\LoggerInterface;
35 /** @var LoggerInterface */
38 /** @var TwoFactor\Factory\TrustedBrowser */
41 protected static $table_name = '2fa_trusted_browser';
43 public function __construct(Database $database, LoggerInterface $logger, TwoFactor\Factory\TrustedBrowser $factory = null)
45 $this->db = $database;
46 $this->logger = $logger;
47 $this->factory = $factory ?? new TwoFactor\Factory\TrustedBrowser($logger);
51 * @param string $cookie_hash
53 * @return TwoFactor\Model\TrustedBrowser|null
55 * @throws TrustedBrowserPersistenceException
56 * @throws TrustedBrowserNotFoundException
58 public function selectOneByHash(string $cookie_hash): TwoFactor\Model\TrustedBrowser
61 $fields = $this->db->selectFirst(self::$table_name, [], ['cookie_hash' => $cookie_hash]);
62 } catch (\Exception $exception) {
63 throw new TrustedBrowserPersistenceException(sprintf('Internal server error when retrieving cookie hash \'%s\'', $cookie_hash));
65 if (!$this->db->isResult($fields)) {
66 throw new TrustedBrowserNotFoundException(sprintf('Cookie hash \'%s\' not found', $cookie_hash));
69 return $this->factory->createFromTableRow($fields);
75 * @return TwoFactor\Collection\TrustedBrowsers
77 * @throws TrustedBrowserPersistenceException
79 public function selectAllByUid(int $uid): TwoFactor\Collection\TrustedBrowsers
82 $rows = $this->db->selectToArray(self::$table_name, [], ['uid' => $uid]);
84 $trustedBrowsers = [];
85 foreach ($rows as $fields) {
86 $trustedBrowsers[] = $this->factory->createFromTableRow($fields);
88 return new TwoFactor\Collection\TrustedBrowsers($trustedBrowsers);
90 } catch (\Exception $exception) {
91 throw new TrustedBrowserPersistenceException(sprintf('selection for uid \'%s\' wasn\'t successful.', $uid));
96 * @param TwoFactor\Model\TrustedBrowser $trustedBrowser
100 * @throws TrustedBrowserPersistenceException
102 public function save(TwoFactor\Model\TrustedBrowser $trustedBrowser): bool
105 return $this->db->insert(self::$table_name, $trustedBrowser->toArray(), $this->db::INSERT_UPDATE);
106 } catch (\Exception $exception) {
107 throw new TrustedBrowserPersistenceException(sprintf('Couldn\'t save trusted Browser with cookie_hash \'%s\'', $trustedBrowser->cookie_hash));
112 * @param TwoFactor\Model\TrustedBrowser $trustedBrowser
116 * @throws TrustedBrowserPersistenceException
118 public function remove(TwoFactor\Model\TrustedBrowser $trustedBrowser): bool
121 return $this->db->delete(self::$table_name, ['cookie_hash' => $trustedBrowser->cookie_hash]);
122 } catch (\Exception $exception) {
123 throw new TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browser with cookie hash \'%s\'', $trustedBrowser->cookie_hash));
128 * @param int $local_user
129 * @param string $cookie_hash
133 * @throws TrustedBrowserPersistenceException
135 public function removeForUser(int $local_user, string $cookie_hash): bool
138 return $this->db->delete(self::$table_name, ['cookie_hash' => $cookie_hash, 'uid' => $local_user]);
139 } catch (\Exception $exception) {
140 throw new TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browser for user \'%s\' and cookie hash \'%s\'', $local_user, $cookie_hash));
145 * @param int $local_user
148 public function removeAllForUser(int $local_user): bool
151 return $this->db->delete(self::$table_name, ['uid' => $local_user]);
152 } catch (\Exception $exception) {
153 throw new TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browsers for user \'%s\'', $local_user));