fix: return username format error on registration
This commit is contained in:
+13
-4
@@ -56,8 +56,7 @@ func replyCreateUser(s *Session, msg *ClientComMessage, rec *auth.Rec) {
|
|||||||
}
|
}
|
||||||
if err != nil || !secretMatches {
|
if err != nil || !secretMatches {
|
||||||
logs.Warn.Println("create user: invalid registration account fields, sid=", s.sid)
|
logs.Warn.Println("create user: invalid registration account fields, sid=", s.sid)
|
||||||
s.queueOut(decodeStoreError(types.ErrPolicy, msg.Id, msg.Timestamp,
|
s.queueOut(decodeStoreError(types.ErrPolicy, msg.Id, msg.Timestamp, registrationAccountErrorParams(err)))
|
||||||
map[string]any{"what": "account", "reason": "REGISTRATION_ACCOUNT_INVALID"}))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
authSecret := msg.Acc.Secret
|
authSecret := msg.Acc.Secret
|
||||||
@@ -407,6 +406,7 @@ var (
|
|||||||
errRegistrationTenantForbidden = errors.New("registration tenant forbidden")
|
errRegistrationTenantForbidden = errors.New("registration tenant forbidden")
|
||||||
errRegistrationTenantRequired = errors.New("registration tenant required")
|
errRegistrationTenantRequired = errors.New("registration tenant required")
|
||||||
errRegistrationTenantMismatch = errors.New("registration tenant mismatch")
|
errRegistrationTenantMismatch = errors.New("registration tenant mismatch")
|
||||||
|
errRegistrationUsernameInvalid = errors.New("registration username invalid")
|
||||||
errLoginTypeRequired = errors.New("login_type required")
|
errLoginTypeRequired = errors.New("login_type required")
|
||||||
errLoginTypeInvalid = errors.New("login_type invalid")
|
errLoginTypeInvalid = errors.New("login_type invalid")
|
||||||
)
|
)
|
||||||
@@ -579,13 +579,22 @@ func loginTenantErrorParams(err error) map[string]any {
|
|||||||
return params
|
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 {
|
func validateRegistrationUsername(username string) error {
|
||||||
if username == "" || len(username) > 10 {
|
if username == "" || len(username) > 10 {
|
||||||
return types.ErrPolicy
|
return errRegistrationUsernameInvalid
|
||||||
}
|
}
|
||||||
for i := range username {
|
for i := range username {
|
||||||
if (username[i] < 'a' || username[i] > 'z') && (username[i] < '0' || username[i] > '9') {
|
if (username[i] < 'a' || username[i] > 'z') && (username[i] < '0' || username[i] > '9') {
|
||||||
return types.ErrPolicy
|
return errRegistrationUsernameInvalid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -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) {
|
func TestRegistrationSecretMatchesSelectedAccount(t *testing.T) {
|
||||||
if !registrationSecretMatches([]byte("alice:password:with-colon"), "alice") {
|
if !registrationSecretMatches([]byte("alice:password:with-colon"), "alice") {
|
||||||
t.Fatal("matching account was rejected")
|
t.Fatal("matching account was rejected")
|
||||||
|
|||||||
Reference in New Issue
Block a user