forked from tangled.org/core
this repo has no description

knotserver: add public key from firehose

Changed files
+58 -45
knotserver
+37 -13
knotserver/db/pubkeys.go
···
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
+
}
+
}
-
func (d *DB) AddPublicKey(did, name, key string) error {
+
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")
+
}
+
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
}
···
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,
···
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)
···
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)
+20 -31
knotserver/handler.go
···
})
})
+
// 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
}
···
}
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)
-
}
-
}
+1 -1
knotserver/routes.go
···
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