add metadata

This commit is contained in:
ginuerzh
2021-10-29 23:49:57 +08:00
parent 4969bc1067
commit 57a4d116d4
46 changed files with 466 additions and 381 deletions

40
pkg/auth/auth.go Normal file
View File

@ -0,0 +1,40 @@
package auth
// Authenticator is an interface for user authentication.
type Authenticator interface {
Authenticate(user, password string) bool
}
// LocalAuthenticator is an Authenticator that authenticates client by local key-value pairs.
type LocalAuthenticator struct {
kvs map[string]string
}
// NewLocalAuthenticator creates an Authenticator that authenticates client by local infos.
func NewLocalAuthenticator(kvs map[string]string) *LocalAuthenticator {
if kvs == nil {
kvs = make(map[string]string)
}
return &LocalAuthenticator{
kvs: kvs,
}
}
// Authenticate checks the validity of the provided user-password pair.
func (au *LocalAuthenticator) Authenticate(user, password string) bool {
if au == nil {
return true
}
if len(au.kvs) == 0 {
return true
}
v, ok := au.kvs[user]
return ok && (v == "" || password == v)
}
// Add adds a key-value pair to the Authenticator.
func (au *LocalAuthenticator) Add(k, v string) {
au.kvs[k] = v
}