1
0

DbHandler.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Federation;
  29. use OC\Files\Filesystem;
  30. use OCP\DB\Exception as DBException;
  31. use OCP\DB\QueryBuilder\IQueryBuilder;
  32. use OCP\HintException;
  33. use OCP\IDBConnection;
  34. use OCP\IL10N;
  35. /**
  36. * Class DbHandler
  37. *
  38. * Handles all database calls for the federation app
  39. *
  40. * @todo Port to QBMapper
  41. *
  42. * @group DB
  43. * @package OCA\Federation
  44. */
  45. class DbHandler {
  46. private IDBConnection $connection;
  47. private IL10N $IL10N;
  48. private string $dbTable = 'trusted_servers';
  49. public function __construct(
  50. IDBConnection $connection,
  51. IL10N $il10n
  52. ) {
  53. $this->connection = $connection;
  54. $this->IL10N = $il10n;
  55. }
  56. /**
  57. * Add server to the list of trusted servers
  58. *
  59. * @throws HintException
  60. */
  61. public function addServer(string $url): int {
  62. $hash = $this->hash($url);
  63. $url = rtrim($url, '/');
  64. $query = $this->connection->getQueryBuilder();
  65. $query->insert($this->dbTable)
  66. ->values([
  67. 'url' => $query->createParameter('url'),
  68. 'url_hash' => $query->createParameter('url_hash'),
  69. ])
  70. ->setParameter('url', $url)
  71. ->setParameter('url_hash', $hash);
  72. $result = $query->executeStatement();
  73. if ($result) {
  74. return $query->getLastInsertId();
  75. }
  76. $message = 'Internal failure, Could not add trusted server: ' . $url;
  77. $message_t = $this->IL10N->t('Could not add server');
  78. throw new HintException($message, $message_t);
  79. return -1;
  80. }
  81. /**
  82. * Remove server from the list of trusted servers
  83. */
  84. public function removeServer(int $id): void {
  85. $query = $this->connection->getQueryBuilder();
  86. $query->delete($this->dbTable)
  87. ->where($query->expr()->eq('id', $query->createParameter('id')))
  88. ->setParameter('id', $id);
  89. $query->executeStatement();
  90. }
  91. /**
  92. * Get trusted server with given ID
  93. *
  94. * @return array{id: int, url: string, url_hash: string, token: ?string, shared_secret: ?string, status: int, sync_token: ?string}
  95. * @throws \Exception
  96. */
  97. public function getServerById(int $id): array {
  98. $query = $this->connection->getQueryBuilder();
  99. $query->select('*')->from($this->dbTable)
  100. ->where($query->expr()->eq('id', $query->createParameter('id')))
  101. ->setParameter('id', $id, IQueryBuilder::PARAM_INT);
  102. $qResult = $query->executeQuery();
  103. $result = $qResult->fetchAll();
  104. $qResult->closeCursor();
  105. if (empty($result)) {
  106. throw new \Exception('No Server found with ID: ' . $id);
  107. }
  108. return $result[0];
  109. }
  110. /**
  111. * Get all trusted servers
  112. *
  113. * @return list<array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string}>
  114. * @throws DBException
  115. */
  116. public function getAllServer(): array {
  117. $query = $this->connection->getQueryBuilder();
  118. $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])
  119. ->from($this->dbTable);
  120. $statement = $query->executeQuery();
  121. $result = $statement->fetchAll();
  122. $statement->closeCursor();
  123. return $result;
  124. }
  125. /**
  126. * Check if server already exists in the database table
  127. */
  128. public function serverExists(string $url): bool {
  129. $hash = $this->hash($url);
  130. $query = $this->connection->getQueryBuilder();
  131. $query->select('url')
  132. ->from($this->dbTable)
  133. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  134. ->setParameter('url_hash', $hash);
  135. $statement = $query->executeQuery();
  136. $result = $statement->fetchAll();
  137. $statement->closeCursor();
  138. return !empty($result);
  139. }
  140. /**
  141. * Write token to database. Token is used to exchange the secret
  142. */
  143. public function addToken(string $url, string $token): void {
  144. $hash = $this->hash($url);
  145. $query = $this->connection->getQueryBuilder();
  146. $query->update($this->dbTable)
  147. ->set('token', $query->createParameter('token'))
  148. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  149. ->setParameter('url_hash', $hash)
  150. ->setParameter('token', $token);
  151. $query->executeStatement();
  152. }
  153. /**
  154. * Get token stored in database
  155. * @throws \Exception
  156. */
  157. public function getToken(string $url): string {
  158. $hash = $this->hash($url);
  159. $query = $this->connection->getQueryBuilder();
  160. $query->select('token')->from($this->dbTable)
  161. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  162. ->setParameter('url_hash', $hash);
  163. $statement = $query->executeQuery();
  164. $result = $statement->fetch();
  165. $statement->closeCursor();
  166. if (!isset($result['token'])) {
  167. throw new \Exception('No token found for: ' . $url);
  168. }
  169. return $result['token'];
  170. }
  171. /**
  172. * Add shared Secret to database
  173. */
  174. public function addSharedSecret(string $url, string $sharedSecret): void {
  175. $hash = $this->hash($url);
  176. $query = $this->connection->getQueryBuilder();
  177. $query->update($this->dbTable)
  178. ->set('shared_secret', $query->createParameter('sharedSecret'))
  179. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  180. ->setParameter('url_hash', $hash)
  181. ->setParameter('sharedSecret', $sharedSecret);
  182. $query->executeStatement();
  183. }
  184. /**
  185. * Get shared secret from database
  186. */
  187. public function getSharedSecret(string $url): string {
  188. $hash = $this->hash($url);
  189. $query = $this->connection->getQueryBuilder();
  190. $query->select('shared_secret')->from($this->dbTable)
  191. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  192. ->setParameter('url_hash', $hash);
  193. $statement = $query->executeQuery();
  194. $result = $statement->fetch();
  195. $statement->closeCursor();
  196. return (string)$result['shared_secret'];
  197. }
  198. /**
  199. * Set server status
  200. */
  201. public function setServerStatus(string $url, int $status, ?string $token = null): void {
  202. $hash = $this->hash($url);
  203. $query = $this->connection->getQueryBuilder();
  204. $query->update($this->dbTable)
  205. ->set('status', $query->createNamedParameter($status))
  206. ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash)));
  207. if (!is_null($token)) {
  208. $query->set('sync_token', $query->createNamedParameter($token));
  209. }
  210. $query->executeStatement();
  211. }
  212. /**
  213. * Get server status
  214. */
  215. public function getServerStatus(string $url): int {
  216. $hash = $this->hash($url);
  217. $query = $this->connection->getQueryBuilder();
  218. $query->select('status')->from($this->dbTable)
  219. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  220. ->setParameter('url_hash', $hash);
  221. $statement = $query->executeQuery();
  222. $result = $statement->fetch();
  223. $statement->closeCursor();
  224. return (int)$result['status'];
  225. }
  226. /**
  227. * Create hash from URL
  228. */
  229. protected function hash(string $url): string {
  230. $normalized = $this->normalizeUrl($url);
  231. return sha1($normalized);
  232. }
  233. /**
  234. * Normalize URL, used to create the sha1 hash
  235. */
  236. protected function normalizeUrl(string $url): string {
  237. $normalized = $url;
  238. if (strpos($url, 'https://') === 0) {
  239. $normalized = substr($url, strlen('https://'));
  240. } elseif (strpos($url, 'http://') === 0) {
  241. $normalized = substr($url, strlen('http://'));
  242. }
  243. $normalized = Filesystem::normalizePath($normalized);
  244. $normalized = trim($normalized, '/');
  245. return $normalized;
  246. }
  247. public function auth(string $username, string $password): bool {
  248. if ($username !== 'system') {
  249. return false;
  250. }
  251. $query = $this->connection->getQueryBuilder();
  252. $query->select('url')->from($this->dbTable)
  253. ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password)));
  254. $statement = $query->executeQuery();
  255. $result = $statement->fetch();
  256. $statement->closeCursor();
  257. return !empty($result);
  258. }
  259. }