Sync shared snapshot from 51eef30

This commit is contained in:
Gabriel20xx committed 2026-09-17 00:15:32 +02:00
1 parent b3d27ff37e
commit ec82a8a8b9
4 files changed
+93 -1

No files matched your search

+50 -1
View File
@@ -172,6 +172,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/go-chi/chi/v5"
@@ -276,6 +277,12 @@ type Service struct {
// real base URL.
authGuard *authclient.Client
configurationError error
// loginRequiredOverride is populated by the internal ProjectConfigurator
// endpoint. Some legacy consumers intentionally use a no-op ProxyStore for
// this setting because they historically had no local toggle; the live
// override lets those services still honor the central setting immediately.
loginRequiredMu sync.RWMutex
loginRequiredOverride *bool
}
// ServiceOption configures optional New/NewWithStore construction behavior.
@@ -750,6 +757,15 @@ func (s *Service) AdminNeeded(w http.ResponseWriter, r *http.Request) {
}
func (s *Service) LoginRequired(ctx context.Context) (bool, error) {
s.loginRequiredMu.RLock()
override := s.loginRequiredOverride
s.loginRequiredMu.RUnlock()
if override != nil {
return *override, nil
}
if s.store == nil {
return true, nil
}
return s.store.LoginRequired(ctx)
}
@@ -783,7 +799,40 @@ 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 {
return s.store.SetLoginRequired(ctx, required)
if s.store != nil {
if err := s.store.SetLoginRequired(ctx, required); err != nil {
return err
}
}
s.loginRequiredMu.Lock()
s.loginRequiredOverride = &required
s.loginRequiredMu.Unlock()
return nil
}
// SetLoginRequiredInternal returns the X-Internal-Auth-gated handler used by
// ProjectConfigurator to apply its per-project "Require Login" setting. It
// lives on the shared service so projects outside the core-service family can
// expose the same propagation endpoint without duplicating its JSON handling.
func (s *Service) SetLoginRequiredInternal(secret string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.TrimSpace(secret) == "" || r.Header.Get("X-Internal-Auth") != secret {
writeProxyError(w, http.StatusUnauthorized, "internal service authentication required")
return
}
var input struct {
LoginRequired *bool `json:"loginRequired"`
}
if err := decodeProxyJSON(r, &input); err != nil || input.LoginRequired == nil {
writeProxyError(w, http.StatusBadRequest, "loginRequired must be a boolean")
return
}
if err := s.SetLoginRequired(r.Context(), *input.LoginRequired); err != nil {
writeProxyError(w, http.StatusInternalServerError, "access setting could not be saved")
return
}
writeProxyJSON(w, http.StatusOK, map[string]bool{"loginRequired": *input.LoginRequired})
})
}
func (s *Service) Status(w http.ResponseWriter, r *http.Request) {
+17
View File
@@ -32,6 +32,10 @@ func (s *fakeProxyStore) UnclaimSetup(context.Context) error {
return nil
}
type noOpLoginProxyStore struct{ fakeProxyStore }
func (s *noOpLoginProxyStore) SetLoginRequired(context.Context, bool) error { return nil }
// TestPendingModeStillGatesRequests locks in the fix that made an empty
// authGuardURL at construction mean "pending" (auth still enabled, calls
// fail closed until a real URL arrives via SetAuthGuardBaseURL) instead of
@@ -91,6 +95,19 @@ func TestLoginDisabledUsesAdminIdentityWithoutCredentials(t *testing.T) {
}
}
func TestInternalLoginOverrideWorksWithNoOpStore(t *testing.T) {
store := &noOpLoginProxyStore{fakeProxyStore: fakeProxyStore{loginRequired: true}}
service := NewWithStore(store, "session", false, "", "testproject")
if err := service.SetLoginRequired(context.Background(), false); err != nil {
t.Fatalf("SetLoginRequired() error = %v", err)
}
user, err := service.CurrentUser(httptest.NewRequest(http.MethodGet, "/", nil))
if err != nil || user == nil || user.Username != "Admin" {
t.Fatalf("no-op store override resolved user=%+v err=%v, want Admin", user, err)
}
}
func TestNormalizeProxyCredentials(t *testing.T) {
email, username, password, err := normalizeProxyCredentials(" User@Example.com ", "user", "correct horse battery staple")
if err != nil {