package http import ( "context" "net/http" "net/http/cookiejar" "testing" "time" "github.com/andrew/superbrain/internal/db" "github.com/andrew/superbrain/internal/scripts" ) // AUTH_MODE=oidc (core/federated shape): Authelia exclusively. Password // login, signup and password reset are refused at the API — hiding the web // UI alone must never be the barrier — while the trusted identity bridge // keeps working and provisions operators listed in ADMIN_EMAILS as ADMIN. func TestAuthModeOIDC(t *testing.T) { srv, d := testServerDB(t) t.Setenv("AUTH_MODE", "oidc") t.Setenv("OIDC_ISSUER", "https://auth.test") t.Setenv("OIDC_CLIENT_ID", "superbrain") t.Setenv("OIDC_CLIENT_SECRET", "test-secret") t.Setenv("SMTP_HOST", "mail.test") t.Setenv("SMTP_FROM", "superbrain@test") t.Setenv("ADMIN_EMAILS", "operator@superbrain.test") jar, _ := cookiejar.New(nil) c := apiClient{t, &http.Client{Jar: jar}, srv.URL} c.request("POST", "/api/auth/login", map[string]any{"email": "a@superbrain.test", "password": "a-long-test-password"}, 403) c.request("POST", "/api/signup", map[string]any{"email": "a@superbrain.test", "password": "a-long-test-password"}, 403) c.request("POST", "/api/password/forgot", map[string]any{"email": "a@superbrain.test"}, 403) c.request("POST", "/api/password/reset", map[string]any{"token": "x", "password": "brand-new-password"}, 403) // The identity bridge still provisions. A fresh SSO user is USER... body := map[string]any{"issuer": "https://auth.test", "subject": "sub-user", "email": "someone@superbrain.test", "emailVerified": true} status, out := postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "oidc-provision", time.Now().Add(time.Minute)), body) if status != 200 { t.Fatalf("identity provision: wanted 200, got %d: %v", status, out) } if role := out["user"].(map[string]any)["role"]; role != "USER" { t.Fatalf("fresh SSO user role = %v, wanted USER", role) } // ...and an operator-listed email becomes ADMIN on first SSO login — // the only bootstrap path when password login is disabled. body = map[string]any{"issuer": "https://auth.test", "subject": "sub-operator", "email": "operator@superbrain.test", "emailVerified": true} status, out = postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "oidc-provision", time.Now().Add(time.Minute)), body) if status != 200 { t.Fatalf("operator provision: wanted 200, got %d: %v", status, out) } if role := out["user"].(map[string]any)["role"]; role != "ADMIN" { t.Fatalf("ADMIN_EMAILS SSO user role = %v, wanted ADMIN", role) } // Seed CLI with SEED_SUBJECT links an offline-provisioned operator // without any session (the chicken-and-egg escape hatch for this mode). t.Setenv("SEED_EMAIL", "seeded@superbrain.test") t.Setenv("SEED_SUBJECT", "sub-seeded") t.Setenv("SEED_PASSWORD", "") if err := scripts.Seed(context.Background(), d); err != nil { t.Fatalf("seed with subject: %v", err) } body = map[string]any{"issuer": "https://auth.test", "subject": "sub-seeded", "email": "seeded@superbrain.test", "emailVerified": true} if status, _ := postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "oidc-provision", time.Now().Add(time.Minute)), body); status != 200 { t.Fatalf("seeded subject login: wanted 200, got %d", status) } // Re-running the same seed is idempotent. if err := scripts.Seed(context.Background(), d); err != nil { t.Fatalf("seed must be idempotent: %v", err) } } // A pre-existing PASSWORD account still refuses auto-link by email in oidc // mode (409); the operator links it offline via SEED_SUBJECT, after which // SSO resolves to the SAME local user — exactly the core-upgrade path. func TestAuthModeOIDCLinksExistingPasswordAccount(t *testing.T) { srv, d := testServerDB(t) // Create the password account while passwords still work. jar, _ := cookiejar.New(nil) c := apiClient{t, &http.Client{Jar: jar}, srv.URL} c.request("POST", "/api/signup", map[string]any{"email": "admin@superbrain.test", "password": "a-long-test-password"}, 201) c.request("POST", "/api/auth/login", map[string]any{"email": "admin@superbrain.test", "password": "a-long-test-password"}, 200) adminID := meID(c) t.Setenv("AUTH_MODE", "oidc") t.Setenv("OIDC_ISSUER", "https://auth.test") t.Setenv("ADMIN_EMAILS", "") body := map[string]any{"issuer": "https://auth.test", "subject": "sub-admin", "email": "admin@superbrain.test", "emailVerified": true} if status, _ := postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "oidc-provision", time.Now().Add(time.Minute)), body); status != 409 { t.Fatalf("password account must refuse email auto-link: wanted 409, got %d", status) } t.Setenv("SEED_EMAIL", "admin@superbrain.test") t.Setenv("SEED_SUBJECT", "sub-admin") t.Setenv("SEED_PASSWORD", "") if err := scripts.Seed(context.Background(), d); err != nil { t.Fatalf("offline link: %v", err) } status, out := postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "oidc-provision", time.Now().Add(time.Minute)), body) if status != 200 { t.Fatalf("linked login: wanted 200, got %d: %v", status, out) } if got := out["user"].(map[string]any)["id"]; got != adminID { t.Fatalf("SSO resolved to %v, wanted the pre-existing local user %s", got, adminID) } // The subject can never be re-pointed at another user by re-seeding. t.Setenv("SEED_EMAIL", "other@superbrain.test") if err := scripts.Seed(context.Background(), d); err == nil { t.Fatal("linking an already-linked subject to a different user must fail") } } // AUTH_MODE=password (multi-tenant shape): signup/password login only. The // OIDC bridge is refused even with valid variables AND a valid service token, // and the status endpoint reports OIDC disabled so the UI hides Authelia. func TestAuthModePassword(t *testing.T) { srv, _ := testServerDB(t) t.Setenv("AUTH_MODE", "password") t.Setenv("OIDC_ISSUER", "https://auth.test") t.Setenv("OIDC_CLIENT_ID", "superbrain") t.Setenv("OIDC_CLIENT_SECRET", "test-secret") jar, _ := cookiejar.New(nil) c := apiClient{t, &http.Client{Jar: jar}, srv.URL} c.request("POST", "/api/signup", map[string]any{"email": "tenant@superbrain.test", "password": "a-long-test-password"}, 201) c.request("POST", "/api/auth/login", map[string]any{"email": "tenant@superbrain.test", "password": "a-long-test-password"}, 200) body := map[string]any{"issuer": "https://auth.test", "subject": "sub-x", "email": "x@superbrain.test", "emailVerified": true} if status, _ := postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "oidc-provision", time.Now().Add(time.Minute)), body); status != 403 { t.Fatalf("identity bridge in password mode: wanted 403, got %d", status) } status := c.request("GET", "/api/auth/oidc/status", nil, 200) if status["enabled"] != false { t.Fatalf("oidc status must report disabled in password mode: %v", status) } } // LinkExternal (the mechanism behind SEED_SUBJECT and POST // /api/admin/oidc-link) refuses to move a subject between users. func TestLinkExternalConflict(t *testing.T) { _, d := testServerDB(t) repo := db.NewUserRepo(d) ctx := context.Background() a, _ := repo.Create(ctx, &db.NewUserInput{ID: db.NewID(), Name: "A", Email: "a@superbrain.test", Password: "x", Role: "USER"}) b, _ := repo.Create(ctx, &db.NewUserInput{ID: db.NewID(), Name: "B", Email: "b@superbrain.test", Password: "x", Role: "USER"}) if err := repo.LinkExternal(ctx, a, "authelia", "sub-1"); err != nil { t.Fatal(err) } if err := repo.LinkExternal(ctx, a, "authelia", "sub-1"); err != nil { t.Fatalf("relinking the same user must be idempotent: %v", err) } if err := repo.LinkExternal(ctx, b, "authelia", "sub-1"); err != db.ErrAccountLinked { t.Fatalf("subject move: wanted ErrAccountLinked, got %v", err) } } // TIER=core alone (no AUTH_MODE set) must behave exactly like the oidc // shape: the tier derives the mode end to end. func TestTierCoreDerivesOIDCOnly(t *testing.T) { srv, _ := testServerDB(t) t.Setenv("TIER", "core") t.Setenv("OIDC_ISSUER", "https://auth.test") t.Setenv("OIDC_CLIENT_ID", "superbrain") t.Setenv("OIDC_CLIENT_SECRET", "test-secret") jar, _ := cookiejar.New(nil) c := apiClient{t, &http.Client{Jar: jar}, srv.URL} c.request("POST", "/api/auth/login", map[string]any{"email": "a@superbrain.test", "password": "a-long-test-password"}, 403) c.request("POST", "/api/signup", map[string]any{"email": "a@superbrain.test", "password": "a-long-test-password"}, 403) c.request("POST", "/api/password/forgot", map[string]any{"email": "a@superbrain.test"}, 403) body := map[string]any{"issuer": "https://auth.test", "subject": "sub-tier", "email": "tier@superbrain.test", "emailVerified": true} if status, out := postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "oidc-provision", time.Now().Add(time.Minute)), body); status != 200 { t.Fatalf("identity provision on core tier: wanted 200, got %d: %v", status, out) } } // TIER=multi alone must refuse the OIDC bridge even with real variables and // a valid service token, while signup and password login keep working. func TestTierMultiDerivesPasswordOnly(t *testing.T) { srv, _ := testServerDB(t) t.Setenv("TIER", "multi") t.Setenv("OIDC_ISSUER", "https://auth.test") t.Setenv("OIDC_CLIENT_ID", "superbrain") t.Setenv("OIDC_CLIENT_SECRET", "test-secret") jar, _ := cookiejar.New(nil) c := apiClient{t, &http.Client{Jar: jar}, srv.URL} c.request("POST", "/api/signup", map[string]any{"email": "tenant@superbrain.test", "password": "a-long-test-password"}, 201) c.request("POST", "/api/auth/login", map[string]any{"email": "tenant@superbrain.test", "password": "a-long-test-password"}, 200) body := map[string]any{"issuer": "https://auth.test", "subject": "sub-m", "email": "m@superbrain.test", "emailVerified": true} if status, _ := postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "oidc-provision", time.Now().Add(time.Minute)), body); status != 403 { t.Fatalf("identity bridge on multi tier: wanted 403, got %d", status) } if status := c.request("GET", "/api/auth/oidc/status", nil, 200); status["enabled"] != false { t.Fatalf("oidc status must report disabled on multi tier: %v", status) } } // Explicit variables override tier defaults (the documented escape hatch): // TIER=core + AUTH_MODE=both re-enables password login, while signup stays // off until SIGNUP_ENABLED says otherwise. func TestTierExplicitOverride(t *testing.T) { srv, _ := testServerDB(t) t.Setenv("TIER", "core") t.Setenv("AUTH_MODE", "both") jar, _ := cookiejar.New(nil) c := apiClient{t, &http.Client{Jar: jar}, srv.URL} // Signup stays off via the core-tier default... c.request("POST", "/api/signup", map[string]any{"email": "o@superbrain.test", "password": "a-long-test-password"}, 403) // ...until explicitly re-enabled. t.Setenv("SIGNUP_ENABLED", "1") c.request("POST", "/api/signup", map[string]any{"email": "o@superbrain.test", "password": "a-long-test-password"}, 201) c.request("POST", "/api/auth/login", map[string]any{"email": "o@superbrain.test", "password": "a-long-test-password"}, 200) }