| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | |
| 9 | rauth "amadan.net/rastrillo/rastrillo/auth" |
| 10 | "amadan.net/rastrillo/rastrillo/sessions" |
| 11 | ) |
| 12 | |
| 13 | // beginSignin is POST /signin, rastrillo's Begin, with the destination kept |
| 14 | // Docs' way. |
| 15 | // |
| 16 | // The door's forms post return_to (the screen's ReturnTo), and rastrillo |
| 17 | // would carry it across the mail or Keymail round trip and land the new |
| 18 | // session there itself. Docs lands every local sign-in on |
| 19 | // /auth/local/complete instead (localLinkCompletePath): connect resumes a |
| 20 | // pending account link from there, which is why its README requires the |
| 21 | // local plugin's successful return to be that path, and only then does |
| 22 | // Docs send the person to the page they came from, from its own sealed |
| 23 | // return memory. So the posted return_to is remembered there and taken off |
| 24 | // the form before Begin reads it, and put back on Begin's redirect to the |
| 25 | // door (a problem, Sent, the Keymail continuation), so the next form there |
| 26 | // posts it again and the chooser's links keep it. |
| 27 | // |
| 28 | // With SigninScreen on, Begin never answers with a redirect off this |
| 29 | // origin: a Keymail address continues on the door (?continue=), whose |
| 30 | // meta refresh leaves for Keymail, so form-action stays 'self'. |
| 31 | func (h *handlers) beginSignin(next http.HandlerFunc) http.HandlerFunc { |
| 32 | return func(w http.ResponseWriter, r *http.Request) { |
| 33 | // Off the form whether or not the body parsed: Begin must never |
| 34 | // read a destination of its own. |
| 35 | to := "" |
| 36 | if r.ParseForm() == nil { |
| 37 | to = sessions.SafeReturnTo(r.PostForm.Get("return_to"), "") |
| 38 | } |
| 39 | if r.PostForm != nil { |
| 40 | r.PostForm.Del("return_to") |
| 41 | } |
| 42 | if r.Form != nil { |
| 43 | r.Form.Del("return_to") |
| 44 | } |
| 45 | // A post without one is a sign-in for the list (rastrillo's own |
| 46 | // rule), so a page another tab remembered does not win. |
| 47 | if h.rememberReturn != nil { |
| 48 | landing := to |
| 49 | if landing == "" { |
| 50 | landing = "/" |
| 51 | } |
| 52 | h.rememberReturn(w, landing) |
| 53 | } |
| 54 | result := &signinResponse{header: make(http.Header)} |
| 55 | next(result, r) |
| 56 | if to != "" && result.status == http.StatusSeeOther { |
| 57 | result.header.Set("Location", withReturnTo(result.header.Get("Location"), to)) |
| 58 | } |
| 59 | result.replay(w) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // withReturnTo puts return_to on a redirect to the door, and leaves any |
| 64 | // other Location as it is. |
| 65 | func withReturnTo(location, to string) string { |
| 66 | u, err := url.Parse(location) |
| 67 | if err != nil || u.Scheme != "" || u.Host != "" || u.Path != "/signin" { |
| 68 | return location |
| 69 | } |
| 70 | q := u.Query() |
| 71 | if q.Has("return_to") { |
| 72 | return location |
| 73 | } |
| 74 | q.Set("return_to", to) |
| 75 | u.RawQuery = q.Encode() |
| 76 | return u.String() |
| 77 | } |
| 78 | |
| 79 | type signinResponse struct { |
| 80 | header http.Header |
| 81 | status int |
| 82 | body bytes.Buffer |
| 83 | } |
| 84 | |
| 85 | func (w *signinResponse) Header() http.Header { return w.header } |
| 86 | func (w *signinResponse) WriteHeader(status int) { |
| 87 | if w.status == 0 { |
| 88 | w.status = status |
| 89 | } |
| 90 | } |
| 91 | func (w *signinResponse) Write(p []byte) (int, error) { |
| 92 | if w.status == 0 { |
| 93 | w.WriteHeader(http.StatusOK) |
| 94 | } |
| 95 | return w.body.Write(p) |
| 96 | } |
| 97 | |
| 98 | // replay writes the captured response to w. |
| 99 | func (w *signinResponse) replay(to http.ResponseWriter) { |
| 100 | for name, values := range w.header { |
| 101 | for _, value := range values { |
| 102 | to.Header().Add(name, value) |
| 103 | } |
| 104 | } |
| 105 | if w.status == 0 { |
| 106 | w.status = http.StatusOK |
| 107 | } |
| 108 | to.WriteHeader(w.status) |
| 109 | _, _ = to.Write(w.body.Bytes()) |
| 110 | } |
| 111 | |
| 112 | // signinDoor is what rastrillo/auth calls back into for the two pages it |
| 113 | // hands to the app (newAuthPlugin). The plugin is built before the |
| 114 | // handlers, so these are bound late, by New. |
| 115 | type signinDoor struct { |
| 116 | refused func(http.ResponseWriter, *http.Request) |
| 117 | confirm func(http.ResponseWriter, *http.Request, rauth.ConfirmPageData) |
| 118 | } |
| 119 | |
| 120 | // signinRefused is Config.Refused: Keymail or an emailed link verified |
| 121 | // an address the allowlist does not hold. It answers with the door, in a |
| 122 | // 403, with the refusal marked on the request context (refusedHere), so |
| 123 | // the door draws it in the screen's app-problem slot with the email |
| 124 | // field. rastrillo has already set any cookies the attempt clears. |
| 125 | // |
| 126 | // Rendered, not redirected to ?err=refused: a query parameter is |
| 127 | // something anybody can type, and the sentence means "you proved this |
| 128 | // address and it is not admitted". Only someone who has just verified |
| 129 | // the address reaches it, so it is no oracle. |
| 130 | func (h *handlers) signinRefused(w http.ResponseWriter, r *http.Request) { |
| 131 | w.Header().Set("Cache-Control", "no-store") |
| 132 | h.signin(&forcedStatus{ResponseWriter: w, status: http.StatusForbidden}, |
| 133 | r.WithContext(context.WithValue(r.Context(), refusedKey{}, true))) |
| 134 | } |
| 135 | |
| 136 | // confirmSignin is Config.RenderConfirm, the page an emailed link opens |
| 137 | // (GET /auth/verify): the door, whose SigninState answers StepConfirm on |
| 138 | // this request alone, with one Sign in button that POSTs the token back, |
| 139 | // where rastrillo spends it. The GET spends nothing, so a mail scanner |
| 140 | // that fetches every link cannot sign anybody in. rastrillo has already |
| 141 | // set no-store, Referrer-Policy strict-origin (the page's URL is a live |
| 142 | // credential) and the framing refusals; nothing here may loosen them. |
| 143 | func (h *handlers) confirmSignin(w http.ResponseWriter, r *http.Request, _ rauth.ConfirmPageData) { |
| 144 | h.signin(w, r) |
| 145 | } |
| 146 | |
| 147 | // refusedKey marks a request signinRefused is answering with the door. |
| 148 | type refusedKey struct{} |
| 149 | |
| 150 | // withRefusal marks r as a refusal the door draws: a session the signed-in |
| 151 | // routes no longer admit (admittedOnly, standaloneSignin). |
| 152 | // |
| 153 | // Only on the plain door: the chooser (?ways=) and a way's own page (?way=) |
| 154 | // are how a refused person reaches another way in, another Oficina ID |
| 155 | // account included, and a refusal there would draw the field again and |
| 156 | // leave "Other ways to sign in" going nowhere (Fable on #139, N1). |
| 157 | func withRefusal(r *http.Request) *http.Request { |
| 158 | if q := r.URL.Query(); q.Has("ways") || q.Has("way") { |
| 159 | return r |
| 160 | } |
| 161 | return r.WithContext(context.WithValue(r.Context(), refusedKey{}, true)) |
| 162 | } |
| 163 | |
| 164 | // refusedHere reports whether r is a refusal the door is rendering. |
| 165 | func refusedHere(r *http.Request) bool { |
| 166 | v, _ := r.Context().Value(refusedKey{}).(bool) |
| 167 | return v |
| 168 | } |
| 169 | |
| 170 | // forcedStatus answers with status whatever status the page writes. |
| 171 | type forcedStatus struct { |
| 172 | http.ResponseWriter |
| 173 | status int |
| 174 | wrote bool |
| 175 | } |
| 176 | |
| 177 | func (f *forcedStatus) WriteHeader(int) { |
| 178 | if !f.wrote { |
| 179 | f.wrote = true |
| 180 | f.ResponseWriter.WriteHeader(f.status) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func (f *forcedStatus) Write(p []byte) (int, error) { |
| 185 | f.WriteHeader(f.status) |
| 186 | return f.ResponseWriter.Write(p) |
| 187 | } |
| 188 | |