mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-08 06:32:04 +08:00
@@ -118,3 +118,30 @@ func GroupDel(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
RespSucess(w, nil)
|
||||
}
|
||||
|
||||
func GroupAuthLogin(w http.ResponseWriter, r *http.Request) {
|
||||
type AuthLoginData struct {
|
||||
Name string `json:"name"`
|
||||
Pwd string `json:"pwd"`
|
||||
Auth map[string]interface{} `json:"auth"`
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
RespError(w, RespInternalErr, err)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
v := &AuthLoginData{}
|
||||
err = json.Unmarshal(body, &v)
|
||||
if err != nil {
|
||||
RespError(w, RespInternalErr, err)
|
||||
return
|
||||
}
|
||||
err = dbdata.GroupAuthLogin(v.Name, v.Pwd, v.Auth)
|
||||
if err != nil {
|
||||
RespError(w, RespInternalErr, err)
|
||||
return
|
||||
}
|
||||
RespSucess(w, "ok")
|
||||
}
|
||||
|
@@ -71,6 +71,7 @@ func StartAdmin() {
|
||||
r.HandleFunc("/group/detail", GroupDetail)
|
||||
r.HandleFunc("/group/set", GroupSet)
|
||||
r.HandleFunc("/group/del", GroupDel)
|
||||
r.HandleFunc("/group/auth_login", GroupAuthLogin)
|
||||
|
||||
r.HandleFunc("/statsinfo/list", StatsInfoList)
|
||||
|
||||
|
@@ -225,6 +225,21 @@ func SetGroup(g *Group) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func GroupAuthLogin(name, pwd string, authData map[string]interface{}) error {
|
||||
g := &Group{Auth: authData}
|
||||
authType := g.Auth["type"].(string)
|
||||
if _, ok := authRegistry[authType]; !ok {
|
||||
return errors.New("未知的认证方式: " + authType)
|
||||
}
|
||||
auth := makeInstance(authType).(IUserAuth)
|
||||
err := auth.checkData(g.Auth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = auth.checkUser(name, pwd, g)
|
||||
return err
|
||||
}
|
||||
|
||||
func parseIpNet(s string) (string, *net.IPNet, error) {
|
||||
ip, ipNet, err := net.ParseCIDR(s)
|
||||
if err != nil {
|
||||
|
@@ -46,13 +46,14 @@ func TestGetGroupNames(t *testing.T) {
|
||||
authData = map[string]interface{}{
|
||||
"type": "ldap",
|
||||
"ldap": map[string]interface{}{
|
||||
"addr": "192.168.8.12:389",
|
||||
"tls": true,
|
||||
"bind_name": "userfind@abc.com",
|
||||
"bind_pwd": "afdbfdsafds",
|
||||
"base_dn": "dc=abc,dc=com",
|
||||
"search_attr": "sAMAccountName",
|
||||
"member_of": "cn=vpn,cn=user,dc=abc,dc=com",
|
||||
"addr": "192.168.8.12:389",
|
||||
"tls": true,
|
||||
"bind_name": "userfind@abc.com",
|
||||
"bind_pwd": "afdbfdsafds",
|
||||
"base_dn": "dc=abc,dc=com",
|
||||
"object_class": "person",
|
||||
"search_attr": "sAMAccountName",
|
||||
"member_of": "cn=vpn,cn=user,dc=abc,dc=com",
|
||||
},
|
||||
}
|
||||
g7 := Group{Name: "g7", ClientDns: []ValData{{Val: "114.114.114.114"}}, Auth: authData}
|
||||
|
@@ -70,13 +70,14 @@ func TestCheckUser(t *testing.T) {
|
||||
authData = map[string]interface{}{
|
||||
"type": "ldap",
|
||||
"ldap": map[string]interface{}{
|
||||
"addr": "192.168.8.12:389",
|
||||
"tls": true,
|
||||
"bind_name": "userfind@abc.com",
|
||||
"bind_pwd": "afdbfdsafds",
|
||||
"base_dn": "dc=abc,dc=com",
|
||||
"search_attr": "sAMAccountName",
|
||||
"member_of": "cn=vpn,cn=user,dc=abc,dc=com",
|
||||
"addr": "192.168.8.12:389",
|
||||
"tls": true,
|
||||
"bind_name": "userfind@abc.com",
|
||||
"bind_pwd": "afdbfdsafds",
|
||||
"base_dn": "dc=abc,dc=com",
|
||||
"object_class": "person",
|
||||
"search_attr": "sAMAccountName",
|
||||
"member_of": "cn=vpn,cn=user,dc=abc,dc=com",
|
||||
},
|
||||
}
|
||||
g3 := Group{Name: group3, Status: 1, ClientDns: dns, RouteInclude: route, Auth: authData}
|
||||
|
@@ -15,13 +15,14 @@ import (
|
||||
)
|
||||
|
||||
type AuthLdap struct {
|
||||
Addr string `json:"addr"`
|
||||
Tls bool `json:"tls"`
|
||||
BindName string `json:"bind_name"`
|
||||
BindPwd string `json:"bind_pwd"`
|
||||
BaseDn string `json:"base_dn"`
|
||||
SearchAttr string `json:"search_attr"`
|
||||
MemberOf string `json:"member_of"`
|
||||
Addr string `json:"addr"`
|
||||
Tls bool `json:"tls"`
|
||||
BindName string `json:"bind_name"`
|
||||
BindPwd string `json:"bind_pwd"`
|
||||
BaseDn string `json:"base_dn"`
|
||||
ObjectClass string `json:"object_class"`
|
||||
SearchAttr string `json:"search_attr"`
|
||||
MemberOf string `json:"member_of"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -40,7 +41,7 @@ func (auth AuthLdap) checkData(authData map[string]interface{}) error {
|
||||
return errors.New("LDAP的服务器地址(含端口)填写有误")
|
||||
}
|
||||
if auth.BindName == "" {
|
||||
return errors.New("LDAP的管理员账号不能为空")
|
||||
return errors.New("LDAP的管理员 DN不能为空")
|
||||
}
|
||||
if auth.BindPwd == "" {
|
||||
return errors.New("LDAP的管理员密码不能为空")
|
||||
@@ -48,6 +49,9 @@ func (auth AuthLdap) checkData(authData map[string]interface{}) error {
|
||||
if auth.BaseDn == "" || !ValidateDN(auth.BaseDn) {
|
||||
return errors.New("LDAP的Base DN填写有误")
|
||||
}
|
||||
if auth.ObjectClass == "" {
|
||||
return errors.New("LDAP的用户对象类填写有误")
|
||||
}
|
||||
if auth.SearchAttr == "" {
|
||||
return errors.New("LDAP的用户唯一ID不能为空")
|
||||
}
|
||||
@@ -94,9 +98,12 @@ func (auth AuthLdap) checkUser(name, pwd string, g *Group) error {
|
||||
}
|
||||
err = l.Bind(auth.BindName, auth.BindPwd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s LDAP 管理员账号或密码填写有误 %s", name, err.Error())
|
||||
return fmt.Errorf("%s LDAP 管理员 DN或密码填写有误 %s", name, err.Error())
|
||||
}
|
||||
filterAttr := "(objectClass=person)"
|
||||
if auth.ObjectClass == "" {
|
||||
auth.ObjectClass = "person"
|
||||
}
|
||||
filterAttr := "(objectClass=" + auth.ObjectClass + ")"
|
||||
filterAttr += "(" + auth.SearchAttr + "=" + name + ")"
|
||||
if auth.MemberOf != "" {
|
||||
filterAttr += "(memberOf:=" + auth.MemberOf + ")"
|
||||
|
Reference in New Issue
Block a user