| 1 | package connect |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/home/connect/linkstore" |
| 11 | "amadan.net/oficina/home/handoff" |
| 12 | ) |
| 13 | |
| 14 | // Link routes belong behind the app's CSRF, session, identity and admission |
| 15 | // middleware. They recheck the actual cookie at each sensitive transition. |
| 16 | func (b *Bridge) LinkPage(w http.ResponseWriter, r *http.Request) { |
| 17 | if !b.guard(w, r, http.MethodGet, "/account/home") { |
| 18 | return |
| 19 | } |
| 20 | session, err := b.actualSession(r) |
| 21 | if err != nil || session == nil { |
| 22 | b.fail(w, r, http.StatusForbidden) |
| 23 | return |
| 24 | } |
| 25 | person, err := b.existingPerson(r, *session) |
| 26 | if err != nil { |
| 27 | b.fail(w, r, http.StatusForbidden) |
| 28 | return |
| 29 | } |
| 30 | b.render(w, r, "start", person.Label, http.StatusOK) |
| 31 | } |
| 32 | func (b *Bridge) LinkStart(w http.ResponseWriter, r *http.Request) { |
| 33 | if !b.guard(w, r, http.MethodPost, "/account/home") { |
| 34 | return |
| 35 | } |
| 36 | if status, err := b.startLink(w, r); err != nil { |
| 37 | if status == 0 { |
| 38 | b.linkFailure(w, r, err) |
| 39 | } else { |
| 40 | b.fail(w, r, status) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // resumeLink is LinkStart for a person who just signed in locally after an |
| 46 | // Oficina assertion with no link arrived: the same checks, the same flow. |
| 47 | // It reports false having written nothing when the link cannot begin. |
| 48 | func (b *Bridge) resumeLink(w http.ResponseWriter, r *http.Request) bool { |
| 49 | _, err := b.startLink(w, r) |
| 50 | return err == nil |
| 51 | } |
| 52 | |
| 53 | // startLink begins a link for the actual fresh local session and sends the |
| 54 | // browser to the Home freshness check. It writes nothing unless it succeeds; |
| 55 | // on failure it returns the status to show, or 0 for linkFailure's. |
| 56 | func (b *Bridge) startLink(w http.ResponseWriter, r *http.Request) (int, error) { |
| 57 | for _, name := range []string{homeInitCookie, homeProofCookie} { |
| 58 | if _, _, ok := homeCookie(r, name); !ok { |
| 59 | return http.StatusBadRequest, errIdentityRefused |
| 60 | } |
| 61 | } |
| 62 | local, _, err := b.localBinding(r) |
| 63 | // The reviewed local reauthentication continuation is wired separately. |
| 64 | // Stale local authentication cannot begin or reuse a linking initiation. |
| 65 | if err != nil { |
| 66 | return http.StatusForbidden, err |
| 67 | } |
| 68 | path := "/" |
| 69 | if b.returnPath != nil { |
| 70 | if candidate := b.returnPath(r); handoff.SafePath(candidate) { |
| 71 | path = candidate |
| 72 | } |
| 73 | } |
| 74 | secret, err := b.links.Start(r.Context(), local, path, b.now()) |
| 75 | if err != nil { |
| 76 | return 0, err |
| 77 | } |
| 78 | setHomeCookie(w, homeInitCookie, secret, 300) |
| 79 | setHomeCookie(w, homeProofCookie, "", -1) |
| 80 | http.Redirect(w, r, "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm", http.StatusSeeOther) |
| 81 | return 0, nil |
| 82 | } |
| 83 | |
| 84 | // connectedQuery marks the confirm page's GET after a successful confirm. |
| 85 | // It shows "Accounts connected" only to a person whose link is stored, so |
| 86 | // a crafted URL can show nothing that is not already true. return_to is |
| 87 | // the page the link began from, revalidated before it becomes the page's |
| 88 | // way back. |
| 89 | const connectedQuery = "connected" |
| 90 | |
| 91 | type successReturnKey struct{} |
| 92 | |
| 93 | func connectedURL(path string) string { |
| 94 | return "/account/home/confirm?" + url.Values{connectedQuery: {"1"}, "return_to": {path}}.Encode() |
| 95 | } |
| 96 | |
| 97 | func (b *Bridge) ConfirmPage(w http.ResponseWriter, r *http.Request) { |
| 98 | if q, err := url.ParseQuery(r.URL.RawQuery); err == nil && q.Has(connectedQuery) { |
| 99 | b.connectedPage(w, r, q) |
| 100 | return |
| 101 | } |
| 102 | if !b.guard(w, r, http.MethodGet, "/account/home/confirm") { |
| 103 | return |
| 104 | } |
| 105 | secret, present, ok := homeCookie(r, homeProofCookie) |
| 106 | if !ok || !present { |
| 107 | b.fail(w, r, http.StatusBadRequest) |
| 108 | return |
| 109 | } |
| 110 | local, person, err := b.localBinding(r) |
| 111 | if err != nil { |
| 112 | b.fail(w, r, http.StatusForbidden) |
| 113 | return |
| 114 | } |
| 115 | if _, err := b.links.Pending(r.Context(), secret, local, b.now()); err != nil { |
| 116 | if errors.Is(err, linkstore.ErrRefused) { |
| 117 | setHomeCookie(w, homeProofCookie, "", -1) |
| 118 | } |
| 119 | b.linkFailure(w, r, err) |
| 120 | return |
| 121 | } |
| 122 | b.render(w, r, "confirm", person.Label, http.StatusOK) |
| 123 | } |
| 124 | func (b *Bridge) ConfirmPost(w http.ResponseWriter, r *http.Request) { |
| 125 | if !b.guard(w, r, http.MethodPost, "/account/home/confirm") { |
| 126 | return |
| 127 | } |
| 128 | secret, present, ok := homeCookie(r, homeProofCookie) |
| 129 | if !ok || !present { |
| 130 | b.fail(w, r, http.StatusBadRequest) |
| 131 | return |
| 132 | } |
| 133 | local, _, err := b.localBinding(r) |
| 134 | if err != nil { |
| 135 | b.fail(w, r, http.StatusForbidden) |
| 136 | return |
| 137 | } |
| 138 | pending, err := b.links.Confirm(r.Context(), secret, local, b.now()) |
| 139 | if err == nil || errors.Is(err, linkstore.ErrRefused) { |
| 140 | setHomeCookie(w, homeProofCookie, "", -1) |
| 141 | } |
| 142 | if err != nil { |
| 143 | b.linkFailure(w, r, err) |
| 144 | return |
| 145 | } |
| 146 | // Say that it worked (Paul, 2026-10-24: landing back on the page with |
| 147 | // nothing changed left him unable to tell). A GET, so a reload repeats |
| 148 | // the page rather than the POST. |
| 149 | http.Redirect(w, r, connectedURL(pending.ReturnPath), http.StatusSeeOther) |
| 150 | } |
| 151 | |
| 152 | // connectedPage is "Accounts connected", for the signed-in person whose |
| 153 | // account now has a stored link. |
| 154 | func (b *Bridge) connectedPage(w http.ResponseWriter, r *http.Request, q url.Values) { |
| 155 | w.Header().Set("Cache-Control", "no-store") |
| 156 | if r.Host != strings.TrimPrefix(b.origin, "https://") || r.URL.Path != "/account/home/confirm" { |
| 157 | b.fail(w, r, http.StatusNotFound) |
| 158 | return |
| 159 | } |
| 160 | if r.Method != http.MethodGet { |
| 161 | w.Header().Set("Allow", http.MethodGet) |
| 162 | b.fail(w, r, http.StatusMethodNotAllowed) |
| 163 | return |
| 164 | } |
| 165 | session, err := b.actualSession(r) |
| 166 | if err != nil || session == nil { |
| 167 | b.fail(w, r, http.StatusForbidden) |
| 168 | return |
| 169 | } |
| 170 | person, err := b.existingPerson(r, *session) |
| 171 | if err != nil { |
| 172 | b.fail(w, r, http.StatusForbidden) |
| 173 | return |
| 174 | } |
| 175 | linked, err := b.links.Linked(r.Context(), person.ID) |
| 176 | if err != nil { |
| 177 | b.fail(w, r, http.StatusServiceUnavailable) |
| 178 | return |
| 179 | } |
| 180 | if !linked { |
| 181 | b.fail(w, r, http.StatusForbidden) |
| 182 | return |
| 183 | } |
| 184 | back := "/" |
| 185 | if v, ok := q["return_to"]; ok && len(v) == 1 && handoff.SafePath(v[0]) { |
| 186 | back = v[0] |
| 187 | } |
| 188 | b.render(w, r.WithContext(context.WithValue(r.Context(), successReturnKey{}, back)), "success", person.Label, http.StatusOK) |
| 189 | } |
| 190 | func (b *Bridge) linkFailure(w http.ResponseWriter, r *http.Request, err error) { |
| 191 | status := http.StatusServiceUnavailable |
| 192 | if errors.Is(err, linkstore.ErrRefused) { |
| 193 | status = http.StatusForbidden |
| 194 | } |
| 195 | b.fail(w, r, status) |
| 196 | } |
| 197 | |