github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/auth/auth.go (about)

     1  package auth
     2  
     3  import "github.com/sagernet/sing/common"
     4  
     5  type User struct {
     6  	Username string
     7  	Password string
     8  }
     9  
    10  type Authenticator struct {
    11  	userMap map[string][]string
    12  }
    13  
    14  func NewAuthenticator(users []User) *Authenticator {
    15  	if len(users) == 0 {
    16  		return nil
    17  	}
    18  	au := &Authenticator{
    19  		userMap: make(map[string][]string),
    20  	}
    21  	for _, user := range users {
    22  		au.userMap[user.Username] = append(au.userMap[user.Username], user.Password)
    23  	}
    24  	return au
    25  }
    26  
    27  func (au *Authenticator) Verify(username string, password string) bool {
    28  	passwordList, ok := au.userMap[username]
    29  	return ok && common.Contains(passwordList, password)
    30  }