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" "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 {