Skip to content

Commit

Permalink
Verify signature on Inbox requests
Browse files Browse the repository at this point in the history
  • Loading branch information
thebaer committed Aug 21, 2018
1 parent f30d700 commit 4acf46d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
7 changes: 7 additions & 0 deletions activitypub.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ func handleFetchInbox(app *app, w http.ResponseWriter, r *http.Request) error {
}
}

err = verifyRequest(app, r)
if err != nil {
logError("Unable to verify signature: %v", err)
return err
}
logInfo("Signature OK")

dump, err := httputil.DumpRequest(r, true)
if err != nil {
logError("Can't dump: %v", err)
Expand Down
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func Serve() {
log.Fatal(err)
}

initFederation(app)
err = initKeys(app)
if err != nil {
log.Fatal(err)
Expand Down
15 changes: 15 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,18 @@ func (app *app) getPost(id int64) (*Post, error) {
}
return &p, err
}

func (app *app) getActorKey(id string) ([]byte, error) {
k := []byte{}

stmt := "SELECT public_key FROM userkeys WHERE id = ?"
err := app.db.QueryRow(stmt, id).Scan(&k)
switch {
case err == sql.ErrNoRows:
return nil, impart.HTTPError{http.StatusNotFound, "Key not found"}
case err != nil:
return nil, err
}

return k, nil
}
30 changes: 30 additions & 0 deletions federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@ import (
"net/http/httputil"
)

var (
verifier *httpsig.Verifier
)

func initFederation(app *app) {
verifier = httpsig.NewSigHeaderVerifier(keyGetter{app})
}

type keyGetter struct {
app *app
}

func (kg keyGetter) GetKey(id string) interface{} {
k, err := kg.app.getActorKey(id)
if err != nil {
logError("Unable to get key: %v", err)
return nil
}
pubKey, err := activitypub.DecodePublicKey(k)
if err != nil {
logError("Unable to decode key: %v", err)
return err
}
return pubKey
}

func verifyRequest(app *app, r *http.Request) error {
return verifier.Verify(r)
}

func makeActivityPost(p *activitystreams.Person, url string, m interface{}) error {
logInfo("POST %s", url)
b, err := json.Marshal(m)
Expand Down

0 comments on commit 4acf46d

Please sign in to comment.