+37
-13
knotserver/db/pubkeys.go
+37
-13
knotserver/db/pubkeys.go
···
1
package db
2
3
-
import "time"
4
5
-
func (d *DB) AddPublicKey(did, name, key string) error {
6
query := `insert into public_keys (did, name, key, created) values (?, ?, ?, ?)`
7
-
_, err := d.db.Exec(query, did, name, key, time.Now())
8
return err
9
}
10
···
14
return err
15
}
16
17
-
type PublicKey struct {
18
-
Key string
19
-
Name string
20
-
DID string
21
-
Created time.Time
22
-
}
23
-
24
func (pk *PublicKey) JSON() map[string]interface{} {
25
return map[string]interface{}{
26
-
pk.DID: map[string]interface{}{
27
"key": pk.Key,
28
"name": pk.Name,
29
"created": pk.Created,
···
42
43
for rows.Next() {
44
var publicKey PublicKey
45
-
if err := rows.Scan(&publicKey.Key, &publicKey.Name, &publicKey.DID, &publicKey.Created); err != nil {
46
return nil, err
47
}
48
keys = append(keys, publicKey)
···
66
67
for rows.Next() {
68
var publicKey PublicKey
69
-
if err := rows.Scan(&publicKey.DID, &publicKey.Key, &publicKey.Name, &publicKey.Created); err != nil {
70
return nil, err
71
}
72
keys = append(keys, publicKey)
···
1
package db
2
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
+
}
21
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
+
37
query := `insert into public_keys (did, name, key, created) values (?, ?, ?, ?)`
38
+
_, err := d.db.Exec(query, pk.Did, pk.Name, pk.Key, pk.Created)
39
return err
40
}
41
···
45
return err
46
}
47
48
func (pk *PublicKey) JSON() map[string]interface{} {
49
return map[string]interface{}{
50
+
pk.Did: map[string]interface{}{
51
"key": pk.Key,
52
"name": pk.Name,
53
"created": pk.Created,
···
66
67
for rows.Next() {
68
var publicKey PublicKey
69
+
if err := rows.Scan(&publicKey.Key, &publicKey.Name, &publicKey.Did, &publicKey.Created); err != nil {
70
return nil, err
71
}
72
keys = append(keys, publicKey)
···
90
91
for rows.Next() {
92
var publicKey PublicKey
93
+
if err := rows.Scan(&publicKey.Did, &publicKey.Key, &publicKey.Name, &publicKey.Created); err != nil {
94
return nil, err
95
}
96
keys = append(keys, publicKey)
+20
-31
knotserver/handler.go
+20
-31
knotserver/handler.go
···
56
})
57
})
58
59
r.Route("/repo", func(r chi.Router) {
60
r.Put("/new", h.NewRepo)
61
})
62
63
r.With(h.VerifySignature).Get("/health", h.Health)
64
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
-
})
70
71
return r, nil
72
}
···
90
}
91
92
if kind, ok := data["kind"].(string); ok && kind == "commit" {
93
-
log.Printf("commit event: %+v", data)
94
}
95
}
96
}()
97
98
-
log.Printf("started jetstream")
99
-
100
return nil
101
}
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
-
}
···
56
})
57
})
58
59
+
// Create a new repository
60
r.Route("/repo", func(r chi.Router) {
61
+
r.Use(h.VerifySignature)
62
r.Put("/new", h.NewRepo)
63
})
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.
69
r.With(h.VerifySignature).Get("/health", h.Health)
70
71
+
// All public keys on the knot
72
+
r.Get("/keys", h.Keys)
73
74
return r, nil
75
}
···
93
}
94
95
if kind, ok := data["kind"].(string); ok && kind == "commit" {
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
+
}
107
}
108
+
109
}
110
}()
111
112
return nil
113
}
+1
-1
knotserver/routes.go
+1
-1
knotserver/routes.go