| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/home/connect" |
| 10 | "amadan.net/oficina/home/connect/linkstore" |
| 11 | "amadan.net/oficina/home/roster" |
| 12 | "amadan.net/rastrillo/rastrillo/csrf" |
| 13 | "amadan.net/rastrillo/rastrillo/migrate" |
| 14 | "amadan.net/rastrillo/rastrillo/sessions" |
| 15 | ) |
| 16 | |
| 17 | type homeIntegration struct { |
| 18 | issuer string |
| 19 | bridge *connect.Bridge |
| 20 | routes *connect.Routes |
| 21 | sessions *sessions.Sessions |
| 22 | } |
| 23 | |
| 24 | // WireHome keeps the local account's address as the subject used for existing |
| 25 | // owner and sibling authorization. Home subjects never stand in for addresses. |
| 26 | func (a *app) WireHome(mux *http.ServeMux, cfg Config) error { |
| 27 | if cfg.Home == nil { |
| 28 | return nil |
| 29 | } |
| 30 | if a.home != nil || a.auth == nil || mux == nil { |
| 31 | return errHomeIdentity |
| 32 | } |
| 33 | h := cfg.Home |
| 34 | // In team mode AppWith has applied this already (teamBoot), and this |
| 35 | // finds every migration recorded. |
| 36 | if _, err := migrate.Apply(context.Background(), a.db, homeSchema()); err != nil { |
| 37 | return errHomeIdentity |
| 38 | } |
| 39 | sess, err := sessions.New(sessions.Config{DB: a.db.Writer(), Origin: cfg.Origin, SigninPath: "/signin", Logger: a.logger}) |
| 40 | if err != nil || sess.CookieName() != a.auth.SessionCookie() { |
| 41 | return errHomeIdentity |
| 42 | } |
| 43 | read := func(r *http.Request) (*connect.Session, error) { |
| 44 | s, ok := sess.From(r) |
| 45 | if !ok { |
| 46 | return nil, nil |
| 47 | } |
| 48 | return &connect.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: s.At}, nil |
| 49 | } |
| 50 | // In team mode, connect's three Home-only hooks (Admit, HomeOnly, |
| 51 | // Unlinked): a roster member with no account here is asked to choose |
| 52 | // (connect.ChoosePath) between connecting an existing email account |
| 53 | // and signing in with Home alone (team_home_only.go). connect.New |
| 54 | // refuses HomeOnly or Unlinked without Admit. All nil otherwise, which |
| 55 | // is connect exactly as before. |
| 56 | var unlinked, homeOnly func(context.Context, string, string) (connect.Person, error) |
| 57 | var admit func(context.Context, string, string) error |
| 58 | if a.teamMode() { |
| 59 | admit, unlinked, homeOnly = a.teamAdmit, a.teamUnlinked(a.db.Writer()), a.teamHomeOnly(a.db.Writer()) |
| 60 | } |
| 61 | b, err := connect.New(connect.Config{DB: a.db.Writer(), Origin: cfg.Origin, Issuer: h.Issuer, ConsumerID: h.ConsumerID, Namespace: homeNamespace, SessionCookie: sess.CookieName(), Keys: h.Keys, DEK: h.DEK, ReadSession: read, |
| 62 | Admit: admit, Unlinked: unlinked, HomeOnly: homeOnly, |
| 63 | MintHomeSession: func(w http.ResponseWriter, r *http.Request, s connect.Session) error { |
| 64 | if err := sess.SignIn(w, r, sessions.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime}); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | // The smart door remembers Oficina ID, for an admitted session |
| 68 | // only (signin_admission.go's rememberOficina). It never fails |
| 69 | // the sign-in. |
| 70 | a.rememberOficina(w, r) |
| 71 | return nil |
| 72 | }, |
| 73 | LookupLocalSession: func(ctx context.Context, s connect.Session) (connect.Person, error) { |
| 74 | id, label, err := a.localHomeIdentity(ctx, sessions.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: s.At}) |
| 75 | return connect.Person{ID: id, Label: label}, err |
| 76 | }, |
| 77 | LookupPerson: func(ctx context.Context, q linkstore.Queryer, id int64) (string, error) { |
| 78 | return a.lookupHomeLocalIdentity(ctx, q, id) |
| 79 | }, Render: a.renderHome, Now: a.now, |
| 80 | }) |
| 81 | if err != nil { |
| 82 | return errHomeIdentity |
| 83 | } |
| 84 | // LocalSignin is the door connect's SigninDoor and SigninLocal hand to |
| 85 | // (signin.go); ForgetPath puts "Forget me on this device" on the |
| 86 | // signed-out page. |
| 87 | routes, err := connect.NewRoutes(b, connect.RoutesConfig{Namespace: homeNamespace, Origin: cfg.Origin, Issuer: h.Issuer, SessionCookie: sess.CookieName(), DEK: h.DEK, ReadSession: read, SignOut: sess.SignOut, LocalSignin: a.signinDoor, Protect: csrf.Protect(cfg.Origin), Render: a.renderHome, ForgetPath: signinForgetPath}) |
| 88 | if err != nil { |
| 89 | return errHomeIdentity |
| 90 | } |
| 91 | a.home = &homeIntegration{issuer: h.Issuer, bridge: b, routes: routes, sessions: sess} |
| 92 | a.directory = h.Directory |
| 93 | // The roster runtime, last: it needs the bridge, and nothing serves |
| 94 | // until it is open or boot has been refused (team_config.go). |
| 95 | return a.configureTeam(h) |
| 96 | } |
| 97 | |
| 98 | func (a *app) homeMiddleware(origin string) func(http.Handler) http.Handler { |
| 99 | return func(next http.Handler) http.Handler { |
| 100 | local := csrf.Protect(origin)(next) |
| 101 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 102 | if a.home != nil { |
| 103 | a.home.routes.Middleware(next).ServeHTTP(w, r) |
| 104 | return |
| 105 | } |
| 106 | local.ServeHTTP(w, r) |
| 107 | }) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // homeUnavailable is the answer to a Home session's identity storage that |
| 112 | // could not be read now, outside team mode (team mode answers |
| 113 | // teamUnavailable): it says nothing about who the session is, so it is a |
| 114 | // retryable, no-store 503, never the 404 a refusal gets, which the sign-in |
| 115 | // door's admission probe would read as a refusal and answer by ending the |
| 116 | // session (signin_admission.go). |
| 117 | func (a *app) homeUnavailable(w http.ResponseWriter, r *http.Request) { |
| 118 | w.Header().Set("Cache-Control", "no-store") |
| 119 | w.Header().Set("Retry-After", strconv.Itoa(int(roster.RetryAfter/time.Second))) |
| 120 | a.errorPage(w, r, http.StatusServiceUnavailable, kindStatus, "") |
| 121 | } |
| 122 | |
| 123 | // homeNavigationCookie is connect's sealed navigation marker (unexported |
| 124 | // there): set by the account menu's Connect, and by an Oficina assertion |
| 125 | // this app has no link for. Only its presence is read here; connect opens |
| 126 | // it. TestUnknownOficinaAccountResumesTheLinkAfterLocalSignin fails if |
| 127 | // the name drifts. |
| 128 | const homeNavigationCookie = "__Host-oficina-local-link-navigation" |
| 129 | |
| 130 | // localComplete is GET /auth/local/complete, where a local sign-in lands |
| 131 | // with Home configured. After an Oficina assertion this app had no link |
| 132 | // for, connect begins the link from here, and that needs the person's |
| 133 | // local identity row, which only /account/home allocated before: someone |
| 134 | // who had never opened it could not resume, and fell back to the Connect |
| 135 | // page. So a fresh local sign-in that carries the marker allocates it |
| 136 | // first, as /account/home does; any other sign-in allocates nothing, as |
| 137 | // before. Failing costs only the shortcut; the sign-in has succeeded. |
| 138 | func (a *app) localComplete(w http.ResponseWriter, r *http.Request) { |
| 139 | if _, err := r.Cookie(homeNavigationCookie); err != nil { |
| 140 | a.home.routes.LocalComplete(w, r) |
| 141 | return |
| 142 | } |
| 143 | if s, ok := a.home.sessions.From(r); ok { |
| 144 | if _, fresh := connect.FreshLocal(connect.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: s.At}, a.now()); fresh { |
| 145 | _, _, _ = a.ensureHomeLocalIdentity(r.Context(), s) |
| 146 | } |
| 147 | } |
| 148 | a.home.routes.LocalComplete(w, r) |
| 149 | } |
| 150 | |
| 151 | func (a *app) homeAccount(w http.ResponseWriter, r *http.Request) { |
| 152 | s, ok := a.home.sessions.From(r) |
| 153 | if ok { |
| 154 | if _, fresh := connect.FreshLocal(connect.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: s.At}, time.Now()); fresh { |
| 155 | if _, _, err := a.ensureHomeLocalIdentity(r.Context(), s); err != nil { |
| 156 | a.renderHome(w, r, "failed", "", http.StatusForbidden) |
| 157 | return |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | a.home.routes.Account(w, r) |
| 162 | } |
| 163 | |