+37
-13
knotserver/db/pubkeys.go
+37
-13
knotserver/db/pubkeys.go
···
1
1
package db
2
2
3
-
import "time"
3
+
import (
4
+
"time"
5
+
6
+
shbild "github.com/icyphox/bild/api/bild"
7
+
)
8
+
9
+
type PublicKey struct {
10
+
Did string
11
+
shbild.PublicKey
12
+
}
13
+
14
+
func (d *DB) AddPublicKeyFromRecord(recordIface map[string]interface{}) error {
15
+
record := make(map[string]string)
16
+
for k, v := range recordIface {
17
+
if str, ok := v.(string); ok {
18
+
record[k] = str
19
+
}
20
+
}
4
21
5
-
func (d *DB) AddPublicKey(did, name, key string) error {
22
+
pk := PublicKey{
23
+
Did: record["did"],
24
+
}
25
+
pk.Name = record["name"]
26
+
pk.Key = record["key"]
27
+
pk.Created = record["created"]
28
+
29
+
return d.AddPublicKey(pk)
30
+
}
31
+
32
+
func (d *DB) AddPublicKey(pk PublicKey) error {
33
+
if pk.Created == "" {
34
+
pk.Created = time.Now().Format("2006-01-02 15:04:05.99999999 -0700 MST m=-0000.000000000")
35
+
}
36
+
6
37
query := `insert into public_keys (did, name, key, created) values (?, ?, ?, ?)`
7
-
_, err := d.db.Exec(query, did, name, key, time.Now())
38
+
_, err := d.db.Exec(query, pk.Did, pk.Name, pk.Key, pk.Created)
8
39
return err
9
40
}
10
41
···
14
45
return err
15
46
}
16
47
17
-
type PublicKey struct {
18
-
Key string
19
-
Name string
20
-
DID string
21
-
Created time.Time
22
-
}
23
-
24
48
func (pk *PublicKey) JSON() map[string]interface{} {
25
49
return map[string]interface{}{
26
-
pk.DID: map[string]interface{}{
50
+
pk.Did: map[string]interface{}{
27
51
"key": pk.Key,
28
52
"name": pk.Name,
29
53
"created": pk.Created,
···
42
66
43
67
for rows.Next() {
44
68
var publicKey PublicKey
45
-
if err := rows.Scan(&publicKey.Key, &publicKey.Name, &publicKey.DID, &publicKey.Created); err != nil {
69
+
if err := rows.Scan(&publicKey.Key, &publicKey.Name, &publicKey.Did, &publicKey.Created); err != nil {
46
70
return nil, err
47
71
}
48
72
keys = append(keys, publicKey)
···
66
90
67
91
for rows.Next() {
68
92
var publicKey PublicKey
69
-
if err := rows.Scan(&publicKey.DID, &publicKey.Key, &publicKey.Name, &publicKey.Created); err != nil {
93
+
if err := rows.Scan(&publicKey.Did, &publicKey.Key, &publicKey.Name, &publicKey.Created); err != nil {
70
94
return nil, err
71
95
}
72
96
keys = append(keys, publicKey)
+20
-31
knotserver/handler.go
+20
-31
knotserver/handler.go
···
56
56
})
57
57
})
58
58
59
+
// Create a new repository
59
60
r.Route("/repo", func(r chi.Router) {
61
+
r.Use(h.VerifySignature)
60
62
r.Put("/new", h.NewRepo)
61
63
})
62
64
65
+
// Add a new user to the knot
66
+
// r.With(h.VerifySignature).Put("/user", h.AddUser)
67
+
68
+
// Health check. Used for two-way verification with appview.
63
69
r.With(h.VerifySignature).Get("/health", h.Health)
64
70
65
-
r.Group(func(r chi.Router) {
66
-
r.Use(h.VerifySignature)
67
-
r.Get("/keys", h.Keys)
68
-
r.Put("/keys", h.Keys)
69
-
})
71
+
// All public keys on the knot
72
+
r.Get("/keys", h.Keys)
70
73
71
74
return r, nil
72
75
}
···
90
93
}
91
94
92
95
if kind, ok := data["kind"].(string); ok && kind == "commit" {
93
-
log.Printf("commit event: %+v", data)
96
+
commit := data["commit"].(map[string]interface{})
97
+
98
+
switch commit["collection"].(string) {
99
+
case tangled.PublicKeyNSID:
100
+
record := commit["record"].(map[string]interface{})
101
+
if err := h.db.AddPublicKeyFromRecord(record); err != nil {
102
+
log.Printf("failed to add public key: %v", err)
103
+
}
104
+
log.Printf("added public key from firehose: %s", data["did"])
105
+
default:
106
+
}
94
107
}
108
+
95
109
}
96
110
}()
97
111
98
-
log.Printf("started jetstream")
99
-
100
112
return nil
101
113
}
102
-
103
-
func (h *Handle) Multiplex(w http.ResponseWriter, r *http.Request) {
104
-
path := chi.URLParam(r, "*")
105
-
106
-
if r.URL.RawQuery == "service=git-receive-pack" {
107
-
w.WriteHeader(http.StatusBadRequest)
108
-
w.Write([]byte("no pushing allowed!"))
109
-
return
110
-
}
111
-
112
-
fmt.Println(r.URL.RawQuery)
113
-
fmt.Println(r.Method)
114
-
115
-
if path == "info/refs" &&
116
-
r.URL.RawQuery == "service=git-upload-pack" &&
117
-
r.Method == "GET" {
118
-
h.InfoRefs(w, r)
119
-
} else if path == "git-upload-pack" && r.Method == "POST" {
120
-
h.UploadPack(w, r)
121
-
} else if r.Method == "GET" {
122
-
h.RepoIndex(w, r)
123
-
}
124
-
}
+1
-1
knotserver/routes.go
+1
-1
knotserver/routes.go
···
356
356
writeError(w, "invalid pubkey", http.StatusBadRequest)
357
357
}
358
358
359
-
if err := h.db.AddPublicKey(pk.DID, pk.Name, pk.Key); err != nil {
359
+
if err := h.db.AddPublicKey(pk); err != nil {
360
360
writeError(w, err.Error(), http.StatusInternalServerError)
361
361
log.Printf("adding public key: %s", err)
362
362
return