feat: honor project login requirement in shared auth

This commit is contained in:
Gabriel20xx committed 2026-09-17 00:14:56 +02:00
1 parent 78067cc890
commit 51eef30971
4 files changed
+223 -43

No files matched your search

+47 -4
View File
@@ -14,7 +14,7 @@ type fakeProxyStore struct {
setupClaimed bool
}
func (s *fakeProxyStore) LoginRequired(context.Context) (bool, error) { return s.loginRequired, nil }
func (s *fakeProxyStore) LoginRequired(context.Context) (bool, error) { return s.loginRequired, nil }
func (s *fakeProxyStore) SetLoginRequired(_ context.Context, v bool) error {
s.loginRequired = v
return nil
@@ -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
@@ -39,7 +43,7 @@ func (s *fakeProxyStore) UnclaimSetup(context.Context) error {
// made AUTHGUARD_URL a de-facto requirement even though ProjectConfigurator
// can push it live after startup.
func TestPendingModeStillGatesRequests(t *testing.T) {
service := NewWithStore(&fakeProxyStore{}, "session", false, "", "testproject")
service := NewWithStore(&fakeProxyStore{loginRequired: true}, "session", false, "", "testproject")
if !service.enabled() {
t.Fatal("Service with no authGuardURL and no Disabled() option should still be enabled (pending), not disabled")
}
@@ -60,8 +64,47 @@ func TestDisabledOptionSkipsGating(t *testing.T) {
}
req := httptest.NewRequest(http.MethodGet, "/", nil)
user, err := service.CurrentUser(req)
if err != nil || user == nil || user.Username != "local" {
t.Fatalf("Disabled() Service should resolve the fixed local superadmin profile, got user=%+v err=%v", user, err)
if err != nil || user == nil || user.Username != "Admin" || !IsAdmin(user) {
t.Fatalf("Disabled() Service should resolve the fixed Admin superadmin profile, got user=%+v err=%v", user, err)
}
}
func TestLoginDisabledUsesAdminIdentityWithoutCredentials(t *testing.T) {
store := &fakeProxyStore{loginRequired: false}
service := NewWithStore(store, "session", false, "", "testproject")
req := httptest.NewRequest(http.MethodGet, "/", nil)
user, err := service.CurrentUser(req)
if err != nil || user == nil || user.Username != "Admin" || !IsAdmin(user) {
t.Fatalf("login-disabled Service should resolve the Admin identity, got user=%+v err=%v", user, err)
}
called := false
handler := service.RequireAdminIfLoginRequired(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
contextUser := UserFromContext(r.Context())
if contextUser == nil || contextUser.Username != "Admin" || !IsAdmin(contextUser) {
t.Errorf("middleware context user = %+v, want Admin superadmin", contextUser)
}
w.WriteHeader(http.StatusNoContent)
}))
response := httptest.NewRecorder()
handler.ServeHTTP(response, req)
if response.Code != http.StatusNoContent || !called {
t.Fatalf("login-disabled admin middleware status = %d/called=%v, want %d/true", response.Code, called, http.StatusNoContent)
}
}
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)
}
}