oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3// The sign-in door with Home configured (home/connect MIGRATING.md, step 6):
4// connect's three door routes behind Sheets' admission check, and the
5// memory of an Oficina ID sign-in.
6
7import (
8 "net/http"
9 "net/url"
10
11 "amadan.net/oficina/home/connect"
12 "amadan.net/rastrillo/rastrillo/lastsignin"
13)
14
15// admittedOnly runs connect's SigninDoor, SigninLocal or SigninOficina for
16// a browser whose session the signed-in routes would admit, and as signed
17// out for any other. SigninDoor and SigninOficina take any live session
18// cookie as signed in and send it straight to the page; a session the
19// guard refuses (a member who lost admission, a guest whose last share was
20// revoked) would then go round /signin, the page, the guard, and back
21// (Astra, #39), so they are not shown that cookie.
22//
23// The door then says why (the refusal, in the screen's app-problem slot,
24// with the email field), and a remembered Oficina ID is forgotten: offered
25// again, it would sign the same refused person in and send them back here.
26// A remembered email way is kept: it is no loop. The refused session's
27// cookie is cleared on this response. SigninLocal is wrapped too, for the
28// refusals that land there.
29func (a *App) admittedOnly(next http.HandlerFunc) http.HandlerFunc {
30 return func(w http.ResponseWriter, r *http.Request) {
31 if signedIn, admitted := a.admission(r, false); signedIn && !admitted {
32 // The probe's guard has already ended the session; clear its
33 // cookie on this response too, so a reload is plainly signed
34 // out rather than a dead cookie over the plain door.
35 a.homeSessions.SignOut(w, r)
36 r = withRefusal(withoutCookie(r, a.homeSessions.CookieName()))
37 jar := a.auth.RememberJar()
38 if rec, res := jar.Read(r); res == lastsignin.Valid && rec.Method == lastsignin.MethodFederated {
39 jar.Clear(w)
40 }
41 }
42 next(w, r)
43 }
44}
45
46// admission reports whether r holds a session, and whether the signed-in
47// routes would let it in right now, by running the same guard
48// (signedInRequire: session, identity, admission, team standing) around a
49// probe GET / whose answer is thrown away. A session is not enough: a
50// guest whose last share was revoked still holds a valid one. The guard
51// ends a session it refuses, as it would on the person's next page. A
52// guard that cannot decide (its 503: storage or the roster unavailable)
53// counts as admitted unless strict: the door must not tell somebody they
54// cannot sign in because a read failed, and nothing is remembered on one.
55func (a *App) admission(r *http.Request, strict bool) (signedIn, admitted bool) {
56 if _, ok := a.homeSessions.From(r); !ok {
57 return false, false
58 }
59 probe := r.Clone(r.Context())
60 probe.Method = http.MethodGet
61 probe.URL = &url.URL{Path: "/"}
62 probe.RequestURI = "/"
63 reached := false
64 rec := &bufferedResponse{header: make(http.Header)}
65 a.signedInRequire(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached = true })).ServeHTTP(rec, probe)
66 return true, reached || (!strict && rec.status == http.StatusServiceUnavailable)
67}
68
69// rememberOficina is the smart door's memory of a Home sign-in, written in
70// MintHomeSession once the session is stored, so it is written only where
71// connect turns a Home sign-in into a session here (a finished exchange,
72// "I'm new here"), never on link, choose, unknown or failed. The record is
73// Oficina ID, with the address of the person connect resolved for this
74// very sign-in (connect.MintedPerson), or none for a Home-only member.
75// Nothing is carried over from the record it replaces: that address could
76// be somebody else's. Remembering is a convenience, and Remember fails
77// silently, so it can never fail the sign-in.
78//
79// Only for a session the signed-in routes admit, asked of the session just
80// minted (admittedAs): connect mints for any linked person, and a record
81// for a refused one would offer Oficina ID again, round a loop of sign-in,
82// refusal and door (Fable on Docs #139, A2).
83func (a *App) rememberOficina(w http.ResponseWriter, r *http.Request) {
84 if a.auth == nil || a.homeSessions == nil {
85 return
86 }
87 if _, admitted := a.admission(a.admittedAs(w, r), true); !admitted {
88 return
89 }
90 address := ""
91 if p, ok := connect.MintedPerson(r); ok {
92 address = p.Label
93 }
94 a.auth.RememberJar().Remember(w, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: address})
95}
96
97// admittedAs is r as the session MintHomeSession just set on w would send
98// it: the request's own cookies, with that session's in place of any other.
99func (a *App) admittedAs(w http.ResponseWriter, r *http.Request) *http.Request {
100 name := a.homeSessions.CookieName()
101 var minted *http.Cookie
102 for _, c := range (&http.Response{Header: w.Header()}).Cookies() {
103 if c.Name == name && c.MaxAge >= 0 && c.Value != "" {
104 minted = c
105 }
106 }
107 probe := withoutCookie(r, name)
108 if minted != nil {
109 probe.AddCookie(&http.Cookie{Name: minted.Name, Value: minted.Value})
110 }
111 return probe
112}
113
114// withoutCookie is r, as seen by a handler that must not see the named
115// cookie. Only admittedOnly and admittedAs use it.
116func withoutCookie(r *http.Request, name string) *http.Request {
117 c := r.Clone(r.Context())
118 c.Header.Del("Cookie")
119 for _, cookie := range r.Cookies() {
120 if cookie.Name != name {
121 c.AddCookie(cookie)
122 }
123 }
124 return c
125}
126