Phase 12, part 5: roles from the Core's groups

Settings → Sign-in: roles follow the Core's groups, with a prefix.
<prefix>-admins makes an admin, <prefix>-<module> edits a module,
<prefix>-<module>-view reads it. Set at each sign-in with the Core; someone
in a group joins; the owner never changes; someone in none of them keeps
the access set here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 15:13:45 -07:00
co-authored by Claude Opus 5.5
parent 6d3eb18b5e
commit 2f235ad260
5 changed files with 219 additions and 9 deletions
+21 -6
View File
@@ -66,8 +66,9 @@ Core, multi-tenant.
**Phase 12 (signing in with the Core, OIDC) is built** on branch **Phase 12 (signing in with the Core, OIDC) is built** on branch
`phase12`, which starts from `phase13` (2026-11-26), with the Core's `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 `idproviders.yml` as the guide, all five parts (part 5, the Core's groups
Authelia. 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):** **Coming back to it (for the agent working on it):**
- Run the tests inside the sandbox with - 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. owner's, kept as a way in if the Core is down.
4. **Signing out**, which also ends the Authelia session where Authelia 4. **Signing out**, which also ends the Authelia session where Authelia
supports it. supports it.
5. **Groups, later:** Authelia's groups could map to roles (a 5. **Groups:** Authelia's groups can map to roles (a
`supermarket-admins` group makes admins). Until then roles are set in `supermarket-admins` group makes admins), and to modules.
Supermarket.
**What the Core's `idproviders.yml` shows (shared 2026-11-26).** Each **What the Core's `idproviders.yml` shows (shared 2026-11-26).** Each
service's client is an entry under `identity_providers.oidc.clients` in 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). `idproviders.yml` (2026-11-26).
**Built 2026-11-26** on branch `phase12` (from `phase13`). Parts 1–4; part 5 **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 - `internal/oidc`: discovery (the issuer must match), the code flow with
PKCE (S256), a nonce and a state, the token exchange PKCE (S256), a nonce and a state, the token exchange
(`client_secret_post`), and the ID token's claims checked (issuer, (`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 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. 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-<module>` edits a module and
`supermarket-<module>-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 #### Phase 13: Multi-tenant
+7
View File
@@ -920,6 +920,13 @@ Hermes:
to the Core; the owner's still works. to the Core; the owner's still works.
- [ ] Signing out of Supermarket also signs out of the Core, if Authelia - [ ] Signing out of Supermarket also signs out of the Core, if Authelia
offers it (*Test* says so). 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. - [ ] Activity shows the sign-ins *with the Core*, and the refusals.
## 17. Multi-tenant (Phase 13) ## 17. Multi-tenant (Phase 13)
+113 -3
View File
@@ -37,6 +37,50 @@ type coreSignin struct {
AutoJoin bool `json:"auto_join"` // anyone at the Core's domain joins AutoJoin bool `json:"auto_join"` // anyone at the Core's domain joins
AutoModules map[string]users.Access `json:"auto_modules"` AutoModules map[string]users.Access `json:"auto_modules"`
Issuer string `json:"issuer,omitempty"` // empty: https://authelia.<the Core's domain> Issuer string `json:"issuer,omitempty"` // empty: https://authelia.<the Core's domain>
// Roles follow the Core's groups: <prefix>-admins, <prefix>-<module>
// (edit), <prefix>-<module>-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 { func (s *Server) coreSignin() coreSignin {
@@ -134,6 +178,7 @@ type signinSettings struct {
Grants []grant Grants []grant
Problem string Problem string
Tested string Tested string
Prefix string // the groups'
} }
func (s *Server) signinSettingsPage(w http.ResponseWriter, r *http.Request) { 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(), d := signinSettings{Core: s.core() != nil, Issuer: s.coreIssuer(), Callback: s.coreCallback(), C: s.coreSignin(),
Block: s.clientBlock(), Grants: grants(), Problem: problem, Tested: tested} Block: s.clientBlock(), Grants: grants(), Problem: problem, Tested: tested}
d.Prefix = d.C.prefix()
if c := s.core(); c != nil { if c := s.core(); c != nil {
d.Domain = c.Domain 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.Enabled = r.PostFormValue("enabled") == "on"
c.RequireCore = r.PostFormValue("require_core") == "on" c.RequireCore = r.PostFormValue("require_core") == "on"
c.AutoJoin = r.PostFormValue("auto_join") == "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://") { if i := strings.TrimSpace(r.PostFormValue("issuer")); i == "" || strings.HasPrefix(i, "https://") {
c.Issuer = strings.TrimRight(i, "/") c.Issuer = strings.TrimRight(i, "/")
} else { } else {
@@ -234,7 +289,7 @@ func (s *Server) signinSettingsAction(w http.ResponseWriter, r *http.Request) {
s.serverError(w, err) s.serverError(w, err)
return 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") redirect(w, r, "/settings/signin", "saved")
default: default:
http.NotFound(w, r) http.NotFound(w, r)
@@ -403,9 +458,45 @@ func (s *Server) finishCore(w http.ResponseWriter, r *http.Request, p *oidc.Prov
return return
} }
member := false member := false
var was users.Membership
if u != nil { if u != nil {
_, merr := users.MembershipOf(s.platform(), s.team(), u.ID) m, merr := users.MembershipOf(s.platform(), s.team(), u.ID)
member = merr == nil 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 { if !member {
// Anyone at the Core's domain may join, if the team says so. // 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") 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
}
+75
View File
@@ -237,3 +237,78 @@ func TestCoreSigninMultiTenant(t *testing.T) {
t.Errorf("session %+v", sess) 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)
}
}
@@ -35,6 +35,9 @@
<label class="check"><input type="checkbox" name="auto_join" {{if $.Data.C.AutoJoin}}checked{{end}}> <span>Anyone with an address at <strong>{{$.Data.Domain}}</strong> may join, as a member with these modules:</span></label> <label class="check"><input type="checkbox" name="auto_join" {{if $.Data.C.AutoJoin}}checked{{end}}> <span>Anyone with an address at <strong>{{$.Data.Domain}}</strong> may join, as a member with these modules:</span></label>
{{template "access-fields" (dict "Role" "member" "Modules" $.Data.C.AutoModules "Grants" $.Data.Grants)}} {{template "access-fields" (dict "Role" "member" "Modules" $.Data.C.AutoModules "Grants" $.Data.Grants)}}
<p class="help">Otherwise only people invited may sign in: matched by their email the first time, then by their account at the Core.</p> <p class="help">Otherwise only people invited may sign in: matched by their email the first time, then by their account at the Core.</p>
<label class="check"><input type="checkbox" name="groups" {{if $.Data.C.Groups}}checked{{end}}> <span>Roles follow the Core's groups</span></label>
<label class="small">Groups' prefix <input name="group_prefix" value="{{$.Data.C.GroupPrefix}}" placeholder="supermarket" size="16"></label>
<p class="help">In Authelia's users, <code>{{$.Data.Prefix}}-admins</code> makes an admin; <code>{{$.Data.Prefix}}-<em>module</em></code> lets someone edit a module, and <code>{{$.Data.Prefix}}-<em>module</em>-view</code> read it ({{range $i, $g := $.Data.Grants}}{{if $i}}, {{end}}<code>{{$g.ID}}</code>{{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.</p>
<button type="submit">Save</button> <button type="submit">Save</button>
</form> </form>
</section> </section>