added pin.go to contain all PIN related functionality

This commit is contained in:
Alfred Kamanda 2025-01-02 13:16:38 +03:00
parent ed1aeecf7d
commit 48d63fb43f
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703
3 changed files with 18 additions and 16 deletions

14
common/pin.go Normal file
View File

@ -0,0 +1,14 @@
package common
import "regexp"
// Define the regex pattern as a constant
const (
pinPattern = `^\d{4}$`
)
// checks whether the given input is a 4 digit number
func IsValidPIN(pin string) bool {
match, _ := regexp.MatchString(pinPattern, pin)
return match
}

View File

@ -5,7 +5,6 @@ import (
"context"
"fmt"
"path"
"regexp"
"strconv"
"strings"
@ -34,17 +33,6 @@ var (
translationDir = path.Join(scriptDir, "locale")
)
// Define the regex patterns as constants
const (
pinPattern = `^\d{4}$`
)
// checks whether the given input is a 4 digit number
func isValidPIN(pin string) bool {
match, _ := regexp.MatchString(pinPattern, pin)
return match
}
// FlagManager handles centralized flag management
type FlagManager struct {
parser *asm.FlagParser
@ -281,7 +269,7 @@ func (h *Handlers) VerifyNewPin(ctx context.Context, sym string, input []byte) (
flag_valid_pin, _ := h.flagManager.GetFlag("flag_valid_pin")
pinInput := string(input)
// Validate that the PIN is a 4-digit number.
if isValidPIN(pinInput) {
if common.IsValidPIN(pinInput) {
res.FlagSet = append(res.FlagSet, flag_valid_pin)
} else {
res.FlagReset = append(res.FlagReset, flag_valid_pin)
@ -306,7 +294,7 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt
accountPIN := string(input)
// Validate that the PIN is a 4-digit number.
if !isValidPIN(accountPIN) {
if !common.IsValidPIN(accountPIN) {
res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
return res, nil
}

View File

@ -1544,9 +1544,9 @@ func TestIsValidPIN(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := isValidPIN(tt.pin)
actual := common.IsValidPIN(tt.pin)
if actual != tt.expected {
t.Errorf("isValidPIN(%q) = %v; expected %v", tt.pin, actual, tt.expected)
t.Errorf("IsValidPIN(%q) = %v; expected %v", tt.pin, actual, tt.expected)
}
})
}