BlueGit

timfa bluegit

Files

bluegit / internal / server / git_http.go

git_http.go

feat: bied dezelfde acties als de UI aan via /api/v1
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
d89195e9 History
git_http.go 3.1 KiB Download Raw
1package server
2
3import (
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
12func (s *Server) gitHTTP(w http.ResponseWriter, r *http.Request) {
13 if s.StoragePath() == "" {
14 http.Error(w, s.noStorageText(r), 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, scopes, 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, s.tr(r, "err.rights"), http.StatusInternalServerError)
37 return
38 }
39 }
40 if write {
41 if !authed {
42 s.askAuth(w)
43 return
44 }
45 if !scopes.RepoWrite() {
46 http.Error(w, s.tr(r, "err.apiScope"), http.StatusForbidden)
47 return
48 }
49 if !role.CanWriteCode() {
50 http.Error(w, s.tr(r, "err.noWrite"), http.StatusForbidden)
51 return
52 }
53 } else if !role.CanRead() && repo.Visibility != "public" {
54 if !authed {
55 s.askAuth(w)
56 return
57 }
58 http.Error(w, s.tr(r, "err.noRead"), http.StatusForbidden)
59 return
60 } else if authed && !scopes.RepoRead() {
61 http.Error(w, s.tr(r, "err.apiScope"), http.StatusForbidden)
62 return
63 }
64 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
65 var before map[string]string
66 var extra []string
67 if write {
68 before, _ = gitrepo.Refs(r.Context(), s.Git, dir)
69 patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
70 tagPatterns, _ := s.Store.ProtectedTagPatterns(r.Context(), repo.ID)
71 extra = gitrepo.PushEnv(role, patterns, tagPatterns)
72 }
73 rest := strings.TrimPrefix(r.URL.Path, "/"+owner+"/"+repoSeg)
74 pathInfo := gitrepo.PathInfo(owner, repoSeg, rest)
75 if err := gitrepo.ServeSmartHTTP(w, r, s.Git, gitrepo.ProjectRoot(s.StoragePath()), pathInfo, extra); err != nil && s.Log != nil {
76 s.Log.Error("git http", "err", err, "path", r.URL.Path)
77 }
78 if write {
79 after, err := gitrepo.Refs(r.Context(), s.Git, dir)
80 if err == nil {
81 s.AfterPush(repo, user, before, after)
82 }
83 }
84}
85
86func (s *Server) gitUser(r *http.Request) (store.User, access.Scopes, bool) {
87 _, pass, ok := r.BasicAuth()
88 if !ok {
89 h := r.Header.Get("Authorization")
90 if strings.HasPrefix(strings.ToLower(h), "bearer ") {
91 pass = strings.TrimSpace(h[7:])
92 ok = pass != ""
93 }
94 }
95 if !ok || !strings.HasPrefix(pass, "bg_") {
96 return store.User{}, access.Scopes{}, false
97 }
98 auth, err := s.Store.AuthenticateToken(r.Context(), pass)
99 if err != nil {
100 return store.User{}, access.Scopes{}, false
101 }
102 return auth.User, access.StoredScopes(auth.Token.Scopes), true
103}
104
105func (s *Server) askAuth(w http.ResponseWriter) {
106 w.Header().Set("WWW-Authenticate", `Basic realm="BlueGit"`)
107 http.Error(w, "aanmelden met een toegangstoken als wachtwoord", http.StatusUnauthorized)
108}