integration.go 12 KB

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