Move CreateSignedRequest to the activitypub/crypto package

This commit is contained in:
Gabe Kangas 2022-04-07 13:44:36 -07:00
parent e4589a4462
commit 9b2ef76773
No known key found for this signature in database
GPG Key ID: 9A56337728BC81EA
4 changed files with 26 additions and 28 deletions

View File

@ -1,12 +1,16 @@
package crypto package crypto
import ( import (
"bytes"
"crypto" "crypto"
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"time" "time"
"github.com/go-fed/httpsig" "github.com/go-fed/httpsig"
"github.com/owncast/owncast/config"
log "github.com/sirupsen/logrus"
) )
// SignResponse will sign a response using the provided response body and public key. // SignResponse will sign a response using the provided response body and public key.
@ -27,7 +31,6 @@ func signResponse(privateKey crypto.PrivateKey, pubKeyID url.URL, body []byte, w
} }
signer, _, err := httpsig.NewSigner(prefs, digestAlgorithm, headersToSign, httpsig.Signature, 0) signer, _, err := httpsig.NewSigner(prefs, digestAlgorithm, headersToSign, httpsig.Signature, 0)
if err != nil { if err != nil {
return err return err
} }
@ -60,7 +63,6 @@ func signRequest(privateKey crypto.PrivateKey, pubKeyID string, body []byte, r *
} }
signer, _, err := httpsig.NewSigner(prefs, digestAlgorithm, headersToSign, httpsig.Signature, 0) signer, _, err := httpsig.NewSigner(prefs, digestAlgorithm, headersToSign, httpsig.Signature, 0)
if err != nil { if err != nil {
return err return err
} }
@ -68,3 +70,21 @@ func signRequest(privateKey crypto.PrivateKey, pubKeyID string, body []byte, r *
// If r were a http.ResponseWriter, call SignResponse instead. // If r were a http.ResponseWriter, call SignResponse instead.
return signer.SignRequest(privateKey, pubKeyID, r, body) return signer.SignRequest(privateKey, pubKeyID, r, body)
} }
// CreateSignedRequest will create a signed POST request of a payload to the provided destination.
func CreateSignedRequest(payload []byte, url *url.URL, fromActorIRI *url.URL) (*http.Request, error) {
log.Debugln("Sending", string(payload), "to", url)
req, _ := http.NewRequest("POST", url.String(), bytes.NewBuffer(payload))
ua := fmt.Sprintf("%s; https://owncast.online", config.GetReleaseString())
req.Header.Set("User-Agent", ua)
req.Header.Set("Content-Type", "application/activity+json")
if err := SignRequest(req, payload, fromActorIRI); err != nil {
log.Errorln("error signing request:", err)
return nil, err
}
return req, nil
}

View File

@ -11,8 +11,8 @@ import (
"github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab" "github.com/go-fed/activity/streams/vocab"
"github.com/owncast/owncast/activitypub/apmodels" "github.com/owncast/owncast/activitypub/apmodels"
"github.com/owncast/owncast/activitypub/crypto"
"github.com/owncast/owncast/activitypub/persistence" "github.com/owncast/owncast/activitypub/persistence"
"github.com/owncast/owncast/activitypub/requests"
"github.com/owncast/owncast/activitypub/workerpool" "github.com/owncast/owncast/activitypub/workerpool"
"github.com/owncast/owncast/config" "github.com/owncast/owncast/config"
@ -180,7 +180,7 @@ func SendToFollowers(payload []byte) error {
for _, follower := range followers { for _, follower := range followers {
inbox, _ := url.Parse(follower.Inbox) inbox, _ := url.Parse(follower.Inbox)
req, err := requests.CreateSignedRequest(payload, inbox, localActor) req, err := crypto.CreateSignedRequest(payload, inbox, localActor)
if err != nil { if err != nil {
log.Errorln("unable to create outbox request", follower.Inbox, err) log.Errorln("unable to create outbox request", follower.Inbox, err)
return errors.New("unable to create outbox request: " + follower.Inbox) return errors.New("unable to create outbox request: " + follower.Inbox)

View File

@ -7,6 +7,7 @@ import (
"github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab" "github.com/go-fed/activity/streams/vocab"
"github.com/owncast/owncast/activitypub/apmodels" "github.com/owncast/owncast/activitypub/apmodels"
"github.com/owncast/owncast/activitypub/crypto"
"github.com/owncast/owncast/activitypub/workerpool" "github.com/owncast/owncast/activitypub/workerpool"
"github.com/teris-io/shortid" "github.com/teris-io/shortid"
@ -20,7 +21,7 @@ func SendFollowAccept(inbox *url.URL, followRequestIRI *url.URL, fromLocalAccoun
var jsonmap map[string]interface{} var jsonmap map[string]interface{}
jsonmap, _ = streams.Serialize(followAccept) jsonmap, _ = streams.Serialize(followAccept)
b, _ := json.Marshal(jsonmap) b, _ := json.Marshal(jsonmap)
req, err := CreateSignedRequest(b, inbox, localAccountIRI) req, err := crypto.CreateSignedRequest(b, inbox, localAccountIRI)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,18 +1,13 @@
package requests package requests
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"net/url"
"github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab" "github.com/go-fed/activity/streams/vocab"
"github.com/owncast/owncast/activitypub/crypto" "github.com/owncast/owncast/activitypub/crypto"
"github.com/owncast/owncast/config"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -55,21 +50,3 @@ func WriteResponse(payload []byte, w http.ResponseWriter, publicKey crypto.Publi
return nil return nil
} }
// CreateSignedRequest will create a signed POST request of a payload to the provided destination.
func CreateSignedRequest(payload []byte, url *url.URL, fromActorIRI *url.URL) (*http.Request, error) {
log.Debugln("Sending", string(payload), "to", url)
req, _ := http.NewRequest("POST", url.String(), bytes.NewBuffer(payload))
ua := fmt.Sprintf("%s; https://owncast.online", config.GetReleaseString())
req.Header.Set("User-Agent", ua)
req.Header.Set("Content-Type", "application/activity+json")
if err := crypto.SignRequest(req, payload, fromActorIRI); err != nil {
log.Errorln("error signing request:", err)
return nil, err
}
return req, nil
}