37 Star 384 Fork 117

联犀/物联网模块

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
accessAuthLogic.go 2.74 KB
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2024-10-10 22:13 . feat: 更新mod
package deviceauthlogic
import (
"context"
"gitee.com/unitedrhino/share/devices"
"gitee.com/unitedrhino/share/domain/deviceAuth"
"gitee.com/unitedrhino/share/errors"
"gitee.com/unitedrhino/share/utils"
"gitee.com/unitedrhino/things/service/dgsvr/internal/svc"
"gitee.com/unitedrhino/things/service/dgsvr/pb/dg"
"gitee.com/unitedrhino/things/service/dmsvr/pb/dm"
"github.com/zeromicro/go-zero/core/logx"
)
type AccessAuthLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAccessAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AccessAuthLogic {
return &AccessAuthLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
var (
AccessMap = map[string]devices.Direction{
devices.Pub: devices.Up,
devices.Sub: devices.Down,
}
AccessToActionMap = map[string]string{
devices.Pub: "publish",
devices.Sub: "subscribe",
}
)
// DeviceSelfAuth 设备自己的topic认证
func (l *AccessAuthLogic) DeviceSelfAuth(in *dg.AccessAuthReq) (err error) {
l.Infof("%s in:%v", utils.FuncName(), utils.Fmt(in))
access, ok := AccessMap[in.Access]
if !ok {
return errors.Permissions
}
return deviceAuth.AccessAuth(deviceAuth.AuthInfo{
Username: in.Username,
Topic: in.Topic,
ClientID: in.ClientID,
Access: access,
Ip: in.Ip,
})
}
// SubSetAuth 网关代理子设备topic校验
func (l *AccessAuthLogic) SubSetAuth(in *dg.AccessAuthReq,
ld *deviceAuth.LoginDevice, ti *devices.TopicInfo) (err error) {
ret, err := l.svcCtx.DeviceM.DeviceGatewayIndex(l.ctx, &dm.DeviceGatewayIndexReq{
Gateway: &dm.DeviceCore{
ProductID: ld.ProductID,
DeviceName: ld.DeviceName,
},
SubDevice: &dm.DeviceCore{
ProductID: ti.ProductID,
DeviceName: ti.DeviceName,
},
})
if err != nil {
return errors.Fmt(err)
}
if ret.Total == 0 { //未找到该网关和设备的关系
return errors.Permissions
}
access, ok := AccessMap[in.Access]
if !ok {
return errors.Permissions
}
if access != ti.Direction {
return errors.Permissions
}
return nil
}
func (l *AccessAuthLogic) Auth(in *dg.AccessAuthReq) (err error) {
ld, err := deviceAuth.GetClientIDInfo(in.ClientID)
if err != nil {
return err
}
err = func() error {
topicInfo, err := devices.GetTopicInfo(in.Topic)
if err != nil {
return errors.Permissions
}
if ld.ProductID == topicInfo.ProductID && ld.DeviceName == topicInfo.DeviceName {
return l.DeviceSelfAuth(in)
}
return l.SubSetAuth(in, ld, topicInfo)
}()
return err
}
// 设备操作认证
func (l *AccessAuthLogic) AccessAuth(in *dg.AccessAuthReq) (*dg.Response, error) {
l.Infof("%s req=%+v", utils.FuncName(), utils.Fmt(in))
err := l.Auth(in)
if err != nil {
l.Infof("%s auth failure=%v", utils.FuncName(), err)
}
return &dg.Response{}, err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/unitedrhino/things.git
git@gitee.com:unitedrhino/things.git
unitedrhino
things
物联网模块
v1.0.3

搜索帮助

0d507c66 1850385 C8b1a773 1850385