From 032ce43dd55ea095a48020fcd53b5633daa56f31 Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Tue, 3 Jan 2023 22:15:13 -0500 Subject: start of remote public key fetching --- main.go | 2 +- registry/registry.go | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 453a4e3..e5f530a 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,7 @@ func main() { persister := models.NewPersister(log, db) // Registry - registry, err := registry.NewRegistry(cfg, persister) + registry, err := registry.NewRegistry(cfg, persister, log) if err != nil { log.Fatal(err) } diff --git a/registry/registry.go b/registry/registry.go index dc83328..50986c9 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -2,9 +2,12 @@ package registry import ( "bytes" + "context" "crypto/x509" "encoding/gob" + "encoding/json" "encoding/pem" + "io" "net/http" "net/url" "strings" @@ -13,6 +16,10 @@ import ( "git.capotej.com/capotej/communique/models" "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" ) type Handler struct { @@ -24,9 +31,10 @@ type Registry struct { cfg config.Config persister *models.Persister handlerMap map[string]Handler + log *zap.SugaredLogger } -func NewRegistry(cfg config.Config, persister *models.Persister) (*Registry, error) { +func NewRegistry(cfg config.Config, persister *models.Persister, log *zap.SugaredLogger) (*Registry, error) { reg := Registry{cfg: cfg, persister: persister} reg.handlerMap = make(map[string]Handler) for _, v := range cfg.Handlers { @@ -153,10 +161,36 @@ func (r *Registry) Inbox(name string, req *http.Request) error { if handler == nil { return nil } - // verifier, err := httpsig.NewVerifier(req) - // if err != nil { - // return err - // } + logger := r.log.With("type", "inbox") + + verifier, err := httpsig.NewVerifier(req) + if err != nil { + return err + } + + keyId := verifier.KeyId() + logger.With("keyId", keyId).Debugf("fetching") + resp, err := http.Get(keyId) + if err != nil { + return err + } + defer resp.Body.Close() + keyPage, err := io.ReadAll(resp.Body) + logger.With("keyId", keyId).With("response", string(keyPage)).Debugf("received response") + var keyPageData map[string]interface{} + err = json.Unmarshal(keyPage, &keyPageData) + if err != nil { + return err + } + var t vocab.ActivityStreamsPerson + streams.ToType(context.Background(), keyPageData) + pubKeyProp := t.GetW3IDSecurityV1PublicKey() + iter := pubKeyProp.At(0) + pubKey := iter.Get() + pemProp := pubKey.GetW3IDSecurityV1PublicKeyPem() + pem := pemProp.Get() + logger.With("keyId", keyId).With("pem", pem).Debugf("extracted pem") + return nil } -- cgit v1.2.3