package web import ( "encoding/base64" "net/http" "amadan.net/oficina/home/connect" "amadan.net/rastrillo/rastrillo/sessions" "amadan.net/oficina/base/internal/store" ) type settingsPage struct { Name string Address string // Passkeys is how many passkeys sign this member in; PasskeyAdded says // the page follows adding one. CanAddPasskey is false for an Oficina // session, which cannot file a passkey under the member's address. Passkeys int PasskeyAdded bool CanAddPasskey bool // UserID is the WebAuthn user handle a new passkey stores: the member's // random id, never their address. UserID string ModuleURL, RegisterBegin, RegisterEnd string // Oficina says Oficina ID is on, so signing out also signs out of the // Oficina account. Connect offers to connect one: there is no link yet. Oficina, Connect bool ConnectLabel string // Equipo is the member's Equipo connection (connect, pending, ready or // reconnect), empty with the chat off. EquipoPerson is the Equipo // address it speaks for, once Base has read it. Equipo, EquipoPerson string // Themes are the Appearance choices, the member's pressed. Themes []themeChoice } // themeChoice is one Appearance button in Settings. type themeChoice struct { Value, Label string On bool } // appearances are Settings' Appearance choices and what each stores: // System follows the device (""). var appearances = []struct{ value, label, stored string }{ {"system", "System", ""}, {"light", "Light", "light"}, {"dark", "Dark", "dark"}, } func (a *app) settings(w http.ResponseWriter, r *http.Request) { me, _ := CurrentMember(r) s, _ := sessions.Current(r) viaOficina := connect.IsHomeDerived(s.Method) // Passkeys are filed under the address a member signs in with, which an // Oficina session's subject is not. subject := s.Subject if viaOficina { subject = me.Email } keys, err := a.pk.List(subject) if err != nil { a.logger.Error("settings: passkeys", "err", err) http.Error(w, "unavailable", http.StatusServiceUnavailable) return } page := settingsPage{ Name: a.cfg.Name, Address: me.Email, Passkeys: len(keys), PasskeyAdded: len(keys) > 0 && r.URL.Query().Get("passkey") == "added", CanAddPasskey: !viaOficina, UserID: base64.RawURLEncoding.EncodeToString([]byte(me.ID)), ModuleURL: webauthnModulePath, RegisterBegin: registerBeginPath, RegisterEnd: registerFinishPath, } for _, c := range appearances { page.Themes = append(page.Themes, themeChoice{Value: c.value, Label: c.label, On: c.stored == me.Theme}) } if a.ask != nil { l, _, state, err := a.askLink(r.Context(), me) if err != nil { a.logger.Error("settings: equipo link", "err", err) http.Error(w, "unavailable", http.StatusServiceUnavailable) return } page.Equipo = homeState(state) if state == askReady { page.EquipoPerson = l.Person } } if a.oficina != nil { page.Oficina = true page.Connect = !a.oficina.linked(r.Context(), me) page.ConnectLabel = connect.English["menu_action"] // Sign out hops on to Oficina's /signout, which admits the hop by // its Referer when the browser sends Origin: null. strict-origin // sends this origin and nothing more. w.Header().Set("Referrer-Policy", "strict-origin") } w.Header().Set("Cache-Control", "no-store") a.render(w, r, "settings", page) } // settingsAppearance keeps the member's Appearance (System, Light or Dark): // a plain form, so it works without scripts. func (a *app) settingsAppearance(w http.ResponseWriter, r *http.Request) { me, _ := CurrentMember(r) choice := r.PostFormValue("theme") for _, c := range appearances { if c.value != choice { continue } if err := store.SetTheme(r.Context(), a.db, me.ID, c.stored); err != nil { a.logger.Error("settings: appearance", "err", err) http.Error(w, "unavailable", http.StatusServiceUnavailable) return } http.Redirect(w, r, "/settings", http.StatusSeeOther) return } http.Error(w, "bad request", http.StatusBadRequest) }