diff --git a/knotserver/db/pubkeys.go b/knotserver/db/pubkeys.go index 0ab6fd02..6bc131fb 100644 --- a/knotserver/db/pubkeys.go +++ b/knotserver/db/pubkeys.go @@ -1,10 +1,41 @@ package db -import "time" +import ( + "time" + + shbild "github.com/icyphox/bild/api/bild" +) + +type PublicKey struct { + Did string + shbild.PublicKey +} + +func (d *DB) AddPublicKeyFromRecord(recordIface map[string]interface{}) error { + record := make(map[string]string) + for k, v := range recordIface { + if str, ok := v.(string); ok { + record[k] = str + } + } + + pk := PublicKey{ + Did: record["did"], + } + pk.Name = record["name"] + pk.Key = record["key"] + pk.Created = record["created"] + + return d.AddPublicKey(pk) +} + +func (d *DB) AddPublicKey(pk PublicKey) error { + if pk.Created == "" { + pk.Created = time.Now().Format("2006-01-02 15:04:05.99999999 -0700 MST m=-0000.000000000") + } -func (d *DB) AddPublicKey(did, name, key string) error { query := `insert into public_keys (did, name, key, created) values (?, ?, ?, ?)` - _, err := d.db.Exec(query, did, name, key, time.Now()) + _, err := d.db.Exec(query, pk.Did, pk.Name, pk.Key, pk.Created) return err } @@ -14,16 +45,9 @@ func (d *DB) RemovePublicKey(did string) error { return err } -type PublicKey struct { - Key string - Name string - DID string - Created time.Time -} - func (pk *PublicKey) JSON() map[string]interface{} { return map[string]interface{}{ - pk.DID: map[string]interface{}{ + pk.Did: map[string]interface{}{ "key": pk.Key, "name": pk.Name, "created": pk.Created, @@ -42,7 +66,7 @@ func (d *DB) GetAllPublicKeys() ([]PublicKey, error) { for rows.Next() { var publicKey PublicKey - if err := rows.Scan(&publicKey.Key, &publicKey.Name, &publicKey.DID, &publicKey.Created); err != nil { + if err := rows.Scan(&publicKey.Key, &publicKey.Name, &publicKey.Did, &publicKey.Created); err != nil { return nil, err } keys = append(keys, publicKey) @@ -66,7 +90,7 @@ func (d *DB) GetPublicKeys(did string) ([]PublicKey, error) { for rows.Next() { var publicKey PublicKey - if err := rows.Scan(&publicKey.DID, &publicKey.Key, &publicKey.Name, &publicKey.Created); err != nil { + if err := rows.Scan(&publicKey.Did, &publicKey.Key, &publicKey.Name, &publicKey.Created); err != nil { return nil, err } keys = append(keys, publicKey) diff --git a/knotserver/handler.go b/knotserver/handler.go index b7b74dee..a81bd65a 100644 --- a/knotserver/handler.go +++ b/knotserver/handler.go @@ -56,17 +56,20 @@ func Setup(ctx context.Context, c *config.Config, db *db.DB) (http.Handler, erro }) }) + // Create a new repository r.Route("/repo", func(r chi.Router) { + r.Use(h.VerifySignature) r.Put("/new", h.NewRepo) }) + // Add a new user to the knot + // r.With(h.VerifySignature).Put("/user", h.AddUser) + + // Health check. Used for two-way verification with appview. r.With(h.VerifySignature).Get("/health", h.Health) - r.Group(func(r chi.Router) { - r.Use(h.VerifySignature) - r.Get("/keys", h.Keys) - r.Put("/keys", h.Keys) - }) + // All public keys on the knot + r.Get("/keys", h.Keys) return r, nil } @@ -90,35 +93,21 @@ func (h *Handle) StartJetstream(ctx context.Context) error { } if kind, ok := data["kind"].(string); ok && kind == "commit" { - log.Printf("commit event: %+v", data) + commit := data["commit"].(map[string]interface{}) + + switch commit["collection"].(string) { + case tangled.PublicKeyNSID: + record := commit["record"].(map[string]interface{}) + if err := h.db.AddPublicKeyFromRecord(record); err != nil { + log.Printf("failed to add public key: %v", err) + } + log.Printf("added public key from firehose: %s", data["did"]) + default: + } } + } }() - log.Printf("started jetstream") - return nil } - -func (h *Handle) Multiplex(w http.ResponseWriter, r *http.Request) { - path := chi.URLParam(r, "*") - - if r.URL.RawQuery == "service=git-receive-pack" { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("no pushing allowed!")) - return - } - - fmt.Println(r.URL.RawQuery) - fmt.Println(r.Method) - - if path == "info/refs" && - r.URL.RawQuery == "service=git-upload-pack" && - r.Method == "GET" { - h.InfoRefs(w, r) - } else if path == "git-upload-pack" && r.Method == "POST" { - h.UploadPack(w, r) - } else if r.Method == "GET" { - h.RepoIndex(w, r) - } -} diff --git a/knotserver/routes.go b/knotserver/routes.go index a971e73c..a44891d2 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -356,7 +356,7 @@ func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) { writeError(w, "invalid pubkey", http.StatusBadRequest) } - if err := h.db.AddPublicKey(pk.DID, pk.Name, pk.Key); err != nil { + if err := h.db.AddPublicKey(pk); err != nil { writeError(w, err.Error(), http.StatusInternalServerError) log.Printf("adding public key: %s", err) return