feat: Token生成|存储|验证

This commit is contained in:
Akkia
2022-05-18 10:10:35 +08:00
parent 5d356a30e2
commit 990394bf46
6 changed files with 188 additions and 55 deletions

View File

@@ -5,6 +5,11 @@ import (
"github.com/naiba/nezha/pkg/utils"
)
var (
ApiTokenList = make(map[string]*model.ApiToken)
UserIDToApiTokenList = make(map[uint64][]string)
)
type ServerAPI struct {
Token string // 传入Token 后期可能会需要用于scope判定
IDList []uint64
@@ -45,6 +50,21 @@ type ServerInfoResponse struct {
Result []*CommonServerInfo `json:"result"`
}
func InitAPI() {
ApiTokenList = make(map[string]*model.ApiToken)
UserIDToApiTokenList = make(map[uint64][]string)
}
func LoadAPI() {
InitAPI()
var tokenList []*model.ApiToken
DB.Find(&tokenList)
for _, token := range tokenList {
ApiTokenList[token.Token] = token
UserIDToApiTokenList[token.UserID] = append(UserIDToApiTokenList[token.UserID], token.Token)
}
}
// GetStatusByIDList 获取传入IDList的服务器状态信息
func (s *ServerAPI) GetStatusByIDList() *ServerStatusResponse {
res := &ServerStatusResponse{}

View File

@@ -38,6 +38,7 @@ func LoadSingleton() {
LoadNotifications() // 加载通知服务
LoadServers() // 加载服务器列表
LoadCronTasks() // 加载定时任务
LoadAPI()
}
// InitConfigFromPath 从给出的文件路径中加载配置