Compare commits

..
2 Commits
Author SHA1 Message Date
Gabriel20xx 407966fc94 feat: honor project login requirement in CollabTable
Lint / Lint (push) Successful in 24s
Tests / Go backend (push) Successful in 1m7s
Tests / Dashboard checks (push) Successful in 51s
Docker / Docker Build & Push (push) Successful in 4m17s
2026-09-17 00:32:46 +02:00
Gabriel20xx ec82a8a8b9 Sync shared snapshot from 51eef30 2026-09-17 00:15:32 +02:00
6 changed files with 113 additions and 22 deletions

No files matched your search

+4 -6
View File
@@ -69,12 +69,10 @@ const (
legacySessionCookieName = "collabtable_session" legacySessionCookieName = "collabtable_session"
) )
// collabTableAuthStore adapts *DB to sharedauth.ProxyStore. CollabTable never // collabTableAuthStore adapts *DB to sharedauth.ProxyStore. The login
// had an independent "require login" toggle (a session was always required // requirement is runtime-controlled by ProjectConfigurator through
// whenever AUTHGUARD_URL was configured at all) so LoginRequired always // sharedauth.Service's internal endpoint; this store supplies the conservative
// reports true and SetLoginRequired is a no-op -- server.go correspondingly // required-by-default fallback. IsSetupClaimed/TryClaimSetup/UnclaimSetup delegate
// wires sharedauth.Service.Require rather than the *IfLoginRequired
// middleware family. IsSetupClaimed/TryClaimSetup/UnclaimSetup delegate
// straight through to *DB's existing methods of the same name/signature (see // straight through to *DB's existing methods of the same name/signature (see
// db.go), reusing the collabtable_setup_state table/queries verbatim. // db.go), reusing the collabtable_setup_state table/queries verbatim.
type collabTableAuthStore struct { type collabTableAuthStore struct {
+16 -15
View File
@@ -175,19 +175,20 @@ func (s *Server) registerRoutes(mux *http.ServeMux, webRoot fs.FS) {
mux.HandleFunc("GET /api/auth/status", s.auth.Status) mux.HandleFunc("GET /api/auth/status", s.auth.Status)
mux.HandleFunc("POST /api/auth/setup", s.auth.Signup) mux.HandleFunc("POST /api/auth/setup", s.auth.Signup)
mux.HandleFunc("POST /api/auth/login", s.auth.Login) mux.HandleFunc("POST /api/auth/login", s.auth.Login)
mux.Handle("POST /api/auth/logout", s.auth.Require(http.HandlerFunc(s.auth.Logout))) mux.Handle("PUT /api/auth/access/internal", s.auth.SetLoginRequiredInternal(s.internalServiceSecret))
mux.Handle("POST /api/auth/credentials", s.auth.Require(http.HandlerFunc(s.auth.UpdateMe))) mux.Handle("POST /api/auth/logout", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.Logout)))
mux.Handle("GET /api/auth/users", s.auth.Require(http.HandlerFunc(s.auth.ManagedUsers))) mux.Handle("POST /api/auth/credentials", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.UpdateMe)))
mux.Handle("POST /api/auth/users", s.auth.Require(http.HandlerFunc(s.auth.ManagedCreateUser))) mux.Handle("GET /api/auth/users", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.ManagedUsers)))
mux.Handle("PUT /api/auth/users/{id}", s.auth.Require(http.HandlerFunc(s.auth.ManagedUpdateUser))) mux.Handle("POST /api/auth/users", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.ManagedCreateUser)))
mux.Handle("PATCH /api/auth/users/{id}", s.auth.Require(http.HandlerFunc(s.auth.ManagedUpdateUser))) mux.Handle("PUT /api/auth/users/{id}", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.ManagedUpdateUser)))
mux.Handle("DELETE /api/auth/users/{id}", s.auth.Require(http.HandlerFunc(s.auth.ManagedDeleteUser))) mux.Handle("PATCH /api/auth/users/{id}", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.ManagedUpdateUser)))
mux.Handle("GET /api/auth/totp/status", s.auth.Require(http.HandlerFunc(s.auth.TOTPStatus))) mux.Handle("DELETE /api/auth/users/{id}", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.ManagedDeleteUser)))
mux.Handle("POST /api/auth/totp/setup", s.auth.Require(http.HandlerFunc(s.auth.TOTPInitiate))) mux.Handle("GET /api/auth/totp/status", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.TOTPStatus)))
mux.Handle("POST /api/auth/totp/enable", s.auth.Require(http.HandlerFunc(s.auth.TOTPVerify))) mux.Handle("POST /api/auth/totp/setup", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.TOTPInitiate)))
mux.Handle("POST /api/auth/totp/disable", s.auth.Require(http.HandlerFunc(s.auth.TOTPDisable))) mux.Handle("POST /api/auth/totp/enable", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.TOTPVerify)))
mux.Handle("GET /api/auth/token/status", s.auth.Require(http.HandlerFunc(s.handleTokenStatus))) mux.Handle("POST /api/auth/totp/disable", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.auth.TOTPDisable)))
mux.Handle("POST /api/auth/token/regenerate", s.auth.Require(http.HandlerFunc(s.handleTokenRegenerate))) mux.Handle("GET /api/auth/token/status", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.handleTokenStatus)))
mux.Handle("POST /api/auth/token/regenerate", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.handleTokenRegenerate)))
// Sync/collaboration API routes use a long-lived bearer token shared by the // Sync/collaboration API routes use a long-lived bearer token shared by the
// user's devices. This is deliberately separate from browser sessions. // user's devices. This is deliberately separate from browser sessions.
@@ -217,8 +218,8 @@ func (s *Server) registerRoutes(mux *http.ServeMux, webRoot fs.FS) {
// The dashboard is a public SPA shell, but its data and tech-stack endpoints // The dashboard is a public SPA shell, but its data and tech-stack endpoints
// are protected by the browser session. // are protected by the browser session.
mux.Handle("GET /api/tech-stack", s.auth.Require(http.HandlerFunc(s.handleTechStack))) mux.Handle("GET /api/tech-stack", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.handleTechStack)))
mux.Handle("GET /web/data", s.auth.Require(http.HandlerFunc(s.handleWebData))) mux.Handle("GET /web/data", s.auth.RequireIfLoginRequired(http.HandlerFunc(s.handleWebData)))
mux.Handle("/", spaFileServer(webRoot)) mux.Handle("/", spaFileServer(webRoot))
} }
+50 -1
View File
@@ -172,6 +172,7 @@ import (
"os" "os"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
@@ -276,6 +277,12 @@ type Service struct {
// real base URL. // real base URL.
authGuard *authclient.Client authGuard *authclient.Client
configurationError error 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. // 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) { 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) 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 // central "Require Login" toggle (ConfigWorkspace.svelte's loginControls) apply here without an
// admin session of its own. // admin session of its own.
func (s *Service) SetLoginRequired(ctx context.Context, required bool) error { 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) { 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 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 // TestPendingModeStillGatesRequests locks in the fix that made an empty
// authGuardURL at construction mean "pending" (auth still enabled, calls // authGuardURL at construction mean "pending" (auth still enabled, calls
// fail closed until a real URL arrives via SetAuthGuardBaseURL) instead of // 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) { func TestNormalizeProxyCredentials(t *testing.T) {
email, username, password, err := normalizeProxyCredentials(" User@Example.com ", "user", "correct horse battery staple") email, username, password, err := normalizeProxyCredentials(" User@Example.com ", "user", "correct horse battery staple")
if err != nil { if err != nil {
@@ -123,6 +123,11 @@
editUser.projects = checked ? [...current, key] : current.filter((project) => project !== key); editUser.projects = checked ? [...current, key] : current.filter((project) => project !== key);
} }
function toggleNewProject(key: string, checked: boolean): void {
const current = newUser.projects ?? [];
newUser.projects = checked ? [...current, key] : current.filter((project) => project !== key);
}
function projectLabel(key: string): string { function projectLabel(key: string): string {
return projects?.find((project) => project.key === key)?.label ?? key; return projects?.find((project) => project.key === key)?.label ?? key;
} }
@@ -198,6 +203,21 @@
{#if canAssignAdmin}<option value="admin">Admin</option>{/if} {#if canAssignAdmin}<option value="admin">Admin</option>{/if}
</select> </select>
</div> </div>
{#if projects && projects.length > 0}
<fieldset class="sc-user-projects">
<legend>Projects (none checked = all projects)</legend>
{#each projects as project (project.key)}
<label class="sc-user-project-option">
<input
type="checkbox"
checked={(newUser.projects ?? []).includes(project.key)}
onchange={(event) => toggleNewProject(project.key, event.currentTarget.checked)}
/>
{project.label}
</label>
{/each}
</fieldset>
{/if}
<div class="sc-form-actions"><Button disabled={saving} type="submit">Add user</Button></div> <div class="sc-form-actions"><Button disabled={saving} type="submit">Add user</Button></div>
</form> </form>
+6
View File
@@ -85,6 +85,12 @@ export interface UserCreateInput {
email: string; email: string;
password: string; password: string;
role?: UserRole; role?: UserRole;
/**
* Project keys this new account is restricted to. An empty array means
* unrestricted; omitted when the host application does not expose project
* scoping.
*/
projects?: string[];
} }
export interface UserUpdateInput { export interface UserUpdateInput {