| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | |
| 9 | "gitlab.com/Timfa/bluegit/internal/store" |
| 10 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 11 | ) |
| 12 | |
| 13 | func (s *Server) repoWebhookSecrets(w http.ResponseWriter, r *http.Request) { |
| 14 | repo, _, ok := s.webhookAPIRepo(w, r) |
| 15 | if !ok { |
| 16 | return |
| 17 | } |
| 18 | s.writeSecretChoices(w, s.repoSecretChoices(r, repo)) |
| 19 | } |
| 20 | |
| 21 | func (s *Server) groupWebhookSecrets(w http.ResponseWriter, r *http.Request) { |
| 22 | g, _, ok := s.webhookAPIGroup(w, r) |
| 23 | if !ok { |
| 24 | return |
| 25 | } |
| 26 | s.writeSecretChoices(w, s.groupSecretChoices(r, g)) |
| 27 | } |
| 28 | |
| 29 | func (s *Server) repoWebhookAPI(w http.ResponseWriter, r *http.Request) { |
| 30 | repo, _, ok := s.webhookAPIRepo(w, r) |
| 31 | if !ok { |
| 32 | return |
| 33 | } |
| 34 | s.writeWebhookCreate(w, r, "repo", repo.ID) |
| 35 | } |
| 36 | |
| 37 | func (s *Server) groupWebhookAPI(w http.ResponseWriter, r *http.Request) { |
| 38 | g, _, ok := s.webhookAPIGroup(w, r) |
| 39 | if !ok { |
| 40 | return |
| 41 | } |
| 42 | s.writeWebhookCreate(w, r, "group", g.ID) |
| 43 | } |
| 44 | |
| 45 | func (s *Server) writeSecretChoices(w http.ResponseWriter, choices []SecretChoice) { |
| 46 | if choices == nil { |
| 47 | choices = []SecretChoice{} |
| 48 | } |
| 49 | w.Header().Set("Content-Type", "application/json") |
| 50 | _ = json.NewEncoder(w).Encode(map[string]any{"secrets": choices}) |
| 51 | } |
| 52 | |
| 53 | func (s *Server) writeWebhookCreate(w http.ResponseWriter, r *http.Request, kind string, scopeID int64) { |
| 54 | var body struct { |
| 55 | Name string `json:"name"` |
| 56 | Preset string `json:"preset"` |
| 57 | BranchFilter string `json:"branch_filter"` |
| 58 | Events []string `json:"events"` |
| 59 | URL string `json:"url"` |
| 60 | Username string `json:"username"` |
| 61 | Avatar string `json:"avatar"` |
| 62 | Icon string `json:"icon"` |
| 63 | Alias string `json:"alias"` |
| 64 | Token string `json:"token"` |
| 65 | Chat string `json:"chat"` |
| 66 | Server string `json:"server"` |
| 67 | Topic string `json:"topic"` |
| 68 | Homeserver string `json:"homeserver"` |
| 69 | Room string `json:"room"` |
| 70 | Method string `json:"method"` |
| 71 | ContentType string `json:"content_type"` |
| 72 | Headers string `json:"headers"` |
| 73 | Body string `json:"body"` |
| 74 | } |
| 75 | if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil { |
| 76 | http.Error(w, s.tr(r, "err.json"), http.StatusBadRequest) |
| 77 | return |
| 78 | } |
| 79 | id, err := s.createWebhookRecord(r.Context(), kind, scopeID, strings.TrimSpace(body.Name), body.Preset, body.BranchFilter, body.Events, webhook.Input{ |
| 80 | URL: body.URL, Username: body.Username, Avatar: body.Avatar, Icon: body.Icon, Alias: body.Alias, |
| 81 | Token: body.Token, Chat: body.Chat, Server: body.Server, Topic: body.Topic, |
| 82 | Homeserver: body.Homeserver, Room: body.Room, Method: body.Method, ContentType: body.ContentType, |
| 83 | Headers: body.Headers, Body: body.Body, |
| 84 | }) |
| 85 | if err != nil { |
| 86 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 87 | return |
| 88 | } |
| 89 | w.Header().Set("Content-Type", "application/json") |
| 90 | w.WriteHeader(http.StatusCreated) |
| 91 | _ = json.NewEncoder(w).Encode(map[string]any{"id": id}) |
| 92 | } |
| 93 | |
| 94 | func (s *Server) webhookAPIRepo(w http.ResponseWriter, r *http.Request) (store.Repo, store.User, bool) { |
| 95 | u, ok := s.webhookAPIUser(w, r) |
| 96 | if !ok { |
| 97 | return store.Repo{}, store.User{}, false |
| 98 | } |
| 99 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 100 | if err != nil { |
| 101 | http.NotFound(w, r) |
| 102 | return store.Repo{}, store.User{}, false |
| 103 | } |
| 104 | role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID) |
| 105 | if err != nil || !role.CanManageSettings() { |
| 106 | http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden) |
| 107 | return store.Repo{}, store.User{}, false |
| 108 | } |
| 109 | return repo, u, true |
| 110 | } |
| 111 | |
| 112 | func (s *Server) webhookAPIGroup(w http.ResponseWriter, r *http.Request) (store.Group, store.User, bool) { |
| 113 | u, ok := s.webhookAPIUser(w, r) |
| 114 | if !ok { |
| 115 | return store.Group{}, store.User{}, false |
| 116 | } |
| 117 | g, err := s.Store.GroupByPath(r.Context(), r.PathValue("path")) |
| 118 | if err != nil { |
| 119 | http.NotFound(w, r) |
| 120 | return store.Group{}, store.User{}, false |
| 121 | } |
| 122 | role, err := s.Store.GroupRole(r.Context(), g.ID, u.ID) |
| 123 | if err != nil || !role.CanManageSettings() { |
| 124 | http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden) |
| 125 | return store.Group{}, store.User{}, false |
| 126 | } |
| 127 | return g, u, true |
| 128 | } |
| 129 | |
| 130 | func (s *Server) webhookAPIUser(w http.ResponseWriter, r *http.Request) (store.User, bool) { |
| 131 | token := bearer(r) |
| 132 | if token == "" { |
| 133 | token = strings.TrimSpace(r.Header.Get("PRIVATE-TOKEN")) |
| 134 | } |
| 135 | if !strings.HasPrefix(token, "bg_") { |
| 136 | http.Error(w, s.tr(r, "err.tokenAbsent"), http.StatusUnauthorized) |
| 137 | return store.User{}, false |
| 138 | } |
| 139 | u, err := s.Store.UserByToken(r.Context(), token) |
| 140 | if err != nil { |
| 141 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 142 | return store.User{}, false |
| 143 | } |
| 144 | return u, true |
| 145 | } |