mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-08 22:08:19 +08:00
1.重构认证方式的代码,方便未来扩展 2.补充测试用例
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package dbdata
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -33,11 +32,6 @@ type ValData struct {
|
||||
Note string `json:"note"`
|
||||
}
|
||||
|
||||
type AuthRadius struct {
|
||||
Addr string `json:"addr"`
|
||||
Secret string `json:"secret"`
|
||||
}
|
||||
|
||||
// type Group struct {
|
||||
// Id int `json:"id" xorm:"pk autoincr not null"`
|
||||
// Name string `json:"name" xorm:"varchar(60) not null unique"`
|
||||
@@ -154,23 +148,26 @@ func SetGroup(g *Group) error {
|
||||
if err != nil {
|
||||
return errors.New("排除域名有误:" + err.Error())
|
||||
}
|
||||
// 处理认证方式的逻辑
|
||||
// 处理登入方式的逻辑
|
||||
defAuth := map[string]interface{}{
|
||||
"type": "local",
|
||||
}
|
||||
if len(g.Auth) == 0 {
|
||||
g.Auth = defAuth
|
||||
}
|
||||
switch g.Auth["type"] {
|
||||
case "local":
|
||||
authType := g.Auth["type"].(string)
|
||||
if authType == "local" {
|
||||
g.Auth = defAuth
|
||||
case "radius":
|
||||
err = checkRadiusData(g.Auth)
|
||||
} else {
|
||||
_, ok := authRegistry[authType]
|
||||
if !ok {
|
||||
return errors.New("未知的认证方式: " + fmt.Sprintf("%s", g.Auth["type"]))
|
||||
}
|
||||
auth := makeInstance(authType).(IUserAuth)
|
||||
err = auth.checkData(g.Auth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return errors.New("#" + fmt.Sprintf("%s", g.Auth["type"]) + "#未知的认证类型")
|
||||
}
|
||||
|
||||
g.UpdatedAt = time.Now()
|
||||
@@ -195,23 +192,6 @@ func parseIpNet(s string) (string, *net.IPNet, error) {
|
||||
return ipMask, ipNet, nil
|
||||
}
|
||||
|
||||
func checkRadiusData(auth map[string]interface{}) error {
|
||||
radisConf := AuthRadius{}
|
||||
bodyBytes, err := json.Marshal(auth["radius"])
|
||||
if err != nil {
|
||||
return errors.New("Radius的密钥/服务器地址填写有误")
|
||||
}
|
||||
json.Unmarshal(bodyBytes, &radisConf)
|
||||
if !ValidateIpPort(radisConf.Addr) {
|
||||
return errors.New("Radius的服务器地址填写有误")
|
||||
}
|
||||
// freeradius官网最大8000字符, 这里限制200
|
||||
if len(radisConf.Secret) < 8 || len(radisConf.Secret) > 200 {
|
||||
return errors.New("Radius的密钥长度需在8~200个字符之间")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckDomainNames(domains string) error {
|
||||
if domains == "" {
|
||||
return nil
|
||||
@@ -232,8 +212,3 @@ func ValidateDomainName(domain string) bool {
|
||||
RegExp := regexp.MustCompile(`^([a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+[A-Za-z]{2,18}$`)
|
||||
return RegExp.MatchString(domain)
|
||||
}
|
||||
|
||||
func ValidateIpPort(addr string) bool {
|
||||
RegExp := regexp.MustCompile(`^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\:([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5])$$`)
|
||||
return RegExp.MatchString(addr)
|
||||
}
|
||||
|
Reference in New Issue
Block a user