diff --git a/server/user.go b/server/user.go index ebbb2d0..5dd7c21 100644 --- a/server/user.go +++ b/server/user.go @@ -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 diff --git a/server/user_registration_test.go b/server/user_registration_test.go index c9a448a..6d0aab0 100644 --- a/server/user_registration_test.go +++ b/server/user_registration_test.go @@ -71,6 +71,18 @@ func TestResolveRegistrationAccount(t *testing.T) { } } +func TestRegistrationAccountErrorParams(t *testing.T) { + params := registrationAccountErrorParams(errRegistrationUsernameInvalid) + if params["what"] != "username" || params["reason"] != "USERNAME_INVALID_FORMAT" { + t.Fatalf("got params=%v", params) + } + + params = registrationAccountErrorParams(types.ErrPolicy) + if params["what"] != "account" || params["reason"] != "REGISTRATION_ACCOUNT_INVALID" { + t.Fatalf("got fallback params=%v", params) + } +} + func TestRegistrationSecretMatchesSelectedAccount(t *testing.T) { if !registrationSecretMatches([]byte("alice:password:with-colon"), "alice") { t.Fatal("matching account was rejected")