From 17804e764119f786193e0239dd7f338c7ae9f87c Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 5 Sep 2024 19:50:02 +0300 Subject: [PATCH] Save the actual PIN and verify it --- internal/handlers/ussd/menuhandler.go | 48 +++++++++++++-------------- internal/utils/db.go | 2 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 2eb11b1..5ac9456 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -74,14 +74,6 @@ func (fm *FlagManager) GetFlag(label string) (uint32, error) { return fm.parser.GetFlag(label) } -// type Handlers struct { -// fs *FSData -// db *gdbm.Database -// flagManager *FlagManager -// accountFileHandler utils.AccountFileHandlerInterface -// accountService server.AccountServiceInterface -// } - type Handlers struct { st *state.State ca cache.Memory @@ -187,6 +179,10 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) // SavePin persists the user's PIN choice into the filesystem func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin") @@ -199,10 +195,11 @@ func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resou res.FlagReset = append(res.FlagReset, flag_incorrect_pin) - // key := []byte(AccountPin) - // value := []byte(accountPIN) + err := utils.WriteEntry(ctx, h.userdataStore, sessionId, utils.DATA_ACCOUNT_PIN, []byte(accountPIN)) + if err != nil { + return res, nil + } - //h.db.Store(key, value, true) return res, nil } @@ -241,11 +238,13 @@ func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (res flag_pin_mismatch, _ := h.flagManager.GetFlag("flag_pin_mismatch") flag_pin_set, _ := h.flagManager.GetFlag("flag_pin_set") - // AccountPin, err := h.db.Fetch([]byte(AccountPin)) - // if err != nil { - // return res, err - // } - AccountPin := []byte("2768") + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + + AccountPin, _ := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_ACCOUNT_PIN) + if bytes.Equal(input, AccountPin) { res.FlagSet = []uint32{flag_valid_pin} res.FlagReset = []uint32{flag_pin_mismatch} @@ -441,10 +440,9 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b return res, fmt.Errorf("missing session") } - trackingId, err := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_TRACKING_ID) - fmt.Println("Checking status with tracking id:", string(trackingId)) + trackingId, _ := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_TRACKING_ID) - status, err := h.accountService.CheckAccountStatus(string("1234")) + status, err := h.accountService.CheckAccountStatus(string(trackingId)) if err != nil { fmt.Println("Error checking account status:", err) @@ -524,13 +522,15 @@ func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []by // the balance as the result content func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} - // publicKey, err := h.db.Fetch([]byte(PublicKeyKey)) - // if err != nil { - // return res, err - // } + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } - balance, err := h.accountService.CheckBalance(string("publicKey")) + publicKey, _ := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_PUBLIC_KEY) + + balance, err := h.accountService.CheckBalance(string(publicKey)) if err != nil { return res, nil } diff --git a/internal/utils/db.go b/internal/utils/db.go index 1104452..bfaf2e1 100644 --- a/internal/utils/db.go +++ b/internal/utils/db.go @@ -15,6 +15,7 @@ const ( DATA_TRACKING_ID DATA_PUBLIC_KEY DATA_CUSTODIAL_ID + DATA_ACCOUNT_PIN ) func typToBytes(typ DataTyp) []byte { @@ -37,7 +38,6 @@ func ReadEntry(ctx context.Context, store db.Db, sessionId string, typ DataTyp) return nil, err } return b, nil - } func WriteEntry(ctx context.Context, store db.Db, sessionId string, typ DataTyp, value []byte) error {