Make AuthGuard integration decouple from knowing the URL at startup
Every language's AuthGuard client/service used "no URL at construction" to mean "auth disabled forever" - a live SetBaseUrl/setBaseUrl push from ProjectConfigurator could only ever retarget an *already-enabled* client, never turn one on for the first time. That made AUTHGUARD_URL a de-facto required env var everywhere, even though ProjectConfigurator can push it live after startup. - Go: shared/backend/auth's New/NewWithStore now default to a "pending" Service (still enabled, fails closed until a URL arrives) instead of disabled when authGuardURL is empty; add an explicit Disabled() option for the genuine standalone/local-dev opt-out. - Python/Node/Java: add setBaseUrl/set_base_url to each AuthClient so a client built with an empty base URL can be retargeted in place once a URL is pushed, the same as the Go client already supported. - Python/Node: add a coreconfig module (Info/Store), the framework-agnostic equivalent of shared/backend/coreconfig (Go), for services in those languages to receive ProjectConfigurator's core-services push at all. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c09957189f
commit
dcfdb3d7af
18 files changed
+747
-46
No files matched your search
+94
-37
@@ -112,29 +112,45 @@
|
||||
// frontend (webui) confirms neither field was ever actually read
|
||||
// or set by any UI - they were dead weight even before this migration.
|
||||
//
|
||||
// Auth-disabled mode (AUTHGUARD_URL unset)
|
||||
// -----------------------------------------
|
||||
// AUTHGUARD_URL is optional. When it is unset, Service.authGuard is nil
|
||||
// (Service.enabled() reports false) and this service runs with
|
||||
// authentication turned off entirely: CurrentUser always resolves to
|
||||
// localProfile (a fixed placeholder superadmin identity), so every request
|
||||
// is treated as already authenticated regardless of the `auth_required`
|
||||
// setting - there is no login screen and nothing gated. This is an explicit
|
||||
// mode for local development or a standalone deployment that doesn't want
|
||||
// centralized identity, matching every other AuthGuard-migrated project's
|
||||
// exact convention; it is not an error condition, and there is no
|
||||
// degraded/partial "local auth" fallback state.
|
||||
// Auth-disabled mode (the Disabled() option)
|
||||
// -------------------------------------------
|
||||
// Passing Disabled() to New/NewWithStore turns authentication off entirely
|
||||
// for a Service: Service.authGuard is nil (Service.enabled() reports false),
|
||||
// CurrentUser always resolves to localProfile (a fixed placeholder
|
||||
// superadmin identity), so every request is treated as already authenticated
|
||||
// regardless of the `auth_required` setting - there is no login screen and
|
||||
// nothing gated. This is an explicit mode for local development or a
|
||||
// standalone deployment that doesn't want centralized identity at all; it is
|
||||
// not an error condition, and there is no degraded/partial "local auth"
|
||||
// fallback state.
|
||||
//
|
||||
// Without Disabled(), a Service is always "enabled" - including when
|
||||
// authGuardURL is empty at construction. That used to also mean disabled,
|
||||
// but made AUTHGUARD_URL a de-facto requirement: a Service built with no URL
|
||||
// could never start authenticating later, since a live retarget
|
||||
// (SetAuthGuardBaseURL, wired as ProjectConfigurator's coreconfig push) had
|
||||
// no client to retarget. Now an empty authGuardURL just means "pending": the
|
||||
// Service still gates every request, but its calls to AuthGuard fail (with
|
||||
// errAuthGuardUnavailable, a 401) until a URL arrives - normally within
|
||||
// ProjectConfigurator's periodic re-push window (coreconfig's doc comment),
|
||||
// converging on its own with no restart needed, the same way a live
|
||||
// AuthGuard URL *change* already did before this pending mode existed. A
|
||||
// project that wants no AuthGuard integration at all (the old meaning of an
|
||||
// unset AUTHGUARD_URL) must now say so explicitly via Disabled().
|
||||
//
|
||||
// Features that need a *real* backing identity rather than just an access
|
||||
// gate - TOTP (initiate/verify/disable), self-service profile editing, and
|
||||
// admin user-management's mutating calls - have nothing meaningful to do
|
||||
// without AuthGuard (there is no account to attach a TOTP secret to, no
|
||||
// in Disabled() mode (there is no account to attach a TOTP secret to, no
|
||||
// profile to edit, no user store to create/update/delete against) and
|
||||
// return a clear errFeatureRequiresAuthGuard error instead of silently
|
||||
// no-opping or faking data. GET /api/auth/users is the one exception:
|
||||
// consistent with "nothing gated", it returns an empty list rather than an
|
||||
// error, since listing (unlike mutating) has an honest, unsurprising answer
|
||||
// when there is no user store: there are no managed users to show.
|
||||
// when there is no user store: there are no managed users to show. In
|
||||
// pending mode (enabled, but no URL yet) these same calls are attempted for
|
||||
// real and fail closed with errAuthGuardUnavailable instead, since AuthGuard
|
||||
// integration genuinely is turned on here - it just isn't reachable yet.
|
||||
//
|
||||
// No local admin-reset flow
|
||||
// --------------------------
|
||||
@@ -191,9 +207,10 @@ var (
|
||||
ErrForbidden = errors.New("administrator access required")
|
||||
ErrInvalidLogin = errors.New("Invalid username or password.")
|
||||
// errFeatureRequiresAuthGuard is returned by the handlers that need a
|
||||
// real backing identity (not just an access gate) when AUTHGUARD_URL is
|
||||
// unset; see the top-of-file doc comment's "Auth-disabled mode" section.
|
||||
errFeatureRequiresAuthGuard = errors.New("This feature requires AUTHGUARD_URL to be configured.")
|
||||
// real backing identity (not just an access gate) when this Service was
|
||||
// constructed with Disabled(); see the top-of-file doc comment's
|
||||
// "Auth-disabled mode" section.
|
||||
errFeatureRequiresAuthGuard = errors.New("This feature requires AuthGuard to be enabled.")
|
||||
// errAuthGuardUnavailable is returned whenever CurrentUser/currentProfile
|
||||
// propagate a non-nil error: by construction that only happens when
|
||||
// AuthGuard could not be reached or gave an unusable response - a real
|
||||
@@ -215,7 +232,7 @@ const (
|
||||
)
|
||||
|
||||
// localProfile is the fixed placeholder identity CurrentUser/Signup/Login
|
||||
// report when auth is disabled (AUTHGUARD_URL unset). Role is superadmin so
|
||||
// report when auth is disabled (Disabled()). Role is superadmin so
|
||||
// CanManageUsers reports true, consistent with "nothing gated": the
|
||||
// frontend's own admin-only UI sections become visible too, even though the
|
||||
// underlying admin endpoints have nothing to manage.
|
||||
@@ -250,11 +267,34 @@ type Service struct {
|
||||
store ProxyStore
|
||||
cookieName string
|
||||
secureCookies bool
|
||||
// authGuard is nil when AUTHGUARD_URL is unset; see the top-of-file doc
|
||||
// comment's "Auth-disabled mode" section.
|
||||
// authGuard is nil only when this Service was built with Disabled(); see
|
||||
// the top-of-file doc comment's "Auth-disabled mode" section. A "pending"
|
||||
// Service (auth turned on, but no URL known yet) still has a non-nil
|
||||
// client, just one whose calls fail until SetAuthGuardBaseURL supplies a
|
||||
// real base URL.
|
||||
authGuard *authclient.Client
|
||||
}
|
||||
|
||||
// ServiceOption configures optional New/NewWithStore construction behavior.
|
||||
// Variadic and trailing, so every existing call site keeps compiling
|
||||
// unchanged; only a project that wants Disabled() needs to pass one.
|
||||
type ServiceOption func(*serviceOptions)
|
||||
|
||||
type serviceOptions struct {
|
||||
disabled bool
|
||||
}
|
||||
|
||||
// Disabled explicitly builds a Service in auth-disabled mode (see the
|
||||
// package doc comment's "Auth-disabled mode" section): nothing gated, a
|
||||
// fixed superadmin placeholder identity for every request, regardless of
|
||||
// authGuardURL. This is the explicit opt-out for a genuine standalone/
|
||||
// local-dev deployment that doesn't want centralized identity at all -
|
||||
// without it, an empty authGuardURL now means "pending" (auth integration
|
||||
// is on, but no URL is known yet), not disabled.
|
||||
func Disabled() ServiceOption {
|
||||
return func(o *serviceOptions) { o.disabled = true }
|
||||
}
|
||||
|
||||
// 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"
|
||||
@@ -262,12 +302,15 @@ type Service struct {
|
||||
// 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. 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)
|
||||
// authclient.DefaultCookieName. authGuardURL is this project's currently
|
||||
// known AuthGuard base URL - typically empty at construction now that
|
||||
// ProjectConfigurator pushes it live (coreconfig.Store.OnUpdate ->
|
||||
// SetAuthGuardBaseURL); see the "Auth-disabled mode" section for what an
|
||||
// empty authGuardURL means without Disabled(). 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, opts ...ServiceOption) *Service {
|
||||
service := NewWithStore(pgSettingsStore{db: db}, cookieName, secureCookies, authGuardURL, projectKey, opts...)
|
||||
service.db = db
|
||||
return service
|
||||
}
|
||||
@@ -278,10 +321,20 @@ func New(db *pgxpool.Pool, cookieName string, secureCookies bool, authGuardURL,
|
||||
// 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 ProxyStore, cookieName string, secureCookies bool, authGuardURL, projectKey string) *Service {
|
||||
func NewWithStore(store ProxyStore, cookieName string, secureCookies bool, authGuardURL, projectKey string, opts ...ServiceOption) *Service {
|
||||
var options serviceOptions
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
var client *authclient.Client
|
||||
if strings.TrimSpace(authGuardURL) != "" {
|
||||
client = authclient.New(authGuardURL, authclient.WithProjectKey(projectKey))
|
||||
if !options.disabled {
|
||||
// authclient.New/SetBaseURL/Validate/call all tolerate an empty base
|
||||
// URL (every call fails cleanly with "client has no base URL" rather
|
||||
// than panicking or building a malformed request), which is exactly
|
||||
// what makes "pending" mode safe: a Service constructed with no URL
|
||||
// yet still has a real, non-nil client ready to be retargeted the
|
||||
// moment one arrives.
|
||||
client = authclient.New(strings.TrimSpace(authGuardURL), authclient.WithProjectKey(projectKey))
|
||||
}
|
||||
return &Service{store: store, cookieName: cookieName, secureCookies: secureCookies, authGuard: client}
|
||||
}
|
||||
@@ -347,8 +400,10 @@ func (s pgSettingsStore) UnclaimSetup(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// enabled reports whether AUTHGUARD_URL was configured. See the top-of-file
|
||||
// doc comment's "Auth-disabled mode" section.
|
||||
// enabled reports whether this Service was NOT built with Disabled() - true
|
||||
// for both a fully-configured Service and a "pending" one still waiting for
|
||||
// its first AuthGuard URL. See the top-of-file doc comment's "Auth-disabled
|
||||
// mode" section.
|
||||
func (s *Service) enabled() bool {
|
||||
return s.authGuard != nil
|
||||
}
|
||||
@@ -357,12 +412,14 @@ func (s *Service) enabled() bool {
|
||||
// internal authclient.Client calls, taking effect on its very next call -
|
||||
// wire it as a coreconfig.Store's OnUpdate callback (see
|
||||
// ProjectConfigurator/README.md's "Pushing core service info to other
|
||||
// projects" section) so a live authguard_url change applies without a
|
||||
// restart, the same as a project holding its own *authclient.Client
|
||||
// directly. A no-op if this Service was constructed in auth-disabled mode
|
||||
// (AUTHGUARD_URL unset at startup - see the "Auth-disabled mode" section):
|
||||
// there is no client to retarget, and this package has no path to start
|
||||
// authenticating requests it previously always let through.
|
||||
// projects" section) so a live authguard_url push applies without a
|
||||
// restart, whether it's the very first URL a "pending" Service ever
|
||||
// receives or a later change to one already configured, the same as a
|
||||
// project holding its own *authclient.Client directly. A no-op only if this
|
||||
// Service was constructed with Disabled() (see the "Auth-disabled mode"
|
||||
// section): there is no client to retarget, and this package has no path to
|
||||
// start authenticating requests it was explicitly told to always let
|
||||
// through.
|
||||
func (s *Service) SetAuthGuardBaseURL(baseURL string) {
|
||||
if s.authGuard != nil {
|
||||
s.authGuard.SetBaseURL(baseURL)
|
||||
|
||||
Reference in new issue
Block a user