4
0

integration.go 11 KB

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