fix: TOTPHandler.Generate returns 404 for deleted users instead of 500#188
Merged
Merged
Conversation
…error Agent-Logs-Url: https://github.com/amalgamated-tools/goauth/sessions/76d323f6-685a-4371-ba6b-4074d2dfeb8f Co-authored-by: veverkap <22348+veverkap@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
veverkap
May 3, 2026 14:25
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aligns TOTPHandler.Generate with the rest of the authenticated handlers by treating a deleted user record (auth.ErrNotFound) as a client-visible 404 instead of an internal 500, improving correctness when accounts are removed mid-session.
Changes:
- Update
TOTPHandler.Generateto return HTTP 404 onerrors.Is(err, auth.ErrNotFound)and log unexpected user-store errors before returning 500. - Extend/adjust tests to cover both the “user not found” (404) path and the generic user store failure (500) path.
Show a summary per file
| File | Description |
|---|---|
handler/totp.go |
Adds an ErrNotFound → 404 branch and logs unexpected FindByID failures before returning 500. |
handler/totp_test.go |
Adds a dedicated 404 test case and renames the prior test to cover generic store errors (500). |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TOTPHandler.Generatewas the only authenticated handler callingFindByIDwithout checkingauth.ErrNotFound, returning HTTP 500 when an account was deleted mid-session. Every otherFindByIDcall in an authenticated context returns 404 in this case.Changes
handler/totp.go: Adderrors.Is(err, auth.ErrNotFound)→ 404 branch andslog.ErrorContextlog for unexpected store errors before the 500 fallback:handler/totp_test.go: Rename existingTestTOTP_generate_userNotFound→TestTOTP_generate_userStoreError(generic DB error → 500); addTestTOTP_generate_userNotFoundcovering theauth.ErrNotFound→ 404 path.Greptile Summary
This PR fixes
TOTPHandler.Generateto return HTTP 404 instead of 500 when a user cannot be found (deleted mid-session), aligning it with every other authenticatedFindByIDcall in the codebase. The implementation correctly useserrors.Is(err, auth.ErrNotFound), includesslog.ErrorContextwith context for unexpected store errors, and adds both the missing 404 test and renames the existing 500 test for clarity.Confidence Score: 5/5
This PR is safe to merge — it is a minimal, well-tested bug fix with no regressions.
The change is small and targeted: one new error branch, one slog call with context, and two well-scoped tests. All imports were already present, the slog call correctly passes r.Context() per the project rule, and the fix aligns with the established pattern in the rest of the codebase. No P0 or P1 issues found.
No files require special attention.
Important Files Changed
Sequence Diagram
sequenceDiagram participant Client participant TOTPHandler participant UserStore Client->>TOTPHandler: POST /totp/generate TOTPHandler->>TOTPHandler: auth.GenerateTOTPSecret() TOTPHandler->>UserStore: FindByID(ctx, userID) alt user not found (ErrNotFound) UserStore-->>TOTPHandler: nil, auth.ErrNotFound TOTPHandler-->>Client: 404 user not found else store error UserStore-->>TOTPHandler: nil, err TOTPHandler->>TOTPHandler: slog.ErrorContext(ctx, "failed to fetch user") TOTPHandler-->>Client: 500 failed to fetch user else success UserStore-->>TOTPHandler: *auth.User, nil TOTPHandler->>TOTPHandler: TOTPProvisioningURI(secret, email, issuer) TOTPHandler-->>Client: 200 {secret, provisioningURI} endReviews (1): Last reviewed commit: "fix: TOTPHandler.Generate checks ErrNotF..." | Re-trigger Greptile