BlueGit

timfa bluegit

Files

bluegit / internal / server / api_account.go

api_account.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
api_account.go 5.2 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "strconv"
6 "time"
7
8 "gitlab.com/Timfa/bluegit/internal/access"
9 "gitlab.com/Timfa/bluegit/internal/i18n"
10 "gitlab.com/Timfa/bluegit/internal/store"
11)
12
13func rfcTime(t time.Time) any {
14 if t.IsZero() {
15 return nil
16 }
17 return t.UTC().Format(time.RFC3339)
18}
19
20func userJSON(u store.User, self bool) map[string]any {
21 out := map[string]any{
22 "id": u.ID, "username": u.Username, "avatar_url": u.AvatarURL,
23 }
24 if self {
25 out["instance_admin"] = u.InstanceAdmin
26 out["language"] = u.Language
27 }
28 return out
29}
30
31func tokenJSON(t store.TokenInfo, plain string) map[string]any {
32 out := map[string]any{
33 "id": t.ID, "name": t.Name, "prefix": t.Prefix,
34 "scopes": access.StoredScopes(t.Scopes).List(),
35 "created_at": rfcTime(t.CreatedAt),
36 "last_used_at": rfcTime(t.LastUsedAt),
37 "expires_at": rfcTime(t.ExpiresAt),
38 }
39 if plain != "" {
40 out["token"] = plain
41 }
42 return out
43}
44
45func (s *Server) apiGetUser(w http.ResponseWriter, r *http.Request) {
46 writeJSON(w, http.StatusOK, userJSON(apiUser(r), true))
47}
48
49func (s *Server) apiPutUser(w http.ResponseWriter, r *http.Request) {
50 var in struct {
51 Language string `json:"language"`
52 }
53 if err := readJSON(r, &in); err != nil {
54 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
55 return
56 }
57 lang, err := s.opSetLanguage(r.Context(), apiUser(r), in.Language)
58 if !s.apiOK(w, r, err) {
59 return
60 }
61 u := apiUser(r)
62 u.Language = string(lang)
63 writeJSON(w, http.StatusOK, userJSON(u, true))
64}
65
66func (s *Server) apiExport(w http.ResponseWriter, r *http.Request) {
67 writeJSON(w, http.StatusOK, s.opExport(r.Context(), apiUser(r)))
68}
69
70func (s *Server) apiUserByName(w http.ResponseWriter, r *http.Request) {
71 u, err := s.Store.UserByUsername(r.Context(), r.PathValue("username"))
72 if err != nil {
73 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
74 return
75 }
76 writeJSON(w, http.StatusOK, userJSON(u, false))
77}
78
79func (s *Server) apiListTokens(w http.ResponseWriter, r *http.Request) {
80 list, err := s.Store.ListTokens(r.Context(), apiUser(r).ID)
81 if !s.apiOK(w, r, err) {
82 return
83 }
84 page, per := pageQuery(r)
85 rows := make([]map[string]any, 0, len(list))
86 for _, t := range list {
87 rows = append(rows, tokenJSON(t, ""))
88 }
89 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
90}
91
92func (s *Server) apiCreateToken(w http.ResponseWriter, r *http.Request) {
93 var in struct {
94 Name string `json:"name"`
95 Scopes []string `json:"scopes"`
96 ExpiresAt string `json:"expires_at"`
97 }
98 if err := readJSON(r, &in); err != nil {
99 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
100 return
101 }
102 var exp time.Time
103 if in.ExpiresAt != "" {
104 parsed, err := time.Parse(time.RFC3339, in.ExpiresAt)
105 if err != nil {
106 s.writeAPIErr(w, r, fail(400, i18n.E("err.tokenExpired")))
107 return
108 }
109 exp = parsed
110 }
111 plain, info, err := s.opIssueToken(r.Context(), apiUser(r), in.Name, in.Scopes, exp)
112 if !s.apiOK(w, r, err) {
113 return
114 }
115 writeJSON(w, http.StatusCreated, tokenJSON(info, plain))
116}
117
118func (s *Server) apiDeleteToken(w http.ResponseWriter, r *http.Request) {
119 id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
120 if err != nil {
121 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
122 return
123 }
124 if !s.apiOK(w, r, s.opRevokeToken(r.Context(), apiUser(r), id)) {
125 return
126 }
127 writeJSON(w, http.StatusNoContent, nil)
128}
129
130func keyJSON(k store.SSHKey) map[string]any {
131 return map[string]any{
132 "id": k.ID, "name": k.Name, "key": k.PublicKey, "fingerprint": k.Fingerprint,
133 "created_at": rfcTime(k.CreatedAt),
134 }
135}
136
137func (s *Server) apiListKeys(w http.ResponseWriter, r *http.Request) {
138 list, err := s.Store.ListSSHKeys(r.Context(), apiUser(r).ID)
139 if !s.apiOK(w, r, err) {
140 return
141 }
142 page, per := pageQuery(r)
143 rows := make([]map[string]any, 0, len(list))
144 for _, k := range list {
145 rows = append(rows, keyJSON(k))
146 }
147 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
148}
149
150func (s *Server) apiAddKey(w http.ResponseWriter, r *http.Request) {
151 var in struct {
152 Name string `json:"name"`
153 Key string `json:"key"`
154 }
155 if err := readJSON(r, &in); err != nil {
156 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
157 return
158 }
159 if !s.apiOK(w, r, s.opAddSSH(r.Context(), apiUser(r), in.Name, in.Key)) {
160 return
161 }
162 writeJSON(w, http.StatusCreated, map[string]any{"name": in.Name})
163}
164
165func (s *Server) apiDeleteKey(w http.ResponseWriter, r *http.Request) {
166 id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
167 if err != nil {
168 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
169 return
170 }
171 if !s.apiOK(w, r, s.opRemoveSSH(r.Context(), apiUser(r), id)) {
172 return
173 }
174 writeJSON(w, http.StatusNoContent, nil)
175}
176
177func (s *Server) apiAdminStorage(w http.ResponseWriter, r *http.Request) {
178 if !apiUser(r).InstanceAdmin {
179 s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden")))
180 return
181 }
182 if r.Method == http.MethodGet {
183 writeJSON(w, http.StatusOK, map[string]any{"path": s.StoragePath()})
184 return
185 }
186 var in struct {
187 Path string `json:"path"`
188 }
189 if err := readJSON(r, &in); err != nil {
190 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
191 return
192 }
193 info, moved, err := s.opSetStorage(in.Path)
194 if !s.apiOK(w, r, err) {
195 return
196 }
197 writeJSON(w, http.StatusOK, map[string]any{"path": info.Path, "moved": moved, "free": info.Free})
198}