Sync shared snapshot from 663d498
Docker / Docker Build & Push (push) Successful in 3m0s

This commit is contained in:
Gabriel20xx committed 2026-09-13 12:18:24 +02:00
1 parent 1d99f0272d
commit b1412a9016
7 files changed
+476 -51

No files matched your search

+153 -50
View File
@@ -5,6 +5,29 @@
// MediaViewer, ContentScanner, CraftStudio) use one copy instead of
// duplicating it.
//
// Unified auth proxy
// ==================
// This package used to be specific to the 7 media-family backends (each
// built via New, wired directly to their own shared Postgres `settings`
// table). It is now the ONE shared auth-proxy engine for every project in
// this workspace that centralizes identity through AuthGuard - GameStream,
// Bitmagnet, and OmniLogger, which each used to independently reimplement
// this same session/login/TOTP/admin-user proxy against their own storage,
// now build a Service via NewWithStore against a small Store adapter over
// whatever local table they already used for their "login required" toggle
// and one-time setup-claim flag (see the Store interface below), instead of
// duplicating this file's logic. New (the original media-family
// constructor) is unchanged and is really just NewWithStore plus
// pgSettingsStore, the default Store implementation backed by the shared
// `settings` table.
//
// ProjectKey (New/NewWithStore's last parameter) identifies the calling
// project to AuthGuard on every request (authclient.WithProjectKey), so an
// administrator can restrict which projects a given AuthGuard account may
// authenticate against; see AuthGuard's project-scope admin UI. Leave it ""
// to opt out (every account is then unrestricted for this project, the
// pre-existing behavior).
//
// AuthGuard migration
// ====================
// This service used to be a fully self-contained auth implementation:
@@ -190,8 +213,33 @@ const (
// underlying admin endpoints have nothing to manage.
var localProfile = User{ID: 0, Username: "local", Email: "", Role: string(sharedauth.RoleSuperAdmin), CanManageUsers: true}
// Store is the small piece of local, project-owned persistence a Service
// needs beyond AuthGuard itself: the independent "require login" toggle
// (RequireIfLoginRequired/RequireAdminIfLoginRequired predate the AuthGuard
// migration - see the package doc comment's second paragraph) and the
// one-time "has this deployment's setup screen been completed" claim flag
// (see the "Setup flow" section below). pgSettingsStore is the
// implementation New builds automatically over the media family's shared
// `settings` table; NewWithStore accepts any other implementation, letting a
// project with its own storage (GameStream's/Bitmagnet's/OmniLogger's own
// tables) reuse this package without adopting that generic table.
type Store interface {
LoginRequired(ctx context.Context) (bool, error)
SetLoginRequired(ctx context.Context, required bool) error
IsSetupClaimed(ctx context.Context) (bool, error)
TryClaimSetup(ctx context.Context) (bool, error)
UnclaimSetup(ctx context.Context) error
}
type Service struct {
// db backs only the "moderatable platform users" section at the bottom
// of this file (CreateUser/ListUsers/UpdateUser/SetPassword/DeleteUser),
// a media-family-specific concept unrelated to AuthGuard identity - see
// that section's own doc comment. nil for a Service built via
// NewWithStore; those methods are simply never called by a project with
// no such local platform-user table.
db *pgxpool.Pool
store Store
cookieName string
secureCookies bool
// authGuard is nil when AUTHGUARD_URL is unset; see the top-of-file doc
@@ -199,18 +247,96 @@ type Service struct {
authGuard *authclient.Client
}
// New constructs a Service. cookieName/secureCookies are retained only for
// clearing the pre-migration session cookie on logout (see the package doc
// comment's "Session cookie / token model" section) - the live session
// cookie this service actually reads/sets is always AuthGuard's own
// New constructs a Service backed by the media family's shared `settings`
// table (pgSettingsStore) for the login-required toggle and setup-claim
// flag, and by db directly for the unrelated "moderatable platform users"
// section. cookieName/secureCookies are retained only for clearing the
// pre-migration session cookie on logout (see the package doc comment's
// "Session cookie / token model" section) - the live session cookie this
// service actually reads/sets is always AuthGuard's own
// authclient.DefaultCookieName. authGuardURL is AUTHGUARD_URL; empty means
// auth-disabled mode.
func New(db *pgxpool.Pool, cookieName string, secureCookies bool, authGuardURL string) *Service {
// auth-disabled mode. projectKey identifies this project to AuthGuard for
// per-user project-access restriction; see the "Unified auth proxy" section
// above.
func New(db *pgxpool.Pool, cookieName string, secureCookies bool, authGuardURL, projectKey string) *Service {
service := NewWithStore(pgSettingsStore{db: db}, cookieName, secureCookies, authGuardURL, projectKey)
service.db = db
return service
}
// NewWithStore constructs a Service against any Store implementation, for a
// project whose login-required toggle and setup-claim flag live in its own
// storage rather than the media family's shared `settings` table. Its
// resulting Service has no db (the "moderatable platform users" section is
// unavailable - a project adopting this package fresh has no such feature to
// preserve).
func NewWithStore(store Store, cookieName string, secureCookies bool, authGuardURL, projectKey string) *Service {
var client *authclient.Client
if strings.TrimSpace(authGuardURL) != "" {
client = authclient.New(authGuardURL)
client = authclient.New(authGuardURL, authclient.WithProjectKey(projectKey))
}
return &Service{db: db, cookieName: cookieName, secureCookies: secureCookies, authGuard: client}
return &Service{store: store, cookieName: cookieName, secureCookies: secureCookies, authGuard: client}
}
// pgSettingsStore is the default Store New builds over the media family's
// shared `settings` key/value table (see mediacore.SettingValue/
// SaveSetting) - unchanged behavior from before this package accepted a
// pluggable Store.
type pgSettingsStore struct {
db *pgxpool.Pool
}
func (s pgSettingsStore) LoginRequired(ctx context.Context) (bool, error) {
var value string
err := s.db.QueryRow(ctx, `SELECT COALESCE(value, '') FROM settings WHERE key = $1`, loginRequiredSettingKey).Scan(&value)
if errors.Is(err, pgx.ErrNoRows) {
return true, nil
}
if err != nil {
return true, err
}
switch strings.ToLower(strings.TrimSpace(value)) {
case "false", "0", "no", "off":
return false, nil
default:
return true, nil
}
}
func (s pgSettingsStore) SetLoginRequired(ctx context.Context, required bool) error {
_, err := s.db.Exec(ctx, `
INSERT INTO settings (key, value, updated_at) VALUES ($1, $2, NOW())
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()
`, loginRequiredSettingKey, strconv.FormatBool(required))
return err
}
func (s pgSettingsStore) IsSetupClaimed(ctx context.Context) (bool, error) {
var value string
err := s.db.QueryRow(ctx, `SELECT COALESCE(value, '') FROM settings WHERE key = $1`, setupClaimedSettingKey).Scan(&value)
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
}
if err != nil {
return false, err
}
return value == "true", nil
}
func (s pgSettingsStore) TryClaimSetup(ctx context.Context) (bool, error) {
tag, err := s.db.Exec(ctx, `
INSERT INTO settings (key, value, updated_at) VALUES ($1, 'true', NOW())
ON CONFLICT (key) DO NOTHING
`, setupClaimedSettingKey)
if err != nil {
return false, err
}
return tag.RowsAffected() > 0, nil
}
func (s pgSettingsStore) UnclaimSetup(ctx context.Context) error {
_, err := s.db.Exec(ctx, `DELETE FROM settings WHERE key = $1`, setupClaimedSettingKey)
return err
}
// enabled reports whether AUTHGUARD_URL was configured. See the top-of-file
@@ -497,21 +623,7 @@ func (s *Service) AdminNeeded(w http.ResponseWriter, r *http.Request) {
}
func (s *Service) LoginRequired(ctx context.Context) (bool, error) {
var value string
err := s.db.QueryRow(ctx, `SELECT COALESCE(value, '') FROM settings WHERE key = $1`, loginRequiredSettingKey).Scan(&value)
if errors.Is(err, pgx.ErrNoRows) {
return true, nil
}
if err != nil {
return true, err
}
switch strings.ToLower(strings.TrimSpace(value)) {
case "false", "0", "no", "off":
return false, nil
default:
return true, nil
}
return s.store.LoginRequired(ctx)
}
func (s *Service) AccessSettings(w http.ResponseWriter, r *http.Request) {
@@ -544,12 +656,7 @@ func (s *Service) UpdateAccessSettings(w http.ResponseWriter, r *http.Request) {
// central "Require Login" toggle (ConfigWorkspace.svelte's loginControls) apply here without an
// admin session of its own.
func (s *Service) SetLoginRequired(ctx context.Context, required bool) error {
_, err := s.db.Exec(ctx, `
INSERT INTO settings (key, value, updated_at)
VALUES ($1, $2, NOW())
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()
`, loginRequiredSettingKey, strconv.FormatBool(required))
return err
return s.store.SetLoginRequired(ctx, required)
}
func (s *Service) Status(w http.ResponseWriter, r *http.Request) {
@@ -655,7 +762,7 @@ func (s *Service) ManagedCreateUser(w http.ResponseWriter, r *http.Request) {
// enforces authorization (including the "the superadmin account is
// protected" rule) itself.
func (s *Service) ManagedUpdateUser(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
id, err := strconv.ParseInt(idURLParam(r), 10, 64)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid user id")
return
@@ -698,7 +805,7 @@ func (s *Service) ManagedUpdateUser(w http.ResponseWriter, r *http.Request) {
// enforces authorization (including "this user cannot be deleted" rules)
// itself.
func (s *Service) ManagedDeleteUser(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
id, err := strconv.ParseInt(idURLParam(r), 10, 64)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid user id")
return
@@ -976,31 +1083,15 @@ func (s *Service) TOTPDisable(w http.ResponseWriter, r *http.Request) {
// the pre-existing `settings` key/value table (same mechanism as
// LoginRequired's `auth_required` key) rather than a new table.
func (s *Service) isSetupClaimed(ctx context.Context) (bool, error) {
var value string
err := s.db.QueryRow(ctx, `SELECT COALESCE(value, '') FROM settings WHERE key = $1`, setupClaimedSettingKey).Scan(&value)
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
}
if err != nil {
return false, err
}
return value == "true", nil
return s.store.IsSetupClaimed(ctx)
}
func (s *Service) tryClaimSetup(ctx context.Context) (bool, error) {
tag, err := s.db.Exec(ctx, `
INSERT INTO settings (key, value, updated_at) VALUES ($1, 'true', NOW())
ON CONFLICT (key) DO NOTHING
`, setupClaimedSettingKey)
if err != nil {
return false, err
}
return tag.RowsAffected() > 0, nil
return s.store.TryClaimSetup(ctx)
}
func (s *Service) unclaimSetup(ctx context.Context) error {
_, err := s.db.Exec(ctx, `DELETE FROM settings WHERE key = $1`, setupClaimedSettingKey)
return err
return s.store.UnclaimSetup(ctx)
}
// --- Moderatable platform users (unrelated to admin login identity) --------
@@ -1208,6 +1299,18 @@ func normalizeRole(role string) string {
}
}
// idURLParam extracts the "{id}" path segment from ManagedUpdateUser's/
// ManagedDeleteUser's route regardless of which router mounted it: chi's
// URLParam for the (7) media-family services that route through chi, or the
// standard library's 1.22+ ServeMux PathValue (what GameStream, OmniLogger,
// and other non-chi consumers of this package use) when chi reports none.
func idURLParam(r *http.Request) string {
if id := chi.URLParam(r, "id"); id != "" {
return id
}
return r.PathValue("id")
}
func decodeJSON(r *http.Request, target any) error {
defer r.Body.Close()
decoder := json.NewDecoder(io.LimitReader(r.Body, 128*1024))