| 1 | package docs |
| 2 | |
| 3 | // Docs on Home's shared account connection, home/connect. |
| 4 | // |
| 5 | // connect owns the Home exchange (Start, Callback, Finish), the link flow |
| 6 | // (local reauthentication, a fresh Home assertion, an explicit confirm), |
| 7 | // the sealed navigation that resumes a link after a local sign-in, and the |
| 8 | // link table, home_identity_links. Docs supplies its people, its sessions |
| 9 | // and its pages, and in team mode the Unlinked and HomeOnly hooks over |
| 10 | // principalstore (team_access.go's roster decides standing on every |
| 11 | // request; the hooks only decide who a Home subject is). |
| 12 | // |
| 13 | // What stays Docs' own, and why: |
| 14 | // - GET /signin and /signin/oficina skip the door only for a session the |
| 15 | // signed-in routes would admit (h.handlers.admitted), not merely one |
| 16 | // that exists, or a guest whose last share was revoked loops between / |
| 17 | // and /signin (Astra, #65). |
| 18 | // - The sign-in door is Docs' (auth.go's signin), and so is remembering a |
| 19 | // Home sign-in as Oficina ID (rememberOficina): connect imports no |
| 20 | // lastsignin. |
| 21 | // - A local sign-in lands on the page it began from (spec §9.4). connect |
| 22 | // lands it on LocalLandingPath, so Docs keeps its own sealed return |
| 23 | // memory (home_return.go) beside connect's and substitutes it there. |
| 24 | // - The pages are Docs' (home_render.go), in Docs' catalog. |
| 25 | |
| 26 | import ( |
| 27 | "context" |
| 28 | "database/sql" |
| 29 | "encoding/base64" |
| 30 | "errors" |
| 31 | "net/http" |
| 32 | |
| 33 | "amadan.net/oficina/home/connect" |
| 34 | "amadan.net/oficina/home/connect/linkstore" |
| 35 | "amadan.net/oficina/home/handoff" |
| 36 | "amadan.net/oficina/home/handoff/seal" |
| 37 | "amadan.net/oficina/home/teammode" |
| 38 | "amadan.net/rastrillo/rastrillo" |
| 39 | rauth "amadan.net/rastrillo/rastrillo/auth" |
| 40 | "amadan.net/rastrillo/rastrillo/csrf" |
| 41 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 42 | "amadan.net/rastrillo/rastrillo/sessions" |
| 43 | "github.com/carlosframework/docs/internal/docs/principalstore" |
| 44 | "github.com/go-chi/chi/v5" |
| 45 | "gorm.io/gorm" |
| 46 | ) |
| 47 | |
| 48 | // homeNamespace seals connect's handoff and link state, Docs' return |
| 49 | // memory and the roster evidence. It is the namespace Docs' own bridge |
| 50 | // used, so pending handoff rows survive the move onto connect. |
| 51 | const homeNamespace = "oficina/docs" |
| 52 | |
| 53 | // homeRoutes is Docs' side of home/connect. |
| 54 | type homeRoutes struct { |
| 55 | bridge *connect.Bridge |
| 56 | // routes is connect's navigation and route handlers, built by mount |
| 57 | // once the handlers exist. |
| 58 | routes *connect.Routes |
| 59 | // sessions is rastrillo's session store, the one rastrillo/auth uses: |
| 60 | // the same cookie and the same rows, so sessions minted before Docs |
| 61 | // moved onto connect are still read (mount checks the cookie name). |
| 62 | sessions *sessions.Sessions |
| 63 | sessionCookie string |
| 64 | issuer string |
| 65 | origin string |
| 66 | dek []byte |
| 67 | resolver identityResolver |
| 68 | // returns is the page a sign-in began from, for the local landing and |
| 69 | // "Use a different address" (home_return.go). |
| 70 | returns *returnMemory |
| 71 | // team is set after the move, in team mode only; the hooks read it. |
| 72 | team *teamRuntime |
| 73 | // handlers is set by mount; render reads it. |
| 74 | handlers *handlers |
| 75 | // jar is rastrillo auth's remembered way in (auth.RememberJar), set by |
| 76 | // mount: a Home sign-in is remembered here, as Oficina ID. |
| 77 | jar *lastsignin.Jar |
| 78 | } |
| 79 | |
| 80 | // newHome builds connect's Bridge over Docs' people and sessions. New calls |
| 81 | // it after the migrations and before the link move (home_links_move.go), |
| 82 | // the team runtime and the handlers, so the pages and the team hooks are |
| 83 | // bound late; no request is served before mount. |
| 84 | func newHome(settings *homeSettings, origin string, db *sql.DB, store *Store, team bool) (*homeRoutes, error) { |
| 85 | if settings == nil || db == nil || store == nil { |
| 86 | return nil, errHomeConfig |
| 87 | } |
| 88 | sess, err := sessions.New(sessions.Config{DB: db, Origin: origin}) |
| 89 | if err != nil { |
| 90 | return nil, errHomeConfig |
| 91 | } |
| 92 | sealer, err := seal.New(settings.dek, homeNamespace) |
| 93 | if err != nil { |
| 94 | return nil, errHomeConfig |
| 95 | } |
| 96 | returns := newReturnMemory(sealer, origin) |
| 97 | if returns == nil { |
| 98 | return nil, errHomeConfig |
| 99 | } |
| 100 | h := &homeRoutes{sessions: sess, sessionCookie: sess.CookieName(), issuer: settings.issuer, origin: origin, dek: settings.dek, returns: returns} |
| 101 | cfg := connect.Config{ |
| 102 | DB: db, Origin: origin, Issuer: settings.issuer, ConsumerID: settings.consumerID, Namespace: homeNamespace, |
| 103 | Keys: settings.keys, DEK: settings.dek, SessionCookie: h.sessionCookie, |
| 104 | ReadSession: h.readSession, |
| 105 | MintHomeSession: func(w http.ResponseWriter, r *http.Request, s connect.Session) error { |
| 106 | if err := sess.SignIn(w, r, sessions.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime}); err != nil { |
| 107 | return err |
| 108 | } |
| 109 | h.rememberOficina(w, r) |
| 110 | return nil |
| 111 | }, |
| 112 | // The native app's session (connect.NativeExchange): the same |
| 113 | // store and cookie name, minted without a cookie and handed back |
| 114 | // in the answer. Its own DB handle, never connect's transaction. |
| 115 | MintNativeSession: func(_ context.Context, s connect.Session) (string, error) { |
| 116 | return sess.Mint(sessions.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime}) |
| 117 | }, |
| 118 | LookupPerson: linkedAddress, |
| 119 | LookupLocalSession: localPerson(store), |
| 120 | Render: h.render, |
| 121 | } |
| 122 | if team { |
| 123 | // Team mode only. With all three nil, connect refuses a Home subject |
| 124 | // with no link as "unknown", as Docs always has outside team mode. |
| 125 | cfg.Admit, cfg.HomeOnly, cfg.Unlinked = h.admit, h.homeOnly, h.unlinked |
| 126 | } |
| 127 | if h.bridge, err = connect.New(cfg); err != nil { |
| 128 | return nil, errHomeConfig |
| 129 | } |
| 130 | h.resolver = identityResolver{store: store, issuer: settings.issuer, links: h.bridge} |
| 131 | return h, nil |
| 132 | } |
| 133 | |
| 134 | // mount builds connect's routes once the handlers exist. rastrillo/auth's |
| 135 | // cookie must be the session store's, or a local sign-in would mint a |
| 136 | // session connect cannot read. |
| 137 | func (h *homeRoutes) mount(handlers *handlers, auth *rauth.Auth) error { |
| 138 | if handlers == nil || auth == nil || auth.SessionCookie() != h.sessionCookie { |
| 139 | return errHomeConfig |
| 140 | } |
| 141 | routes, err := connect.NewRoutes(h.bridge, connect.RoutesConfig{ |
| 142 | LocalLandingPath: "/", Namespace: homeNamespace, Origin: h.origin, Issuer: h.issuer, SessionCookie: h.sessionCookie, |
| 143 | FormPolicy: homeFormPolicy(h.issuer), DEK: h.dek, ReadSession: h.readSession, SignOut: h.sessions.SignOut, |
| 144 | LocalSignin: handlers.signin, Protect: csrf.Protect(h.origin), Render: h.render, |
| 145 | ForgetPath: signinForgetPath, |
| 146 | }) |
| 147 | if err != nil { |
| 148 | return errHomeConfig |
| 149 | } |
| 150 | h.routes, h.handlers, h.jar = routes, handlers, auth.RememberJar() |
| 151 | return nil |
| 152 | } |
| 153 | |
| 154 | // rememberOficina is the smart door's memory of a Home sign-in, written in |
| 155 | // MintHomeSession once the session is stored, so it is written only where |
| 156 | // connect turns a Home sign-in into a session here (a finished exchange, |
| 157 | // "I'm new here"), never on link, choose, unknown or failed. The record is |
| 158 | // Oficina ID, with the address of the person connect resolved for this |
| 159 | // very sign-in (connect.MintedPerson), or none for a Home-only person. |
| 160 | // Nothing is carried over from the record it replaces: that address could |
| 161 | // be somebody else's. Remembering is a convenience, and Remember fails |
| 162 | // silently, so it can never fail the sign-in. |
| 163 | // |
| 164 | // Only for a session the signed-in routes admit, asked of the session just |
| 165 | // minted (admittedAs): connect mints for any linked person, admitted or |
| 166 | // not, and a record for a refused one would offer Oficina ID again, round |
| 167 | // a loop of sign-in, refusal and door (Fable on #139, A2). |
| 168 | func (h *homeRoutes) rememberOficina(w http.ResponseWriter, r *http.Request) { |
| 169 | if h.jar == nil || h.handlers == nil || !h.handlers.admitted(h.admittedAs(w, r)) { |
| 170 | return |
| 171 | } |
| 172 | address := "" |
| 173 | if p, ok := connect.MintedPerson(r); ok { |
| 174 | address = p.Label |
| 175 | } |
| 176 | h.jar.Remember(w, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: address}) |
| 177 | } |
| 178 | |
| 179 | // readSession is the actual cookie-backed session, never a supplied one. |
| 180 | func (h *homeRoutes) readSession(r *http.Request) (*connect.Session, error) { |
| 181 | s, ok := h.sessions.From(r) |
| 182 | if !ok { |
| 183 | return nil, nil |
| 184 | } |
| 185 | return &connect.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: s.At}, nil |
| 186 | } |
| 187 | |
| 188 | // linkedAddress is connect's LookupPerson: a person's address, read through |
| 189 | // q (connect's transaction when it has one). It is a plain read of people, |
| 190 | // as Docs' own link store's was. It never consults the roster or the |
| 191 | // allowlist: a link names who somebody is, and admission is asked on every |
| 192 | // request. A roster-consulting lookup would also refuse every member's |
| 193 | // link at boot, while the roster is still Unknown (Fable, oficina/home #99 |
| 194 | // note 9), and the move would abort. |
| 195 | func linkedAddress(ctx context.Context, q linkstore.Queryer, id int64) (string, error) { |
| 196 | if id <= 0 { |
| 197 | return "", sql.ErrNoRows |
| 198 | } |
| 199 | var email string |
| 200 | if err := q.QueryRowContext(ctx, "SELECT email FROM people WHERE id = ?", id).Scan(&email); err != nil { |
| 201 | return "", err |
| 202 | } |
| 203 | if email == "" { |
| 204 | return "", sql.ErrNoRows |
| 205 | } |
| 206 | return email, nil |
| 207 | } |
| 208 | |
| 209 | // localPerson is connect's LookupLocalSession: the existing person a local |
| 210 | // (magic link or Keymail) session's verified address names. It never |
| 211 | // creates one; resolve's EnsurePerson does that, on the signed-in routes. |
| 212 | func localPerson(store *Store) func(context.Context, connect.Session) (connect.Person, error) { |
| 213 | return func(ctx context.Context, s connect.Session) (connect.Person, error) { |
| 214 | var person Person |
| 215 | err := store.db.WithContext(ctx).Where("email = ?", subjectOf(s.Subject)).First(&person).Error |
| 216 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 217 | return connect.Person{}, errIdentityRefused |
| 218 | } |
| 219 | if err != nil { |
| 220 | return connect.Person{}, errIdentityStorage |
| 221 | } |
| 222 | if person.ID <= 0 || subjectOf(person.Email) == "" { |
| 223 | return connect.Person{}, errIdentityRefused |
| 224 | } |
| 225 | return connect.Person{ID: person.ID, Label: person.Email}, nil |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // homeOnly is connect's HomeOnly: the Home-only principal unlinked gave a |
| 230 | // subject, read and never created. connect asks it only after the link |
| 231 | // table refused the subject, so a principal principalstore resolves |
| 232 | // through a link row (one whose binding connect no longer accepts) is not |
| 233 | // one: answering with it would sign the subject in as that local account. |
| 234 | func (h *homeRoutes) homeOnly(ctx context.Context, issuer, subject string) (connect.Person, error) { |
| 235 | t := h.team |
| 236 | if t == nil { |
| 237 | return connect.Person{}, errTeamUnavailable |
| 238 | } |
| 239 | p, err := t.principals.Resolve(ctx, issuer, subject) |
| 240 | switch { |
| 241 | case errors.Is(err, principalstore.ErrRefused): |
| 242 | return connect.Person{}, connect.ErrNoPerson |
| 243 | case err != nil: |
| 244 | return connect.Person{}, errTeamUnavailable |
| 245 | case p.Kind != principalstore.Home: |
| 246 | return connect.Person{}, connect.ErrNoPerson |
| 247 | } |
| 248 | return connect.Person{ID: p.ID}, nil |
| 249 | } |
| 250 | |
| 251 | // admit is connect's Admit, the roster decision taken first (Home spec |
| 252 | // 2026-11-14-oficina-id-native.md §8) for a subject with neither a link |
| 253 | // nor a Home-only principal: Runtime.AdmitUnlinked, which allocates |
| 254 | // nothing and never asks whether Docs has local accounts. A Member is |
| 255 | // asked to choose (connect's choose page, or 409 natively); a stranger is |
| 256 | // connect's "unknown"; an undecidable roster is a 503. |
| 257 | func (h *homeRoutes) admit(ctx context.Context, issuer, subject string) error { |
| 258 | t := h.team |
| 259 | if t == nil { |
| 260 | return errTeamUnavailable |
| 261 | } |
| 262 | switch err := t.cache.AdmitUnlinked(ctx, issuer, subject); { |
| 263 | case err == nil: |
| 264 | return nil |
| 265 | case errors.Is(err, teammode.ErrNotMember): |
| 266 | return connect.ErrNoPerson |
| 267 | } |
| 268 | return errTeamUnavailable |
| 269 | } |
| 270 | |
| 271 | // unlinked is connect's Unlinked, asked only after a Member chose "I'm new |
| 272 | // here" with nobody signed in, for a subject with neither a link nor a |
| 273 | // Home-only principal. It creates one only for a subject the roster says |
| 274 | // is a Member now, under a valid lease (Runtime.AdmitUnlinked). |
| 275 | // principalstore's EnsureHome asks the same gate again inside |
| 276 | // (RequireMembership) and opens its own writer transaction, so the |
| 277 | // allocation condition holds for every caller of EnsureHome, not only this |
| 278 | // one. A stranger is connect's "unknown"; an undecidable roster is a 503. |
| 279 | func (h *homeRoutes) unlinked(ctx context.Context, issuer, subject string) (connect.Person, error) { |
| 280 | t := h.team |
| 281 | if t == nil { |
| 282 | return connect.Person{}, errTeamUnavailable |
| 283 | } |
| 284 | if err := t.cache.AdmitUnlinked(ctx, issuer, subject); err != nil { |
| 285 | if errors.Is(err, teammode.ErrNotMember) { |
| 286 | return connect.Person{}, connect.ErrNoPerson |
| 287 | } |
| 288 | return connect.Person{}, errTeamUnavailable |
| 289 | } |
| 290 | p, err := t.principals.EnsureHome(ctx, principalstore.HomeIdentity{Issuer: issuer, Subject: subject}) |
| 291 | switch { |
| 292 | case errors.Is(err, principalstore.ErrRefused): |
| 293 | // Not a member after all, or linked to a local account in the |
| 294 | // meantime: that person signs in as that account, not a new one. |
| 295 | return connect.Person{}, connect.ErrNoPerson |
| 296 | case err != nil: |
| 297 | return connect.Person{}, errTeamUnavailable |
| 298 | } |
| 299 | return connect.Person{ID: p.ID}, nil |
| 300 | } |
| 301 | |
| 302 | // render draws connect's states as Docs' own pages. |
| 303 | func (h *homeRoutes) render(w http.ResponseWriter, r *http.Request, state, label string, status int) { |
| 304 | if h.handlers == nil { |
| 305 | w.Header().Set("Cache-Control", "no-store") |
| 306 | http.Error(w, rastrillo.T(r, "docs.error.generic"), http.StatusServiceUnavailable) |
| 307 | return |
| 308 | } |
| 309 | back := "" |
| 310 | if state == "success" { |
| 311 | // The page the link began from, as connect validated it. |
| 312 | back = connect.PageFor(r, state, label).Action |
| 313 | } |
| 314 | h.handlers.render.homeLinkBack(w, r, state, label, back, status) |
| 315 | } |
| 316 | |
| 317 | // mountPublic mounts the sign-in door's three routes (home/connect |
| 318 | // MIGRATING.md, step 6): /signin is always the door, offering Continue |
| 319 | // with Oficina ID; /signin/local is the door with email as the one way in, |
| 320 | // where connect sends people Oficina ID cannot help; /signin/oficina |
| 321 | // starts the Home exchange. connect hands both door routes to Docs' door |
| 322 | // (RoutesConfig.LocalSignin, handlers.signin). |
| 323 | func (h *homeRoutes) mountPublic(r chi.Router) { |
| 324 | r.Get(connect.SigninPath, h.admittedOnly(h.routes.SigninDoor)) |
| 325 | r.Get(connect.SigninLocalPath, h.admittedOnly(h.routes.SigninLocal)) |
| 326 | r.Get(connect.SigninOficinaPath, h.admittedOnly(h.routes.SigninOficina)) |
| 327 | r.Get("/signed-out", h.routes.SignedOut) |
| 328 | r.Get(localLinkCompletePath, h.localComplete) |
| 329 | r.Get(handoff.StartPath, h.bridge.Start) |
| 330 | r.Post(handoff.CallbackPath, h.bridge.Callback) |
| 331 | r.Get(handoff.FinishPath, h.bridge.Finish) |
| 332 | // The member choice (team mode; connect answers 404 without the team |
| 333 | // hooks): no session yet, so outside the session guards, and the POST |
| 334 | // inside the app-wide CSRF check like any form. |
| 335 | r.Get(connect.ChoosePath, h.bridge.ChoosePage) |
| 336 | r.Post(connect.ChoosePath, h.bridge.ChoosePost) |
| 337 | // The native app's Oficina ID exchange. connect's Middleware lets this |
| 338 | // one path past the CSRF check: it refuses anything a browser sends. |
| 339 | r.Post(connect.NativeExchangePath, h.bridge.NativeExchange) |
| 340 | } |
| 341 | |
| 342 | // mountAccount is connecting an Oficina account: connect's link flow, |
| 343 | // behind the caller's session, identity and admission middleware. Linking |
| 344 | // needs a local sign-in no older than five minutes (connect.FreshLocal, |
| 345 | // the rule Docs' own bridge had); without one, GET /account/home sends the |
| 346 | // person to sign in locally first. |
| 347 | func (h *homeRoutes) mountAccount(r chi.Router) { |
| 348 | r.Get("/account/home", h.routes.Account) |
| 349 | r.Post("/account/home", h.routes.LinkStart) |
| 350 | r.Get("/account/home/confirm", h.bridge.ConfirmPage) |
| 351 | r.Post("/account/home/confirm", h.routes.ConfirmPost) |
| 352 | } |
| 353 | |
| 354 | // admittedOnly runs connect's SigninDoor or SigninOficina for a browser |
| 355 | // whose session the signed-in routes would admit, and as signed out for |
| 356 | // any other. Both take any live session cookie as signed in and send it |
| 357 | // straight to the page; a session admission refuses (a guest whose last |
| 358 | // share was revoked, a removed member) would then loop between that page |
| 359 | // and /signin forever (Astra, #65), so they are not shown that cookie. |
| 360 | // |
| 361 | // The door then says why (the refusal, in the screen's app-problem slot, |
| 362 | // with the email field), and a remembered Oficina ID is forgotten: offered |
| 363 | // again, it would sign the same refused person in and send them back here. |
| 364 | // SigninLocal is wrapped too, for the refusals that land there. |
| 365 | func (h *homeRoutes) admittedOnly(next http.HandlerFunc) http.HandlerFunc { |
| 366 | return func(w http.ResponseWriter, r *http.Request) { |
| 367 | if _, signedIn := h.sessions.From(r); signedIn && !h.handlers.admitted(r) { |
| 368 | r = withRefusal(withoutCookie(r, h.sessionCookie)) |
| 369 | if rec, res := h.jar.Read(r); res == lastsignin.Valid && rec.Method == lastsignin.MethodFederated { |
| 370 | h.jar.Clear(w) |
| 371 | } |
| 372 | } |
| 373 | next(w, r) |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // admittedAs is r as the session MintHomeSession just set on w would send |
| 378 | // it: the request's own cookies, with that session's in place of any other. |
| 379 | func (h *homeRoutes) admittedAs(w http.ResponseWriter, r *http.Request) *http.Request { |
| 380 | var minted *http.Cookie |
| 381 | for _, c := range (&http.Response{Header: w.Header()}).Cookies() { |
| 382 | if c.Name == h.sessionCookie && c.MaxAge >= 0 && c.Value != "" { |
| 383 | minted = c |
| 384 | } |
| 385 | } |
| 386 | probe := withoutCookie(r, h.sessionCookie) |
| 387 | if minted != nil { |
| 388 | probe.AddCookie(&http.Cookie{Name: minted.Name, Value: minted.Value}) |
| 389 | } |
| 390 | return probe |
| 391 | } |
| 392 | |
| 393 | // localComplete is where a local sign-in lands. connect resumes a link from |
| 394 | // here, or goes to /account/home; otherwise it lands on "/", and Docs lands |
| 395 | // on the page the sign-in began from instead, as it always has. |
| 396 | func (h *homeRoutes) localComplete(w http.ResponseWriter, r *http.Request) { |
| 397 | rec := &signinResponse{header: make(http.Header)} |
| 398 | h.routes.LocalComplete(rec, r) |
| 399 | if rec.status == http.StatusSeeOther && rec.header.Get("Location") == "/" { |
| 400 | rec.header.Set("Location", h.returns.returnPath(r)) |
| 401 | } |
| 402 | rec.replay(w) |
| 403 | } |
| 404 | |
| 405 | // signout is connect's, which ends the session, clears the hand-off |
| 406 | // cookies and chains to {issuer}/signout (spec shared-chrome §9.5), plus |
| 407 | // Docs' own return memory. |
| 408 | func (h *homeRoutes) signout(w http.ResponseWriter, r *http.Request) { |
| 409 | localNavigationCookie(w, homeReturnCookie, "", -1) |
| 410 | h.routes.Signout(w, r) |
| 411 | } |
| 412 | |
| 413 | // Only the assertion callback crosses origins (connect's Middleware). |
| 414 | func homeCSRF(origin string, home *homeRoutes) func(http.Handler) http.Handler { |
| 415 | if home == nil { |
| 416 | return csrf.Protect(origin) |
| 417 | } |
| 418 | return home.routes.Middleware |
| 419 | } |
| 420 | |
| 421 | // The local plugin resolves the first pending cookie, so reject ambiguity |
| 422 | // before it can verify a gesture or rotate the existing session. |
| 423 | func localAuthGuard(home *homeRoutes, next http.HandlerFunc) http.HandlerFunc { |
| 424 | if home == nil { |
| 425 | return next |
| 426 | } |
| 427 | return home.routes.LocalGuard(next) |
| 428 | } |
| 429 | |
| 430 | // homeLinkLabel is the account menu's "Connect your Oficina account", or "" |
| 431 | // when there is nothing to connect: offered only to a local person with no |
| 432 | // stored link, whichever way this session signed in. Before, it was offered |
| 433 | // to every session that had not come from a Home assertion, so a linked |
| 434 | // person who used the email link still saw it (Paul, as paul@tito.io). |
| 435 | // A team instance already resolved the link with the identity; otherwise |
| 436 | // it is one indexed lookup per menu render. Not knowing offers nothing. |
| 437 | func (h *handlers) homeLinkLabel(r *http.Request) string { |
| 438 | if !h.homeEnabled || h.homeLinked == nil { |
| 439 | return "" |
| 440 | } |
| 441 | id, ok := currentIdentity(r) |
| 442 | if !ok || connect.IsHomeDerived(id.Session.Method) || id.HomeSubject != "" { |
| 443 | return "" |
| 444 | } |
| 445 | if linked, err := h.homeLinked(r.Context(), id.Actor.PersonID); err != nil || linked { |
| 446 | return "" |
| 447 | } |
| 448 | return rastrillo.T(r, "docs.home.menu_action") |
| 449 | } |
| 450 | |
| 451 | func cookieCount(r *http.Request, name string) int { |
| 452 | n := 0 |
| 453 | for _, c := range r.Cookies() { |
| 454 | if c.Name == name { |
| 455 | n++ |
| 456 | } |
| 457 | } |
| 458 | return n |
| 459 | } |
| 460 | |
| 461 | // homeCookie distinguishes absent cookies from malformed or duplicate values. |
| 462 | // Neither a duplicate nor an invalid cookie may select ordinary sign-in. |
| 463 | func homeCookie(r *http.Request, name string) (value string, present, valid bool) { |
| 464 | count := 0 |
| 465 | for _, c := range r.Cookies() { |
| 466 | if c.Name == name { |
| 467 | count++ |
| 468 | value = c.Value |
| 469 | } |
| 470 | } |
| 471 | if count == 0 { |
| 472 | return "", false, true |
| 473 | } |
| 474 | if count != 1 || len(value) != 43 { |
| 475 | return "", true, false |
| 476 | } |
| 477 | raw, err := base64.RawURLEncoding.Strict().DecodeString(value) |
| 478 | return value, true, err == nil && len(raw) == 32 && base64.RawURLEncoding.EncodeToString(raw) == value |
| 479 | } |
| 480 | |
| 481 | // withoutCookie is r, as seen by a handler that must not see the named |
| 482 | // cookie. Only admittedOnly uses it, on connect's SigninDoor and |
| 483 | // SigninOficina. |
| 484 | func withoutCookie(r *http.Request, name string) *http.Request { |
| 485 | c := r.Clone(r.Context()) |
| 486 | c.Header.Del("Cookie") |
| 487 | for _, cookie := range r.Cookies() { |
| 488 | if cookie.Name != name { |
| 489 | c.AddCookie(cookie) |
| 490 | } |
| 491 | } |
| 492 | return c |
| 493 | } |
| 494 | |