Repository navigation
Go CLI: fix register user - #14665
Conversation
Signed-off-by: Jin Hai <[email protected]>
📝 WalkthroughWalkthroughThis PR reorganizes the user registration endpoint from ChangesUser Registration API Route Restructuring with Client-Side Password Encryption
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/router/router.go (1)
93-97:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winSwagger annotation for
Logoutis now inconsistent with the registered GET method
internal/handler/user.goline 337 still reads//@router/v1/user/logout [post], but this PR correctly registers the route asGET /v1/user/logout. The POST logout lives at/api/v1/auth/logout(line 138).📝 Fix in `internal/handler/user.go`
-// `@Router` /v1/user/logout [post] +// `@Router` /v1/user/logout [get]🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/router/router.go` around lines 93 - 97, Update the Swagger router annotation for the Logout handler to match the registered GET route: change the comment in internal/handler/user.go that currently reads "// `@Router` /v1/user/logout [post]" to use "[get]" so it aligns with the registered route engine.GET("/v1/user/logout", r.userHandler.Logout); also verify the Logout method signature in the same file remains compatible with GET requests.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/service/user.go`:
- Around line 321-333: Get rid of the account-enumeration leak by returning the
same generic authentication error when s.userDAO.GetByEmail(req.Email) fails and
when the password verification fails: replace the current distinct message from
the s.userDAO.GetByEmail error branch with the same generic authentication error
used for password mismatch (use common.CodeAuthenticationError and a generic
message like "invalid email or password"), while keeping decryptPassword errors
as server errors; update references in this block (s.userDAO.GetByEmail,
s.decryptPassword, s.VerifyPassword, user.Password) so both "user not found" and
"password mismatch" paths produce the identical authentication response.
---
Outside diff comments:
In `@internal/router/router.go`:
- Around line 93-97: Update the Swagger router annotation for the Logout handler
to match the registered GET route: change the comment in
internal/handler/user.go that currently reads "// `@Router` /v1/user/logout
[post]" to use "[get]" so it aligns with the registered route
engine.GET("/v1/user/logout", r.userHandler.Logout); also verify the Logout
method signature in the same file remains compatible with GET requests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: abc1206f-8bfc-4826-8a0b-e06579815e8f
📒 Files selected for processing (5)
internal/cli/user_command.gointernal/handler/user.gointernal/router/router.gointernal/service/user.goweb/vite.config.ts
| user, err := s.userDAO.GetByEmail(req.Email) | ||
| if err != nil { | ||
| return nil, common.CodeAuthenticationError, fmt.Errorf("Email: %s is not registered!", req.Email) | ||
| return nil, common.CodeAuthenticationError, fmt.Errorf("email: %s is not registered!", req.Email) | ||
| } | ||
|
|
||
| decryptedPassword, err := s.decryptPassword(req.Password) | ||
| if err != nil { | ||
| return nil, common.CodeServerError, fmt.Errorf("Fail to crypt password") | ||
| return nil, common.CodeServerError, fmt.Errorf("fail to crypt password") | ||
| } | ||
|
|
||
| if user.Password == nil || !s.VerifyPassword(*user.Password, decryptedPassword) { | ||
| return nil, common.CodeAuthenticationError, fmt.Errorf("Email and password do not match!") | ||
| return nil, common.CodeAuthenticationError, fmt.Errorf("email and password do not match!") | ||
| } |
There was a problem hiding this comment.
Avoid account enumeration via login error messages.
The new message at Line 323 reveals whether an email is registered. This weakens authentication security and allows user enumeration. Please return the same generic authentication error for both “user not found” and “password mismatch” branches.
Suggested patch
func (s *UserService) LoginByEmail(req *EmailLoginRequest) (*entity.User, common.ErrorCode, error) {
user, err := s.userDAO.GetByEmail(req.Email)
if err != nil {
- return nil, common.CodeAuthenticationError, fmt.Errorf("email: %s is not registered!", req.Email)
+ return nil, common.CodeAuthenticationError, fmt.Errorf("invalid email or password")
}
@@
if user.Password == nil || !s.VerifyPassword(*user.Password, decryptedPassword) {
- return nil, common.CodeAuthenticationError, fmt.Errorf("email and password do not match!")
+ return nil, common.CodeAuthenticationError, fmt.Errorf("invalid email or password")
}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/service/user.go` around lines 321 - 333, Get rid of the
account-enumeration leak by returning the same generic authentication error when
s.userDAO.GetByEmail(req.Email) fails and when the password verification fails:
replace the current distinct message from the s.userDAO.GetByEmail error branch
with the same generic authentication error used for password mismatch (use
common.CodeAuthenticationError and a generic message like "invalid email or
password"), while keeping decryptPassword errors as server errors; update
references in this block (s.userDAO.GetByEmail, s.decryptPassword,
s.VerifyPassword, user.Password) so both "user not found" and "password
mismatch" paths produce the identical authentication response.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14665 +/- ##
=======================================
Coverage 94.16% 94.16%
=======================================
Files 10 10
Lines 703 703
Branches 112 112
=======================================
Hits 662 662
Misses 25 25
Partials 16 16 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
### What problem does this PR solve? 1. Update API URL 2. Add password encryption ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) Signed-off-by: Jin Hai <[email protected]>
### What problem does this PR solve? 1. Update API URL 2. Add password encryption ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) Signed-off-by: Jin Hai <[email protected]>
What problem does this PR solve?
Type of change