This commit is contained in:
1 parent
08a6d7bd1d
commit
b3d27ff37e
5 files changed
+165
-114
No files matched your search
@@ -116,7 +116,7 @@
|
||||
// -------------------------------------------
|
||||
// Passing Disabled() to New/NewWithStore turns authentication off entirely
|
||||
// for a Service: Service.authGuard is nil (Service.enabled() reports false),
|
||||
// CurrentUser always resolves to localProfile (a fixed placeholder
|
||||
// CurrentUser always resolves to adminProfile (a fixed placeholder
|
||||
// superadmin identity), so every request is treated as already authenticated
|
||||
// regardless of the `auth_required` setting - there is no login screen and
|
||||
// nothing gated. This is an explicit mode for local development or a
|
||||
@@ -233,12 +233,12 @@ const (
|
||||
proxyMaxEmailLength = 254
|
||||
)
|
||||
|
||||
// localProfile is the fixed placeholder identity CurrentUser/Signup/Login
|
||||
// report when auth is disabled (Disabled()). Role is superadmin so
|
||||
// CanManageUsers reports true, consistent with "nothing gated": the
|
||||
// frontend's own admin-only UI sections become visible too, even though the
|
||||
// underlying admin endpoints have nothing to manage.
|
||||
var localProfile = ProxyUser{ID: 0, Username: "local", Email: "", Role: string(RoleSuperAdmin), CanManageUsers: true}
|
||||
// adminProfile is the fixed placeholder identity used when authentication is
|
||||
// disabled, either explicitly through Disabled() or through a project's
|
||||
// ProjectConfigurator "Require Login" toggle. Role is superadmin so all
|
||||
// application routes see the same administrator identity even though no
|
||||
// credentials were supplied.
|
||||
var adminProfile = ProxyUser{ID: 0, Username: "Admin", Email: "", Role: string(RoleSuperAdmin), CanManageUsers: true}
|
||||
|
||||
// ProxyStore is the small piece of local, project-owned persistence a
|
||||
// Service needs beyond AuthGuard itself: the independent "require login"
|
||||
@@ -499,7 +499,15 @@ func (s *Service) CurrentUser(r *http.Request) (*ProxyUser, error) {
|
||||
return nil, s.configurationError
|
||||
}
|
||||
if !s.enabled() {
|
||||
user := localProfile
|
||||
user := adminProfile
|
||||
return &user, nil
|
||||
}
|
||||
loginRequired, err := s.LoginRequired(r.Context())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !loginRequired {
|
||||
user := adminProfile
|
||||
return &user, nil
|
||||
}
|
||||
profile, err := s.currentProfile(r.Context(), s.sessionToken(r))
|
||||
@@ -537,9 +545,10 @@ func (s *Service) Require(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
// RequireIfLoginRequired protects actions when the application has login
|
||||
// enabled, while allowing explicitly public installations to use actions that
|
||||
// do not need a user identity. This toggle is independent of AuthGuard - see
|
||||
// the package doc comment's second paragraph.
|
||||
// enabled. When login is disabled, CurrentUser supplies the synthetic Admin
|
||||
// superadmin identity, so handlers can use the same authenticated code path
|
||||
// without requiring credentials. This toggle is independent of AuthGuard -
|
||||
// see the package doc comment's second paragraph.
|
||||
func (s *Service) RequireIfLoginRequired(next http.Handler) http.Handler {
|
||||
return s.Optional(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if UserFromContext(r.Context()) != nil {
|
||||
@@ -572,8 +581,9 @@ func (s *Service) RequireAdmin(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
// RequireAdminIfLoginRequired keeps administrative actions protected for
|
||||
// authenticated users, while allowing explicitly public installations to use
|
||||
// configuration endpoints that do not require a user identity.
|
||||
// authenticated users. When login is disabled, CurrentUser supplies the
|
||||
// synthetic Admin superadmin identity so configuration endpoints remain
|
||||
// available without credentials.
|
||||
func (s *Service) RequireAdminIfLoginRequired(next http.Handler) http.Handler {
|
||||
return s.Optional(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
user := UserFromContext(r.Context())
|
||||
@@ -713,6 +723,15 @@ func (s *Service) AdminNeeded(w http.ResponseWriter, r *http.Request) {
|
||||
writeProxyJSON(w, http.StatusOK, map[string]bool{"adminNeeded": false})
|
||||
return
|
||||
}
|
||||
loginRequired, err := s.LoginRequired(r.Context())
|
||||
if err != nil {
|
||||
writeProxyError(w, http.StatusInternalServerError, "authentication status unavailable")
|
||||
return
|
||||
}
|
||||
if !loginRequired {
|
||||
writeProxyJSON(w, http.StatusOK, map[string]bool{"adminNeeded": false})
|
||||
return
|
||||
}
|
||||
claimed, err := s.isSetupClaimed(r.Context())
|
||||
if err != nil {
|
||||
writeProxyError(w, http.StatusInternalServerError, "authentication status unavailable")
|
||||
@@ -773,8 +792,20 @@ func (s *Service) Status(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if !s.enabled() {
|
||||
user := localProfile
|
||||
writeProxyJSON(w, http.StatusOK, map[string]any{"setupRequired": false, "authenticated": true, "authRequired": true, "user": &user})
|
||||
user := adminProfile
|
||||
writeProxyJSON(w, http.StatusOK, map[string]any{"setupRequired": false, "authenticated": true, "authRequired": false, "user": &user})
|
||||
return
|
||||
}
|
||||
loginRequired, err := s.LoginRequired(r.Context())
|
||||
if err != nil {
|
||||
writeProxyError(w, http.StatusInternalServerError, "authentication status unavailable")
|
||||
return
|
||||
}
|
||||
if !loginRequired {
|
||||
user := adminProfile
|
||||
writeProxyJSON(w, http.StatusOK, map[string]any{
|
||||
"setupRequired": false, "authenticated": true, "authRequired": false, "user": &user,
|
||||
})
|
||||
return
|
||||
}
|
||||
claimed, err := s.isSetupClaimed(r.Context())
|
||||
@@ -795,11 +826,6 @@ func (s *Service) Status(w http.ResponseWriter, r *http.Request) {
|
||||
writeProxyError(w, http.StatusInternalServerError, errAuthGuardUnavailable.Error())
|
||||
return
|
||||
}
|
||||
loginRequired, err := s.LoginRequired(r.Context())
|
||||
if err != nil {
|
||||
writeProxyError(w, http.StatusInternalServerError, "authentication status unavailable")
|
||||
return
|
||||
}
|
||||
writeProxyJSON(w, http.StatusOK, map[string]any{
|
||||
"setupRequired": setupRequired, "authenticated": user != nil, "authRequired": loginRequired, "user": user,
|
||||
})
|
||||
@@ -938,7 +964,7 @@ func (s *Service) ManagedDeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
// why this needs a small local claim flag instead of a count-based check.
|
||||
func (s *Service) Signup(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.enabled() {
|
||||
user := localProfile
|
||||
user := adminProfile
|
||||
writeProxyJSON(w, http.StatusCreated, map[string]any{"user": &user})
|
||||
return
|
||||
}
|
||||
@@ -1017,7 +1043,7 @@ func (s *Service) Signup(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Service) Login(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.enabled() {
|
||||
user := localProfile
|
||||
user := adminProfile
|
||||
writeProxyJSON(w, http.StatusOK, map[string]any{"user": &user})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ type fakeProxyStore struct {
|
||||
setupClaimed bool
|
||||
}
|
||||
|
||||
func (s *fakeProxyStore) LoginRequired(context.Context) (bool, error) { return s.loginRequired, nil }
|
||||
func (s *fakeProxyStore) LoginRequired(context.Context) (bool, error) { return s.loginRequired, nil }
|
||||
func (s *fakeProxyStore) SetLoginRequired(_ context.Context, v bool) error {
|
||||
s.loginRequired = v
|
||||
return nil
|
||||
@@ -39,7 +39,7 @@ func (s *fakeProxyStore) UnclaimSetup(context.Context) error {
|
||||
// made AUTHGUARD_URL a de-facto requirement even though ProjectConfigurator
|
||||
// can push it live after startup.
|
||||
func TestPendingModeStillGatesRequests(t *testing.T) {
|
||||
service := NewWithStore(&fakeProxyStore{}, "session", false, "", "testproject")
|
||||
service := NewWithStore(&fakeProxyStore{loginRequired: true}, "session", false, "", "testproject")
|
||||
if !service.enabled() {
|
||||
t.Fatal("Service with no authGuardURL and no Disabled() option should still be enabled (pending), not disabled")
|
||||
}
|
||||
@@ -60,8 +60,34 @@ func TestDisabledOptionSkipsGating(t *testing.T) {
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
user, err := service.CurrentUser(req)
|
||||
if err != nil || user == nil || user.Username != "local" {
|
||||
t.Fatalf("Disabled() Service should resolve the fixed local superadmin profile, got user=%+v err=%v", user, err)
|
||||
if err != nil || user == nil || user.Username != "Admin" || !IsAdmin(user) {
|
||||
t.Fatalf("Disabled() Service should resolve the fixed Admin superadmin profile, got user=%+v err=%v", user, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginDisabledUsesAdminIdentityWithoutCredentials(t *testing.T) {
|
||||
store := &fakeProxyStore{loginRequired: false}
|
||||
service := NewWithStore(store, "session", false, "", "testproject")
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
user, err := service.CurrentUser(req)
|
||||
if err != nil || user == nil || user.Username != "Admin" || !IsAdmin(user) {
|
||||
t.Fatalf("login-disabled Service should resolve the Admin identity, got user=%+v err=%v", user, err)
|
||||
}
|
||||
|
||||
called := false
|
||||
handler := service.RequireAdminIfLoginRequired(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
contextUser := UserFromContext(r.Context())
|
||||
if contextUser == nil || contextUser.Username != "Admin" || !IsAdmin(contextUser) {
|
||||
t.Errorf("middleware context user = %+v, want Admin superadmin", contextUser)
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, req)
|
||||
if response.Code != http.StatusNoContent || !called {
|
||||
t.Fatalf("login-disabled admin middleware status = %d/called=%v, want %d/true", response.Code, called, http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user