| 1 | package web |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | |
| 9 | "amadan.net/oficina/base/internal/equipo" |
| 10 | "amadan.net/oficina/base/internal/store" |
| 11 | ) |
| 12 | |
| 13 | // Connecting the person's Oficina account from the chat: a card link sends |
| 14 | // them to /ask/oficina/connect, Base asks Equipo where to consent and sends |
| 15 | // the browser there, and the browser returns to /ask/oficina/done, where |
| 16 | // Base finishes it with the member's own bearer. The person never visits |
| 17 | // Equipo. Neither route logs, stores or echoes the state or code. |
| 18 | |
| 19 | const oficinaDonePath = "/ask/oficina/done" |
| 20 | |
| 21 | // oficinaNote is the cookie that carries the outcome from /done to the next |
| 22 | // Home, once. |
| 23 | const oficinaNote = "oficina_note" |
| 24 | |
| 25 | // oficinaNotes are the outcomes the chat says, by the word they travel as. |
| 26 | var oficinaNotes = map[string]string{ |
| 27 | "connected": "Oficina connected.", |
| 28 | "denied": "Oficina wasn't connected.", |
| 29 | "expired": "That link expired. Try again.", |
| 30 | "unavailable": "Couldn't reach Oficina. Try again later.", |
| 31 | } |
| 32 | |
| 33 | // quiet marks a response as one no cache keeps and no Referer follows: its |
| 34 | // URL may carry a code. |
| 35 | func quiet(w http.ResponseWriter) { |
| 36 | w.Header().Set("Cache-Control", "no-store") |
| 37 | w.Header().Set("Referrer-Policy", "no-referrer") |
| 38 | } |
| 39 | |
| 40 | // oficinaLink reports whether raw is somewhere Base may send the browser to |
| 41 | // consent: an https address, or the Equipo origin, with a host and no user |
| 42 | // info. |
| 43 | func (a *app) oficinaLink(raw string) bool { |
| 44 | u, err := url.Parse(raw) |
| 45 | if err != nil || u.Host == "" || u.Hostname() == "" || u.User != nil || strings.HasPrefix(raw, "//") { |
| 46 | return false |
| 47 | } |
| 48 | if u.Scheme == "https" { |
| 49 | return true |
| 50 | } |
| 51 | eq, err := url.Parse(a.ask.client.Origin()) |
| 52 | return err == nil && u.Scheme == eq.Scheme && u.Host == eq.Host |
| 53 | } |
| 54 | |
| 55 | // oficinaBearer is the member's bearer, or says where to send them: the chat |
| 56 | // is not connected, so /ask. |
| 57 | func (a *app) oficinaBearer(w http.ResponseWriter, r *http.Request) (askConn, bool) { |
| 58 | me, _ := CurrentMember(r) |
| 59 | l, sec, state, err := a.askLink(r.Context(), me) |
| 60 | switch { |
| 61 | case err != nil: |
| 62 | a.logger.Error("ask: link", "err", err) |
| 63 | http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| 64 | case state != askReady: |
| 65 | http.Redirect(w, r, "/ask", http.StatusSeeOther) |
| 66 | default: |
| 67 | return askConn{me, l.DeviceID, sec.Token}, true |
| 68 | } |
| 69 | return askConn{}, false |
| 70 | } |
| 71 | |
| 72 | // oficinaBack sends the member Home, with the outcome for the chat to say |
| 73 | // ("" says nothing), and the chat open so they see it. |
| 74 | func (a *app) oficinaBack(w http.ResponseWriter, r *http.Request, me store.Member, outcome string) { |
| 75 | if _, ok := oficinaNotes[outcome]; ok { |
| 76 | if me.AskClosed { |
| 77 | if err := store.SetAskClosed(r.Context(), a.db, me.ID, false); err != nil { |
| 78 | a.logger.Error("ask: dock", "err", err) |
| 79 | } |
| 80 | } |
| 81 | http.SetCookie(w, &http.Cookie{Name: oficinaNote, Value: outcome, Path: "/", MaxAge: 120, |
| 82 | HttpOnly: true, Secure: strings.HasPrefix(a.cfg.Origin, "https://"), SameSite: http.SameSiteLaxMode}) |
| 83 | } |
| 84 | http.Redirect(w, r, "/", http.StatusSeeOther) |
| 85 | } |
| 86 | |
| 87 | // oficinaLost handles Equipo refusing the bearer: reconnect. |
| 88 | func (a *app) oficinaFailed(w http.ResponseWriter, r *http.Request, c askConn, err error) { |
| 89 | if errors.Is(err, equipo.ErrUnauthenticated) { |
| 90 | a.askLost(r.Context(), c.me, c.device) |
| 91 | http.Redirect(w, r, "/ask", http.StatusSeeOther) |
| 92 | return |
| 93 | } |
| 94 | a.logger.Warn("ask: oficina", "err", err) |
| 95 | a.oficinaBack(w, r, c.me, "unavailable") |
| 96 | } |
| 97 | |
| 98 | // askOficinaConnect is GET /ask/oficina/connect: ask Equipo where to |
| 99 | // consent, and go there. It only starts a flow for the signed-in member. |
| 100 | func (a *app) askOficinaConnect(w http.ResponseWriter, r *http.Request) { |
| 101 | quiet(w) |
| 102 | c, ok := a.oficinaBearer(w, r) |
| 103 | if !ok { |
| 104 | return |
| 105 | } |
| 106 | to, err := a.ask.client.OficinaConnect(r.Context(), c.token, a.cfg.Origin+oficinaDonePath) |
| 107 | if err != nil { |
| 108 | a.oficinaFailed(w, r, c, err) |
| 109 | return |
| 110 | } |
| 111 | if !a.oficinaLink(to) { |
| 112 | a.logger.Warn("ask: oficina consent address refused") |
| 113 | a.oficinaBack(w, r, c.me, "unavailable") |
| 114 | return |
| 115 | } |
| 116 | http.Redirect(w, r, to, http.StatusSeeOther) |
| 117 | } |
| 118 | |
| 119 | // askOficinaDone is GET /ask/oficina/done: the browser is back from |
| 120 | // consent. With a state and code, finish with the member's bearer; with an |
| 121 | // outcome Equipo already knows (denied, expired, unavailable), just say it. |
| 122 | func (a *app) askOficinaDone(w http.ResponseWriter, r *http.Request) { |
| 123 | quiet(w) |
| 124 | c, ok := a.oficinaBearer(w, r) |
| 125 | if !ok { |
| 126 | return |
| 127 | } |
| 128 | q := r.URL.Query() |
| 129 | state, code := q.Get("oficina_state"), q.Get("oficina_code") |
| 130 | if state == "" || code == "" { |
| 131 | outcome := q.Get("oficina") |
| 132 | if outcome == "connected" { // only finishing connects |
| 133 | outcome = "" |
| 134 | } |
| 135 | a.oficinaBack(w, r, c.me, outcome) |
| 136 | return |
| 137 | } |
| 138 | status, err := a.ask.client.OficinaFinish(r.Context(), c.token, state, code) |
| 139 | if err != nil { |
| 140 | a.oficinaFailed(w, r, c, err) |
| 141 | return |
| 142 | } |
| 143 | if _, known := oficinaNotes[status]; !known { |
| 144 | status = "unavailable" |
| 145 | } |
| 146 | a.oficinaBack(w, r, c.me, status) |
| 147 | } |
| 148 | |
| 149 | // takeOficinaNote is the outcome waiting for this member's Home, said once: |
| 150 | // the cookie is cleared as it is read. |
| 151 | func takeOficinaNote(w http.ResponseWriter, r *http.Request) string { |
| 152 | ck, err := r.Cookie(oficinaNote) |
| 153 | if err != nil { |
| 154 | return "" |
| 155 | } |
| 156 | http.SetCookie(w, &http.Cookie{Name: oficinaNote, Value: "", Path: "/", MaxAge: -1, HttpOnly: true}) |
| 157 | return oficinaNotes[ck.Value] |
| 158 | } |
| 159 | |