From 997316d81e2dbd1a8f3a120e6d193fa66c8c37de Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Fri, 6 Jan 2023 15:13:08 -0500 Subject: refactor signed request --- registry/registry.go | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) (limited to 'registry/registry.go') diff --git a/registry/registry.go b/registry/registry.go index e15d861..aa61d58 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -12,17 +12,15 @@ import ( "net/http" "net/url" "strings" - "sync" - "time" "git.capotej.com/capotej/communique/config" + "git.capotej.com/capotej/communique/delivery" "git.capotej.com/capotej/communique/models" "git.capotej.com/capotej/communique/tools" "git.capotej.com/capotej/communique/urls" "git.capotej.com/capotej/communique/views" "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" - "github.com/go-fed/httpsig" "go.uber.org/zap" ) @@ -36,8 +34,7 @@ type Registry struct { persister *models.Persister handlerMap map[string]Handler log *zap.SugaredLogger - signer httpsig.Signer - mu sync.Mutex + delivery *delivery.Signed } func NewRegistry(cfg config.Config, persister *models.Persister, log *zap.SugaredLogger) (*Registry, error) { @@ -51,10 +48,7 @@ func NewRegistry(cfg config.Config, persister *models.Persister, log *zap.Sugare return nil, err } } - prefs := []httpsig.Algorithm{} - digestAlgorithm := httpsig.DigestSha256 - headersToSign := []string{httpsig.RequestTarget, "host", "date", "digest"} - reg.signer, _, err = httpsig.NewSigner(prefs, digestAlgorithm, headersToSign, httpsig.Signature, 0) + reg.delivery, err = delivery.NewSigned(persister) if err != nil { return nil, err } @@ -261,30 +255,14 @@ func (r *Registry) deliverAcceptToInbox(url, actorUrl, actorKeyUrl *url.URL, fol return err } jsonData, err := json.Marshal(payload) - - aso := models.NewKeypair(handler) - result, err := r.persister.Find(aso) if err != nil { return err } - buf := bytes.NewBuffer(result) - dec := gob.NewDecoder(buf) - var keypair models.Keypair - err = dec.Decode(&keypair) + + request, err := r.delivery.SignedRequest(handler, jsonData, url, actorKeyUrl) if err != nil { return err } - privKey := &keypair.PrivateKey - - request, err := http.NewRequest("POST", url.String(), bytes.NewBuffer(jsonData)) - date := time.Now().UTC().Format(http.TimeFormat) - request.Header.Set("Date", date) - request.Header.Set("Content-Type", "application/activity+json") - request.Header.Set("Host", url.Host) - - r.mu.Lock() - err = r.signer.SignRequest(privKey, actorKeyUrl.String(), request, jsonData) - r.mu.Unlock() r.log.With( "type", -- cgit v1.2.3