diff --git a/PLAN.md b/PLAN.md index eca5e95..5d07e61 100644 --- a/PLAN.md +++ b/PLAN.md @@ -66,8 +66,9 @@ Core, multi-tenant. **Phase 12 (signing in with the Core, OIDC) is built** on branch `phase12`, which starts from `phase13` (2026-11-26), with the Core's -`idproviders.yml` as the guide. Not yet tried against a real Authelia. Phase 12 still needs who adds Supermarket's client to -Authelia. +`idproviders.yml` as the guide, all five parts (part 5, the Core's groups +to roles, on 2026-11-26). Not yet tried against a real Authelia; the +Core's admin adds Supermarket's client to `idproviders.yml`. **Coming back to it (for the agent working on it):** - Run the tests inside the sandbox with @@ -2760,9 +2761,8 @@ it at once. owner's, kept as a way in if the Core is down. 4. **Signing out**, which also ends the Authelia session where Authelia supports it. -5. **Groups, later:** Authelia's groups could map to roles (a - `supermarket-admins` group makes admins). Until then roles are set in - Supermarket. +5. **Groups:** Authelia's groups can map to roles (a + `supermarket-admins` group makes admins), and to modules. **What the Core's `idproviders.yml` shows (shared 2026-11-26).** Each service's client is an entry under `identity_providers.oidc.clients` in @@ -2789,7 +2789,7 @@ client: the Core's admin, pasting Supermarket's entry into `idproviders.yml` (2026-11-26). **Built 2026-11-26** on branch `phase12` (from `phase13`). Parts 1–4; part 5 -(groups to roles) stays for later. +(groups to roles) followed, below. - `internal/oidc`: discovery (the issuer must match), the code flow with PKCE (S256), a nonce and a state, the token exchange (`client_secret_post`), and the ID token's claims checked (issuer, @@ -2832,6 +2832,21 @@ client: the Core's admin, pasting Supermarket's entry into goes to the Core; and on multi-tenant (Postgres), the domain finds the team and the session works there. Not tried against a real Authelia yet. +**Part 5 built 2026-11-26** (on `main`, after Phase 14). +- Settings → Sign-in: *Roles follow the Core's groups*, with a prefix + (`supermarket` unless changed). In Authelia, `supermarket-admins` makes + an admin, `supermarket-` edits a module and + `supermarket--view` reads it (module IDs: content, prospects, + seo, metrics, sites, market). +- Each sign-in with the Core sets the person's role and modules from their + groups (the `groups` claim, from the ID token or userinfo), and the + change goes in the audit log. Someone not yet in the team who is in one + of the groups joins (with a seat free, on multi-tenant). The owner is + never changed. Someone in none of these groups keeps the access set in + Users, so people invited by hand still work; to take someone's access + away, turn them off in Users or at the Core. +- Access follows at the next sign-in, not mid-session. + #### Phase 13: Multi-tenant diff --git a/TEST.md b/TEST.md index 7672172..d4b309e 100644 --- a/TEST.md +++ b/TEST.md @@ -920,6 +920,13 @@ Hermes: to the Core; the owner's still works. - [ ] Signing out of Supermarket also signs out of the Core, if Authelia offers it (*Test* says so). +- [ ] **(real)** *Roles follow the Core's groups*: in the Core's + Authelia users, put someone in `supermarket-admins`; they sign in + with the Core and are an admin. Move them to `supermarket-content` + and `supermarket-seo-view`: at their next sign-in they edit Content + and read SEO, nothing else. Someone never invited but in + `supermarket-content` joins. Someone in none of these groups keeps + the access set in Users. The owner's role never changes. - [ ] Activity shows the sign-ins *with the Core*, and the refusals. ## 17. Multi-tenant (Phase 13) diff --git a/internal/web/coresignin.go b/internal/web/coresignin.go index 6496f65..9a828e2 100644 --- a/internal/web/coresignin.go +++ b/internal/web/coresignin.go @@ -37,6 +37,50 @@ type coreSignin struct { AutoJoin bool `json:"auto_join"` // anyone at the Core's domain joins AutoModules map[string]users.Access `json:"auto_modules"` Issuer string `json:"issuer,omitempty"` // empty: https://authelia. + // Roles follow the Core's groups: -admins, - + // (edit), --view. + Groups bool `json:"groups"` + GroupPrefix string `json:"group_prefix,omitempty"` // empty: supermarket +} + +func (c coreSignin) prefix() string { + if c.GroupPrefix == "" { + return "supermarket" + } + return c.GroupPrefix +} + +// groupAccess is the role and modules the Core's groups give, and whether +// any of them is one of Supermarket's (if none is, access stays as it was +// set here). +func groupAccess(prefix string, groups []string) (users.Role, map[string]users.Access, bool) { + role, mods, any := users.Member, map[string]users.Access{}, false + for _, g := range groups { + rest, ok := strings.CutPrefix(strings.ToLower(strings.TrimSpace(g)), strings.ToLower(prefix)+"-") + if !ok { + continue + } + if rest == "admins" { + role, any = users.Admin, true + continue + } + id, view := strings.CutSuffix(rest, "-view") + for _, gr := range grants() { + if gr.ID != id { + continue + } + any = true + if !view { + mods[id] = users.Edit + } else if mods[id] != users.Edit { + mods[id] = users.View + } + } + } + if role == users.Admin { + mods = map[string]users.Access{} + } + return role, mods, any } func (s *Server) coreSignin() coreSignin { @@ -134,6 +178,7 @@ type signinSettings struct { Grants []grant Problem string Tested string + Prefix string // the groups' } func (s *Server) signinSettingsPage(w http.ResponseWriter, r *http.Request) { @@ -147,6 +192,7 @@ func (s *Server) renderSigninSettings(w http.ResponseWriter, r *http.Request, pr } d := signinSettings{Core: s.core() != nil, Issuer: s.coreIssuer(), Callback: s.coreCallback(), C: s.coreSignin(), Block: s.clientBlock(), Grants: grants(), Problem: problem, Tested: tested} + d.Prefix = d.C.prefix() if c := s.core(); c != nil { d.Domain = c.Domain } @@ -207,6 +253,15 @@ func (s *Server) signinSettingsAction(w http.ResponseWriter, r *http.Request) { c.Enabled = r.PostFormValue("enabled") == "on" c.RequireCore = r.PostFormValue("require_core") == "on" c.AutoJoin = r.PostFormValue("auto_join") == "on" + c.Groups = r.PostFormValue("groups") == "on" + c.GroupPrefix = strings.ToLower(strings.TrimSpace(r.PostFormValue("group_prefix"))) + if c.GroupPrefix == "supermarket" { + c.GroupPrefix = "" + } + if strings.ContainsAny(c.GroupPrefix, " ,") { + s.renderSigninSettings(w, r, "The groups' prefix is one word, such as supermarket.", "") + return + } if i := strings.TrimSpace(r.PostFormValue("issuer")); i == "" || strings.HasPrefix(i, "https://") { c.Issuer = strings.TrimRight(i, "/") } else { @@ -234,7 +289,7 @@ func (s *Server) signinSettingsAction(w http.ResponseWriter, r *http.Request) { s.serverError(w, err) return } - s.audit(r, "signin-settings", "", fmt.Sprintf("with the Core %v, the Core only %v, anyone at the Core's domain joins %v", c.Enabled, c.RequireCore, c.AutoJoin)) + s.audit(r, "signin-settings", "", fmt.Sprintf("with the Core %v, the Core only %v, anyone at the Core's domain joins %v, roles follow the Core's groups %v", c.Enabled, c.RequireCore, c.AutoJoin, c.Groups)) redirect(w, r, "/settings/signin", "saved") default: http.NotFound(w, r) @@ -403,9 +458,45 @@ func (s *Server) finishCore(w http.ResponseWriter, r *http.Request, p *oidc.Prov return } member := false + var was users.Membership if u != nil { - _, merr := users.MembershipOf(s.platform(), s.team(), u.ID) - member = merr == nil + m, merr := users.MembershipOf(s.platform(), s.team(), u.ID) + member, was = merr == nil, m + } + gRole, gMods, inGroups := groupAccess(cfg.prefix(), id.Groups) + inGroups = inGroups && cfg.Groups + if !member && inGroups { + // In one of Supermarket's groups: invited by the Core. + if !id.Verified || id.Email == "" { + refuse("The Core didn't vouch for an email address, so Supermarket can't tell who you are here.") + return + } + if problem, _, _ := s.seatRoom(); problem != "" { + refuse("The team has no seat free for you. Ask its owner.") + return + } + if u == nil { + u = &users.User{Email: id.Email, Name: id.Name, Status: users.Active} + if err := users.Create(s.platform(), u, now); err != nil { + s.serverError(w, err) + return + } + } + if err := users.Join(s.platform(), s.team(), u.ID, gRole, gMods, now); err != nil { + s.serverError(w, err) + return + } + s.record(audit.Entry{UserID: u.ID, UserName: displayName(u), Action: "joined", Target: u.Email, + Detail: "with the Core, from its groups: " + accessText(users.Membership{Role: gRole, Modules: gMods}), IP: clientIP(r)}) + member, was = true, users.Membership{Role: gRole, Modules: gMods} + } else if member && inGroups && was.Role != users.Owner && !sameAccess(was, gRole, gMods) { + // The Core's groups changed: so does their access. + if err := users.Join(s.platform(), s.team(), u.ID, gRole, gMods, now); err != nil { + s.serverError(w, err) + return + } + s.record(audit.Entry{UserID: u.ID, UserName: displayName(u), Action: "access-changed", Target: u.Email, + Detail: accessText(users.Membership{Role: gRole, Modules: gMods}) + ", from the Core's groups", IP: clientIP(r)}) } if !member { // Anyone at the Core's domain may join, if the team says so. @@ -504,3 +595,22 @@ func (s *Server) coreSignOut(ctx context.Context) string { } return oidc.SignOutURL(p, "", s.PublicURL+"/signin?msg=signed-out") } + +// sameAccess says whether a membership already has a role and modules. +func sameAccess(m users.Membership, role users.Role, mods map[string]users.Access) bool { + if m.Role != role { + return false + } + if role != users.Member { + return true + } + if len(m.Modules) != len(mods) { + return false + } + for k, v := range mods { + if m.Modules[k] != v { + return false + } + } + return true +} diff --git a/internal/web/coresignin_test.go b/internal/web/coresignin_test.go index d5b379c..ad7df6e 100644 --- a/internal/web/coresignin_test.go +++ b/internal/web/coresignin_test.go @@ -237,3 +237,78 @@ func TestCoreSigninMultiTenant(t *testing.T) { t.Errorf("session %+v", sess) } } + +func TestGroupAccess(t *testing.T) { + role, mods, any := groupAccess("supermarket", []string{"staff", "supermarket-content", "Supermarket-SEO-view", "supermarket-seo", "supermarket-metrics-view", "supermarket-nope"}) + if !any || role != users.Member || mods["content"] != users.Edit || mods["seo"] != users.Edit || mods["metrics"] != users.View || len(mods) != 3 { + t.Errorf("%v %v %v", role, mods, any) + } + if role, mods, _ := groupAccess("supermarket", []string{"supermarket-admins", "supermarket-content"}); role != users.Admin || len(mods) != 0 { + t.Errorf("admin: %v %v", role, mods) + } + if _, _, any := groupAccess("supermarket", []string{"admins", "nextcloud-users", "supermarket-nope"}); any { + t.Error("someone else's groups counted") + } + if _, mods, _ := groupAccess("mkt", []string{"mkt-sites"}); mods["sites"] != users.Edit { + t.Errorf("prefix: %v", mods) + } +} + +func TestRolesFromCoreGroups(t *testing.T) { + s, h := newServerEdition(t) + owner := addUser(t, s, "owner@example.com", users.Owner, nil) + addUser(t, s, "sam@example.com", users.Member, map[string]users.Access{"seo": users.View}) + oc := sessionFor(t, s, owner) + f := newFakeCore(t) + setUpCore(t, s, h, oc, f, url.Values{"groups": {"on"}}) + access := func(email string) users.Membership { + t.Helper() + u, err := users.ByEmail(s.DB, email) + if err != nil { + t.Fatal(err) + } + m, _ := users.MembershipOf(s.DB, users.Team, u.ID) + return m + } + signIn := func(sub, email string, groups ...string) int { + f.who = map[string]any{"sub": sub, "email": email, "email_verified": true, "name": sub, "groups": groups} + return f.signInWithCore(t, h, url.Values{}).Code + } + + // In a group: joins, with its access, though never invited. + if code := signIn("lee", "lee@anywhere.example", "staff", "supermarket-content"); code != http.StatusSeeOther { + t.Fatalf("lee: %d", code) + } + if m := access("lee@anywhere.example"); m.Role != users.Member || m.Modules["content"] != users.Edit || len(m.Modules) != 1 { + t.Errorf("lee %+v", m) + } + // Not in one, not invited: refused. + if code := signIn("out", "out@anywhere.example", "staff"); code != http.StatusForbidden { + t.Errorf("out: %d", code) + } + // Sam's groups make an admin; later, back to reading SEO and editing Sites. + if signIn("sam", "sam@example.com", "supermarket-admins"); access("sam@example.com").Role != users.Admin { + t.Error("sam isn't an admin") + } + signIn("sam", "sam@example.com", "supermarket-seo-view", "supermarket-sites") + if m := access("sam@example.com"); m.Role != users.Member || m.Modules["seo"] != users.View || m.Modules["sites"] != users.Edit { + t.Errorf("sam %+v", m) + } + // In none of them: as set here. + signIn("sam", "sam@example.com", "staff") + if m := access("sam@example.com"); m.Modules["sites"] != users.Edit { + t.Errorf("sam lost access: %+v", m) + } + // The owner's role is theirs. + signIn("own", "owner@example.com", "supermarket-content-view") + if m := access("owner@example.com"); m.Role != users.Owner { + t.Errorf("owner %+v", m) + } + // Turned off, the groups change nothing. + cfg := s.coreSignin() + cfg.Groups = false + s.DB.PutJSON(coreSigninKey, cfg) + if code := signIn("pat", "pat@anywhere.example", "supermarket-content"); code != http.StatusForbidden { + t.Errorf("pat joined with groups off: %d", code) + } +} diff --git a/internal/web/templates/signin-settings.html b/internal/web/templates/signin-settings.html index 0decc32..0e65f16 100644 --- a/internal/web/templates/signin-settings.html +++ b/internal/web/templates/signin-settings.html @@ -35,6 +35,9 @@ {{template "access-fields" (dict "Role" "member" "Modules" $.Data.C.AutoModules "Grants" $.Data.Grants)}}

Otherwise only people invited may sign in: matched by their email the first time, then by their account at the Core.

+ + +

In Authelia's users, {{$.Data.Prefix}}-admins makes an admin; {{$.Data.Prefix}}-module lets someone edit a module, and {{$.Data.Prefix}}-module-view read it ({{range $i, $g := $.Data.Grants}}{{if $i}}, {{end}}{{$g.ID}}{{end}}). Each time someone signs in with the Core, their role and modules are set from their groups, and someone not yet in the team who is in one joins (if there's a seat free). The owner is never changed; someone in none of these groups keeps the access set here.