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