chore: inMemoryAuthenticator unneed sync map
This commit is contained in:
parent
8755618910
commit
e1e999180a
1 changed files with 9 additions and 17 deletions
|
@ -1,9 +1,5 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"github.com/puzpuzpuz/xsync/v2"
|
||||
)
|
||||
|
||||
type Authenticator interface {
|
||||
Verify(user string, pass string) bool
|
||||
Users() []string
|
||||
|
@ -15,12 +11,12 @@ type AuthUser struct {
|
|||
}
|
||||
|
||||
type inMemoryAuthenticator struct {
|
||||
storage *xsync.MapOf[string, string]
|
||||
storage map[string]string
|
||||
usernames []string
|
||||
}
|
||||
|
||||
func (au *inMemoryAuthenticator) Verify(user string, pass string) bool {
|
||||
realPass, ok := au.storage.Load(user)
|
||||
realPass, ok := au.storage[user]
|
||||
return ok && realPass == pass
|
||||
}
|
||||
|
||||
|
@ -30,17 +26,13 @@ func NewAuthenticator(users []AuthUser) Authenticator {
|
|||
if len(users) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
au := &inMemoryAuthenticator{storage: xsync.NewMapOf[string]()}
|
||||
for _, user := range users {
|
||||
au.storage.Store(user.User, user.Pass)
|
||||
au := &inMemoryAuthenticator{
|
||||
storage: make(map[string]string),
|
||||
usernames: make([]string, 0, len(users)),
|
||||
}
|
||||
for _, user := range users {
|
||||
au.storage[user.User] = user.Pass
|
||||
au.usernames = append(au.usernames, user.User)
|
||||
}
|
||||
usernames := make([]string, 0, len(users))
|
||||
au.storage.Range(func(key string, value string) bool {
|
||||
usernames = append(usernames, key)
|
||||
return true
|
||||
})
|
||||
au.usernames = usernames
|
||||
|
||||
return au
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue