jlelse
/
jsonpub
Archived
1
Fork 0

Don't create new signer for every new request

This commit is contained in:
Jan-Lukas Else 2020-04-11 21:53:47 +02:00
parent a0afd43471
commit 6edfd4e02b
1 changed files with 15 additions and 8 deletions

View File

@ -15,6 +15,7 @@ import (
"net/url"
"os"
"strings"
"sync"
"time"
)
@ -23,6 +24,8 @@ type Actor struct {
followers map[string]interface{}
privateKey crypto.PrivateKey
publicKeyID string
postSigner httpsig.Signer
postSignMutex *sync.Mutex
}
type ActorToSave struct {
@ -49,13 +52,16 @@ func GetActor(name, iri, feed, pk string) (Actor, error) {
if err != nil {
return Actor{}, errors.New("failed to parse the private key")
}
postSigner, _, _ := httpsig.NewSigner([]httpsig.Algorithm{httpsig.RSA_SHA256}, "SHA-256", []string{"(request-target)", "date", "host", "digest"}, httpsig.Signature)
actor := Actor{
Name: name,
iri: iri,
feed: feed,
followers: make(map[string]interface{}),
privateKey: privateKey,
publicKeyID: iri + "#main-key",
Name: name,
iri: iri,
feed: feed,
followers: make(map[string]interface{}),
privateKey: privateKey,
publicKeyID: iri + "#main-key",
postSigner: postSigner,
postSignMutex: &sync.Mutex{},
}
jsonFile := storage + slash + "actors" + slash + name + slash + name + ".json"
fileHandle, err := os.Open(jsonFile)
@ -162,7 +168,6 @@ func (a *Actor) signedHTTPPost(content *map[string]interface{}, to string) (err
if err != nil {
return
}
postSigner, _, _ := httpsig.NewSigner([]httpsig.Algorithm{httpsig.RSA_SHA256}, "SHA-256", []string{"(request-target)", "date", "host", "digest"}, httpsig.Signature)
byteCopy := make([]byte, len(b))
copy(byteCopy, b)
buf := bytes.NewBuffer(byteCopy)
@ -180,7 +185,9 @@ func (a *Actor) signedHTTPPost(content *map[string]interface{}, to string) (err
req.Header.Add("Host", iri.Host)
req.Header.Add("Accept", "application/activity+json; charset=utf-8")
req.Header.Add("Content-Type", "application/activity+json; charset=utf-8")
err = postSigner.SignRequest(a.privateKey, a.publicKeyID, req, byteCopy)
a.postSignMutex.Lock()
err = a.postSigner.SignRequest(a.privateKey, a.publicKeyID, req, byteCopy)
a.postSignMutex.Unlock()
if err != nil {
return
}