shaarli.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package shaarli // import "miniflux.app/v2/internal/integration/shaarli"
  4. import (
  5. "bytes"
  6. "crypto/hmac"
  7. "crypto/sha512"
  8. "encoding/base64"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "net/http"
  13. "time"
  14. "miniflux.app/v2/internal/urllib"
  15. "miniflux.app/v2/internal/version"
  16. )
  17. const defaultClientTimeout = 10 * time.Second
  18. type Client struct {
  19. baseURL string
  20. apiSecret string
  21. }
  22. func NewClient(baseURL, apiSecret string) *Client {
  23. return &Client{baseURL: baseURL, apiSecret: apiSecret}
  24. }
  25. func (c *Client) CreateLink(entryURL, entryTitle string) error {
  26. if c.baseURL == "" || c.apiSecret == "" {
  27. return errors.New("shaarli: missing base URL or API secret")
  28. }
  29. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/v1/links")
  30. if err != nil {
  31. return fmt.Errorf("shaarli: invalid API endpoint: %v", err)
  32. }
  33. requestBody, err := json.Marshal(&addLinkRequest{
  34. URL: entryURL,
  35. Title: entryTitle,
  36. Private: true,
  37. })
  38. if err != nil {
  39. return fmt.Errorf("shaarli: unable to encode request body: %v", err)
  40. }
  41. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  42. if err != nil {
  43. return fmt.Errorf("shaarli: unable to create request: %v", err)
  44. }
  45. request.Header.Set("Content-Type", "application/json")
  46. request.Header.Set("Accept", "application/json")
  47. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  48. request.Header.Set("Authorization", "Bearer "+c.generateBearerToken())
  49. httpClient := &http.Client{Timeout: defaultClientTimeout}
  50. response, err := httpClient.Do(request)
  51. if err != nil {
  52. return fmt.Errorf("shaarli: unable to send request: %v", err)
  53. }
  54. defer response.Body.Close()
  55. if response.StatusCode != http.StatusCreated {
  56. return fmt.Errorf("shaarli: unable to add link: url=%s status=%d", apiEndpoint, response.StatusCode)
  57. }
  58. return nil
  59. }
  60. func (c *Client) generateBearerToken() string {
  61. header := base64.RawURLEncoding.EncodeToString([]byte(`{"typ":"JWT","alg":"HS512"}`))
  62. payload := base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf(`{"iat":%d}`, time.Now().Unix())))
  63. data := header + "." + payload
  64. mac := hmac.New(sha512.New, []byte(c.apiSecret))
  65. mac.Write([]byte(data))
  66. signature := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
  67. return data + "." + signature
  68. }
  69. type addLinkRequest struct {
  70. URL string `json:"url"`
  71. Title string `json:"title"`
  72. Private bool `json:"private"`
  73. }