feat(auth): require changing default admin password (#778)

This commit is contained in:
h44z
2026-11-09 19:57:23 +01:00
committed by GitHub
parent 137286d46b
commit 276f120b60
30 changed files with 641 additions and 90 deletions
@@ -18,6 +18,7 @@ const (
type UserAuthenticator interface {
IsUserValid(ctx context.Context, id domain.UserIdentifier) bool
IsDefaultPasswordInUse(ctx context.Context) bool
}
type UserRepository interface {
@@ -74,6 +75,12 @@ func (h AuthenticationHandler) LoggedIn(scopes ...Scope) func(next http.Handler)
return
}
if h.authenticator.IsDefaultPasswordInUse(r.Context()) {
respond.JSON(w, http.StatusForbidden,
model.Error{Code: http.StatusForbidden, Message: "default admin password must be changed first"})
return
}
if !UserHasScopes(user, scopes...) {
// Abort the request with the appropriate error code
respond.JSON(w, http.StatusForbidden,
@@ -12,13 +12,18 @@ import (
)
type stubUserAuthenticator struct {
valid bool
valid bool
defaultPasswordInUse bool
}
func (s stubUserAuthenticator) IsUserValid(ctx context.Context, id domain.UserIdentifier) bool {
return s.valid
}
func (s stubUserAuthenticator) IsDefaultPasswordInUse(ctx context.Context) bool {
return s.defaultPasswordInUse
}
type stubUserRepository struct {
user *domain.User
err error
@@ -114,3 +119,35 @@ func TestAuthenticationHandler_LoggedInAllowsValidUser(t *testing.T) {
t.Fatal("expected downstream handler to be called")
}
}
func TestAuthenticationHandler_LoggedInRejectsDefaultPassword(t *testing.T) {
user := &domain.User{
Identifier: "test-user",
ApiToken: "token",
}
handler := NewAuthenticationHandler(stubUserAuthenticator{valid: true, defaultPasswordInUse: true}, stubUserRepository{user: user})
nextCalled := false
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
nextCalled = true
w.WriteHeader(http.StatusOK)
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.SetBasicAuth(string(user.Identifier), user.ApiToken)
rr := httptest.NewRecorder()
handler.LoggedIn()(next).ServeHTTP(rr, req)
if rr.Code != http.StatusForbidden {
t.Fatalf("expected status %d, got %d", http.StatusForbidden, rr.Code)
}
if nextCalled {
t.Fatal("expected downstream handler not to be called")
}
if !strings.Contains(rr.Body.String(), "default admin password must be changed first") {
t.Fatalf("expected error message to mention default password, got %q", rr.Body.String())
}
}