Skip to content

Commit 62eae89

Browse files
committed
Fix dashboard multi-user auth flow
1 parent 8570013 commit 62eae89

53 files changed

Lines changed: 166 additions & 109 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/admin/admin_coverage2_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,30 @@ func TestSettingsPut(t *testing.T) {
273273
}
274274
}
275275

276+
func TestSettingsPutEnablesAuthManager(t *testing.T) {
277+
s := testServer()
278+
dir := t.TempDir()
279+
cfgPath := filepath.Join(dir, "uwas.yaml")
280+
os.WriteFile(cfgPath, []byte("global: {}"), 0644)
281+
s.SetConfigPath(cfgPath)
282+
s.config.Global.WebRoot = dir
283+
s.authMgr = nil
284+
285+
req := httptest.NewRequest("PUT", "/api/v1/settings", strings.NewReader(`{"global.users.enabled":true}`))
286+
rec := httptest.NewRecorder()
287+
s.handleSettingsPut(rec, withAdminContext(req))
288+
289+
if rec.Code != 200 {
290+
t.Fatalf("status = %d, want 200, body: %s", rec.Code, rec.Body.String())
291+
}
292+
if s.authMgr == nil {
293+
t.Fatal("auth manager was not initialized")
294+
}
295+
if mgr, ok := s.authMgr.(*auth.Manager); ok {
296+
mgr.Stop()
297+
}
298+
}
299+
276300
func TestSettingsPutBadJSON(t *testing.T) {
277301
s := testServer()
278302
rec := httptest.NewRecorder()
@@ -627,7 +651,7 @@ func TestUserCreateAuthInvalidRole(t *testing.T) {
627651
s := testServer()
628652
s.SetAuthManager(newMockAuthManager())
629653
rec := httptest.NewRecorder()
630-
req := withAdminContext(httptest.NewRequest("POST", "/api/v1/auth/users", strings.NewReader(`{"username":"newuser","email":"a@b.com","password":"pass","role":"admin"}`)))
654+
req := withAdminContext(httptest.NewRequest("POST", "/api/v1/auth/users", strings.NewReader(`{"username":"newuser","email":"a@b.com","password":"pass","role":"superadmin"}`)))
631655
req.RemoteAddr = "10.0.0.1:1234"
632656
s.handleUserCreateAuth(rec, req)
633657
if rec.Code != 400 {

internal/admin/api.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4032,11 +4032,37 @@ func (s *Server) handleSettingsPut(w http.ResponseWriter, r *http.Request) {
40324032
}
40334033
s.configMu.Unlock()
40344034

4035+
s.ensureAuthManagerFromConfig()
40354036
s.persistConfig()
40364037
s.recordAuditR(r, "settings.update", fmt.Sprintf("%d fields", len(updates)), true)
40374038
jsonResponse(w, map[string]any{"status": "saved", "updated": len(updates)})
40384039
}
40394040

4041+
func (s *Server) ensureAuthManagerFromConfig() {
4042+
s.configMu.RLock()
4043+
enabled := s.config.Global.Users.Enabled
4044+
webRoot := s.config.Global.WebRoot
4045+
apiKey := s.config.Global.Admin.APIKey
4046+
allowLegacyPlaintext := s.config.Global.Users.AllowLegacyPlaintextAPIKey
4047+
s.configMu.RUnlock()
4048+
4049+
if !enabled {
4050+
return
4051+
}
4052+
if s.authMgr == nil {
4053+
mgr := auth.NewManager(webRoot, apiKey)
4054+
mgr.SetAllowLegacyPlaintextKey(allowLegacyPlaintext)
4055+
s.authMgr = mgr
4056+
if s.logger != nil {
4057+
s.logger.Info("multi-user auth enabled from settings")
4058+
}
4059+
return
4060+
}
4061+
if mgr, ok := s.authMgr.(*auth.Manager); ok {
4062+
mgr.SetAllowLegacyPlaintextKey(allowLegacyPlaintext)
4063+
}
4064+
}
4065+
40404066
func toInt(v any) int {
40414067
switch n := v.(type) {
40424068
case float64:
@@ -5136,7 +5162,7 @@ func (s *Server) handleUserCreateAuth(w http.ResponseWriter, r *http.Request) {
51365162
}
51375163

51385164
role := auth.Role(req.Role)
5139-
if role != auth.RoleUser && role != auth.RoleReseller {
5165+
if role != auth.RoleAdmin && role != auth.RoleUser && role != auth.RoleReseller {
51405166
jsonError(w, "invalid role", http.StatusBadRequest)
51415167
return
51425168
}

internal/admin/dashboard/dist/assets/About-DME9fk-h.js renamed to internal/admin/dashboard/dist/assets/About-C-EgJ9ek.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/admin/dashboard/dist/assets/AdminUsers-B2bJkADW.js renamed to internal/admin/dashboard/dist/assets/AdminUsers-B68oNZfp.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/admin/dashboard/dist/assets/Analytics-BePm6zWI.js renamed to internal/admin/dashboard/dist/assets/Analytics-CqOn-2IC.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/admin/dashboard/dist/assets/Apps-j79nCnQF.js renamed to internal/admin/dashboard/dist/assets/Apps-CFAvSRUp.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/admin/dashboard/dist/assets/AuditLog-ChzeDGKc.js renamed to internal/admin/dashboard/dist/assets/AuditLog-DHn_ojj6.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)