fix: allow legacy enterprise registration payloads

This commit is contained in:
richjohnrambo
2026-09-22 00:23:53 +08:00
parent dc36ce292e
commit 3276c6d1cd
2 changed files with 127 additions and 1 deletions
+62 -1
View File
@@ -156,6 +156,14 @@ func replyCreateUser(s *Session, msg *ClientComMessage, rec *auth.Rec) {
continue
}
challengeID, _ := cr.Params["challenge_id"].(string)
if strings.TrimSpace(challengeID) == "" && phoneRegistration && isLegacyRegistrationSMSCode(cr.Response) {
if err := attachLegacyRegistrationSMSChallenge(s.tenantID, cr, phone, time.Now().UTC().Round(time.Millisecond)); err != nil {
s.queueOut(decodeStoreError(types.ErrCredentials, msg.Id, msg.Timestamp,
map[string]any{"creds": []string{"tel"}}))
return
}
challengeID, _ = cr.Params["challenge_id"].(string)
}
if phone == "" || strings.TrimSpace(challengeID) == "" || strings.TrimSpace(cr.Response) == "" ||
(registrationPhone != "" && registrationPhone != phone) {
s.queueOut(decodeStoreError(types.ErrCredentials, msg.Id, msg.Timestamp,
@@ -400,7 +408,15 @@ var (
func validateAccountRegistrationTenant(s *Session, msg *ClientComMessage) error {
if msg == nil || msg.Acc == nil || msg.Acc.RegistrationType == nil {
return errRegistrationTypeRequired
if msg != nil && msg.Acc != nil && s != nil && !strings.EqualFold(s.tenantCode, imDefaultTenantCode) {
registrationType := registrationTypeEnterprise
msg.Acc.RegistrationType = &registrationType
if strings.TrimSpace(msg.Acc.TenantCode) == "" {
msg.Acc.TenantCode = s.tenantCode
}
} else {
return errRegistrationTypeRequired
}
}
tenantCode := strings.TrimSpace(msg.Acc.TenantCode)
switch *msg.Acc.RegistrationType {
@@ -427,6 +443,51 @@ func validateAccountRegistrationTenant(s *Session, msg *ClientComMessage) error
return nil
}
func isLegacyRegistrationSMSCode(value string) bool {
if len(value) != imVerificationCodeLength {
return false
}
for i := range value {
if value[i] < '0' || value[i] > '9' {
return false
}
}
return true
}
func attachLegacyRegistrationSMSChallenge(tenantID types.TenantID, cr *MsgCredClient, phone string, now time.Time) error {
if store.Verifications == nil {
return types.ErrInternal
}
phoneHash := hashIMSecret(tenantID, "register", phone, "phone")
vc, err := store.Verifications.GetActive(tenantID, "register", phoneHash, now)
if err != nil {
return err
}
if vc.CodeHash != hashIMSecret(tenantID, vc.Scene, vc.ChallengeID, vc.PhoneHash, strings.TrimSpace(cr.Response)) {
nextState := types.VerificationStatePending
if vc.Attempts+1 >= vc.MaxAttempts {
nextState = types.VerificationStateLocked
}
_ = store.Verifications.Fail(tenantID, vc.ChallengeID, nextState)
return types.ErrInvalidResponse
}
token, err := randomIMToken(32)
if err != nil {
return err
}
tokenHash := hashIMSecret(tenantID, vc.Scene, vc.ChallengeID, vc.PhoneHash, token)
if err = store.Verifications.MarkVerified(tenantID, vc.ChallengeID, tokenHash, now); err != nil {
return err
}
if cr.Params == nil {
cr.Params = map[string]any{}
}
cr.Params["challenge_id"] = vc.ChallengeID
cr.Response = token
return nil
}
func registrationTenantErrorParams(err error) map[string]any {
params := map[string]any{"what": "registration_type", "reason": "REGISTRATION_TYPE_INVALID"}
switch {