fix: allow legacy enterprise registration payloads
This commit is contained in:
+62
-1
@@ -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 = ®istrationType
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user