Skip to content

Commit

Permalink
refactor verification logic to be case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
mojocn committed Jan 9, 2025
1 parent 5ab86bd commit 59f6320
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Captcha) Generate() (id, b64s, answer string, err error) {
// You may want to call `store.Verify` method instead.
func (c *Captcha) Verify(id, answer string, clear bool) (match bool) {
vv := c.Store.Get(id, clear)
//fix issue for some redis key-value string value
vv = strings.TrimSpace(vv)
return vv == strings.TrimSpace(answer)
vv = strings.TrimSpace(vv)
answer = strings.TrimSpace(answer)
return strings.EqualFold(vv, answer)
}
3 changes: 2 additions & 1 deletion store_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package base64Captcha

import (
"container/list"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -70,7 +71,7 @@ func (s *memoryStore) Verify(id, answer string, clear bool) bool {
return false
}
v := s.Get(id, clear)
return v != "" && v == answer
return strings.EqualFold(v, answer)
}

func (s *memoryStore) Get(id string, clear bool) (value string) {
Expand Down
4 changes: 3 additions & 1 deletion store_sync_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package base64Captcha

import (
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -59,5 +60,6 @@ func (s StoreSyncMap) Get(id string, clear bool) string {

// Verify check a string value
func (s StoreSyncMap) Verify(id, answer string, clear bool) bool {
return s.Get(id, clear) == answer
vv := s.Get(id, clear)
return strings.EqualFold(vv, answer)
}

0 comments on commit 59f6320

Please sign in to comment.