oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "net/http"
5 "net/url"
6 "strings"
7
8 "amadan.net/oficina/estilo.git/base"
9 "amadan.net/oficina/estilo.git/chrome"
10 "amadan.net/oficina/estilo.git/menu"
11 "amadan.net/oficina/home/handoff"
12 "amadan.net/rastrillo/rastrillo"
13 "amadan.net/rastrillo/rastrillo/sessions"
14 "github.com/go-chi/chi/v5"
15)
16
17// The shared chrome (estilo spec shared-chrome, §12.1 row 8). Every page
18// Home draws for a person, other than the public front page, sits under
19// estilo's 52px bar with the brand "Oficina":
20//
21// - on the directory, a signed-in person gets the account menu: who is
22// signed in, Passkeys and Recovery codes (pages on id.*), Appearance,
23// Help → Open source, and the one Sign out. Signed out, the corner is
24// "Sign in", except on the signed-out page itself.
25// - on id.*, the bar is the brand alone, linking back to the directory:
26// every page there is the door, and the credential origin renders
27// nothing account-shaped (AGENTS.md, Identity).
28//
29// No chrome.js: Home prefers no script (AGENTS.md), and every menu works
30// without it. The stated cost is estilo's: with scripts off, an open menu
31// closes when its own trigger is pressed again, not on Escape or a click
32// outside.
33
34// page is what every template is executed with: the handler's own data,
35// and the request's chrome.
36type page struct {
37 Data any
38 Chrome chrome.Bar
39 // Theme is <html data-theme>: "light", "dark", or empty to follow the
40 // system (the Appearance preference, per host).
41 Theme string
42 // Brand is the name at the head of a door or hop card: "Oficina ID"
43 // on id.*, as its sign-in door says (doorName); empty, which draws
44 // "Oficina", on the directory.
45 Brand string
46}
47
48// The account menu's and the bar's words. Home speaks English only today;
49// estilo's templates carry none of their own.
50var (
51 barLabels = chrome.BarLabels{Skip: "Skip to content", Menu: "Menu"}
52 accountLabels = chrome.AccountLabels{SignedInAs: "Signed in", Appearance: "Appearance", Help: "Help"}
53)
54
55// onCredential reports whether r arrived for id.* rather than the
56// directory.
57func (a *App) onCredential(r *http.Request) bool {
58 return r != nil && strings.ToLower(r.Host) == a.credentialHost
59}
60
61// pageFor wraps a handler's data in the request's chrome. door marks the
62// sign-in, sent and signed-out pages, whose corner stays empty. hop marks
63// a page that moves on by itself: its bar is the brand alone, as plain
64// text, so the page still holds exactly one link, the way on.
65func (a *App) pageFor(r *http.Request, status int, data any, door, hop bool) page {
66 p := page{Data: data}
67 if a.onCredential(r) {
68 p.Brand = doorName
69 }
70 brand := chrome.App{Name: suiteName, Slug: "oficina", Home: "/", Icon: versioned("/static/brand/icon.svg")}
71 if hop {
72 brand.Home = ""
73 p.Chrome = chrome.Bar{App: brand, Labels: barLabels}
74 return p
75 }
76 if a.onCredential(r) || r == nil {
77 brand.Home = a.origins.Directory + "/"
78 p.Chrome = chrome.Bar{App: brand, Labels: barLabels}
79 return p
80 }
81 p.Theme = chrome.ThemeAttr(chrome.Appearance(r))
82 p.Chrome = chrome.Bar{App: brand, Labels: barLabels}
83 if _, ok := sessions.Current(r); ok {
84 p.Chrome.Account = a.account(r, status)
85 } else if !door {
86 p.Chrome.SignIn = &chrome.Link{Href: signInHref(r), Label: "Sign in"}
87 }
88 return p
89}
90
91// account is the directory's account menu. The directory cannot read the
92// address (it holds a person ref, never an address: the one rule), so the
93// identity row says only "Signed in" and the trigger is the person glyph.
94//
95// The Team page (team-members.md §2) is linked here, once per directory team
96// the person is on.
97func (a *App) account(r *http.Request, status int) *chrome.Account {
98 var items []menu.Item
99 if s, ok := sessions.Current(r); ok {
100 items = a.teamMenu(r, s.Subject)
101 }
102 items = append(items,
103 menu.Item{Href: a.origins.Credential + "/passkey/enrol", Label: "Passkeys"},
104 menu.Item{Href: a.origins.Credential + "/passkey/codes", Label: "Recovery codes"},
105 menu.Item{Href: a.origins.Credential + signoutAllPath, Label: "Sign out all devices"},
106 )
107 return &chrome.Account{
108 TriggerLabel: "Account",
109 Labels: accountLabels,
110 Items: items,
111 Appearance: &chrome.AppearanceMenu{
112 Action: "/appearance",
113 Return: appearanceReturn(r, status),
114 Options: chrome.AppearanceOptions(chrome.Appearance(r), "Light", "Dark", "System"),
115 },
116 Help: []menu.Item{{Href: "/help/open-source", Label: "Open source"}},
117 SignOut: chrome.SignOut{Action: signoutPath, Label: "Sign out"},
118 }
119}
120
121// appearanceReturn is where choosing an appearance comes back to: this
122// page, if it is one a GET can come back to, otherwise the directory. Only
123// a GET that drew a 200 qualifies. A 404's or an error page's own path is
124// known not to work, and the path of a page drawn for a POST is POST-only,
125// so a 303 back to it would land on a 404. The path alone: no directory
126// page reads its query, and a query is where a crafted link would carry
127// its text.
128func appearanceReturn(r *http.Request, status int) string {
129 if r.Method != http.MethodGet || status != http.StatusOK {
130 return "/"
131 }
132 return r.URL.EscapedPath()
133}
134
135// signInHref starts the directory's sign-in and comes back to this page.
136// An error page comes back to the directory instead: its own URL is the
137// one thing known not to exist.
138func signInHref(r *http.Request) string {
139 back := "/"
140 if r.Method == http.MethodGet && r.URL.Path == "/help/open-source" {
141 back = r.URL.Path
142 }
143 return handoff.StartPath + "?return_to=" + url.QueryEscape(back)
144}
145
146// mountChrome serves estilo's base, menu and chrome stylesheets, on both
147// origins.
148func mountChrome(r chi.Router) {
149 mountCSS(r, "/static/estilo/base.css", base.CSS())
150 mountCSS(r, "/static/estilo/menu.css", menu.CSS())
151 mountCSS(r, "/static/estilo/chrome.css", chrome.CSS())
152}
153
154// wantsHTML reports whether the request is a browser's page load: its
155// Accept names text/html. Everything else (a fetch, curl, a monitor) gets
156// plain text, as it always has (spec §3.3).
157func wantsHTML(r *http.Request) bool {
158 return strings.Contains(r.Header.Get("Accept"), "text/html")
159}
160
161// errorWords is one status's error page: the tab title, the heading and
162// the line under it.
163type errorWords struct{ page, heading, body string }
164
165// errorCopy is estilo spec §3.2's copy for the statuses Home answers, and
166// rastrillo's error copy (locales/en.toml) for the rest an HTTP handler
167// can plausibly be asked to draw. Any other status gets errorFallback.
168var errorCopy = map[int]errorWords{
169 http.StatusBadRequest: {"Bad request", "That didn’t go through", "Something in what was sent wasn’t right. Go back and try again."},
170 http.StatusUnauthorized: {"Sign in needed", "You need to sign in", "This page is only for someone who is signed in. Sign in, then try again."},
171 http.StatusForbidden: {"No access", "You can’t see this", "Your account doesn’t have access here. If you think it should, ask whoever runs this site."},
172 http.StatusNotFound: {"Page not found", "We can’t find that page", "The link may be out of date, or the page may have moved."},
173 http.StatusMethodNotAllowed: {"Not available", "That can’t be done here", "This page doesn’t take that kind of request. Go back and try again from the page itself."},
174 http.StatusRequestEntityTooLarge: {"Too large", "That was too large", "What was sent is bigger than this page accepts. Try again with less."},
175 http.StatusTooManyRequests: {"Too many tries", "Too many tries", "Wait a minute, then try again."},
176 http.StatusInternalServerError: {"Something went wrong", "Something went wrong", "Try again in a moment. If it keeps happening, quote the reference below."},
177 http.StatusServiceUnavailable: {"Briefly unavailable", "We’re briefly unavailable", "Oficina is being updated or is busy. Try again in a minute."},
178}
179
180// errorFallback is the error page for a status with no words of its own.
181var errorFallback = errorWords{"Something’s not right", "Something’s not right", "That request couldn’t be completed. Go back and try again."}
182
183// errorCopyFor is status's words, or the general ones.
184func errorCopyFor(status int) errorWords {
185 if c, ok := errorCopy[status]; ok {
186 return c
187 }
188 return errorFallback
189}
190
191// errorPage answers status as estilo's error page, under the bar, for a
192// browser, and as plain text for anything else. A 500 carries a reference
193// to quote, which is also logged, so the two can be matched.
194func (a *App) errorPage(w http.ResponseWriter, r *http.Request, status int) {
195 w.Header().Set("Cache-Control", "no-store")
196 if !wantsHTML(r) {
197 http.Error(w, strings.ToLower(http.StatusText(status)), status)
198 return
199 }
200 c := errorCopyFor(status)
201 home := chrome.Link{Href: "/", Label: "Go to Oficina"}
202 if a.onCredential(r) {
203 home.Href = a.origins.Directory + "/"
204 }
205 data := chrome.ErrorPage{Status: status, Title: c.heading, Body: c.body, Home: home}
206 if status >= 500 {
207 data.Ref, data.RefLabel = rastrillo.NewRef(), "Reference:"
208 a.logger.Error("error page", "ref", data.Ref, "path", r.URL.Path)
209 if r.Method == http.MethodGet {
210 // Beside the way home, not instead of it: the way home is the
211 // one link known to work.
212 data.Back = &chrome.Link{Href: r.URL.RequestURI(), Label: "Try again"}
213 }
214 }
215 // On id.* the same words sit in the door's card (Paul, 2026-11-20:
216 // every id.* page in the door's card); elsewhere, estilo's error page.
217 name := "error.html"
218 if a.onCredential(r) {
219 name = "id_error.html"
220 }
221 a.renderPage(w, r, status, name, errorView{chrome.Title(c.page, suiteName), data})
222}
223
224// errorView is error.html's data: the tab title and estilo-error's.
225type errorView struct {
226 Title string
227 Error chrome.ErrorPage
228}
229
230// notFound is the 404 for an unknown route, on either origin.
231func (a *App) notFound(w http.ResponseWriter, r *http.Request) {
232 a.errorPage(w, r, http.StatusNotFound)
233}
234
235// methodNotAllowed: a browser's GET or HEAD for a path that only takes a
236// POST is, to the person who typed it, a page that is not there; anything
237// else is told 405 in plain text, as before.
238func (a *App) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
239 if (r.Method == http.MethodGet || r.Method == http.MethodHead) && wantsHTML(r) {
240 a.notFound(w, r)
241 return
242 }
243 w.Header().Set("Cache-Control", "no-store")
244 http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
245}
246
247// serverError is the 500 for a handler that failed after deciding to
248// answer: the error page with a reference.
249func (a *App) serverError(w http.ResponseWriter, r *http.Request) {
250 a.errorPage(w, r, http.StatusInternalServerError)
251}
252