fix: return username format error on registration

This commit is contained in:
richjohnrambo
2026-09-24 16:47:14 +08:00
parent d41956019a
commit 6453abdbd1
2 changed files with 25 additions and 4 deletions
+13 -4
View File
@@ -56,8 +56,7 @@ func replyCreateUser(s *Session, msg *ClientComMessage, rec *auth.Rec) {
}
if err != nil || !secretMatches {
logs.Warn.Println("create user: invalid registration account fields, sid=", s.sid)
s.queueOut(decodeStoreError(types.ErrPolicy, msg.Id, msg.Timestamp,
map[string]any{"what": "account", "reason": "REGISTRATION_ACCOUNT_INVALID"}))
s.queueOut(decodeStoreError(types.ErrPolicy, msg.Id, msg.Timestamp, registrationAccountErrorParams(err)))
return
}
authSecret := msg.Acc.Secret
@@ -407,6 +406,7 @@ var (
errRegistrationTenantForbidden = errors.New("registration tenant forbidden")
errRegistrationTenantRequired = errors.New("registration tenant required")
errRegistrationTenantMismatch = errors.New("registration tenant mismatch")
errRegistrationUsernameInvalid = errors.New("registration username invalid")
errLoginTypeRequired = errors.New("login_type required")
errLoginTypeInvalid = errors.New("login_type invalid")
)
@@ -579,13 +579,22 @@ func loginTenantErrorParams(err error) map[string]any {
return params
}
func registrationAccountErrorParams(err error) map[string]any {
params := map[string]any{"what": "account", "reason": "REGISTRATION_ACCOUNT_INVALID"}
if errors.Is(err, errRegistrationUsernameInvalid) {
params["what"] = "username"
params["reason"] = "USERNAME_INVALID_FORMAT"
}
return params
}
func validateRegistrationUsername(username string) error {
if username == "" || len(username) > 10 {
return types.ErrPolicy
return errRegistrationUsernameInvalid
}
for i := range username {
if (username[i] < 'a' || username[i] > 'z') && (username[i] < '0' || username[i] > '9') {
return types.ErrPolicy
return errRegistrationUsernameInvalid
}
}
return nil