connector.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2018 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 pocket // import "miniflux.app/integration/pocket"
  5. import (
  6. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "net/url"
  10. "miniflux.app/http/client"
  11. )
  12. // Connector manages the authorization flow with Pocket to get a personal access token.
  13. type Connector struct {
  14. consumerKey string
  15. }
  16. // RequestToken fetches a new request token from Pocket API.
  17. func (c *Connector) RequestToken(redirectURL string) (string, error) {
  18. type req struct {
  19. ConsumerKey string `json:"consumer_key"`
  20. RedirectURI string `json:"redirect_uri"`
  21. }
  22. clt := client.New("https://getpocket.com/v3/oauth/request")
  23. response, err := clt.PostJSON(&req{ConsumerKey: c.consumerKey, RedirectURI: redirectURL})
  24. if err != nil {
  25. return "", fmt.Errorf("pocket: unable to fetch request token: %v", err)
  26. }
  27. if response.HasServerFailure() {
  28. return "", fmt.Errorf("pocket: unable to fetch request token, status=%d", response.StatusCode)
  29. }
  30. body, err := ioutil.ReadAll(response.Body)
  31. if err != nil {
  32. return "", fmt.Errorf("pocket: unable to read response body: %v", err)
  33. }
  34. values, err := url.ParseQuery(string(body))
  35. if err != nil {
  36. return "", fmt.Errorf("pocket: unable to parse response: %v", err)
  37. }
  38. code := values.Get("code")
  39. if code == "" {
  40. return "", errors.New("pocket: code is empty")
  41. }
  42. return code, nil
  43. }
  44. // AccessToken fetches a new access token once the end-user authorized the application.
  45. func (c *Connector) AccessToken(requestToken string) (string, error) {
  46. type req struct {
  47. ConsumerKey string `json:"consumer_key"`
  48. Code string `json:"code"`
  49. }
  50. clt := client.New("https://getpocket.com/v3/oauth/authorize")
  51. response, err := clt.PostJSON(&req{ConsumerKey: c.consumerKey, Code: requestToken})
  52. if err != nil {
  53. return "", fmt.Errorf("pocket: unable to fetch access token: %v", err)
  54. }
  55. if response.HasServerFailure() {
  56. return "", fmt.Errorf("pocket: unable to fetch access token, status=%d", response.StatusCode)
  57. }
  58. body, err := ioutil.ReadAll(response.Body)
  59. if err != nil {
  60. return "", fmt.Errorf("pocket: unable to read response body: %v", err)
  61. }
  62. values, err := url.ParseQuery(string(body))
  63. if err != nil {
  64. return "", fmt.Errorf("pocket: unable to parse response: %v", err)
  65. }
  66. token := values.Get("access_token")
  67. if token == "" {
  68. return "", errors.New("pocket: access_token is empty")
  69. }
  70. return token, nil
  71. }
  72. // AuthorizationURL returns the authorization URL for the end-user.
  73. func (c *Connector) AuthorizationURL(requestToken, redirectURL string) string {
  74. return fmt.Sprintf(
  75. "https://getpocket.com/auth/authorize?request_token=%s&redirect_uri=%s",
  76. requestToken,
  77. redirectURL,
  78. )
  79. }
  80. // NewConnector returns a new Pocket Connector.
  81. func NewConnector(consumerKey string) *Connector {
  82. return &Connector{consumerKey}
  83. }