| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | |
| 7 | "gitlab.com/Timfa/bluegit/internal/access" |
| 8 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 9 | "gitlab.com/Timfa/bluegit/internal/store" |
| 10 | ) |
| 11 | |
| 12 | func (s *Server) gitHTTP(w http.ResponseWriter, r *http.Request) { |
| 13 | if s.StoragePath() == "" { |
| 14 | http.Error(w, noStorageMsg, http.StatusServiceUnavailable) |
| 15 | return |
| 16 | } |
| 17 | owner := r.PathValue("owner") |
| 18 | repoSeg := r.PathValue("repo") |
| 19 | if !strings.HasSuffix(repoSeg, ".git") { |
| 20 | http.NotFound(w, r) |
| 21 | return |
| 22 | } |
| 23 | name := strings.TrimSuffix(repoSeg, ".git") |
| 24 | repo, err := s.Store.RepoByPath(r.Context(), owner, name) |
| 25 | if err != nil { |
| 26 | http.NotFound(w, r) |
| 27 | return |
| 28 | } |
| 29 | service := r.URL.Query().Get("service") |
| 30 | write := service == "git-receive-pack" || strings.HasSuffix(r.URL.Path, "/git-receive-pack") |
| 31 | user, authed := s.gitUser(r) |
| 32 | var role access.Role |
| 33 | if authed { |
| 34 | role, err = s.Store.EffectiveRole(r.Context(), user.ID, repo.ID) |
| 35 | if err != nil { |
| 36 | http.Error(w, "rechten", http.StatusInternalServerError) |
| 37 | return |
| 38 | } |
| 39 | } |
| 40 | if write { |
| 41 | if !authed { |
| 42 | s.askAuth(w) |
| 43 | return |
| 44 | } |
| 45 | if !role.CanWriteCode() { |
| 46 | http.Error(w, "geen schrijfrechten", http.StatusForbidden) |
| 47 | return |
| 48 | } |
| 49 | } else if !role.CanRead() && repo.Visibility != "public" { |
| 50 | if !authed { |
| 51 | s.askAuth(w) |
| 52 | return |
| 53 | } |
| 54 | http.Error(w, "geen leesrechten", http.StatusForbidden) |
| 55 | return |
| 56 | } |
| 57 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 58 | var before map[string]string |
| 59 | var extra []string |
| 60 | if write { |
| 61 | before, _ = gitrepo.Refs(r.Context(), s.Git, dir) |
| 62 | patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID) |
| 63 | extra = gitrepo.PushEnv(role, patterns) |
| 64 | } |
| 65 | rest := strings.TrimPrefix(r.URL.Path, "/"+owner+"/"+repoSeg) |
| 66 | pathInfo := gitrepo.PathInfo(owner, repoSeg, rest) |
| 67 | if err := gitrepo.ServeSmartHTTP(w, r, s.Git, gitrepo.ProjectRoot(s.StoragePath()), pathInfo, extra); err != nil && s.Log != nil { |
| 68 | s.Log.Error("git http", "err", err, "path", r.URL.Path) |
| 69 | } |
| 70 | if write { |
| 71 | after, err := gitrepo.Refs(r.Context(), s.Git, dir) |
| 72 | if err == nil { |
| 73 | s.AfterPush(repo, user, before, after) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func (s *Server) gitUser(r *http.Request) (store.User, bool) { |
| 79 | _, pass, ok := r.BasicAuth() |
| 80 | if !ok { |
| 81 | h := r.Header.Get("Authorization") |
| 82 | if strings.HasPrefix(strings.ToLower(h), "bearer ") { |
| 83 | pass = strings.TrimSpace(h[7:]) |
| 84 | ok = pass != "" |
| 85 | } |
| 86 | } |
| 87 | if !ok || !strings.HasPrefix(pass, "bg_") { |
| 88 | return store.User{}, false |
| 89 | } |
| 90 | u, err := s.Store.UserByToken(r.Context(), pass) |
| 91 | return u, err == nil |
| 92 | } |
| 93 | |
| 94 | func (s *Server) askAuth(w http.ResponseWriter) { |
| 95 | w.Header().Set("WWW-Authenticate", `Basic realm="BlueGit"`) |
| 96 | http.Error(w, "aanmelden met een toegangstoken als wachtwoord", http.StatusUnauthorized) |
| 97 | } |