From 4155b267eee9069d27465cf4b3ad2165f95871cd Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 6 Feb 2025 14:23:42 +0300 Subject: [PATCH 1/3] Clear the temporary value after use --- handlers/application/menuhandler.go | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index dc3326b..b190936 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -479,6 +479,13 @@ func (h *MenuHandlers) ConfirmPinChange(ctx context.Context, sym string, input [ return res, err } + // clear the temporary value as it has been used + err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte("")) + if err != nil { + logg.ErrorCtxf(ctx, "failed to clear DATA_TEMPORARY_VALUE entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", "empty", "error", err) + return res, err + } + return res, nil } @@ -515,6 +522,13 @@ func (h *MenuHandlers) ResetOthersPin(ctx context.Context, sym string, input []b return res, err } + // clear the temporary value as it has been used + err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte("")) + if err != nil { + logg.ErrorCtxf(ctx, "failed to clear DATA_TEMPORARY_VALUE entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", "empty", "error", err) + return res, err + } + return res, nil } @@ -681,6 +695,12 @@ func (h *MenuHandlers) SaveFirstname(ctx context.Context, sym string, input []by if err != nil { return res, err } + // clear the temporary value as it has been used + err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte("")) + if err != nil { + logg.ErrorCtxf(ctx, "failed to clear DATA_TEMPORARY_VALUE entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", "empty", "error", err) + return res, err + } res.FlagSet = append(res.FlagSet, flag_firstname_set) } else { if firstNameSet { @@ -721,6 +741,12 @@ func (h *MenuHandlers) SaveFamilyname(ctx context.Context, sym string, input []b logg.ErrorCtxf(ctx, "failed to write familyName entry with", "key", storedb.DATA_FAMILY_NAME, "value", temporaryFamilyName, "error", err) return res, err } + // clear the temporary value as it has been used + err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte("")) + if err != nil { + logg.ErrorCtxf(ctx, "failed to clear DATA_TEMPORARY_VALUE entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", "empty", "error", err) + return res, err + } res.FlagSet = append(res.FlagSet, flag_familyname_set) } else { if familyNameSet { @@ -791,6 +817,12 @@ func (h *MenuHandlers) SaveYob(ctx context.Context, sym string, input []byte) (r logg.ErrorCtxf(ctx, "failed to write yob entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", temporaryYob, "error", err) return res, err } + // clear the temporary value as it has been used + err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte("")) + if err != nil { + logg.ErrorCtxf(ctx, "failed to clear DATA_TEMPORARY_VALUE entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", "empty", "error", err) + return res, err + } res.FlagSet = append(res.FlagSet, flag_yob_set) } else { if yobSet { @@ -830,6 +862,12 @@ func (h *MenuHandlers) SaveLocation(ctx context.Context, sym string, input []byt logg.ErrorCtxf(ctx, "failed to write location entry with", "key", storedb.DATA_LOCATION, "value", temporaryLocation, "error", err) return res, err } + // clear the temporary value as it has been used + err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte("")) + if err != nil { + logg.ErrorCtxf(ctx, "failed to clear DATA_TEMPORARY_VALUE entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", "empty", "error", err) + return res, err + } res.FlagSet = append(res.FlagSet, flag_location_set) } else { if locationSet { @@ -871,6 +909,12 @@ func (h *MenuHandlers) SaveGender(ctx context.Context, sym string, input []byte) logg.ErrorCtxf(ctx, "failed to write gender entry with", "key", storedb.DATA_GENDER, "value", gender, "error", err) return res, err } + // clear the temporary value as it has been used + err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte("")) + if err != nil { + logg.ErrorCtxf(ctx, "failed to clear DATA_TEMPORARY_VALUE entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", "empty", "error", err) + return res, err + } res.FlagSet = append(res.FlagSet, flag_gender_set) } else { if genderSet { @@ -912,6 +956,12 @@ func (h *MenuHandlers) SaveOfferings(ctx context.Context, sym string, input []by logg.ErrorCtxf(ctx, "failed to write offerings entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) return res, err } + // clear the temporary value as it has been used + err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte("")) + if err != nil { + logg.ErrorCtxf(ctx, "failed to clear DATA_TEMPORARY_VALUE entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", "empty", "error", err) + return res, err + } res.FlagSet = append(res.FlagSet, flag_offerings_set) } else { if offeringsSet { @@ -1584,6 +1634,12 @@ func (h *MenuHandlers) InviteValidRecipient(ctx context.Context, sym string, inp // res.Content = l.Get("Your invitation to %s to join Sarafu Network has been sent.", string(recipient)) res.Content = l.Get("Your invite request for %s to Sarafu Network failed. Please try again later.", string(recipient)) + // clear the temporary value as it has been used + err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte("")) + if err != nil { + logg.ErrorCtxf(ctx, "failed to clear DATA_TEMPORARY_VALUE entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", "empty", "error", err) + return res, err + } return res, nil } From 66474161152d09169b653f8db63e6ee271e8000a Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 6 Feb 2025 14:38:11 +0300 Subject: [PATCH 2/3] Return an error if the temporary value is empty --- handlers/application/menuhandler.go | 54 +++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index b190936..5c044b1 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -436,6 +436,11 @@ func (h *MenuHandlers) CheckBlockedNumPinMisMatch(ctx context.Context, sym strin logg.ErrorCtxf(ctx, "failed to read hashedTemporaryPin entry with", "key", storedb.DATA_TEMPORARY_VALUE, "error", err) return res, err } + if len(hashedTemporaryPin) == 0 { + logg.ErrorCtxf(ctx, "hashedTemporaryPin is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } + if pin.VerifyPIN(string(hashedTemporaryPin), string(input)) { res.FlagReset = append(res.FlagReset, flag_pin_mismatch) } else { @@ -464,6 +469,10 @@ func (h *MenuHandlers) ConfirmPinChange(ctx context.Context, sym string, input [ logg.ErrorCtxf(ctx, "failed to read hashedTemporaryPin entry with", "key", storedb.DATA_TEMPORARY_VALUE, "error", err) return res, err } + if len(hashedTemporaryPin) == 0 { + logg.ErrorCtxf(ctx, "hashedTemporaryPin is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } if pin.VerifyPIN(string(hashedTemporaryPin), string(input)) { res.FlagReset = append(res.FlagReset, flag_pin_mismatch) @@ -505,13 +514,17 @@ func (h *MenuHandlers) ResetOthersPin(ctx context.Context, sym string, input []b logg.ErrorCtxf(ctx, "failed to read blockedPhonenumber entry with", "key", storedb.DATA_BLOCKED_NUMBER, "error", err) return res, err } - hashedTmporaryPin, err := store.ReadEntry(ctx, string(blockedPhonenumber), storedb.DATA_TEMPORARY_VALUE) + hashedTemporaryPin, err := store.ReadEntry(ctx, string(blockedPhonenumber), storedb.DATA_TEMPORARY_VALUE) if err != nil { logg.ErrorCtxf(ctx, "failed to read hashedTmporaryPin entry with", "key", storedb.DATA_TEMPORARY_VALUE, "error", err) return res, err } + if len(hashedTemporaryPin) == 0 { + logg.ErrorCtxf(ctx, "hashedTemporaryPin is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } - err = store.WriteEntry(ctx, string(blockedPhonenumber), storedb.DATA_ACCOUNT_PIN, []byte(hashedTmporaryPin)) + err = store.WriteEntry(ctx, string(blockedPhonenumber), storedb.DATA_ACCOUNT_PIN, []byte(hashedTemporaryPin)) if err != nil { return res, err } @@ -651,6 +664,11 @@ func (h *MenuHandlers) VerifyCreatePin(ctx context.Context, sym string, input [] logg.ErrorCtxf(ctx, "failed to read hashedTemporaryPin entry with", "key", storedb.DATA_TEMPORARY_VALUE, "error", err) return res, err } + if len(hashedTemporaryPin) == 0 { + logg.ErrorCtxf(ctx, "hashedTemporaryPin is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } + if pin.VerifyPIN(string(hashedTemporaryPin), string(input)) { res.FlagSet = []uint32{flag_valid_pin} res.FlagReset = []uint32{flag_pin_mismatch} @@ -686,6 +704,10 @@ func (h *MenuHandlers) SaveFirstname(ctx context.Context, sym string, input []by firstNameSet := h.st.MatchFlag(flag_firstname_set, true) if allowUpdate { temporaryFirstName, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE) + if len(temporaryFirstName) == 0 { + logg.ErrorCtxf(ctx, "temporaryFirstName is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } err = store.WriteEntry(ctx, sessionId, storedb.DATA_FIRST_NAME, []byte(temporaryFirstName)) if err != nil { logg.ErrorCtxf(ctx, "failed to write firstName entry with", "key", storedb.DATA_FIRST_NAME, "value", temporaryFirstName, "error", err) @@ -736,6 +758,10 @@ func (h *MenuHandlers) SaveFamilyname(ctx context.Context, sym string, input []b if allowUpdate { temporaryFamilyName, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE) + if len(temporaryFamilyName) == 0 { + logg.ErrorCtxf(ctx, "temporaryFamilyName is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } err = store.WriteEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME, []byte(temporaryFamilyName)) if err != nil { logg.ErrorCtxf(ctx, "failed to write familyName entry with", "key", storedb.DATA_FAMILY_NAME, "value", temporaryFamilyName, "error", err) @@ -812,6 +838,10 @@ func (h *MenuHandlers) SaveYob(ctx context.Context, sym string, input []byte) (r if allowUpdate { temporaryYob, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE) + if len(temporaryYob) == 0 { + logg.ErrorCtxf(ctx, "temporaryYob is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } err = store.WriteEntry(ctx, sessionId, storedb.DATA_YOB, []byte(temporaryYob)) if err != nil { logg.ErrorCtxf(ctx, "failed to write yob entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", temporaryYob, "error", err) @@ -857,6 +887,10 @@ func (h *MenuHandlers) SaveLocation(ctx context.Context, sym string, input []byt if allowUpdate { temporaryLocation, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE) + if len(temporaryLocation) == 0 { + logg.ErrorCtxf(ctx, "temporaryLocation is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } err = store.WriteEntry(ctx, sessionId, storedb.DATA_LOCATION, []byte(temporaryLocation)) if err != nil { logg.ErrorCtxf(ctx, "failed to write location entry with", "key", storedb.DATA_LOCATION, "value", temporaryLocation, "error", err) @@ -904,6 +938,10 @@ func (h *MenuHandlers) SaveGender(ctx context.Context, sym string, input []byte) if allowUpdate { temporaryGender, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE) + if len(temporaryGender) == 0 { + logg.ErrorCtxf(ctx, "temporaryGender is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } err = store.WriteEntry(ctx, sessionId, storedb.DATA_GENDER, []byte(temporaryGender)) if err != nil { logg.ErrorCtxf(ctx, "failed to write gender entry with", "key", storedb.DATA_GENDER, "value", gender, "error", err) @@ -951,6 +989,10 @@ func (h *MenuHandlers) SaveOfferings(ctx context.Context, sym string, input []by if allowUpdate { temporaryOfferings, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE) + if len(temporaryOfferings) == 0 { + logg.ErrorCtxf(ctx, "temporaryOfferings is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } err = store.WriteEntry(ctx, sessionId, storedb.DATA_OFFERINGS, []byte(temporaryOfferings)) if err != nil { logg.ErrorCtxf(ctx, "failed to write offerings entry with", "key", storedb.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) @@ -1627,6 +1669,10 @@ func (h *MenuHandlers) InviteValidRecipient(ctx context.Context, sym string, inp l.AddDomain("default") recipient, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE) + if len(recipient) == 0 { + logg.ErrorCtxf(ctx, "recipient is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } // TODO // send an invitation SMS @@ -1751,6 +1797,10 @@ func (h *MenuHandlers) GetRecipient(ctx context.Context, sym string, input []byt } store := h.userdataStore recipient, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE) + if len(recipient) == 0 { + logg.ErrorCtxf(ctx, "recipient is empty", "key", storedb.DATA_TEMPORARY_VALUE) + return res, fmt.Errorf("Data error encountered") + } res.Content = string(recipient) From 6b3b8ffabe1e6a6b5bd42eec75931989f4e9abbb Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Thu, 6 Feb 2025 14:57:29 +0300 Subject: [PATCH 3/3] add alias request and resolve logs --- handlers/application/menuhandler.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index 5c044b1..78a1e37 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -1596,6 +1596,8 @@ func (h *MenuHandlers) ValidateRecipient(ctx context.Context, sym string, input AliasAddress, err = h.accountService.CheckAliasAddress(ctx, recipient) if err == nil { AliasAddressResult = AliasAddress.Address + } else { + logg.ErrorCtxf(ctx, "failed to resolve alias", "alias", recipient, "error_alias_check", err) } } else { //Perform a search for each search domain,break on first match @@ -1605,6 +1607,8 @@ func (h *MenuHandlers) ValidateRecipient(ctx context.Context, sym string, input if err == nil { AliasAddressResult = AliasAddress.Address continue + } else { + logg.ErrorCtxf(ctx, "failed to resolve alias", "alias", recipient, "error_alias_check", err) } } } @@ -2422,6 +2426,7 @@ func (h *MenuHandlers) constructAccountAlias(ctx context.Context) error { aliasInput := fmt.Sprintf("%s%s", firstName, familyName) aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), aliasInput) if err != nil { + logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", aliasInput, "error_alias_request", err) return fmt.Errorf("Failed to retrieve alias: %s", err.Error()) } alias = aliasResult.Alias