Skip to content

Commit

Permalink
radius 添加 CallingStationID
Browse files Browse the repository at this point in the history
  • Loading branch information
bjdgyc committed Nov 15, 2024
1 parent 8a2350e commit bda2328
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion server/dbdata/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ func GroupAuthLogin(name, pwd string, authData map[string]interface{}) error {
if err != nil {
return err
}
err = auth.checkUser(name, pwd, g)
ext := map[string]interface{}{}
err = auth.checkUser(name, pwd, g, ext)
return err
}

Expand Down
8 changes: 4 additions & 4 deletions server/dbdata/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func SetUser(v *User) error {
}

// 验证用户登录信息
func CheckUser(name, pwd, group string) error {
func CheckUser(name, pwd, group string, ext map[string]interface{}) error {
// 获取登入的group数据
groupData := &Group{}
err := One("Name", group, groupData)
Expand All @@ -82,19 +82,19 @@ func CheckUser(name, pwd, group string) error {
authType := groupData.Auth["type"].(string)
// 本地认证方式
if authType == "local" {
return checkLocalUser(name, pwd, group)
return checkLocalUser(name, pwd, group, ext)
}
// 其它认证方式, 支持自定义
_, ok := authRegistry[authType]
if !ok {
return fmt.Errorf("%s %s", "未知的认证方式: ", authType)
}
auth := makeInstance(authType).(IUserAuth)
return auth.checkUser(name, pwd, groupData)
return auth.checkUser(name, pwd, groupData, ext)
}

// 验证本地用户登录信息
func checkLocalUser(name, pwd, group string) error {
func checkLocalUser(name, pwd, group string, ext map[string]interface{}) error {
// TODO 严重问题
// return nil

Expand Down
2 changes: 1 addition & 1 deletion server/dbdata/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var authRegistry = make(map[string]reflect.Type)

type IUserAuth interface {
checkData(authData map[string]interface{}) error
checkUser(name, pwd string, g *Group) error
checkUser(name, pwd string, g *Group, ext map[string]interface{}) error
}

func makeInstance(name string) interface{} {
Expand Down
2 changes: 1 addition & 1 deletion server/dbdata/userauth_ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (auth AuthLdap) checkData(authData map[string]interface{}) error {
return nil
}

func (auth AuthLdap) checkUser(name, pwd string, g *Group) error {
func (auth AuthLdap) checkUser(name, pwd string, g *Group, ext map[string]interface{}) error {
pl := len(pwd)
if name == "" || pl < 1 {
return fmt.Errorf("%s %s", name, "密码错误")
Expand Down
12 changes: 10 additions & 2 deletions server/dbdata/userauth_radius.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (auth AuthRadius) checkData(authData map[string]interface{}) error {
return nil
}

func (auth AuthRadius) checkUser(name, pwd string, g *Group) error {
func (auth AuthRadius) checkUser(name, pwd string, g *Group, ext map[string]interface{}) error {
pl := len(pwd)
if name == "" || pl < 1 {
return fmt.Errorf("%s %s", name, "密码错误")
Expand Down Expand Up @@ -74,15 +74,23 @@ func (auth AuthRadius) checkUser(name, pwd string, g *Group) error {
return fmt.Errorf("%s %s", name, "Radius set nasip 出现错误")
}
}
macAddr := ext["mac_addr"].(string)
if macAddr != "" {
err = rfc2865.CallingStationID_SetString(packet, macAddr)
if err != nil {
return fmt.Errorf("%s %s", name, "Radius set CallingStationID 出现错误")
}
}

ctx, done := context.WithTimeout(context.Background(), 3*time.Second)
defer done()
response, err := radius.Exchange(ctx, packet, auth.Addr)
if err != nil {
return fmt.Errorf("%s %s", name, "Radius服务器连接异常, 请检测服务器和端口")
return fmt.Errorf("%s %s %s", name, "Radius服务器连接异常, 请检测服务器和端口", err)
}
if response.Code != radius.CodeAccessAccept {
return fmt.Errorf("%s %s", name, "Radius:用户名或密码错误")
}
return nil

}
3 changes: 2 additions & 1 deletion server/handler/link_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func LinkAuth(w http.ResponseWriter, r *http.Request) {
UserActLog: ua,
}
// TODO 用户密码校验
err = dbdata.CheckUser(cr.Auth.Username, cr.Auth.Password, cr.GroupSelect)
err = dbdata.CheckUser(cr.Auth.Username, cr.Auth.Password, cr.GroupSelect,
map[string]interface{}{"mac_addr": cr.MacAddressList.MacAddress})
if err != nil {
// lockManager.LoginStatus.Store(loginStatusKey, false) // 记录登录失败状态
// hc := r.Context().Value(loginStatusKey).(*HttpContext)
Expand Down

0 comments on commit bda2328

Please sign in to comment.