hash-pin #235
							
								
								
									
										14
									
								
								common/pin.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								common/pin.go
									
									
									
									
									
										Normal 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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -5,7 +5,6 @@ import (
 | 
				
			|||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
	"regexp"
 | 
					 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -34,17 +33,6 @@ var (
 | 
				
			|||||||
	translationDir = path.Join(scriptDir, "locale")
 | 
						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
 | 
					// FlagManager handles centralized flag management
 | 
				
			||||||
type FlagManager struct {
 | 
					type FlagManager struct {
 | 
				
			||||||
	parser *asm.FlagParser
 | 
						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")
 | 
						flag_valid_pin, _ := h.flagManager.GetFlag("flag_valid_pin")
 | 
				
			||||||
	pinInput := string(input)
 | 
						pinInput := string(input)
 | 
				
			||||||
	// Validate that the PIN is a 4-digit number.
 | 
						// Validate that the PIN is a 4-digit number.
 | 
				
			||||||
	if isValidPIN(pinInput) {
 | 
						if common.IsValidPIN(pinInput) {
 | 
				
			||||||
		res.FlagSet = append(res.FlagSet, flag_valid_pin)
 | 
							res.FlagSet = append(res.FlagSet, flag_valid_pin)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		res.FlagReset = append(res.FlagReset, flag_valid_pin)
 | 
							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)
 | 
						accountPIN := string(input)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Validate that the PIN is a 4-digit number.
 | 
						// Validate that the PIN is a 4-digit number.
 | 
				
			||||||
	if !isValidPIN(accountPIN) {
 | 
						if !common.IsValidPIN(accountPIN) {
 | 
				
			||||||
		res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
 | 
							res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
 | 
				
			||||||
		return res, nil
 | 
							return res, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -1544,9 +1544,9 @@ func TestIsValidPIN(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	for _, tt := range tests {
 | 
						for _, tt := range tests {
 | 
				
			||||||
		t.Run(tt.name, func(t *testing.T) {
 | 
							t.Run(tt.name, func(t *testing.T) {
 | 
				
			||||||
			actual := isValidPIN(tt.pin)
 | 
								actual := common.IsValidPIN(tt.pin)
 | 
				
			||||||
			if actual != tt.expected {
 | 
								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)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user