Преглед на файлове

Fix and simplify shaarli's integration

- The jwt token was declared as using HS256 as algorithm, but was using HS512.
- No need to base64-encode then remove the padding when we can simply encode
  without padding.
- Factorize the header+payload concatenation as data

Odds are that this integration was broken from the start (HS512 vs HS256), so
I'm not sure if it's better to add tests or to simply get rid of it.
jvoisin преди 2 години
родител
ревизия
f4746a7306
променени са 1 файла, в които са добавени 6 реда и са изтрити 6 реда
  1. 6 6
      internal/integration/shaarli/shaarli.go

+ 6 - 6
internal/integration/shaarli/shaarli.go

@@ -11,7 +11,6 @@ import (
 	"encoding/json"
 	"fmt"
 	"net/http"
-	"strings"
 	"time"
 
 	"miniflux.app/v2/internal/urllib"
@@ -74,14 +73,15 @@ func (c *Client) CreateLink(entryURL, entryTitle string) error {
 }
 
 func (c *Client) generateBearerToken() string {
-	header := strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(`{"typ":"JWT", "alg":"HS256"}`)), "=")
-	payload := strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf(`{"iat": %d}`, time.Now().Unix()))), "=")
+	header := base64.RawURLEncoding.EncodeToString([]byte(`{"typ":"JWT","alg":"HS512"}`))
+	payload := base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf(`{"iat":%d}`, time.Now().Unix())))
+	data := header + "." + payload
 
 	mac := hmac.New(sha512.New, []byte(c.apiSecret))
-	mac.Write([]byte(header + "." + payload))
-	signature := strings.TrimRight(base64.URLEncoding.EncodeToString(mac.Sum(nil)), "=")
+	mac.Write([]byte(data))
+	signature := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
 
-	return header + "." + payload + "." + signature
+	return data + "." + signature
 }
 
 type addLinkRequest struct {