客户端授权验证

This commit is contained in:
奶爸
2019-12-09 16:02:49 +08:00
parent 986dc6114a
commit 58277ba0b6
13 changed files with 211 additions and 73 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/naiba/com"
"github.com/patrickmn/go-cache"
"github.com/p14yground/nezha/model"
"github.com/p14yground/nezha/pkg/mygin"
@@ -37,9 +38,9 @@ type serverForm struct {
func (ma *memberAPI) addServer(c *gin.Context) {
var sf serverForm
var s model.Server
err := c.ShouldBindJSON(&sf)
if err == nil {
var s model.Server
s.Name = sf.Name
s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, dao.Admin.ID))
s.Secret = s.Secret[:10]
@@ -52,6 +53,7 @@ func (ma *memberAPI) addServer(c *gin.Context) {
})
return
}
dao.Cache.Set(fmt.Sprintf("%s%d%s", model.CtxKeyServer, s.ID, s.Secret), s, cache.NoExpiration)
c.JSON(http.StatusOK, model.Response{
Code: http.StatusOK,
})

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"time"
"github.com/jinzhu/gorm"
@@ -36,6 +37,12 @@ func init() {
func initDB() {
dao.DB.AutoMigrate(model.Server{})
// load cache
var servers []model.Server
dao.DB.Find(&servers)
for _, s := range servers {
dao.Cache.Set(fmt.Sprintf("%s%d%s", model.CtxKeyServer, s.ID, s.Secret), s, cache.NoExpiration)
}
}
func main() {

View File

@@ -14,8 +14,8 @@ func ServeRPC() {
server := grpc.NewServer()
pb.RegisterNezhaServiceServer(server, &rpcService.NezhaHandler{
Auth: &rpcService.AuthHandler{
AppKey: "naiba",
AppSecret: "123456",
ClientID: "naiba",
ClientSecret: "123456",
},
})
listen, err := net.Listen("tcp", ":5555")