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