internal/httpd/account.go
263 lines · 7864 bytes
1package httpd
2
3import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "net/http"
8 "net/url"
9 "strings"
10
11 "gitbay.org/gitbay/internal/control"
12 "gitbay.org/gitbay/internal/protocol"
13 "gitbay.org/gitbay/internal/store"
14)
15
16// accountKey is one SSH key as the settings page shows it: enough to
17// recognise which key this is without printing the whole blob.
18type accountKey struct {
19 Fingerprint string
20 Algo string
21 Scope string
22 Label string
23 Confirm string // the 8 characters after SHA256: — a label can be empty
24}
25
26type accountPGP struct {
27 Fingerprint string
28 UIDs []string
29 Expired bool
30 Revoked bool
31 Confirm string // the fingerprint's first 8 characters
32}
33
34// accountForm renders the account's own settings: keys, addresses, and the
35// commands for everything that stays on SSH.
36func (s *Server) accountForm(w http.ResponseWriter, r *http.Request, u store.User) {
37 var keys []accountKey
38 if list, err := s.st.ListSSHKeys(u.ID); err == nil {
39 for _, k := range list {
40 confirm := prefix8(strings.TrimPrefix(k.Fingerprint, "SHA256:"))
41 keys = append(keys, accountKey{Fingerprint: k.Fingerprint, Algo: k.Algo, Scope: k.Scope, Label: k.Label, Confirm: confirm})
42 }
43 }
44 var pgp []accountPGP
45 if list, err := s.st.ListPGPKeys(u.ID); err == nil {
46 for _, k := range list {
47 var uids []string
48 json.Unmarshal([]byte(k.UIDsJSON), &uids)
49 confirm := prefix8(k.Fingerprint)
50 pgp = append(pgp, accountPGP{
51 Fingerprint: k.Fingerprint, UIDs: uids,
52 Expired: k.ExpiresAt != nil, Revoked: k.RevokedAt != nil, Confirm: confirm,
53 })
54 }
55 }
56 emails, _ := s.st.ListEmails(u.ID)
57
58 var profile control.ProfileOut
59 s.runControlInto(u, []string{"profile", "show"}, &profile)
60 mailOn, _ := s.st.MailEnabled(u.ID)
61 watchOn, _ := s.st.WatchEnabled(u.ID)
62 theme, _ := s.st.Theme(u.ID)
63
64 s.render(w, "account.html", struct {
65 basePage
66 Tab string // marks the rail's Settings row as current
67 Keys []accountKey
68 PGP []accountPGP
69 Emails []store.Email
70 Profile control.ProfileOut
71 LinksText string
72 Host string
73 Notice string
74 Message string
75 MailOn bool
76 WatchOn bool
77 ThemeSetting string // system, light or dark: the form's selected option
78 }{s.baseFor(u), "account", keys, pgp, emails, profile, profileLinksText(profile.Links), s.cfg.SiteHost(),
79 s.takeFlash(w, r), r.URL.Query().Get("m"), mailOn, watchOn, theme})
80}
81
82// accountExport hands the browser the same bundle `account export`
83// writes. The command is ReadOnly, so a GET is enough; the response is an
84// attachment rather than a page because the bundle is a file to keep.
85func (s *Server) accountExport(w http.ResponseWriter, r *http.Request, u store.User) {
86 out, msg, code := s.runControlCode(u, []string{"account", "export"})
87 if code != protocol.ExitOK {
88 s.setFlash(w, msg)
89 http.Redirect(w, r, "/settings", http.StatusSeeOther)
90 return
91 }
92 w.Header().Set("Content-Type", "application/json")
93 w.Header().Set("X-Content-Type-Options", "nosniff")
94 w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", u.Username+".bundle"))
95 io.WriteString(w, out)
96}
97
98// profileLinksText turns a profile's links into the form the textarea
99// shows and reads back: one per line, "label|url" when there is a label
100// and the bare url otherwise.
101func profileLinksText(links []store.ProfileLink) string {
102 lines := make([]string, len(links))
103 for i, l := range links {
104 if l.Label != "" {
105 lines[i] = l.Label + "|" + l.URL
106 } else {
107 lines[i] = l.URL
108 }
109 }
110 return strings.Join(lines, "\n")
111}
112
113// profileLinkArgs turns the textarea back into the --link values profile
114// set expects: one per non-blank line, or a single empty one to clear the
115// list when the field was emptied.
116func profileLinkArgs(raw string) []string {
117 var links []string
118 for _, line := range strings.Split(raw, "\n") {
119 if line = strings.TrimSpace(line); line != "" {
120 links = append(links, line)
121 }
122 }
123 if links == nil {
124 return []string{""}
125 }
126 return links
127}
128
129// accountSubmit routes the account forms to their commands. Keys,
130// addresses and the profile are the whole surface — no secret is accepted
131// over the web.
132func (s *Server) accountSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
133 back := func(msg, note string) {
134 q := ""
135 if note != "" {
136 q = "?m=" + url.QueryEscape(note)
137 }
138 s.setFlash(w, msg)
139 http.Redirect(w, r, "/settings"+q, http.StatusSeeOther)
140 }
141
142 switch r.FormValue("field") {
143 case "key-add":
144 body := strings.TrimSpace(r.FormValue("key"))
145 if body == "" {
146 back("paste a public key in authorized_keys format", "")
147 return
148 }
149 argv := []string{"keys", "add"}
150 if scope := r.FormValue("scope"); scope == "git" {
151 argv = append(argv, "--scope", "git")
152 }
153 if label := strings.TrimSpace(r.FormValue("label")); label != "" {
154 argv = append(argv, "--label", label)
155 }
156 if msg, ok := s.runControlStdin(u, argv, body+"\n"); !ok {
157 back(msg, "")
158 return
159 }
160 back("", "key registered")
161 case "key-remove":
162 want := prefix8(strings.TrimPrefix(r.FormValue("fingerprint"), "SHA256:"))
163 if ok, msg := confirmed(r, want); !ok {
164 back(msg, "")
165 return
166 }
167 if _, msg, ok := s.runControl(u, []string{"keys", "remove", r.FormValue("fingerprint")}); !ok {
168 back(msg, "")
169 return
170 }
171 back("", "key removed")
172 case "pgp-add":
173 body := strings.TrimSpace(r.FormValue("key"))
174 if body == "" {
175 back("paste an armored OpenPGP public key", "")
176 return
177 }
178 if msg, ok := s.runControlStdin(u, []string{"pgp", "add"}, body+"\n"); !ok {
179 back(msg, "")
180 return
181 }
182 back("", "PGP key registered")
183 case "pgp-remove":
184 fp := r.FormValue("fingerprint")
185 want := prefix8(fp)
186 if ok, msg := confirmed(r, want); !ok {
187 back(msg, "")
188 return
189 }
190 if _, msg, ok := s.runControl(u, []string{"pgp", "remove", fp}); !ok {
191 back(msg, "")
192 return
193 }
194 back("", "PGP key removed")
195 case "email-add":
196 if _, msg, ok := s.runControl(u, []string{"email", "add", strings.TrimSpace(r.FormValue("address"))}); !ok {
197 back(msg, "")
198 return
199 }
200 back("", "check that inbox for a verification code")
201 case "email-verify":
202 if _, msg, ok := s.runControl(u, []string{"email", "verify", strings.TrimSpace(r.FormValue("code"))}); !ok {
203 back(msg, "")
204 return
205 }
206 back("", "address verified")
207 case "email-remove":
208 address := r.FormValue("address")
209 if ok, msg := confirmed(r, address); !ok {
210 back(msg, "")
211 return
212 }
213 if _, msg, ok := s.runControl(u, []string{"email", "remove", address}); !ok {
214 back(msg, "")
215 return
216 }
217 back("", "address removed")
218 case "email-primary":
219 if _, msg, ok := s.runControl(u, []string{"email", "primary", r.FormValue("address")}); !ok {
220 back(msg, "")
221 return
222 }
223 back("", "primary address changed")
224 case "theme":
225 if _, msg, ok := s.runControl(u, []string{"web", "theme", "set", r.FormValue("theme")}); !ok {
226 back(msg, "")
227 return
228 }
229 back("", "colour scheme saved")
230 case "notify-mail", "notify-watch":
231 pref := strings.TrimPrefix(r.FormValue("field"), "notify-")
232 state := "off"
233 if r.FormValue(pref) == "on" {
234 state = "on"
235 }
236 if _, msg, ok := s.runControl(u, []string{"notifications", "settings", pref, state}); !ok {
237 back(msg, "")
238 return
239 }
240 back("", "notification preferences saved")
241 case "profile":
242 format := r.FormValue("format")
243 if format != "org" {
244 format = "md"
245 }
246 argv := []string{"profile", "set",
247 "--description", r.FormValue("description"),
248 "--website", r.FormValue("website"),
249 "--about-format", format,
250 "--file", "-",
251 }
252 for _, link := range profileLinkArgs(r.FormValue("links")) {
253 argv = append(argv, "--link", link)
254 }
255 if msg, ok := s.runControlStdin(u, argv, r.FormValue("about")); !ok {
256 back(msg, "")
257 return
258 }
259 back("", "profile updated")
260 default:
261 back("unknown form", "")
262 }
263}