github.com/sagernet/sing@v0.2.6/common/auth/auth.go (about) 1 package auth 2 3 type Authenticator interface { 4 Verify(user string, pass string) bool 5 Users() []string 6 } 7 8 type User struct { 9 Username string `json:"username"` 10 Password string `json:"password"` 11 } 12 13 type inMemoryAuthenticator struct { 14 storage map[string]string 15 usernames []string 16 } 17 18 func (au *inMemoryAuthenticator) Verify(username string, password string) bool { 19 realPass, ok := au.storage[username] 20 return ok && realPass == password 21 } 22 23 func (au *inMemoryAuthenticator) Users() []string { return au.usernames } 24 25 func NewAuthenticator(users []User) Authenticator { 26 if len(users) == 0 { 27 return nil 28 } 29 au := &inMemoryAuthenticator{ 30 storage: make(map[string]string), 31 usernames: make([]string, 0, len(users)), 32 } 33 for _, user := range users { 34 au.storage[user.Username] = user.Password 35 au.usernames = append(au.usernames, user.Username) 36 } 37 return au 38 }