oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3// Returning to the page you came from after signing in (spec §9.4), and
4// the two sign-in behaviours that go with it: an already-signed-in
5// GET /signin goes straight back, and signing out lands on a page that
6// says so.
7//
8// With Home configured, the return path rides the Home hand-off, and
9// home_connect.go's signin also remembers it in Docs' own
10// __Host-oficina-docs-home-return. What would not honour it is the local
11// email link: rastrillo's auth.Verify always lands on its fixed
12// SignedInPath, so the value cannot ride the emailed link and lives in
13// that sealed, 600-second cookie instead, read at /auth/local/complete.
14//
15// Standalone Docs (no Home) had nothing: rastrillo's RequireSession drops
16// the path (its refuseSession redirects to a bare /signin) and its
17// SignedInPath was "/". It now gets the same cookie, sealed under a key
18// derived from the instance key, the same /auth/local/complete landing,
19// and a guard that puts return_to on the sign-in redirect. (Spec §9.4
20// left standalone as "a small return cookie of its own, or lands on /";
21// this is the first, so the Playwright deep-link gate can run against a
22// local dev server.)
23//
24// auth_navigation.go's "never comes from return_to" governs the Keymail
25// authorize URL, which must be exactly https://…/oauth/authorize. The
26// path here is a separate value, validated by handoff.SafePath (a
27// same-origin path: no scheme, no host, no //, no control characters),
28// so it can never become an off-site destination.
29
30import (
31 "crypto/sha256"
32 "net/http"
33 "net/url"
34 "strings"
35
36 "amadan.net/oficina/home/handoff"
37 "amadan.net/oficina/home/handoff/seal"
38 rauth "amadan.net/rastrillo/rastrillo/auth"
39)
40
41// standaloneReturn builds the standalone instance's return memory: the
42// same sealed cookie returnMemory keeps for Home instances, keyed
43// from the instance key rather than Home's DEK (standalone has none).
44func standaloneReturn(instanceKey, origin string) (*returnMemory, error) {
45 key := sha256.Sum256([]byte("oficina/docs standalone return v1\x00" + instanceKey))
46 sealer, err := seal.New(key[:], "oficina/docs")
47 if err != nil {
48 return nil, err
49 }
50 n := newReturnMemory(sealer, origin)
51 if n == nil {
52 return nil, errHomeConfig
53 }
54 return n, nil
55}
56
57// requestedReturn is the return_to a sign-in GET carries, when it is
58// exactly one same-origin path.
59func requestedReturn(r *http.Request) (string, bool) {
60 q, err := url.ParseQuery(r.URL.RawQuery)
61 if err != nil {
62 return "", false
63 }
64 values, ok := q["return_to"]
65 if !ok || len(values) != 1 || !handoff.SafePath(values[0]) {
66 return "", false
67 }
68 return values[0], true
69}
70
71// signinOutcome reports whether GET /signin names a state of the door
72// rather than opening it afresh: an outcome of the email flow (?sent,
73// ?err, ?force), the Keymail continuation (?continue) or a step-up
74// (?reauth). connect's SigninDoor draws the same five for a signed-in
75// browser too (its doorStates), and so does standalone Docs.
76func signinOutcome(r *http.Request) bool {
77 q := r.URL.Query()
78 return q.Has("sent") || q.Has("err") || q.Has("force") || q.Has("continue") || q.Has("reauth")
79}
80
81// alreadySignedIn sends a signed-in GET /signin back where it came
82// from, with no form (spec §9.2: 303 to return_to or /).
83func alreadySignedIn(w http.ResponseWriter, r *http.Request) {
84 target := "/"
85 if path, ok := requestedReturn(r); ok {
86 target = path
87 }
88 w.Header().Set("Cache-Control", "no-store")
89 http.Redirect(w, r, target, http.StatusSeeOther)
90}
91
92// admitted reports whether the signed-in routes would let this request's
93// session in right now, by running the same guard chain (h.requireCurrent:
94// session, identity, admission) around a probe. A session is not enough:
95// a guest whose last share was revoked still holds a valid one, admission
96// sends GET / to /signin, and skipping the form for them sent /signin
97// straight back to / — forever (review finding, Astra, #65).
98func (h *handlers) admitted(r *http.Request) bool {
99 if h.requireCurrent == nil {
100 return false
101 }
102 probe := r.Clone(r.Context())
103 probe.Method = http.MethodGet
104 probe.URL = &url.URL{Path: "/"}
105 probe.RequestURI = "/"
106 reached := false
107 h.requireCurrent(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached = true })).
108 ServeHTTP(&signinResponse{header: make(http.Header)}, probe)
109 return reached
110}
111
112// standaloneSignin is GET /signin without Home: already signed in (and
113// admitted) goes back; otherwise the door, which remembers a return_to
114// for the landing (auth.go's rememberSigninReturn).
115//
116// A session admission refuses gets the door with the refusal, as with Home
117// (admittedOnly).
118func (h *handlers) standaloneSignin(w http.ResponseWriter, r *http.Request) {
119 if _, signedIn := h.peek(r); signedIn {
120 if !h.admitted(r) {
121 r = withRefusal(r)
122 } else if !signinOutcome(r) {
123 alreadySignedIn(w, r)
124 return
125 }
126 }
127 h.signin(w, r)
128}
129
130// completeStandalone is the standalone SignedInPath: land on the
131// remembered page, once, or on the list.
132func (n *returnMemory) completeStandalone(w http.ResponseWriter, r *http.Request) {
133 if !n.guard(w, r, localLinkCompletePath) {
134 return
135 }
136 target := n.returnPath(r)
137 localNavigationCookie(w, homeReturnCookie, "", -1)
138 http.Redirect(w, r, target, http.StatusSeeOther)
139}
140
141// requireSessionReturning is rastrillo's RequireSession, except that a
142// signed-out page request is sent to /signin with return_to, as
143// rastrillo's own sessions.Require already does and auth.RequireSession
144// (its refuseSession) does not. Anything else is exactly what it was.
145func requireSessionReturning(a *rauth.Auth) func(http.Handler) http.Handler {
146 return func(next http.Handler) http.Handler {
147 guarded := a.RequireSession(next)
148 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
149 if r.Method == http.MethodGet || r.Method == http.MethodHead {
150 if _, ok := a.SessionFrom(r); !ok {
151 http.Redirect(w, r, "/signin?return_to="+url.QueryEscape(r.URL.RequestURI()), http.StatusSeeOther)
152 return
153 }
154 }
155 guarded.ServeHTTP(w, r)
156 })
157 }
158}
159
160// signoutToSignedOut is rastrillo's Signout, landing on /signed-out ("You’re
161// signed out") rather than straight back on the sign-in form, which read
162// as though nothing had happened.
163func signoutToSignedOut(signout http.HandlerFunc) http.HandlerFunc {
164 return func(w http.ResponseWriter, r *http.Request) {
165 result := &signinResponse{header: make(http.Header)}
166 signout(result, r)
167 if result.status == http.StatusSeeOther && strings.HasPrefix(result.header.Get("Location"), "/signin") {
168 result.header.Set("Location", "/signed-out")
169 }
170 result.replay(w)
171 }
172}
173