| 1 | package sheets |
| 2 | |
| 3 | // Home sign-in for a roster member with no account here: connect's two |
| 4 | // team-mode hooks over Sheets' people table. connect completes an |
| 5 | // ordinary Home sign-in only for a subject with an account link, and |
| 6 | // refuses the rest as "unknown" — right for an allowlist instance, where |
| 7 | // nobody may enter on a Home identity alone. In team mode a roster member |
| 8 | // who has never had a local account must be able to, so ConfigureHome |
| 9 | // gives connect these, and connect's own completion does the rest (its |
| 10 | // cookie checks, the same-person rule, the link flow). |
| 11 | // |
| 12 | // The member Unlinked creates is identity, never standing: every request |
| 13 | // after this one asks the roster again (team_access.go). |
| 14 | |
| 15 | import ( |
| 16 | "context" |
| 17 | "database/sql" |
| 18 | "errors" |
| 19 | |
| 20 | "amadan.net/oficina/home/connect" |
| 21 | "amadan.net/oficina/home/teammode" |
| 22 | |
| 23 | "amadan.net/oficina/sheets/internal/sheets/teamstate" |
| 24 | ) |
| 25 | |
| 26 | // teamAdmit is connect's Admit, the roster decision taken first (Home spec |
| 27 | // 2026-11-14-oficina-id-native.md §8) for a subject with neither a link |
| 28 | // nor a Home-only member: Runtime.AdmitUnlinked, which allocates nothing |
| 29 | // and never asks whether Sheets has local accounts. A Member is asked to |
| 30 | // choose (connect's choose page, or 409 natively); a stranger is |
| 31 | // connect's "unknown"; an undecidable roster is a 503. |
| 32 | func (a *App) teamAdmit(ctx context.Context, issuer, subject string) error { |
| 33 | if a.team == nil { |
| 34 | return errTeamUnavailable |
| 35 | } |
| 36 | switch err := a.team.AdmitUnlinked(ctx, issuer, subject); { |
| 37 | case err == nil: |
| 38 | return nil |
| 39 | case errors.Is(err, teammode.ErrNotMember): |
| 40 | return connect.ErrNoPerson |
| 41 | } |
| 42 | return errTeamUnavailable |
| 43 | } |
| 44 | |
| 45 | // teamHomeOnly is connect's HomeOnly: the addressless member Unlinked |
| 46 | // gave a subject, read and never created. |
| 47 | func (a *App) teamHomeOnly(d *sql.DB) func(context.Context, string, string) (connect.Person, error) { |
| 48 | return func(ctx context.Context, issuer, subject string) (connect.Person, error) { |
| 49 | id, err := teamstate.HomeMember(ctx, d, issuer, subject) |
| 50 | switch { |
| 51 | case err == nil: |
| 52 | return connect.Person{ID: id}, nil |
| 53 | case errors.Is(err, teamstate.ErrRefused): |
| 54 | return connect.Person{}, connect.ErrNoPerson |
| 55 | } |
| 56 | return connect.Person{}, errTeamUnavailable |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // teamUnlinked is connect's Unlinked, asked only after a Member chose "I'm |
| 61 | // new here" with nobody signed in, for a subject with neither a link nor |
| 62 | // a Home-only member (connect rechecks teamAdmit first): an |
| 63 | // addressless member for a subject the roster says is a Member now, with |
| 64 | // the lease on that answer still valid (Runtime.AdmitUnlinked). A |
| 65 | // stranger is connect's "unknown"; an undecidable roster is a 503. |
| 66 | func (a *App) teamUnlinked(d *sql.DB) func(context.Context, string, string) (connect.Person, error) { |
| 67 | return func(ctx context.Context, issuer, subject string) (connect.Person, error) { |
| 68 | if a.team == nil { |
| 69 | return connect.Person{}, errTeamUnavailable |
| 70 | } |
| 71 | if err := a.team.AdmitUnlinked(ctx, issuer, subject); err != nil { |
| 72 | if errors.Is(err, teammode.ErrNotMember) { |
| 73 | return connect.Person{}, connect.ErrNoPerson |
| 74 | } |
| 75 | return connect.Person{}, errTeamUnavailable |
| 76 | } |
| 77 | id, err := teamstate.EnsureHomeMember(ctx, d, issuer, subject) |
| 78 | switch { |
| 79 | case err == nil: |
| 80 | return connect.Person{ID: id}, nil |
| 81 | case errors.Is(err, teamstate.ErrRefused): |
| 82 | // Linked to a local account in the meantime: that person signs |
| 83 | // in as that account, not as a new one. |
| 84 | return connect.Person{}, connect.ErrNoPerson |
| 85 | } |
| 86 | return connect.Person{}, errTeamUnavailable |
| 87 | } |
| 88 | } |
| 89 | |