| 1 | package web |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "net/http" |
| 6 | |
| 7 | "amadan.net/oficina/home/connect" |
| 8 | "amadan.net/rastrillo/rastrillo/sessions" |
| 9 | |
| 10 | "amadan.net/oficina/base/internal/store" |
| 11 | ) |
| 12 | |
| 13 | type settingsPage struct { |
| 14 | Name string |
| 15 | Address string |
| 16 | // Passkeys is how many passkeys sign this member in; PasskeyAdded says |
| 17 | // the page follows adding one. CanAddPasskey is false for an Oficina |
| 18 | // session, which cannot file a passkey under the member's address. |
| 19 | Passkeys int |
| 20 | PasskeyAdded bool |
| 21 | CanAddPasskey bool |
| 22 | // UserID is the WebAuthn user handle a new passkey stores: the member's |
| 23 | // random id, never their address. |
| 24 | UserID string |
| 25 | ModuleURL, RegisterBegin, RegisterEnd string |
| 26 | // Oficina says Oficina ID is on, so signing out also signs out of the |
| 27 | // Oficina account. Connect offers to connect one: there is no link yet. |
| 28 | Oficina, Connect bool |
| 29 | ConnectLabel string |
| 30 | // Equipo is the member's Equipo connection (connect, pending, ready or |
| 31 | // reconnect), empty with the chat off. EquipoPerson is the Equipo |
| 32 | // address it speaks for, once Base has read it. |
| 33 | Equipo, EquipoPerson string |
| 34 | // Themes are the Appearance choices, the member's pressed. |
| 35 | Themes []themeChoice |
| 36 | } |
| 37 | |
| 38 | // themeChoice is one Appearance button in Settings. |
| 39 | type themeChoice struct { |
| 40 | Value, Label string |
| 41 | On bool |
| 42 | } |
| 43 | |
| 44 | // appearances are Settings' Appearance choices and what each stores: |
| 45 | // System follows the device (""). |
| 46 | var appearances = []struct{ value, label, stored string }{ |
| 47 | {"system", "System", ""}, {"light", "Light", "light"}, {"dark", "Dark", "dark"}, |
| 48 | } |
| 49 | |
| 50 | func (a *app) settings(w http.ResponseWriter, r *http.Request) { |
| 51 | me, _ := CurrentMember(r) |
| 52 | s, _ := sessions.Current(r) |
| 53 | viaOficina := connect.IsHomeDerived(s.Method) |
| 54 | // Passkeys are filed under the address a member signs in with, which an |
| 55 | // Oficina session's subject is not. |
| 56 | subject := s.Subject |
| 57 | if viaOficina { |
| 58 | subject = me.Email |
| 59 | } |
| 60 | keys, err := a.pk.List(subject) |
| 61 | if err != nil { |
| 62 | a.logger.Error("settings: passkeys", "err", err) |
| 63 | http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| 64 | return |
| 65 | } |
| 66 | page := settingsPage{ |
| 67 | Name: a.cfg.Name, Address: me.Email, |
| 68 | Passkeys: len(keys), |
| 69 | PasskeyAdded: len(keys) > 0 && r.URL.Query().Get("passkey") == "added", |
| 70 | CanAddPasskey: !viaOficina, |
| 71 | UserID: base64.RawURLEncoding.EncodeToString([]byte(me.ID)), |
| 72 | ModuleURL: webauthnModulePath, RegisterBegin: registerBeginPath, RegisterEnd: registerFinishPath, |
| 73 | } |
| 74 | for _, c := range appearances { |
| 75 | page.Themes = append(page.Themes, themeChoice{Value: c.value, Label: c.label, On: c.stored == me.Theme}) |
| 76 | } |
| 77 | if a.ask != nil { |
| 78 | l, _, state, err := a.askLink(r.Context(), me) |
| 79 | if err != nil { |
| 80 | a.logger.Error("settings: equipo link", "err", err) |
| 81 | http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| 82 | return |
| 83 | } |
| 84 | page.Equipo = homeState(state) |
| 85 | if state == askReady { |
| 86 | page.EquipoPerson = l.Person |
| 87 | } |
| 88 | } |
| 89 | if a.oficina != nil { |
| 90 | page.Oficina = true |
| 91 | page.Connect = !a.oficina.linked(r.Context(), me) |
| 92 | page.ConnectLabel = connect.English["menu_action"] |
| 93 | // Sign out hops on to Oficina's /signout, which admits the hop by |
| 94 | // its Referer when the browser sends Origin: null. strict-origin |
| 95 | // sends this origin and nothing more. |
| 96 | w.Header().Set("Referrer-Policy", "strict-origin") |
| 97 | } |
| 98 | w.Header().Set("Cache-Control", "no-store") |
| 99 | a.render(w, r, "settings", page) |
| 100 | } |
| 101 | |
| 102 | // settingsAppearance keeps the member's Appearance (System, Light or Dark): |
| 103 | // a plain form, so it works without scripts. |
| 104 | func (a *app) settingsAppearance(w http.ResponseWriter, r *http.Request) { |
| 105 | me, _ := CurrentMember(r) |
| 106 | choice := r.PostFormValue("theme") |
| 107 | for _, c := range appearances { |
| 108 | if c.value != choice { |
| 109 | continue |
| 110 | } |
| 111 | if err := store.SetTheme(r.Context(), a.db, me.ID, c.stored); err != nil { |
| 112 | a.logger.Error("settings: appearance", "err", err) |
| 113 | http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| 114 | return |
| 115 | } |
| 116 | http.Redirect(w, r, "/settings", http.StatusSeeOther) |
| 117 | return |
| 118 | } |
| 119 | http.Error(w, "bad request", http.StatusBadRequest) |
| 120 | } |
| 121 | |