1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package delivery
import (
"bytes"
"encoding/gob"
"net/http"
"net/url"
"sync"
"time"
"git.capotej.com/capotej/communique/config"
"git.capotej.com/capotej/communique/models"
"github.com/go-fed/httpsig"
)
type Signed struct {
signer httpsig.Signer
mu sync.Mutex
persister *models.Persister
}
func NewSigned(persister *models.Persister) (*Signed, error) {
signed := &Signed{persister: persister}
var err error
prefs := []httpsig.Algorithm{}
digestAlgorithm := httpsig.DigestSha256
headersToSign := []string{httpsig.RequestTarget, "host", "date", "digest"}
signed.signer, _, err = httpsig.NewSigner(prefs, digestAlgorithm, headersToSign, httpsig.Signature, 0)
if err != nil {
return nil, err
}
return signed, nil
}
func (s *Signed) SignedRequest(handler config.Handler, payload []byte, inboxUrl, actorKeyUrl *url.URL) (*http.Request, error) {
aso := models.NewKeypair(handler)
result, err := s.persister.Find(aso)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(result)
dec := gob.NewDecoder(buf)
var keypair models.Keypair
err = dec.Decode(&keypair)
if err != nil {
return nil, err
}
privKey := &keypair.PrivateKey
request, err := http.NewRequest("POST", inboxUrl.String(), bytes.NewBuffer(payload))
date := time.Now().UTC().Format(http.TimeFormat)
request.Header.Set("Date", date)
request.Header.Set("Content-Type", "application/activity+json")
request.Header.Set("Host", inboxUrl.Host)
s.mu.Lock()
err = s.signer.SignRequest(privKey, actorKeyUrl.String(), request, payload)
s.mu.Unlock()
if err != nil {
return nil, err
}
return request, nil
}
|