package web import ( "errors" "net/http" "net/url" "strings" "amadan.net/oficina/base/internal/equipo" "amadan.net/oficina/base/internal/store" ) // Connecting the person's Oficina account from the chat: a card link sends // them to /ask/oficina/connect, Base asks Equipo where to consent and sends // the browser there, and the browser returns to /ask/oficina/done, where // Base finishes it with the member's own bearer. The person never visits // Equipo. Neither route logs, stores or echoes the state or code. const oficinaDonePath = "/ask/oficina/done" // oficinaNote is the cookie that carries the outcome from /done to the next // Home, once. const oficinaNote = "oficina_note" // oficinaNotes are the outcomes the chat says, by the word they travel as. var oficinaNotes = map[string]string{ "connected": "Oficina connected.", "denied": "Oficina wasn't connected.", "expired": "That link expired. Try again.", "unavailable": "Couldn't reach Oficina. Try again later.", } // quiet marks a response as one no cache keeps and no Referer follows: its // URL may carry a code. func quiet(w http.ResponseWriter) { w.Header().Set("Cache-Control", "no-store") w.Header().Set("Referrer-Policy", "no-referrer") } // oficinaLink reports whether raw is somewhere Base may send the browser to // consent: an https address, or the Equipo origin, with a host and no user // info. func (a *app) oficinaLink(raw string) bool { u, err := url.Parse(raw) if err != nil || u.Host == "" || u.Hostname() == "" || u.User != nil || strings.HasPrefix(raw, "//") { return false } if u.Scheme == "https" { return true } eq, err := url.Parse(a.ask.client.Origin()) return err == nil && u.Scheme == eq.Scheme && u.Host == eq.Host } // oficinaBearer is the member's bearer, or says where to send them: the chat // is not connected, so /ask. func (a *app) oficinaBearer(w http.ResponseWriter, r *http.Request) (askConn, bool) { me, _ := CurrentMember(r) l, sec, state, err := a.askLink(r.Context(), me) switch { case err != nil: a.logger.Error("ask: link", "err", err) http.Error(w, "unavailable", http.StatusServiceUnavailable) case state != askReady: http.Redirect(w, r, "/ask", http.StatusSeeOther) default: return askConn{me, l.DeviceID, sec.Token}, true } return askConn{}, false } // oficinaBack sends the member Home, with the outcome for the chat to say // ("" says nothing), and the chat open so they see it. func (a *app) oficinaBack(w http.ResponseWriter, r *http.Request, me store.Member, outcome string) { if _, ok := oficinaNotes[outcome]; ok { if me.AskClosed { if err := store.SetAskClosed(r.Context(), a.db, me.ID, false); err != nil { a.logger.Error("ask: dock", "err", err) } } http.SetCookie(w, &http.Cookie{Name: oficinaNote, Value: outcome, Path: "/", MaxAge: 120, HttpOnly: true, Secure: strings.HasPrefix(a.cfg.Origin, "https://"), SameSite: http.SameSiteLaxMode}) } http.Redirect(w, r, "/", http.StatusSeeOther) } // oficinaLost handles Equipo refusing the bearer: reconnect. func (a *app) oficinaFailed(w http.ResponseWriter, r *http.Request, c askConn, err error) { if errors.Is(err, equipo.ErrUnauthenticated) { a.askLost(r.Context(), c.me, c.device) http.Redirect(w, r, "/ask", http.StatusSeeOther) return } a.logger.Warn("ask: oficina", "err", err) a.oficinaBack(w, r, c.me, "unavailable") } // askOficinaConnect is GET /ask/oficina/connect: ask Equipo where to // consent, and go there. It only starts a flow for the signed-in member. func (a *app) askOficinaConnect(w http.ResponseWriter, r *http.Request) { quiet(w) c, ok := a.oficinaBearer(w, r) if !ok { return } to, err := a.ask.client.OficinaConnect(r.Context(), c.token, a.cfg.Origin+oficinaDonePath) if err != nil { a.oficinaFailed(w, r, c, err) return } if !a.oficinaLink(to) { a.logger.Warn("ask: oficina consent address refused") a.oficinaBack(w, r, c.me, "unavailable") return } http.Redirect(w, r, to, http.StatusSeeOther) } // askOficinaDone is GET /ask/oficina/done: the browser is back from // consent. With a state and code, finish with the member's bearer; with an // outcome Equipo already knows (denied, expired, unavailable), just say it. func (a *app) askOficinaDone(w http.ResponseWriter, r *http.Request) { quiet(w) c, ok := a.oficinaBearer(w, r) if !ok { return } q := r.URL.Query() state, code := q.Get("oficina_state"), q.Get("oficina_code") if state == "" || code == "" { outcome := q.Get("oficina") if outcome == "connected" { // only finishing connects outcome = "" } a.oficinaBack(w, r, c.me, outcome) return } status, err := a.ask.client.OficinaFinish(r.Context(), c.token, state, code) if err != nil { a.oficinaFailed(w, r, c, err) return } if _, known := oficinaNotes[status]; !known { status = "unavailable" } a.oficinaBack(w, r, c.me, status) } // takeOficinaNote is the outcome waiting for this member's Home, said once: // the cookie is cleared as it is read. func takeOficinaNote(w http.ResponseWriter, r *http.Request) string { ck, err := r.Cookie(oficinaNote) if err != nil { return "" } http.SetCookie(w, &http.Cookie{Name: oficinaNote, Value: "", Path: "/", MaxAge: -1, HttpOnly: true}) return oficinaNotes[ck.Value] }