Skip to content

Commit

Permalink
added mutex lock on Ask function to avoid race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Apr 5, 2024
1 parent 201b373 commit b19a2b9
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion db/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"strconv"
"sync"
"time"

"github.com/go-chi/chi"
Expand Down Expand Up @@ -132,9 +133,12 @@ func (s StoreData) GetChallengeCache(key string) (string, error) {
}

func Ask(w http.ResponseWriter, r *http.Request) {
var m sync.Mutex
m.Lock()

ts := strconv.Itoa(int(time.Now().Unix()))
h := []byte(ts)
// h := blake2b.Sum256([]byte(ts))

challenge := base64.URLEncoding.EncodeToString(h[:])

Store.SetChallengeCache(challenge, ts)
Expand All @@ -144,6 +148,8 @@ func Ask(w http.ResponseWriter, r *http.Request) {
"challenge": challenge,
"ts": ts,
})

m.Unlock()
}

type VerifyPayload struct {
Expand Down

0 comments on commit b19a2b9

Please sign in to comment.