diff options
author | Julio Capote <jcapote@gmail.com> | 2023-01-04 03:15:13 +0000 |
---|---|---|
committer | Julio Capote <jcapote@gmail.com> | 2023-01-04 03:15:13 +0000 |
commit | 032ce43dd55ea095a48020fcd53b5633daa56f31 (patch) | |
tree | d280843f748123662d368f9035c6b4cd443f5473 | |
parent | 87f34305cf1a5d3b11beadc9497f9cf21f230a3d (diff) | |
download | communique-032ce43dd55ea095a48020fcd53b5633daa56f31.tar.gz |
start of remote public key fetching
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | registry/registry.go | 44 |
2 files changed, 40 insertions, 6 deletions
@@ -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 } |