| 1 | package connect |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/hmac" |
| 6 | "crypto/sha256" |
| 7 | "encoding/base64" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "mime" |
| 11 | "net/http" |
| 12 | "net/url" |
| 13 | |
| 14 | "amadan.net/oficina/home/connect/linkstore" |
| 15 | ) |
| 16 | |
| 17 | // ChoosePath is the member choice page (Home spec |
| 18 | // 2026-11-14-oficina-id-native.md §8). Mount ChoosePage at GET and |
| 19 | // ChoosePost at POST, outside the session guards: the person choosing has |
| 20 | // no session here. ChoosePost is an ordinary same-origin form, so it stays |
| 21 | // inside the app's CSRF middleware as well as checking Origin itself. |
| 22 | const ChoosePath = "/account/home/choose" |
| 23 | |
| 24 | const homeChoiceCookie = "__Host-oficina-home-choice" |
| 25 | const choiceCookieLifetime = 600 |
| 26 | |
| 27 | // ChoicePage is the choose page's form: both buttons POST Action with the |
| 28 | // field "choice" ("connect" or "new") and the hidden field "token". |
| 29 | type ChoicePage struct { |
| 30 | Action, Token string |
| 31 | } |
| 32 | |
| 33 | type choiceKey struct{} |
| 34 | |
| 35 | // ChoiceFor is the form for the choose page being rendered: Render is |
| 36 | // called with state "choose" and a request carrying it. The labels are the |
| 37 | // app's ("choose_title", "choose_body", "choose_connect_action" and |
| 38 | // "choose_new_action" in English). |
| 39 | func ChoiceFor(r *http.Request) (ChoicePage, bool) { |
| 40 | p, ok := r.Context().Value(choiceKey{}).(ChoicePage) |
| 41 | return p, ok |
| 42 | } |
| 43 | |
| 44 | // formKey is the choice form's HMAC key, derived from the storage key and |
| 45 | // bound to this origin, so a token is good for one proof on one instance. |
| 46 | func formKey(dek []byte, origin string) []byte { |
| 47 | m := hmac.New(sha256.New, dek) |
| 48 | m.Write([]byte("oficina-connect/choice-form/v1\x00" + origin)) |
| 49 | return m.Sum(nil) |
| 50 | } |
| 51 | |
| 52 | func (b *Bridge) choiceToken(secret string) string { |
| 53 | m := hmac.New(sha256.New, b.formKey) |
| 54 | m.Write([]byte(secret)) |
| 55 | return base64.RawURLEncoding.EncodeToString(m.Sum(nil)) |
| 56 | } |
| 57 | |
| 58 | // ChoosePage is GET /account/home/choose: "Welcome to the team", with |
| 59 | // "Connect my existing account" and "I'm new here — continue with Oficina |
| 60 | // ID". It consumes nothing; it needs the live, browser-bound proof that a |
| 61 | // Member's finish left. |
| 62 | func (b *Bridge) ChoosePage(w http.ResponseWriter, r *http.Request) { |
| 63 | if !b.guard(w, r, http.MethodGet, ChoosePath) { |
| 64 | return |
| 65 | } |
| 66 | if b.admit == nil { |
| 67 | b.fail(w, r, http.StatusNotFound) |
| 68 | return |
| 69 | } |
| 70 | secret, present, ok := homeCookie(r, homeChoiceCookie) |
| 71 | if !present || !ok { |
| 72 | b.fail(w, r, http.StatusForbidden) |
| 73 | return |
| 74 | } |
| 75 | switch err := b.links.ChoiceLive(r.Context(), secret, b.now()); { |
| 76 | case errors.Is(err, linkstore.ErrRefused): |
| 77 | setHomeCookie(w, homeChoiceCookie, "", -1) |
| 78 | b.fail(w, r, http.StatusForbidden) |
| 79 | return |
| 80 | case err != nil: |
| 81 | b.fail(w, r, http.StatusServiceUnavailable) |
| 82 | return |
| 83 | } |
| 84 | page := ChoicePage{Action: ChoosePath, Token: b.choiceToken(secret)} |
| 85 | b.render(w, r.WithContext(context.WithValue(r.Context(), choiceKey{}, page)), "choose", "", http.StatusOK) |
| 86 | } |
| 87 | |
| 88 | // ChoosePost is the choice. Its form token is an HMAC of the proof cookie |
| 89 | // and it requires this exact origin in Origin. "connect" consumes the proof |
| 90 | // and goes to local sign-in with the link-resuming marker, so the person |
| 91 | // lands on slice 1's linking. "new" refuses while any app session is |
| 92 | // present; otherwise it consumes the proof in its own short transaction |
| 93 | // and then, with no transaction open, re-checks Admit and that the subject |
| 94 | // still has no person, allocates through Unlinked, resolves and mints. A |
| 95 | // consumed proof that yields no session means signing in again. |
| 96 | func (b *Bridge) ChoosePost(w http.ResponseWriter, r *http.Request) { |
| 97 | if !b.guard(w, r, http.MethodPost, ChoosePath) { |
| 98 | return |
| 99 | } |
| 100 | if b.admit == nil { |
| 101 | b.fail(w, r, http.StatusNotFound) |
| 102 | return |
| 103 | } |
| 104 | origins := r.Header.Values("Origin") |
| 105 | if len(origins) != 1 || origins[0] != b.origin { |
| 106 | b.fail(w, r, http.StatusForbidden) |
| 107 | return |
| 108 | } |
| 109 | media, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) |
| 110 | if err != nil || media != "application/x-www-form-urlencoded" { |
| 111 | b.fail(w, r, http.StatusBadRequest) |
| 112 | return |
| 113 | } |
| 114 | body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 1024)) |
| 115 | if err != nil { |
| 116 | b.fail(w, r, http.StatusBadRequest) |
| 117 | return |
| 118 | } |
| 119 | form, err := url.ParseQuery(string(body)) |
| 120 | if err != nil || len(form) != 2 || len(form["choice"]) != 1 || len(form["token"]) != 1 { |
| 121 | b.fail(w, r, http.StatusBadRequest) |
| 122 | return |
| 123 | } |
| 124 | secret, present, ok := homeCookie(r, homeChoiceCookie) |
| 125 | if !present || !ok || !hmac.Equal([]byte(form["token"][0]), []byte(b.choiceToken(secret))) { |
| 126 | b.fail(w, r, http.StatusForbidden) |
| 127 | return |
| 128 | } |
| 129 | choice := form["choice"][0] |
| 130 | if choice != "connect" && choice != "new" { |
| 131 | b.fail(w, r, http.StatusBadRequest) |
| 132 | return |
| 133 | } |
| 134 | if choice == "new" && cookieCount(r, b.sessionCookie) != 0 { |
| 135 | // Allocation is reachable only with no app session at all, as at |
| 136 | // finish; the proof stays for a choice made without one. |
| 137 | b.fail(w, r, http.StatusForbidden) |
| 138 | return |
| 139 | } |
| 140 | taken, err := b.links.TakeChoice(r.Context(), secret, b.now()) |
| 141 | switch { |
| 142 | case errors.Is(err, linkstore.ErrRefused): |
| 143 | setHomeCookie(w, homeChoiceCookie, "", -1) |
| 144 | b.fail(w, r, http.StatusForbidden) |
| 145 | return |
| 146 | case err != nil: |
| 147 | // Nothing was consumed: the same proof may be tried again. |
| 148 | b.fail(w, r, http.StatusServiceUnavailable) |
| 149 | return |
| 150 | } |
| 151 | setHomeCookie(w, homeChoiceCookie, "", -1) |
| 152 | if choice == "connect" { |
| 153 | if b.rememberUnknown != nil { |
| 154 | b.rememberUnknown(w) |
| 155 | } |
| 156 | http.Redirect(w, r, SigninLocalPath, http.StatusSeeOther) |
| 157 | return |
| 158 | } |
| 159 | person, err := b.chooseNew(r.Context(), taken.Subject) |
| 160 | if errors.Is(err, linkstore.ErrRefused) { |
| 161 | if b.rememberUnknown != nil { |
| 162 | b.rememberUnknown(w) |
| 163 | } |
| 164 | b.render(w, r, "unknown", "", http.StatusForbidden) |
| 165 | return |
| 166 | } |
| 167 | if err != nil || person.ID <= 0 { |
| 168 | b.fail(w, r, http.StatusServiceUnavailable) |
| 169 | return |
| 170 | } |
| 171 | if err := b.mint(w, r, Session{Subject: taken.Subject, Method: HomeMethod, AuthTime: taken.AuthTime}, person); err != nil { |
| 172 | b.fail(w, r, http.StatusServiceUnavailable) |
| 173 | return |
| 174 | } |
| 175 | http.Redirect(w, r, "/", http.StatusSeeOther) |
| 176 | } |
| 177 | |
| 178 | // chooseNew is "I'm new here" for a subject, after its proof or its native |
| 179 | // request: a person found now (a link or a racing choice) is used as is; |
| 180 | // otherwise Admit again, then allocate. ErrRefused is "unknown", ErrStorage |
| 181 | // "cannot decide now". It holds no transaction across any hook. |
| 182 | func (b *Bridge) chooseNew(ctx context.Context, subject string) (Person, error) { |
| 183 | if p, err := b.Resolve(ctx, subject); !errors.Is(err, linkstore.ErrRefused) { |
| 184 | return p, err |
| 185 | } |
| 186 | switch err := b.admit(ctx, b.issuer, subject); { |
| 187 | case errors.Is(err, ErrNoPerson): |
| 188 | return Person{}, linkstore.ErrRefused |
| 189 | case err != nil: |
| 190 | return Person{}, linkstore.ErrStorage |
| 191 | } |
| 192 | return b.allocate(ctx, subject) |
| 193 | } |
| 194 | |
| 195 | // allocate is Unlinked followed by a fresh resolution (spec §7 step 4): the |
| 196 | // person is whoever the subject resolves to afterwards, so a hook that |
| 197 | // refused because a racing choice created the person still signs this one |
| 198 | // in, and a refusal with nobody found is "unknown", or "cannot decide now" |
| 199 | // when either step could not read. |
| 200 | func (b *Bridge) allocate(ctx context.Context, subject string) (Person, error) { |
| 201 | _, hookErr := homeOnlyPerson(b.unlinked(ctx, b.issuer, subject)) |
| 202 | p, err := b.Resolve(ctx, subject) |
| 203 | switch { |
| 204 | case err == nil: |
| 205 | return p, nil |
| 206 | case errors.Is(hookErr, linkstore.ErrStorage) || errors.Is(err, linkstore.ErrStorage): |
| 207 | return Person{}, linkstore.ErrStorage |
| 208 | } |
| 209 | return Person{}, linkstore.ErrRefused |
| 210 | } |
| 211 | |