Skip to content

Commit

Permalink
fix: mutex lock on in-memory challenge store
Browse files Browse the repository at this point in the history
  • Loading branch information
connerdouglass committed Aug 12, 2023
1 parent dbc3de0 commit 6c9fda2
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions store_challenges_inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package webauthn

import (
"context"
"sync"

"github.com/spiretechnology/go-webauthn/pkg/challenge"
)
Expand All @@ -27,19 +28,26 @@ func NewChallengesInMemory() Challenges {

type inMemChallenges struct {
challenges map[storedChallege]struct{}
mut sync.RWMutex
}

func (c *inMemChallenges) StoreChallenge(ctx context.Context, user User, challenge challenge.Challenge) error {
c.mut.Lock()
defer c.mut.Unlock()
c.challenges[challengeKey(user, challenge)] = struct{}{}
return nil
}

func (c *inMemChallenges) HasChallenge(ctx context.Context, user User, challenge challenge.Challenge) (bool, error) {
c.mut.RLock()
defer c.mut.RUnlock()
_, ok := c.challenges[challengeKey(user, challenge)]
return ok, nil
}

func (c *inMemChallenges) RemoveChallenge(ctx context.Context, user User, challenge challenge.Challenge) error {
c.mut.Lock()
defer c.mut.Unlock()
delete(c.challenges, challengeKey(user, challenge))
return nil
}

0 comments on commit 6c9fda2

Please sign in to comment.