integration.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 googlereader 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 users
  31. LEFT JOIN integrations ON integrations.user_id=users.id
  32. WHERE
  33. integrations.fever_enabled='t' AND lower(integrations.fever_token)=lower($1)
  34. `
  35. var user model.User
  36. err := s.db.QueryRow(query, token).Scan(&user.ID, &user.IsAdmin, &user.Timezone)
  37. switch {
  38. case err == sql.ErrNoRows:
  39. return nil, nil
  40. case err != nil:
  41. return nil, fmt.Errorf("unable to fetch user: %v", err)
  42. default:
  43. return &user, nil
  44. }
  45. }
  46. // GoogleReaderUserCheckPassword validates the hashed password.
  47. func (s *Storage) GoogleReaderUserCheckPassword(username, password string) error {
  48. var hash string
  49. query := `
  50. SELECT
  51. googlereader_password
  52. FROM integrations
  53. WHERE
  54. integrations.googlereader_enabled='t' AND integrations.googlereader_username=$1
  55. `
  56. err := s.db.QueryRow(query, username).Scan(&hash)
  57. if err == sql.ErrNoRows {
  58. return fmt.Errorf(`store: unable to find this user: %s`, username)
  59. } else if err != nil {
  60. return fmt.Errorf(`store: unable to fetch user: %v`, err)
  61. }
  62. if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
  63. return fmt.Errorf(`store: invalid password for "%s" (%v)`, username, err)
  64. }
  65. return nil
  66. }
  67. // GoogleReaderUserGetIntegration returns part of the google reader parts of the integration struct.
  68. func (s *Storage) GoogleReaderUserGetIntegration(username string) (*model.Integration, error) {
  69. var integration model.Integration
  70. query := `
  71. SELECT
  72. user_id,
  73. googlereader_enabled,
  74. googlereader_username,
  75. googlereader_password
  76. FROM integrations
  77. WHERE
  78. integrations.googlereader_enabled='t' AND integrations.googlereader_username=$1
  79. `
  80. err := s.db.QueryRow(query, username).Scan(&integration.UserID, &integration.GoogleReaderEnabled, &integration.GoogleReaderUsername, &integration.GoogleReaderPassword)
  81. if err == sql.ErrNoRows {
  82. return &integration, fmt.Errorf(`store: unable to find this user: %s`, username)
  83. } else if err != nil {
  84. return &integration, fmt.Errorf(`store: unable to fetch user: %v`, err)
  85. }
  86. return &integration, nil
  87. }
  88. // Integration returns user integration settings.
  89. func (s *Storage) Integration(userID int64) (*model.Integration, error) {
  90. query := `
  91. SELECT
  92. user_id,
  93. pinboard_enabled,
  94. pinboard_token,
  95. pinboard_tags,
  96. pinboard_mark_as_unread,
  97. instapaper_enabled,
  98. instapaper_username,
  99. instapaper_password,
  100. fever_enabled,
  101. fever_username,
  102. fever_token,
  103. googlereader_enabled,
  104. googlereader_username,
  105. googlereader_password,
  106. wallabag_enabled,
  107. wallabag_url,
  108. wallabag_client_id,
  109. wallabag_client_secret,
  110. wallabag_username,
  111. wallabag_password,
  112. nunux_keeper_enabled,
  113. nunux_keeper_url,
  114. nunux_keeper_api_key,
  115. pocket_enabled,
  116. pocket_access_token,
  117. pocket_consumer_key,
  118. telegram_bot_enabled,
  119. telegram_bot_token,
  120. telegram_bot_chat_id
  121. FROM
  122. integrations
  123. WHERE
  124. user_id=$1
  125. `
  126. var integration model.Integration
  127. err := s.db.QueryRow(query, userID).Scan(
  128. &integration.UserID,
  129. &integration.PinboardEnabled,
  130. &integration.PinboardToken,
  131. &integration.PinboardTags,
  132. &integration.PinboardMarkAsUnread,
  133. &integration.InstapaperEnabled,
  134. &integration.InstapaperUsername,
  135. &integration.InstapaperPassword,
  136. &integration.FeverEnabled,
  137. &integration.FeverUsername,
  138. &integration.FeverToken,
  139. &integration.GoogleReaderEnabled,
  140. &integration.GoogleReaderUsername,
  141. &integration.GoogleReaderPassword,
  142. &integration.WallabagEnabled,
  143. &integration.WallabagURL,
  144. &integration.WallabagClientID,
  145. &integration.WallabagClientSecret,
  146. &integration.WallabagUsername,
  147. &integration.WallabagPassword,
  148. &integration.NunuxKeeperEnabled,
  149. &integration.NunuxKeeperURL,
  150. &integration.NunuxKeeperAPIKey,
  151. &integration.PocketEnabled,
  152. &integration.PocketAccessToken,
  153. &integration.PocketConsumerKey,
  154. &integration.TelegramBotEnabled,
  155. &integration.TelegramBotToken,
  156. &integration.TelegramBotChatID,
  157. )
  158. switch {
  159. case err == sql.ErrNoRows:
  160. return &integration, nil
  161. case err != nil:
  162. return &integration, fmt.Errorf(`store: unable to fetch integration row: %v`, err)
  163. default:
  164. return &integration, nil
  165. }
  166. }
  167. // UpdateIntegration saves user integration settings.
  168. func (s *Storage) UpdateIntegration(integration *model.Integration) error {
  169. var err error
  170. if integration.GoogleReaderPassword != "" {
  171. integration.GoogleReaderPassword, err = hashPassword(integration.GoogleReaderPassword)
  172. if err != nil {
  173. return err
  174. }
  175. query := `
  176. UPDATE
  177. integrations
  178. SET
  179. pinboard_enabled=$1,
  180. pinboard_token=$2,
  181. pinboard_tags=$3,
  182. pinboard_mark_as_unread=$4,
  183. instapaper_enabled=$5,
  184. instapaper_username=$6,
  185. instapaper_password=$7,
  186. fever_enabled=$8,
  187. fever_username=$9,
  188. fever_token=$10,
  189. wallabag_enabled=$11,
  190. wallabag_url=$12,
  191. wallabag_client_id=$13,
  192. wallabag_client_secret=$14,
  193. wallabag_username=$15,
  194. wallabag_password=$16,
  195. nunux_keeper_enabled=$17,
  196. nunux_keeper_url=$18,
  197. nunux_keeper_api_key=$19,
  198. pocket_enabled=$20,
  199. pocket_access_token=$21,
  200. pocket_consumer_key=$22,
  201. googlereader_enabled=$23,
  202. googlereader_username=$24,
  203. googlereader_password=$25,
  204. telegram_bot_enabled=$26,
  205. telegram_bot_token=$27,
  206. telegram_bot_chat_id=$28
  207. WHERE
  208. user_id=$29
  209. `
  210. _, err = s.db.Exec(
  211. query,
  212. integration.PinboardEnabled,
  213. integration.PinboardToken,
  214. integration.PinboardTags,
  215. integration.PinboardMarkAsUnread,
  216. integration.InstapaperEnabled,
  217. integration.InstapaperUsername,
  218. integration.InstapaperPassword,
  219. integration.FeverEnabled,
  220. integration.FeverUsername,
  221. integration.FeverToken,
  222. integration.WallabagEnabled,
  223. integration.WallabagURL,
  224. integration.WallabagClientID,
  225. integration.WallabagClientSecret,
  226. integration.WallabagUsername,
  227. integration.WallabagPassword,
  228. integration.NunuxKeeperEnabled,
  229. integration.NunuxKeeperURL,
  230. integration.NunuxKeeperAPIKey,
  231. integration.PocketEnabled,
  232. integration.PocketAccessToken,
  233. integration.PocketConsumerKey,
  234. integration.GoogleReaderEnabled,
  235. integration.GoogleReaderUsername,
  236. integration.GoogleReaderPassword,
  237. integration.TelegramBotEnabled,
  238. integration.TelegramBotToken,
  239. integration.TelegramBotChatID,
  240. integration.UserID,
  241. )
  242. } else {
  243. query := `
  244. UPDATE
  245. integrations
  246. SET
  247. pinboard_enabled=$1,
  248. pinboard_token=$2,
  249. pinboard_tags=$3,
  250. pinboard_mark_as_unread=$4,
  251. instapaper_enabled=$5,
  252. instapaper_username=$6,
  253. instapaper_password=$7,
  254. fever_enabled=$8,
  255. fever_username=$9,
  256. fever_token=$10,
  257. wallabag_enabled=$11,
  258. wallabag_url=$12,
  259. wallabag_client_id=$13,
  260. wallabag_client_secret=$14,
  261. wallabag_username=$15,
  262. wallabag_password=$16,
  263. nunux_keeper_enabled=$17,
  264. nunux_keeper_url=$18,
  265. nunux_keeper_api_key=$19,
  266. pocket_enabled=$20,
  267. pocket_access_token=$21,
  268. pocket_consumer_key=$22,
  269. googlereader_enabled=$23,
  270. googlereader_username=$24,
  271. googlereader_password=$25,
  272. telegram_bot_enabled=$26,
  273. telegram_bot_token=$27,
  274. telegram_bot_chat_id=$28
  275. WHERE
  276. user_id=$29
  277. `
  278. _, err = s.db.Exec(
  279. query,
  280. integration.PinboardEnabled,
  281. integration.PinboardToken,
  282. integration.PinboardTags,
  283. integration.PinboardMarkAsUnread,
  284. integration.InstapaperEnabled,
  285. integration.InstapaperUsername,
  286. integration.InstapaperPassword,
  287. integration.FeverEnabled,
  288. integration.FeverUsername,
  289. integration.FeverToken,
  290. integration.WallabagEnabled,
  291. integration.WallabagURL,
  292. integration.WallabagClientID,
  293. integration.WallabagClientSecret,
  294. integration.WallabagUsername,
  295. integration.WallabagPassword,
  296. integration.NunuxKeeperEnabled,
  297. integration.NunuxKeeperURL,
  298. integration.NunuxKeeperAPIKey,
  299. integration.PocketEnabled,
  300. integration.PocketAccessToken,
  301. integration.PocketConsumerKey,
  302. integration.GoogleReaderEnabled,
  303. integration.GoogleReaderUsername,
  304. integration.GoogleReaderPassword,
  305. integration.TelegramBotEnabled,
  306. integration.TelegramBotToken,
  307. integration.TelegramBotChatID,
  308. integration.UserID,
  309. )
  310. }
  311. if err != nil {
  312. return fmt.Errorf(`store: unable to update integration row: %v`, err)
  313. }
  314. return nil
  315. }
  316. // HasSaveEntry returns true if the given user can save articles to third-parties.
  317. func (s *Storage) HasSaveEntry(userID int64) (result bool) {
  318. query := `
  319. SELECT
  320. true
  321. FROM
  322. integrations
  323. WHERE
  324. user_id=$1
  325. AND
  326. (pinboard_enabled='t' OR instapaper_enabled='t' OR wallabag_enabled='t' OR nunux_keeper_enabled='t' OR pocket_enabled='t')
  327. `
  328. if err := s.db.QueryRow(query, userID).Scan(&result); err != nil {
  329. result = false
  330. }
  331. return result
  332. }