Add per-user project-access restriction; generalize mediaauth into a unified auth proxy

shared/backend/auth: adds SchemaOptions.ProjectScoping and Store methods
(SetUserProjects/UserProjects/UserProjectsByUser/IsProjectAllowed,
SetKnownProjects/KnownProjects) backing AuthGuard's new per-user
project-access restriction.

shared/backend/authclient: Client can now identify its calling project to
AuthServer via WithProjectKey (sent as X-Project-Key on every request), so
AuthServer can enforce the restriction above per project.

shared/backend/mediaauth: generalized from a media-family-only package
into the one shared auth-proxy engine every AuthGuard-integrated project
should use. New accepts a projectKey; the login-required/setup-claim
storage it needs is now expressed as a small Store interface
(NewWithStore lets any project's own storage satisfy it, not just the
media family's shared `settings` table, which pgSettingsStore still backs
for existing callers). ManagedUpdateUser/ManagedDeleteUser now resolve
their {id} path param via chi.URLParam OR the stdlib 1.22+ ServeMux's
PathValue, so non-chi consumers work unchanged.

packages/web-ui: UserManagement gains an optional `projects` prop (a
per-account project-access picker), used only by AuthGuard's own admin UI.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Gabriel20xxandClaude Sonnet 5 committed 2026-09-13 12:16:17 +02:00
1 parent 6ac2fcefeb
commit 663d498a03
7 files changed
+476 -51

No files matched your search

+33
View File
@@ -63,8 +63,20 @@ type Client struct {
// when Middleware has no Authorization header to use. Defaults to
// "auth_session", matching AuthServer's SessionCookieName.
cookieName string
// projectKey identifies the calling project to AuthServer (sent as the
// ProjectKeyHeader on every request) so per-user project-access
// restrictions can be enforced. Empty means "don't identify" - the
// caller is treated as unrestricted; see AuthServer's project-scoping
// feature and WithProjectKey.
projectKey string
}
// ProjectKeyHeader is the header Client attaches to every request identifying
// which project is calling, so AuthServer can enforce per-user project-access
// restriction (see WithProjectKey). AuthServer treats a missing/empty header
// the same as "this caller has no project identity": never restricted.
const ProjectKeyHeader = "X-Project-Key"
// Option configures optional Client behavior.
type Option func(*Client)
@@ -81,6 +93,16 @@ func WithCookieName(name string) Option {
return func(c *Client) { c.cookieName = name }
}
// WithProjectKey identifies this Client's project to AuthServer (see
// ProjectKeyHeader), enabling per-user project-access restriction. key is
// normally a project's own lowercase identifier, matching the same key
// ProjectConfigurator's Projects workspace registers it under (e.g.
// "carriernode", "gamestream", "omnilogger") - see AuthServer's project-scope
// admin UI, which assigns users access using these same keys.
func WithProjectKey(key string) Option {
return func(c *Client) { c.projectKey = strings.ToLower(strings.TrimSpace(key)) }
}
// New creates a Client for the AuthServer instance at baseURL. baseURL may
// be given without a scheme (e.g. "auth.example.com" or "192.168.1.50:8080")
// - the scheme is then inferred from the host: a loopback, RFC1918/RFC4193
@@ -124,6 +146,15 @@ func (c *Client) getBaseURL() string {
return c.baseURL
}
// setProjectKeyHeader attaches ProjectKeyHeader when this Client was built
// with WithProjectKey; a no-op otherwise, so an unconfigured Client behaves
// exactly as it did before project scoping existed.
func (c *Client) setProjectKeyHeader(req *http.Request) {
if c.projectKey != "" {
req.Header.Set(ProjectKeyHeader, c.projectKey)
}
}
func normalizeBaseURL(baseURL string) string {
trimmed := strings.TrimRight(strings.TrimSpace(baseURL), "/")
if trimmed == "" {
@@ -186,6 +217,7 @@ func (c *Client) Validate(ctx context.Context, token string) (*User, error) {
return nil, fmt.Errorf("authclient: build request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token)
c.setProjectKeyHeader(req)
resp, err := c.httpClient.Do(req)
if err != nil {
@@ -418,6 +450,7 @@ func (c *Client) call(ctx context.Context, method, path, token string, body, out
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
c.setProjectKeyHeader(req)
resp, err := c.httpClient.Do(req)
if err != nil {