integration.go 11 KB

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