访问控制支持基本认证和子请求认证

This commit is contained in:
刘祥超
2021-06-19 21:36:13 +08:00
parent dcdb6a923a
commit ddacb945e1
5 changed files with 53 additions and 46 deletions

View File

@@ -3,9 +3,7 @@
package serverconfigs
import (
"encoding/base64"
"encoding/json"
stringutil "github.com/iwind/TeaGo/utils/string"
"net/http"
)
@@ -13,23 +11,17 @@ import (
type HTTPAuthBasicMethodUser struct {
Username string `json:"username"`
Password string `json:"password"`
Encoder string `json:"encoding"`
}
func (this *HTTPAuthBasicMethodUser) Validate(password string) (bool, error) {
switch this.Encoder {
case "md5":
return this.Password == stringutil.Md5(password), nil
case "base64":
return this.Password == base64.StdEncoding.EncodeToString([]byte(password)), nil
default:
return this.Password == password, nil
}
return this.Password == password, nil
}
// HTTPAuthBasicMethod BasicAuth方法定义
type HTTPAuthBasicMethod struct {
Users []*HTTPAuthBasicMethodUser `json:"users"`
Users []*HTTPAuthBasicMethodUser `json:"users"`
Realm string `json:"realm"`
Charset string `json:"charset"`
userMap map[string]*HTTPAuthBasicMethodUser // username => *User
}