integration.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package storage // import "miniflux.app/storage"
  4. import (
  5. "database/sql"
  6. "fmt"
  7. "golang.org/x/crypto/bcrypt"
  8. "miniflux.app/model"
  9. )
  10. // HasDuplicateFeverUsername checks if another user have the same Fever username.
  11. func (s *Storage) HasDuplicateFeverUsername(userID int64, feverUsername string) bool {
  12. query := `SELECT true FROM integrations WHERE user_id != $1 AND fever_username=$2`
  13. var result bool
  14. s.db.QueryRow(query, userID, feverUsername).Scan(&result)
  15. return result
  16. }
  17. // HasDuplicateGoogleReaderUsername checks if another user have the same Google Reader username.
  18. func (s *Storage) HasDuplicateGoogleReaderUsername(userID int64, googleReaderUsername string) bool {
  19. query := `SELECT true FROM integrations WHERE user_id != $1 AND googlereader_username=$2`
  20. var result bool
  21. s.db.QueryRow(query, userID, googleReaderUsername).Scan(&result)
  22. return result
  23. }
  24. // UserByFeverToken returns a user by using the Fever API token.
  25. func (s *Storage) UserByFeverToken(token string) (*model.User, error) {
  26. query := `
  27. SELECT
  28. users.id, users.is_admin, users.timezone
  29. FROM
  30. users
  31. LEFT JOIN
  32. integrations ON integrations.user_id=users.id
  33. WHERE
  34. integrations.fever_enabled='t' AND lower(integrations.fever_token)=lower($1)
  35. `
  36. var user model.User
  37. err := s.db.QueryRow(query, token).Scan(&user.ID, &user.IsAdmin, &user.Timezone)
  38. switch {
  39. case err == sql.ErrNoRows:
  40. return nil, nil
  41. case err != nil:
  42. return nil, fmt.Errorf("store: unable to fetch user: %v", err)
  43. default:
  44. return &user, nil
  45. }
  46. }
  47. // GoogleReaderUserCheckPassword validates the Google Reader hashed password.
  48. func (s *Storage) GoogleReaderUserCheckPassword(username, password string) error {
  49. var hash string
  50. query := `
  51. SELECT
  52. googlereader_password
  53. FROM
  54. integrations
  55. WHERE
  56. integrations.googlereader_enabled='t' AND integrations.googlereader_username=$1
  57. `
  58. err := s.db.QueryRow(query, username).Scan(&hash)
  59. if err == sql.ErrNoRows {
  60. return fmt.Errorf(`store: unable to find this user: %s`, username)
  61. } else if err != nil {
  62. return fmt.Errorf(`store: unable to fetch user: %v`, err)
  63. }
  64. if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
  65. return fmt.Errorf(`store: invalid password for "%s" (%v)`, username, err)
  66. }
  67. return nil
  68. }
  69. // GoogleReaderUserGetIntegration returns part of the Google Reader parts of the integration struct.
  70. func (s *Storage) GoogleReaderUserGetIntegration(username string) (*model.Integration, error) {
  71. var integration model.Integration
  72. query := `
  73. SELECT
  74. user_id,
  75. googlereader_enabled,
  76. googlereader_username,
  77. googlereader_password
  78. FROM
  79. integrations
  80. WHERE
  81. integrations.googlereader_enabled='t' AND integrations.googlereader_username=$1
  82. `
  83. err := s.db.QueryRow(query, username).Scan(&integration.UserID, &integration.GoogleReaderEnabled, &integration.GoogleReaderUsername, &integration.GoogleReaderPassword)
  84. if err == sql.ErrNoRows {
  85. return &integration, fmt.Errorf(`store: unable to find this user: %s`, username)
  86. } else if err != nil {
  87. return &integration, fmt.Errorf(`store: unable to fetch user: %v`, err)
  88. }
  89. return &integration, nil
  90. }
  91. // Integration returns user integration settings.
  92. func (s *Storage) Integration(userID int64) (*model.Integration, error) {
  93. query := `
  94. SELECT
  95. user_id,
  96. pinboard_enabled,
  97. pinboard_token,
  98. pinboard_tags,
  99. pinboard_mark_as_unread,
  100. instapaper_enabled,
  101. instapaper_username,
  102. instapaper_password,
  103. fever_enabled,
  104. fever_username,
  105. fever_token,
  106. googlereader_enabled,
  107. googlereader_username,
  108. googlereader_password,
  109. wallabag_enabled,
  110. wallabag_only_url,
  111. wallabag_url,
  112. wallabag_client_id,
  113. wallabag_client_secret,
  114. wallabag_username,
  115. wallabag_password,
  116. nunux_keeper_enabled,
  117. nunux_keeper_url,
  118. nunux_keeper_api_key,
  119. espial_enabled,
  120. espial_url,
  121. espial_api_key,
  122. espial_tags,
  123. pocket_enabled,
  124. pocket_access_token,
  125. pocket_consumer_key,
  126. telegram_bot_enabled,
  127. telegram_bot_token,
  128. telegram_bot_chat_id,
  129. linkding_enabled,
  130. linkding_url,
  131. linkding_api_key,
  132. linkding_tags,
  133. linkding_mark_as_unread,
  134. matrix_bot_enabled,
  135. matrix_bot_user,
  136. matrix_bot_password,
  137. matrix_bot_url,
  138. matrix_bot_chat_id
  139. FROM
  140. integrations
  141. WHERE
  142. user_id=$1
  143. `
  144. var integration model.Integration
  145. err := s.db.QueryRow(query, userID).Scan(
  146. &integration.UserID,
  147. &integration.PinboardEnabled,
  148. &integration.PinboardToken,
  149. &integration.PinboardTags,
  150. &integration.PinboardMarkAsUnread,
  151. &integration.InstapaperEnabled,
  152. &integration.InstapaperUsername,
  153. &integration.InstapaperPassword,
  154. &integration.FeverEnabled,
  155. &integration.FeverUsername,
  156. &integration.FeverToken,
  157. &integration.GoogleReaderEnabled,
  158. &integration.GoogleReaderUsername,
  159. &integration.GoogleReaderPassword,
  160. &integration.WallabagEnabled,
  161. &integration.WallabagOnlyURL,
  162. &integration.WallabagURL,
  163. &integration.WallabagClientID,
  164. &integration.WallabagClientSecret,
  165. &integration.WallabagUsername,
  166. &integration.WallabagPassword,
  167. &integration.NunuxKeeperEnabled,
  168. &integration.NunuxKeeperURL,
  169. &integration.NunuxKeeperAPIKey,
  170. &integration.EspialEnabled,
  171. &integration.EspialURL,
  172. &integration.EspialAPIKey,
  173. &integration.EspialTags,
  174. &integration.PocketEnabled,
  175. &integration.PocketAccessToken,
  176. &integration.PocketConsumerKey,
  177. &integration.TelegramBotEnabled,
  178. &integration.TelegramBotToken,
  179. &integration.TelegramBotChatID,
  180. &integration.LinkdingEnabled,
  181. &integration.LinkdingURL,
  182. &integration.LinkdingAPIKey,
  183. &integration.LinkdingTags,
  184. &integration.LinkdingMarkAsUnread,
  185. &integration.MatrixBotEnabled,
  186. &integration.MatrixBotUser,
  187. &integration.MatrixBotPassword,
  188. &integration.MatrixBotURL,
  189. &integration.MatrixBotChatID,
  190. )
  191. switch {
  192. case err == sql.ErrNoRows:
  193. return &integration, nil
  194. case err != nil:
  195. return &integration, fmt.Errorf(`store: unable to fetch integration row: %v`, err)
  196. default:
  197. return &integration, nil
  198. }
  199. }
  200. // UpdateIntegration saves user integration settings.
  201. func (s *Storage) UpdateIntegration(integration *model.Integration) error {
  202. var err error
  203. if integration.GoogleReaderPassword != "" {
  204. integration.GoogleReaderPassword, err = hashPassword(integration.GoogleReaderPassword)
  205. if err != nil {
  206. return err
  207. }
  208. query := `
  209. UPDATE
  210. integrations
  211. SET
  212. pinboard_enabled=$1,
  213. pinboard_token=$2,
  214. pinboard_tags=$3,
  215. pinboard_mark_as_unread=$4,
  216. instapaper_enabled=$5,
  217. instapaper_username=$6,
  218. instapaper_password=$7,
  219. fever_enabled=$8,
  220. fever_username=$9,
  221. fever_token=$10,
  222. wallabag_enabled=$11,
  223. wallabag_only_url=$12,
  224. wallabag_url=$13,
  225. wallabag_client_id=$14,
  226. wallabag_client_secret=$15,
  227. wallabag_username=$16,
  228. wallabag_password=$17,
  229. nunux_keeper_enabled=$18,
  230. nunux_keeper_url=$19,
  231. nunux_keeper_api_key=$20,
  232. pocket_enabled=$21,
  233. pocket_access_token=$22,
  234. pocket_consumer_key=$23,
  235. googlereader_enabled=$24,
  236. googlereader_username=$25,
  237. googlereader_password=$26,
  238. telegram_bot_enabled=$27,
  239. telegram_bot_token=$28,
  240. telegram_bot_chat_id=$29,
  241. espial_enabled=$30,
  242. espial_url=$31,
  243. espial_api_key=$32,
  244. espial_tags=$33,
  245. linkding_enabled=$34,
  246. linkding_url=$35,
  247. linkding_api_key=$36,
  248. linkding_tags=$37,
  249. linkding_mark_as_unread=$38,
  250. matrix_bot_enabled=$39,
  251. matrix_bot_user=$40,
  252. matrix_bot_password=$41,
  253. matrix_bot_url=$42,
  254. matrix_bot_chat_id=$43
  255. WHERE
  256. user_id=$44
  257. `
  258. _, err = s.db.Exec(
  259. query,
  260. integration.PinboardEnabled,
  261. integration.PinboardToken,
  262. integration.PinboardTags,
  263. integration.PinboardMarkAsUnread,
  264. integration.InstapaperEnabled,
  265. integration.InstapaperUsername,
  266. integration.InstapaperPassword,
  267. integration.FeverEnabled,
  268. integration.FeverUsername,
  269. integration.FeverToken,
  270. integration.WallabagEnabled,
  271. integration.WallabagOnlyURL,
  272. integration.WallabagURL,
  273. integration.WallabagClientID,
  274. integration.WallabagClientSecret,
  275. integration.WallabagUsername,
  276. integration.WallabagPassword,
  277. integration.NunuxKeeperEnabled,
  278. integration.NunuxKeeperURL,
  279. integration.NunuxKeeperAPIKey,
  280. integration.PocketEnabled,
  281. integration.PocketAccessToken,
  282. integration.PocketConsumerKey,
  283. integration.GoogleReaderEnabled,
  284. integration.GoogleReaderUsername,
  285. integration.GoogleReaderPassword,
  286. integration.TelegramBotEnabled,
  287. integration.TelegramBotToken,
  288. integration.TelegramBotChatID,
  289. integration.EspialEnabled,
  290. integration.EspialURL,
  291. integration.EspialAPIKey,
  292. integration.EspialTags,
  293. integration.LinkdingEnabled,
  294. integration.LinkdingURL,
  295. integration.LinkdingAPIKey,
  296. integration.LinkdingTags,
  297. integration.LinkdingMarkAsUnread,
  298. integration.MatrixBotEnabled,
  299. integration.MatrixBotUser,
  300. integration.MatrixBotPassword,
  301. integration.MatrixBotURL,
  302. integration.MatrixBotChatID,
  303. integration.UserID,
  304. )
  305. } else {
  306. query := `
  307. UPDATE
  308. integrations
  309. SET
  310. pinboard_enabled=$1,
  311. pinboard_token=$2,
  312. pinboard_tags=$3,
  313. pinboard_mark_as_unread=$4,
  314. instapaper_enabled=$5,
  315. instapaper_username=$6,
  316. instapaper_password=$7,
  317. fever_enabled=$8,
  318. fever_username=$9,
  319. fever_token=$10,
  320. wallabag_enabled=$11,
  321. wallabag_only_url=$12,
  322. wallabag_url=$13,
  323. wallabag_client_id=$14,
  324. wallabag_client_secret=$15,
  325. wallabag_username=$16,
  326. wallabag_password=$17,
  327. nunux_keeper_enabled=$18,
  328. nunux_keeper_url=$19,
  329. nunux_keeper_api_key=$20,
  330. pocket_enabled=$21,
  331. pocket_access_token=$22,
  332. pocket_consumer_key=$23,
  333. googlereader_enabled=$24,
  334. googlereader_username=$25,
  335. googlereader_password=$26,
  336. telegram_bot_enabled=$27,
  337. telegram_bot_token=$28,
  338. telegram_bot_chat_id=$29,
  339. espial_enabled=$30,
  340. espial_url=$31,
  341. espial_api_key=$32,
  342. espial_tags=$33,
  343. linkding_enabled=$34,
  344. linkding_url=$35,
  345. linkding_api_key=$36,
  346. linkding_tags=$37,
  347. linkding_mark_as_unread=$38,
  348. matrix_bot_enabled=$39,
  349. matrix_bot_user=$40,
  350. matrix_bot_password=$41,
  351. matrix_bot_url=$42,
  352. matrix_bot_chat_id=$43
  353. WHERE
  354. user_id=$44
  355. `
  356. _, err = s.db.Exec(
  357. query,
  358. integration.PinboardEnabled,
  359. integration.PinboardToken,
  360. integration.PinboardTags,
  361. integration.PinboardMarkAsUnread,
  362. integration.InstapaperEnabled,
  363. integration.InstapaperUsername,
  364. integration.InstapaperPassword,
  365. integration.FeverEnabled,
  366. integration.FeverUsername,
  367. integration.FeverToken,
  368. integration.WallabagEnabled,
  369. integration.WallabagOnlyURL,
  370. integration.WallabagURL,
  371. integration.WallabagClientID,
  372. integration.WallabagClientSecret,
  373. integration.WallabagUsername,
  374. integration.WallabagPassword,
  375. integration.NunuxKeeperEnabled,
  376. integration.NunuxKeeperURL,
  377. integration.NunuxKeeperAPIKey,
  378. integration.PocketEnabled,
  379. integration.PocketAccessToken,
  380. integration.PocketConsumerKey,
  381. integration.GoogleReaderEnabled,
  382. integration.GoogleReaderUsername,
  383. integration.GoogleReaderPassword,
  384. integration.TelegramBotEnabled,
  385. integration.TelegramBotToken,
  386. integration.TelegramBotChatID,
  387. integration.EspialEnabled,
  388. integration.EspialURL,
  389. integration.EspialAPIKey,
  390. integration.EspialTags,
  391. integration.LinkdingEnabled,
  392. integration.LinkdingURL,
  393. integration.LinkdingAPIKey,
  394. integration.LinkdingTags,
  395. integration.LinkdingMarkAsUnread,
  396. integration.MatrixBotEnabled,
  397. integration.MatrixBotUser,
  398. integration.MatrixBotPassword,
  399. integration.MatrixBotURL,
  400. integration.MatrixBotChatID,
  401. integration.UserID,
  402. )
  403. }
  404. if err != nil {
  405. return fmt.Errorf(`store: unable to update integration row: %v`, err)
  406. }
  407. return nil
  408. }
  409. // HasSaveEntry returns true if the given user can save articles to third-parties.
  410. func (s *Storage) HasSaveEntry(userID int64) (result bool) {
  411. query := `
  412. SELECT
  413. true
  414. FROM
  415. integrations
  416. WHERE
  417. user_id=$1
  418. AND
  419. (pinboard_enabled='t' OR instapaper_enabled='t' OR wallabag_enabled='t' OR nunux_keeper_enabled='t' OR espial_enabled='t' OR pocket_enabled='t' OR linkding_enabled='t')
  420. `
  421. if err := s.db.QueryRow(query, userID).Scan(&result); err != nil {
  422. result = false
  423. }
  424. return result
  425. }