-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: 支持优先使用第三方用户名 #2900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: 支持优先使用第三方用户名 #2900
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -237,6 +237,13 @@ func findOrCreateOAuthUser(c *gin.Context, provider oauth.Provider, oauthUser *o | |
|
|
||
| // Set up new user | ||
| user.Username = provider.GetProviderPrefix() + strconv.Itoa(model.GetMaxUserId()+1) | ||
|
|
||
| if oauthUser.Username != "" { | ||
| if exists, err := model.CheckUserExistOrDeleted(oauthUser.Username, ""); err == nil && !exists { | ||
| user.Username = oauthUser.Username | ||
| } | ||
| } | ||
|
Comment on lines
+241
to
+245
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: rg -n -C3 'func.*Insert' --type=go -g '*user*' | head -50Repository: QuantumNous/new-api Length of output: 924 🏁 Script executed: rg -nP 'Username.*valid|validateUsername|isValidUsername|usernameRegex|username.*regexp' --type=go | head -50Repository: QuantumNous/new-api Length of output: 168 🏁 Script executed: rg -nP 'func.*Register' --type=go -g '*/controller/*' | head -30Repository: QuantumNous/new-api Length of output: 211 🏁 Script executed: cat controller/oauth.go | head -250 | tail -30Repository: QuantumNous/new-api Length of output: 1104 🏁 Script executed: rg -n 'CheckUserExistOrDeleted' --type=go -B2 -A2 | head -40Repository: QuantumNous/new-api Length of output: 1105 🏁 Script executed: cat -n model/user.go | sed -n '435,480p'Repository: QuantumNous/new-api Length of output: 1806 🏁 Script executed: rg -n 'Validate|validator' --type=go -B2 -A2 | grep -A5 -B5 'InsertWithTx\|oauth'Repository: QuantumNous/new-api Length of output: 876 🏁 Script executed: rg -n 'oauthUser\.' --type=go controller/oauth.go | head -20Repository: QuantumNous/new-api Length of output: 1269 🏁 Script executed: rg -n 'struct.*OAuthUser|type.*OAuthUser' --type=go -A10 | head -30Repository: QuantumNous/new-api Length of output: 1215 🏁 Script executed: rg -n 'Username.*sanitize|sanitize.*Username|clean.*Username' --type=goRepository: QuantumNous/new-api Length of output: 264 🏁 Script executed: cat -n controller/user.go | sed -n '140,200p'Repository: QuantumNous/new-api Length of output: 2384 🏁 Script executed: rg -n 'cleanUser.*:=' --type=go -B3 -A1 controller/user.goRepository: QuantumNous/new-api Length of output: 515 🏁 Script executed: rg -n 'func.*Clean|func.*Sanitize' --type=go -A5 | head -40Repository: QuantumNous/new-api Length of output: 2118 🏁 Script executed: rg -n 'gorm.*validate|Create.*validate' --type=go | head -20Repository: QuantumNous/new-api Length of output: 703 Add validation for OAuth-provided username to match regular registration constraints.
Apply the same validation to the User object before calling if err := common.Validate.Struct(&user); err != nil {
return nil, fmt.Errorf("invalid user data: %w", err)
}🤖 Prompt for AI Agents |
||
|
|
||
| if oauthUser.DisplayName != "" { | ||
| user.DisplayName = oauthUser.DisplayName | ||
| } else if oauthUser.Username != "" { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 84
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 663
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 168
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 45
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 45
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 702
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 1406
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 853
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 627
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 145
Add graceful handling for unique constraint violation on username.
The TOCTOU race condition exists between
CheckUserExistOrDeleted(line 242) andInsertWithTx: a concurrent request could claim the same username before the insert executes. The same race applies to the generated fallback username on line 239 (GetMaxUserId()+1).The username column has a unique constraint (
gorm:"unique;index"in model/user.go), so the database will reject duplicate insertions. However, there's currently no graceful error handling—the raw constraint violation error bubbles up to the user. Consider catching the unique constraint violation fromInsertWithTxand retrying with an alternative (e.g., the generated fallback username) to improve the user experience.🤖 Prompt for AI Agents