| 1 | package slides |
| 2 | |
| 3 | // Returning to the page a sign-in began from, across the door (spec §9.4). |
| 4 | // |
| 5 | // rastrillo would carry return_to across the mail or Keymail round trip |
| 6 | // itself and land the new session there, skipping SignedInPath. Slides |
| 7 | // lands every local sign-in on one fixed path instead: /auth/landed |
| 8 | // standalone (landed), and with Home configured connect's |
| 9 | // /auth/local/complete, because connect resumes a pending account link |
| 10 | // from there, and only then is the person sent on, to "/". So the page is |
| 11 | // kept in Slides' own sealed, ten-minute return memory (auth.go's |
| 12 | // rememberReturn), read at /auth/landed and put in place of "/" at |
| 13 | // /auth/local/complete (homeLocalComplete). An Oficina ID sign-in keeps |
| 14 | // its page in connect's own memory (SigninOficina). |
| 15 | // |
| 16 | // The remembered path is validated by handoff.SafePath (a same-origin |
| 17 | // path: no scheme, no host, no //, no control characters, not /signin or |
| 18 | // /auth/*), so it can never become an off-site destination, and it |
| 19 | // authorizes nothing: not linking, and not access to the deck it names. |
| 20 | |
| 21 | import ( |
| 22 | "bytes" |
| 23 | "net/http" |
| 24 | "net/url" |
| 25 | |
| 26 | "amadan.net/oficina/home/connect" |
| 27 | rauth "amadan.net/rastrillo/rastrillo/auth" |
| 28 | "amadan.net/rastrillo/rastrillo/sessions" |
| 29 | ) |
| 30 | |
| 31 | // requestedReturn is the return_to a sign-in GET carries, when it is |
| 32 | // exactly one same-origin path. |
| 33 | func requestedReturn(r *http.Request) (string, bool) { |
| 34 | q, err := url.ParseQuery(r.URL.RawQuery) |
| 35 | if err != nil { |
| 36 | return "", false |
| 37 | } |
| 38 | return safeReturn(q) |
| 39 | } |
| 40 | |
| 41 | // rememberSigninReturn keeps the page a sign-in began from in the return |
| 42 | // memory, when the door is opened afresh: a plain GET of /signin or |
| 43 | // /signin/local that is not one of the door's states. A plain /signin |
| 44 | // remembers the list, so a page remembered by an earlier, abandoned |
| 45 | // attempt never wins over the one being made now. A plain /signin/local |
| 46 | // does not: connect sends people there (failed, unknown, "Connect my |
| 47 | // existing account") without return_to, mid-way through a sign-in that |
| 48 | // already remembered its page, so the door carries that page on its forms |
| 49 | // instead (st.ReturnTo), and the post remembers it again (beginSignin). |
| 50 | // The chooser's links carry return_to, so moving between ways keeps it. |
| 51 | // The confirm step and a refusal are drawn on /auth/*, and remember |
| 52 | // nothing. |
| 53 | func (a *app) rememberSigninReturn(w http.ResponseWriter, r *http.Request, st *rauth.SigninState) { |
| 54 | if r.Method != http.MethodGet || signinOutcome(r) || |
| 55 | (r.URL.Path != connect.SigninPath && r.URL.Path != connect.SigninLocalPath) { |
| 56 | return |
| 57 | } |
| 58 | path, ok := requestedReturn(r) |
| 59 | switch { |
| 60 | case ok: |
| 61 | a.rememberReturn(w, path) |
| 62 | case r.URL.Path == connect.SigninPath: |
| 63 | a.rememberReturn(w, "/") |
| 64 | case st.ReturnTo == "": |
| 65 | if kept, ok := a.rememberedReturn(r); ok && kept != "/" { |
| 66 | st.ReturnTo = kept |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // beginSignin is POST /signin, in both modes: rastrillo's Begin, with the |
| 72 | // destination kept Slides' way. |
| 73 | // |
| 74 | // The door's forms post return_to, and rastrillo would carry it across |
| 75 | // the round trip and land the new session there itself, skipping |
| 76 | // /auth/landed and /auth/local/complete, where connect resumes a pending |
| 77 | // account link. So the posted return_to is remembered in the return |
| 78 | // memory and taken off the form before Begin reads it, and put back on |
| 79 | // Begin's redirect to the door (a problem, Sent, the Keymail |
| 80 | // continuation), so the next form there posts it again and the chooser's |
| 81 | // links keep it. |
| 82 | // |
| 83 | // With SigninScreen on, Begin never answers with a redirect off this |
| 84 | // origin: a Keymail address continues on the door (?continue=), whose |
| 85 | // meta refresh leaves for Keymail, so form-action stays 'self' (Safari |
| 86 | // applies it to a form submission's whole redirect chain). |
| 87 | func (a *app) beginSignin(next http.HandlerFunc) http.HandlerFunc { |
| 88 | return func(w http.ResponseWriter, r *http.Request) { |
| 89 | // Off the form whether or not the body parsed: Begin must never |
| 90 | // read a destination of its own. |
| 91 | to := "" |
| 92 | if r.ParseForm() == nil { |
| 93 | to = sessions.SafeReturnTo(r.PostForm.Get("return_to"), "") |
| 94 | } |
| 95 | if r.PostForm != nil { |
| 96 | r.PostForm.Del("return_to") |
| 97 | } |
| 98 | if r.Form != nil { |
| 99 | r.Form.Del("return_to") |
| 100 | } |
| 101 | // A post without one is a sign-in for the list (rastrillo's own |
| 102 | // rule), so a page another tab remembered does not win. |
| 103 | landing := to |
| 104 | if landing == "" { |
| 105 | landing = "/" |
| 106 | } |
| 107 | a.rememberReturn(w, landing) |
| 108 | result := &bufferedResponse{header: make(http.Header)} |
| 109 | next(result, r) |
| 110 | if to != "" && result.status == http.StatusSeeOther { |
| 111 | result.header.Set("Location", withReturnTo(result.header.Get("Location"), to)) |
| 112 | } |
| 113 | result.replay(w) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // withReturnTo puts return_to on a redirect to the door, and leaves any |
| 118 | // other Location as it is. |
| 119 | func withReturnTo(location, to string) string { |
| 120 | u, err := url.Parse(location) |
| 121 | if err != nil || u.Scheme != "" || u.Host != "" || u.Path != connect.SigninPath { |
| 122 | return location |
| 123 | } |
| 124 | q := u.Query() |
| 125 | if q.Has("return_to") { |
| 126 | return location |
| 127 | } |
| 128 | q.Set("return_to", to) |
| 129 | u.RawQuery = q.Encode() |
| 130 | return u.String() |
| 131 | } |
| 132 | |
| 133 | // bufferedResponse holds a handler's answer so the caller can read it or |
| 134 | // change its Location before it is sent. |
| 135 | type bufferedResponse struct { |
| 136 | header http.Header |
| 137 | status int |
| 138 | body bytes.Buffer |
| 139 | } |
| 140 | |
| 141 | func (w *bufferedResponse) Header() http.Header { return w.header } |
| 142 | func (w *bufferedResponse) WriteHeader(status int) { |
| 143 | if w.status == 0 { |
| 144 | w.status = status |
| 145 | } |
| 146 | } |
| 147 | func (w *bufferedResponse) Write(p []byte) (int, error) { |
| 148 | if w.status == 0 { |
| 149 | w.WriteHeader(http.StatusOK) |
| 150 | } |
| 151 | return w.body.Write(p) |
| 152 | } |
| 153 | |
| 154 | // replay writes the held answer to w. |
| 155 | func (w *bufferedResponse) replay(to http.ResponseWriter) { |
| 156 | for name, values := range w.header { |
| 157 | for _, value := range values { |
| 158 | to.Header().Add(name, value) |
| 159 | } |
| 160 | } |
| 161 | if w.status == 0 { |
| 162 | w.status = http.StatusOK |
| 163 | } |
| 164 | to.WriteHeader(w.status) |
| 165 | _, _ = to.Write(w.body.Bytes()) |
| 166 | } |
| 167 | |