github.com/metacubex/mihomo@v1.18.5/component/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 AuthUser struct {
     9  	User string
    10  	Pass string
    11  }
    12  
    13  type inMemoryAuthenticator struct {
    14  	storage   map[string]string
    15  	usernames []string
    16  }
    17  
    18  func (au *inMemoryAuthenticator) Verify(user string, pass string) bool {
    19  	realPass, ok := au.storage[user]
    20  	return ok && realPass == pass
    21  }
    22  
    23  func (au *inMemoryAuthenticator) Users() []string { return au.usernames }
    24  
    25  func NewAuthenticator(users []AuthUser) 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.User] = user.Pass
    35  		au.usernames = append(au.usernames, user.User)
    36  	}
    37  	return au
    38  }