64 lines
1.2 kB
1
package knotserver
2
3
import (
4
"context"
5
"net/http"
6
7
"github.com/go-chi/chi/v5"
8
"github.com/sotangled/tangled/knotserver/db"
9
"github.com/sotangled/tangled/rbac"
10
)
11
12
type InternalHandle struct {
13
db *db.DB
14
e *rbac.Enforcer
15
}
16
17
func (h *InternalHandle) PushAllowed(w http.ResponseWriter, r *http.Request) {
18
user := r.URL.Query().Get("user")
19
repo := r.URL.Query().Get("repo")
20
21
if user == "" || repo == "" {
22
w.WriteHeader(http.StatusBadRequest)
23
return
24
}
25
26
ok, err := h.e.IsPushAllowed(user, ThisServer, repo)
27
if err != nil || !ok {
28
w.WriteHeader(http.StatusForbidden)
29
return
30
}
31
32
w.WriteHeader(http.StatusNoContent)
33
return
34
}
35
36
func (h *InternalHandle) InternalKeys(w http.ResponseWriter, r *http.Request) {
37
keys, err := h.db.GetAllPublicKeys()
38
if err != nil {
39
writeError(w, err.Error(), http.StatusInternalServerError)
40
return
41
}
42
43
data := make([]map[string]interface{}, 0)
44
for _, key := range keys {
45
j := key.JSON()
46
data = append(data, j)
47
}
48
writeJSON(w, data)
49
return
50
}
51
52
func Internal(ctx context.Context, db *db.DB, e *rbac.Enforcer) http.Handler {
53
r := chi.NewRouter()
54
55
h := InternalHandle{
56
db,
57
e,
58
}
59
60
r.Get("/push-allowed", h.PushAllowed)
61
r.Get("/keys", h.InternalKeys)
62
63
return r
64
}
65