Skip to content

Commit

Permalink
fix: remove sync.Map from authenticator and make it not safe for conc…
Browse files Browse the repository at this point in the history
…urrent ac
  • Loading branch information
shaj13 committed May 22, 2020
1 parent a69cb5e commit 512fa4b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
35 changes: 9 additions & 26 deletions auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"net/http"
"strings"
"sync"

gerrors "github.com/shaj13/go-guardian/errors"
)
Expand Down Expand Up @@ -50,7 +49,7 @@ type Authenticator interface {
}

type authenticator struct {
strategies *sync.Map
strategies map[StrategyKey]Strategy
paths map[string]struct{}
}

Expand All @@ -60,48 +59,32 @@ func (a *authenticator) Authenticate(r *http.Request) (Info, error) {
return nil, ErrDisabledPath
}

var info Info
authenticated := false
errs := gerrors.MultiError{ErrNoMatch}

a.strategies.Range(func(key, value interface{}) bool {
strategy := value.(Strategy)
result, err := strategy.Authenticate(r.Context(), r)
for _, strategy := range a.strategies {
info, err := strategy.Authenticate(r.Context(), r)
if err == nil {
info = result
authenticated = true
return false
return info, nil
}
errs = append(errs, err)
return true
})

if authenticated {
return info, nil
}

return nil, errs
}

func (a *authenticator) Strategy(key StrategyKey) Strategy {
v, ok := a.strategies.Load(key)
if !ok {
return nil
}
return v.(Strategy)
}

func (a *authenticator) disabledPath(path string) bool {
path = strings.TrimPrefix(path, "/")
_, ok := a.paths[path]
return ok
}

func (a *authenticator) EnableStrategy(key StrategyKey, s Strategy) { a.strategies.Store(key, s) }
func (a *authenticator) DisableStrategy(key StrategyKey) { a.strategies.Delete(key) }
func (a *authenticator) Strategy(key StrategyKey) Strategy { return a.strategies[key] }
func (a *authenticator) EnableStrategy(key StrategyKey, s Strategy) { a.strategies[key] = s }
func (a *authenticator) DisableStrategy(key StrategyKey) { delete(a.strategies, key) }
func (a *authenticator) DisabledPaths() map[string]struct{} { return a.paths }

// New return new Authenticator and disables authentication process at a given paths.
// The returned authenticator not safe for concurrent access.
func New(paths ...string) Authenticator {
p := make(map[string]struct{})

Expand All @@ -111,7 +94,7 @@ func New(paths ...string) Authenticator {
}

return &authenticator{
strategies: &sync.Map{},
strategies: make(map[StrategyKey]Strategy),
paths: p,
}
}
2 changes: 1 addition & 1 deletion auth/strategies/bearer/cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const CachedStrategyKey = auth.StrategyKey("Bearer.Cached.Strategy")
type Authenticate func(ctx context.Context, r *http.Request, token string) (auth.Info, error)

// New return new auth.Strategy.
// The returned strategy caches the invocation result of authenticate function, See Authenticate.
// The returned strategy, caches the invocation result of authenticate function, See Authenticate.
// Use NoOpAuthenticate to refresh/mangae token directly using cache or Append function, See NoOpAuthenticate.
func New(auth Authenticate, c store.Cache) auth.Strategy {
if auth == nil {
Expand Down

0 comments on commit 512fa4b

Please sign in to comment.