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>
92 lines
3.3 KiB
Go
92 lines
3.3 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// fakeProxyStore is a minimal in-memory ProxyStore for constructing a
|
|
// Service in tests without a real database.
|
|
type fakeProxyStore struct {
|
|
loginRequired bool
|
|
setupClaimed bool
|
|
}
|
|
|
|
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
|
|
}
|
|
func (s *fakeProxyStore) IsSetupClaimed(context.Context) (bool, error) { return s.setupClaimed, nil }
|
|
func (s *fakeProxyStore) TryClaimSetup(context.Context) (bool, error) {
|
|
if s.setupClaimed {
|
|
return false, nil
|
|
}
|
|
s.setupClaimed = true
|
|
return true, nil
|
|
}
|
|
func (s *fakeProxyStore) UnclaimSetup(context.Context) error {
|
|
s.setupClaimed = false
|
|
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
|
|
// "disabled" (nothing gated, a fixed superadmin identity) - the bug that
|
|
// 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")
|
|
if !service.enabled() {
|
|
t.Fatal("Service with no authGuardURL and no Disabled() option should still be enabled (pending), not disabled")
|
|
}
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.AddCookie(&http.Cookie{Name: "auth_session", Value: "sometoken"})
|
|
if _, err := service.CurrentUser(req); err == nil {
|
|
t.Fatal("pending Service (no AuthGuard URL yet) should fail closed on a real session cookie, not silently resolve a user")
|
|
}
|
|
}
|
|
|
|
// TestDisabledOptionSkipsGating confirms the explicit opt-out still works
|
|
// exactly like the old "empty authGuardURL" behavior.
|
|
func TestDisabledOptionSkipsGating(t *testing.T) {
|
|
service := NewWithStore(&fakeProxyStore{}, "session", false, "", "testproject", Disabled())
|
|
if service.enabled() {
|
|
t.Fatal("Service built with Disabled() should report enabled() == false")
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeProxyCredentials(t *testing.T) {
|
|
email, username, password, err := normalizeProxyCredentials(" User@Example.com ", "user", "correct horse battery staple")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if email != "user@example.com" || username != "user" || password == "" {
|
|
t.Fatalf("normalized credentials = %q, %q, %q", email, username, password)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeProxyCredentialsAllowsAdminEmailOnly(t *testing.T) {
|
|
if _, _, _, err := normalizeProxyCredentials("admin@example.com", "", "correct horse battery staple"); err != nil {
|
|
t.Fatalf("empty optional username rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNormalizePlatformRole(t *testing.T) {
|
|
if normalizePlatformRole("MODERATOR") != "moderator" {
|
|
t.Fatal("moderator role was not normalized")
|
|
}
|
|
if normalizePlatformRole("unknown") != "user" {
|
|
t.Fatal("unknown role was not reduced to user")
|
|
}
|
|
}
|