Files
EdgeAPI/internal/rpc/utils/utils.go

158 lines
3.9 KiB
Go
Raw Normal View History

2020-07-29 19:02:28 +08:00
package rpcutils
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/encrypt"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"google.golang.org/grpc/metadata"
"time"
)
type UserType = string
const (
UserTypeNone = "none"
UserTypeAdmin = "admin"
UserTypeUser = "user"
UserTypeProvider = "provider"
UserTypeNode = "node"
UserTypeCluster = "cluster"
2020-07-29 19:02:28 +08:00
UserTypeMonitor = "monitor"
UserTypeStat = "stat"
UserTypeDNS = "dns"
UserTypeLog = "log"
2020-10-13 20:05:13 +08:00
UserTypeAPI = "api"
2020-07-29 19:02:28 +08:00
)
// 校验请求
func ValidateRequest(ctx context.Context, userTypes ...UserType) (userType UserType, userId int64, err error) {
2021-01-01 20:49:09 +08:00
if ctx == nil {
err = errors.New("context should not be nil")
return
}
// 支持直接认证
plainCtx, ok := ctx.(*PlainContext)
if ok {
userType = plainCtx.UserType
userId = plainCtx.UserId
if len(userTypes) > 0 && !lists.ContainsString(userTypes, userType) {
userType = UserTypeNone
userId = 0
}
if userId <= 0 {
err = errors.New("context: can not find user or permission denied")
}
return
}
2020-07-29 19:02:28 +08:00
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return UserTypeNone, 0, errors.New("context: need 'nodeId'")
}
nodeIds := md.Get("nodeid")
if len(nodeIds) == 0 || len(nodeIds[0]) == 0 {
return UserTypeNone, 0, errors.New("context: need 'nodeId'")
}
nodeId := nodeIds[0]
2020-08-21 12:32:33 +08:00
// 获取角色Node信息
2020-09-06 16:19:54 +08:00
// TODO 缓存节点ID相关信息
apiToken, err := models.SharedApiTokenDAO.FindEnabledTokenWithNode(nil, nodeId)
2020-07-29 19:02:28 +08:00
if err != nil {
utils.PrintError(err)
return UserTypeNone, 0, err
}
2020-08-21 12:32:33 +08:00
nodeUserId := int64(0)
2020-07-29 19:02:28 +08:00
if apiToken == nil {
return UserTypeNode, 0, errors.New("context: can not find api token for node '" + nodeId + "'")
2020-07-29 19:02:28 +08:00
}
tokens := md.Get("token")
if len(tokens) == 0 || len(tokens[0]) == 0 {
return UserTypeNone, 0, errors.New("context: need 'token'")
}
token := tokens[0]
data, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return UserTypeNone, 0, err
}
method, err := encrypt.NewMethodInstance(teaconst.EncryptMethod, apiToken.Secret, nodeId)
if err != nil {
utils.PrintError(err)
return UserTypeNone, 0, err
}
data, err = method.Decrypt(data)
if err != nil {
return UserTypeNone, 0, err
}
if len(data) == 0 {
return UserTypeNone, 0, errors.New("invalid token")
}
m := maps.Map{}
err = json.Unmarshal(data, &m)
if err != nil {
return UserTypeNone, 0, errors.New("decode token error: " + err.Error())
}
timestamp := m.GetInt64("timestamp")
if time.Now().Unix()-timestamp > 600 {
// 请求超过10分钟认为超时
return UserTypeNone, 0, errors.New("authenticate timeout")
}
t := m.GetString("type")
if len(userTypes) > 0 && !lists.ContainsString(userTypes, t) {
2020-12-15 11:52:57 +08:00
return UserTypeNone, 0, errors.New("not supported node type: '" + t + "'")
2020-07-29 19:02:28 +08:00
}
2020-10-14 18:44:34 +08:00
switch apiToken.Role {
case UserTypeNode:
nodeIntId, err := models.SharedNodeDAO.FindEnabledNodeIdWithUniqueId(nil, nodeId)
2020-10-14 18:44:34 +08:00
if err != nil {
return UserTypeNode, 0, errors.New("context: " + err.Error())
}
if nodeIntId <= 0 {
return UserTypeNode, 0, errors.New("context: not found node with id '" + nodeId + "'")
}
nodeUserId = nodeIntId
case UserTypeCluster:
clusterId, err := models.SharedNodeClusterDAO.FindEnabledClusterIdWithUniqueId(nil, nodeId)
if err != nil {
return UserTypeCluster, 0, errors.New("context: " + err.Error())
}
if clusterId <= 0 {
return UserTypeCluster, 0, errors.New("context: not found cluster with id '" + nodeId + "'")
}
nodeUserId = clusterId
2020-12-14 21:25:11 +08:00
case UserTypeUser:
2020-10-14 18:44:34 +08:00
}
2020-08-21 12:32:33 +08:00
if nodeUserId > 0 {
return t, nodeUserId, nil
} else {
return t, m.GetInt64("userId"), nil
}
2020-07-29 19:02:28 +08:00
}
2020-09-17 10:16:00 +08:00
2020-09-26 19:54:15 +08:00
// 包装错误
func Wrap(description string, err error) error {
if err == nil {
return errors.New(description)
}
return errors.New(description + ": " + err.Error())
}