The callback now starts a session only when the verified groups claim
contains a group from MEALQUEST_OIDC_ALLOWED_GROUPS. When the ID token has
no groups claim, groups come from the userinfo endpoint, whose subject must
match the ID token. A refused sign-in gets a 403 HTML page ("This kitchen is
for the household only") with a sign-out button, and no session. The check
runs on every login, so an account removed from the group is refused at its
next one. auth.New requires at least one allowed group and a discovered
userinfo endpoint that passes the endpoint rule.
The RequireSession middleware wraps the /api/v1 and /media subrouters: no
live session gets 401 {"error":{"code":"unauthenticated",...}}, a lookup
failure 500, and a live session reaches the handler with the actor in the
context. It wraps the subrouters instead of using chi's Use because chi
skips middleware on a subrouter with no routes. /health, /auth and the web
build stay public.
auth.WithActor and auth.ActorFrom are the one way to read the signed-in
household member; handlers record Actor.Subject as updated_by.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
36 lines
973 B
Go
36 lines
973 B
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
)
|
|
|
|
// Actor is the signed-in household member a request acts for. Handlers
|
|
// record Actor.Subject as updated_by on edits.
|
|
type Actor struct {
|
|
Subject string
|
|
Email string
|
|
Name string
|
|
Groups []string
|
|
}
|
|
|
|
// Actor returns the household member s belongs to.
|
|
func (s Session) Actor() Actor {
|
|
return Actor{Subject: s.Subject, Email: s.Email, Name: s.Name, Groups: slices.Clone(s.Groups)}
|
|
}
|
|
|
|
type actorKey struct{}
|
|
|
|
// WithActor returns a copy of ctx carrying actor. The session middleware
|
|
// calls it for every authenticated request.
|
|
func WithActor(ctx context.Context, actor Actor) context.Context {
|
|
return context.WithValue(ctx, actorKey{}, actor)
|
|
}
|
|
|
|
// ActorFrom returns the actor WithActor stored in ctx. ok is false when ctx
|
|
// has none, which behind the session middleware means a wiring bug.
|
|
func ActorFrom(ctx context.Context) (actor Actor, ok bool) {
|
|
actor, ok = ctx.Value(actorKey{}).(Actor)
|
|
return actor, ok
|
|
}
|