| 1 | package fichas |
| 2 | |
| 3 | // The door's admission check (home connect/MIGRATING.md, step 6, item 2): |
| 4 | // before /signin may treat a live session as signed in, the gate every |
| 5 | // signed-in page runs (requireSession) is asked about it, on a probe. A |
| 6 | // session it refuses is ended at the door and gets the door with the |
| 7 | // refusal; one it cannot decide on is left alone. Without it, a session |
| 8 | // whose address was taken off FICHAS_OWNERS would go round /signin, the |
| 9 | // page's 404 and back. |
| 10 | // |
| 11 | // What the door judges, and what it never touches: a web sign-in (an |
| 12 | // emailed link or Keymail, the two ways Fichas mints a session) is judged. |
| 13 | // Any other session is never judged, refused or ended here: the door is |
| 14 | // drawn for it as for anybody signed out. Fichas has no other kind today; |
| 15 | // the rule is the suite's (Calendar #48), so one added later is not |
| 16 | // signed out by a door that cannot speak for it. |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "net/http" |
| 21 | "net/url" |
| 22 | |
| 23 | "amadan.net/rastrillo/rastrillo/sessions" |
| 24 | "github.com/keymaildev/signin" |
| 25 | ) |
| 26 | |
| 27 | // admissionResult is what the probe found: whether r holds a web sign-in |
| 28 | // at all, and whether the signed-in routes let it in. |
| 29 | type admissionResult struct { |
| 30 | signedIn, admitted bool |
| 31 | } |
| 32 | |
| 33 | // admission runs the gate every signed-in page runs (requireSession: |
| 34 | // rastrillo auth's RequireSession, then the owners list) around a probe |
| 35 | // GET / whose answer is thrown away. A session is not enough: one for an |
| 36 | // address taken off the owners list is still valid. The probe ends |
| 37 | // nothing itself; the door does, for a session it decides to refuse. |
| 38 | func (a *app) admission(r *http.Request) admissionResult { |
| 39 | if a.allowUnauthenticated { |
| 40 | // AppForTest: the stub identity is signed in and admitted, as |
| 41 | // requireSession says. |
| 42 | return admissionResult{signedIn: a.testIdentity != "", admitted: a.testIdentity != ""} |
| 43 | } |
| 44 | if a.auth == nil { |
| 45 | return admissionResult{} |
| 46 | } |
| 47 | id, ok := a.auth.SessionFrom(r) |
| 48 | if !ok || !doorJudges(signin.Method(id.Method)) { |
| 49 | // Signed out, a session store that could not be read (rastrillo |
| 50 | // logs it and keeps the cookie), or a session that is not a web |
| 51 | // sign-in: the door is drawn, and nothing is refused or ended. |
| 52 | return admissionResult{} |
| 53 | } |
| 54 | probe := r.Clone(r.Context()) |
| 55 | probe.Method = http.MethodGet |
| 56 | probe.URL = &url.URL{Path: "/"} |
| 57 | probe.RequestURI = "/" |
| 58 | probe.Body = http.NoBody |
| 59 | reached := false |
| 60 | rec := &capturedResponse{header: make(http.Header)} |
| 61 | a.requireSession(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached = true })).ServeHTTP(rec, probe) |
| 62 | return probeVerdict(reached, rec.status) |
| 63 | } |
| 64 | |
| 65 | // probeVerdict reads the probe's answer. The gate let the request through: |
| 66 | // admitted. It refused it as a page that is not there (404, requireSession's |
| 67 | // refusal): refused. It could not decide (any 5xx): undecided, which counts |
| 68 | // as admitted, because the door must not tell somebody they cannot sign |
| 69 | // in, nor sign them out, because something failed; the page they go on to |
| 70 | // says so. Anything else (the session gone between the two reads, sent |
| 71 | // back to the door): signed out, nothing refused or ended. |
| 72 | func probeVerdict(reached bool, status int) admissionResult { |
| 73 | switch { |
| 74 | case reached: |
| 75 | return admissionResult{signedIn: true, admitted: true} |
| 76 | case status >= http.StatusInternalServerError: |
| 77 | return admissionResult{signedIn: true, admitted: true} |
| 78 | case status == http.StatusNotFound: |
| 79 | return admissionResult{signedIn: true} |
| 80 | } |
| 81 | return admissionResult{} |
| 82 | } |
| 83 | |
| 84 | // doorJudges reports whether the door's probe judges a session by method: |
| 85 | // the two web sign-ins requireSession speaks for. |
| 86 | func doorJudges(method signin.Method) bool { |
| 87 | return method == signin.MethodMagicLink || method == signin.MethodKeymail |
| 88 | } |
| 89 | |
| 90 | // refusedSession answers for a session the probe refused: it is ended and |
| 91 | // its cookie cleared on this response, so a reload is plainly signed out |
| 92 | // rather than a dead cookie over the plain door, and the door is marked |
| 93 | // to say why. Removal happens HERE, at the door, never inside the probe. |
| 94 | // The remembered email way is kept: offered again, it is no loop, because |
| 95 | // Begin's own refusal answers it. |
| 96 | func (a *app) refusedSession(w http.ResponseWriter, r *http.Request) *http.Request { |
| 97 | if a.sess != nil { |
| 98 | a.sess.SignOut(w, r) |
| 99 | } |
| 100 | w.Header().Set("Cache-Control", "no-store") |
| 101 | return withRefusal(withoutCookie(r, a.auth.SessionCookie())) |
| 102 | } |
| 103 | |
| 104 | // withoutCookie is r, as seen by a handler that must not see the named |
| 105 | // cookie: the door drawn after a refused session's end. |
| 106 | func withoutCookie(r *http.Request, name string) *http.Request { |
| 107 | c := r.Clone(r.Context()) |
| 108 | c.Header.Del("Cookie") |
| 109 | for _, cookie := range r.Cookies() { |
| 110 | if cookie.Name != name { |
| 111 | c.AddCookie(cookie) |
| 112 | } |
| 113 | } |
| 114 | return c |
| 115 | } |
| 116 | |
| 117 | // signinOutcome reports whether GET /signin names a state of the door |
| 118 | // rather than opening it afresh: an outcome of the email flow (?sent, |
| 119 | // ?err, ?force), the Keymail continuation (?continue) or a step-up |
| 120 | // (?reauth). Those are drawn for a signed-in browser too. |
| 121 | func signinOutcome(r *http.Request) bool { |
| 122 | q := r.URL.Query() |
| 123 | return q.Has("sent") || q.Has("err") || q.Has("force") || q.Has("continue") || q.Has("reauth") |
| 124 | } |
| 125 | |
| 126 | // alreadySignedIn sends a signed-in GET /signin back where it came from, |
| 127 | // with no door: 303 to a safe return_to, or "/". |
| 128 | func alreadySignedIn(w http.ResponseWriter, r *http.Request) { |
| 129 | target := "/" |
| 130 | if q, err := url.ParseQuery(r.URL.RawQuery); err == nil && len(q["return_to"]) == 1 { |
| 131 | if to := sessions.SafeReturnTo(q.Get("return_to"), ""); to != "" && keepableReturn(to) { |
| 132 | target = to |
| 133 | } |
| 134 | } |
| 135 | w.Header().Set("Cache-Control", "no-store") |
| 136 | http.Redirect(w, r, target, http.StatusSeeOther) |
| 137 | } |
| 138 | |
| 139 | // capturedResponse records what a handler wrote, so the probe can read |
| 140 | // the gate's answer without sending it. |
| 141 | type capturedResponse struct { |
| 142 | header http.Header |
| 143 | status int |
| 144 | body bytes.Buffer |
| 145 | } |
| 146 | |
| 147 | func (c *capturedResponse) Header() http.Header { return c.header } |
| 148 | func (c *capturedResponse) WriteHeader(status int) { |
| 149 | if c.status == 0 { |
| 150 | c.status = status |
| 151 | } |
| 152 | } |
| 153 | func (c *capturedResponse) Write(p []byte) (int, error) { |
| 154 | if c.status == 0 { |
| 155 | c.WriteHeader(http.StatusOK) |
| 156 | } |
| 157 | return c.body.Write(p) |
| 158 | } |
| 159 | |