diff --git a/Server/internal/auth/auth_test.go b/Server/internal/auth/auth_test.go index c2e61c2..aba2f9c 100644 --- a/Server/internal/auth/auth_test.go +++ b/Server/internal/auth/auth_test.go @@ -25,6 +25,9 @@ type fakePB struct { passwords map[string]string verifyCalls atomic.Int32 mfaFor string // email that requires MFA + // rejectSuperuser makes superuser reads of a user fail as PocketBase does + // when docwell-server's own credentials are wrong. + rejectSuperuser bool // resetSubject is the users collection's reset subject now; sentWith // collects it at each password-reset request. resetSubject string @@ -91,6 +94,10 @@ func newFakePB(t *testing.T) (*fakePB, *pocketbase.Client) { mux.HandleFunc("GET /api/collections/users/records/{id}", func(w http.ResponseWriter, r *http.Request) { f.mu.Lock() defer f.mu.Unlock() + if f.rejectSuperuser { + reply(w, 403, map[string]any{"status": 403, "message": "Only superusers can perform this action."}) + return + } if u := byID(r.PathValue("id")); u != nil { reply(w, 200, u) return @@ -286,6 +293,16 @@ func TestLogin(t *testing.T) { if _, err := s.Login(ctx, "ann@docwell.test", "password123", "mfa123"); err != nil { t.Errorf("mfa second step err = %v", err) } + f.mfaFor = "" + + // Right password, but the server's own PocketBase superuser is rejected: + // that is a server fault, not the person's expired session. + f.rejectSuperuser = true + _, err := s.Login(ctx, "ann@docwell.test", "password123", "") + var pbErr *pocketbase.Error + if err == nil || errors.Is(err, ErrUnauthorized) || !errors.As(err, &pbErr) || pbErr.Status != 403 { + t.Errorf("superuser rejected err = %v, want the PocketBase 403 passed through", err) + } } func TestVerifyCachesAndRejectsDisabled(t *testing.T) { diff --git a/Server/internal/auth/totp.go b/Server/internal/auth/totp.go index 2017480..b127ae6 100644 --- a/Server/internal/auth/totp.go +++ b/Server/internal/auth/totp.go @@ -20,6 +20,7 @@ import ( "github.com/pquerna/otp" "github.com/pquerna/otp/totp" + "docwell/server/internal/apperr" "docwell/server/internal/store/pocketbase" ) @@ -75,8 +76,12 @@ type totpRecord struct { } func (s *Service) totpRecord(ctx context.Context, userID string) (totpRecord, error) { + // This read runs as the server's superuser, not as the person signing in: + // a 401/403 here means docwell-server's own PocketBase credentials are + // wrong. Passing it through (not as ErrUnauthorized) gets it logged and + // answered as a server error instead of "your session has expired". rec, err := pocketbase.Get[totpRecord](ctx, s.pb, "users", userID) - return rec, tokenError(err) + return rec, err } func randomToken(n int) string { @@ -300,5 +305,8 @@ func (s *Service) ResetTOTP(ctx context.Context, userID string) error { "totp_secret": "", "totp_pending": "", "totp_enabled": false, "recovery_codes": []string{}, }) s.Forget(userID) - return tokenError(err) + if pocketbase.IsNotFound(err) { + return apperr.NotFound("user not found") + } + return err // a superuser call, like totpRecord }