integration.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. matrix_bot_enabled,
  134. matrix_bot_user,
  135. matrix_bot_password,
  136. matrix_bot_url,
  137. matrix_bot_chat_id
  138. FROM
  139. integrations
  140. WHERE
  141. user_id=$1
  142. `
  143. var integration model.Integration
  144. err := s.db.QueryRow(query, userID).Scan(
  145. &integration.UserID,
  146. &integration.PinboardEnabled,
  147. &integration.PinboardToken,
  148. &integration.PinboardTags,
  149. &integration.PinboardMarkAsUnread,
  150. &integration.InstapaperEnabled,
  151. &integration.InstapaperUsername,
  152. &integration.InstapaperPassword,
  153. &integration.FeverEnabled,
  154. &integration.FeverUsername,
  155. &integration.FeverToken,
  156. &integration.GoogleReaderEnabled,
  157. &integration.GoogleReaderUsername,
  158. &integration.GoogleReaderPassword,
  159. &integration.WallabagEnabled,
  160. &integration.WallabagOnlyURL,
  161. &integration.WallabagURL,
  162. &integration.WallabagClientID,
  163. &integration.WallabagClientSecret,
  164. &integration.WallabagUsername,
  165. &integration.WallabagPassword,
  166. &integration.NunuxKeeperEnabled,
  167. &integration.NunuxKeeperURL,
  168. &integration.NunuxKeeperAPIKey,
  169. &integration.EspialEnabled,
  170. &integration.EspialURL,
  171. &integration.EspialAPIKey,
  172. &integration.EspialTags,
  173. &integration.PocketEnabled,
  174. &integration.PocketAccessToken,
  175. &integration.PocketConsumerKey,
  176. &integration.TelegramBotEnabled,
  177. &integration.TelegramBotToken,
  178. &integration.TelegramBotChatID,
  179. &integration.LinkdingEnabled,
  180. &integration.LinkdingURL,
  181. &integration.LinkdingAPIKey,
  182. &integration.MatrixBotEnabled,
  183. &integration.MatrixBotUser,
  184. &integration.MatrixBotPassword,
  185. &integration.MatrixBotURL,
  186. &integration.MatrixBotChatID,
  187. )
  188. switch {
  189. case err == sql.ErrNoRows:
  190. return &integration, nil
  191. case err != nil:
  192. return &integration, fmt.Errorf(`store: unable to fetch integration row: %v`, err)
  193. default:
  194. return &integration, nil
  195. }
  196. }
  197. // UpdateIntegration saves user integration settings.
  198. func (s *Storage) UpdateIntegration(integration *model.Integration) error {
  199. var err error
  200. if integration.GoogleReaderPassword != "" {
  201. integration.GoogleReaderPassword, err = hashPassword(integration.GoogleReaderPassword)
  202. if err != nil {
  203. return err
  204. }
  205. query := `
  206. UPDATE
  207. integrations
  208. SET
  209. pinboard_enabled=$1,
  210. pinboard_token=$2,
  211. pinboard_tags=$3,
  212. pinboard_mark_as_unread=$4,
  213. instapaper_enabled=$5,
  214. instapaper_username=$6,
  215. instapaper_password=$7,
  216. fever_enabled=$8,
  217. fever_username=$9,
  218. fever_token=$10,
  219. wallabag_enabled=$11,
  220. wallabag_only_url=$12,
  221. wallabag_url=$13,
  222. wallabag_client_id=$14,
  223. wallabag_client_secret=$15,
  224. wallabag_username=$16,
  225. wallabag_password=$17,
  226. nunux_keeper_enabled=$18,
  227. nunux_keeper_url=$19,
  228. nunux_keeper_api_key=$20,
  229. pocket_enabled=$21,
  230. pocket_access_token=$22,
  231. pocket_consumer_key=$23,
  232. googlereader_enabled=$24,
  233. googlereader_username=$25,
  234. googlereader_password=$26,
  235. telegram_bot_enabled=$27,
  236. telegram_bot_token=$28,
  237. telegram_bot_chat_id=$29,
  238. espial_enabled=$30,
  239. espial_url=$31,
  240. espial_api_key=$32,
  241. espial_tags=$33,
  242. linkding_enabled=$34,
  243. linkding_url=$35,
  244. linkding_api_key=$36,
  245. matrix_bot_enabled=$37,
  246. matrix_bot_user=$38,
  247. matrix_bot_password=$39,
  248. matrix_bot_url=$40,
  249. matrix_bot_chat_id=$41
  250. WHERE
  251. user_id=$42
  252. `
  253. _, err = s.db.Exec(
  254. query,
  255. integration.PinboardEnabled,
  256. integration.PinboardToken,
  257. integration.PinboardTags,
  258. integration.PinboardMarkAsUnread,
  259. integration.InstapaperEnabled,
  260. integration.InstapaperUsername,
  261. integration.InstapaperPassword,
  262. integration.FeverEnabled,
  263. integration.FeverUsername,
  264. integration.FeverToken,
  265. integration.WallabagEnabled,
  266. integration.WallabagOnlyURL,
  267. integration.WallabagURL,
  268. integration.WallabagClientID,
  269. integration.WallabagClientSecret,
  270. integration.WallabagUsername,
  271. integration.WallabagPassword,
  272. integration.NunuxKeeperEnabled,
  273. integration.NunuxKeeperURL,
  274. integration.NunuxKeeperAPIKey,
  275. integration.PocketEnabled,
  276. integration.PocketAccessToken,
  277. integration.PocketConsumerKey,
  278. integration.GoogleReaderEnabled,
  279. integration.GoogleReaderUsername,
  280. integration.GoogleReaderPassword,
  281. integration.TelegramBotEnabled,
  282. integration.TelegramBotToken,
  283. integration.TelegramBotChatID,
  284. integration.EspialEnabled,
  285. integration.EspialURL,
  286. integration.EspialAPIKey,
  287. integration.EspialTags,
  288. integration.LinkdingEnabled,
  289. integration.LinkdingURL,
  290. integration.LinkdingAPIKey,
  291. integration.MatrixBotEnabled,
  292. integration.MatrixBotUser,
  293. integration.MatrixBotPassword,
  294. integration.MatrixBotURL,
  295. integration.MatrixBotChatID,
  296. integration.UserID,
  297. )
  298. } else {
  299. query := `
  300. UPDATE
  301. integrations
  302. SET
  303. pinboard_enabled=$1,
  304. pinboard_token=$2,
  305. pinboard_tags=$3,
  306. pinboard_mark_as_unread=$4,
  307. instapaper_enabled=$5,
  308. instapaper_username=$6,
  309. instapaper_password=$7,
  310. fever_enabled=$8,
  311. fever_username=$9,
  312. fever_token=$10,
  313. wallabag_enabled=$11,
  314. wallabag_only_url=$12,
  315. wallabag_url=$13,
  316. wallabag_client_id=$14,
  317. wallabag_client_secret=$15,
  318. wallabag_username=$16,
  319. wallabag_password=$17,
  320. nunux_keeper_enabled=$18,
  321. nunux_keeper_url=$19,
  322. nunux_keeper_api_key=$20,
  323. pocket_enabled=$21,
  324. pocket_access_token=$22,
  325. pocket_consumer_key=$23,
  326. googlereader_enabled=$24,
  327. googlereader_username=$25,
  328. googlereader_password=$26,
  329. telegram_bot_enabled=$27,
  330. telegram_bot_token=$28,
  331. telegram_bot_chat_id=$29,
  332. espial_enabled=$30,
  333. espial_url=$31,
  334. espial_api_key=$32,
  335. espial_tags=$33,
  336. linkding_enabled=$34,
  337. linkding_url=$35,
  338. linkding_api_key=$36,
  339. matrix_bot_enabled=$37,
  340. matrix_bot_user=$38,
  341. matrix_bot_password=$39,
  342. matrix_bot_url=$40,
  343. matrix_bot_chat_id=$41
  344. WHERE
  345. user_id=$42
  346. `
  347. _, err = s.db.Exec(
  348. query,
  349. integration.PinboardEnabled,
  350. integration.PinboardToken,
  351. integration.PinboardTags,
  352. integration.PinboardMarkAsUnread,
  353. integration.InstapaperEnabled,
  354. integration.InstapaperUsername,
  355. integration.InstapaperPassword,
  356. integration.FeverEnabled,
  357. integration.FeverUsername,
  358. integration.FeverToken,
  359. integration.WallabagEnabled,
  360. integration.WallabagOnlyURL,
  361. integration.WallabagURL,
  362. integration.WallabagClientID,
  363. integration.WallabagClientSecret,
  364. integration.WallabagUsername,
  365. integration.WallabagPassword,
  366. integration.NunuxKeeperEnabled,
  367. integration.NunuxKeeperURL,
  368. integration.NunuxKeeperAPIKey,
  369. integration.PocketEnabled,
  370. integration.PocketAccessToken,
  371. integration.PocketConsumerKey,
  372. integration.GoogleReaderEnabled,
  373. integration.GoogleReaderUsername,
  374. integration.GoogleReaderPassword,
  375. integration.TelegramBotEnabled,
  376. integration.TelegramBotToken,
  377. integration.TelegramBotChatID,
  378. integration.EspialEnabled,
  379. integration.EspialURL,
  380. integration.EspialAPIKey,
  381. integration.EspialTags,
  382. integration.LinkdingEnabled,
  383. integration.LinkdingURL,
  384. integration.LinkdingAPIKey,
  385. integration.MatrixBotEnabled,
  386. integration.MatrixBotUser,
  387. integration.MatrixBotPassword,
  388. integration.MatrixBotURL,
  389. integration.MatrixBotChatID,
  390. integration.UserID,
  391. )
  392. }
  393. if err != nil {
  394. return fmt.Errorf(`store: unable to update integration row: %v`, err)
  395. }
  396. return nil
  397. }
  398. // HasSaveEntry returns true if the given user can save articles to third-parties.
  399. func (s *Storage) HasSaveEntry(userID int64) (result bool) {
  400. query := `
  401. SELECT
  402. true
  403. FROM
  404. integrations
  405. WHERE
  406. user_id=$1
  407. AND
  408. (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')
  409. `
  410. if err := s.db.QueryRow(query, userID).Scan(&result); err != nil {
  411. result = false
  412. }
  413. return result
  414. }