1. TODO: guest_id 2. jwt的封装

This commit is contained in:
eson 2023-06-12 19:52:11 +08:00
parent b6095d67ad
commit bdaa3bf48b
9 changed files with 100 additions and 88 deletions

View File

@ -28,19 +28,19 @@ func main() {
// var testConfigFile = flag.String("f", "../etc/{{.serviceName}}.yaml", "the config file")
// var cnf config.Config
// func GetTestServer() *rest.Server {
// flag.Parse()
// var c config.Config
// conf.MustLoad(*testConfigFile, &c)
// conf.MustLoad(*testConfigFile, &cnf)
// server := rest.MustNewServer(c.RestConf)
// server := rest.MustNewServer(cnf.RestConf)
// defer server.Stop()
// ctx := svc.NewServiceContext(c)
// ctx := svc.NewServiceContext(cnf)
// handler.RegisterHandlers(server, ctx)
// fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
// fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port)
// return server
// }

View File

@ -33,19 +33,19 @@ func main() {
// 测试代码 可以直接复制新建
// var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
// var cnf config.Config
// func GetTestServer() *rest.Server {
// flag.Parse()
// var c config.Config
// conf.MustLoad(*testConfigFile, &c)
// conf.MustLoad(*testConfigFile, &cnf)
// server := rest.MustNewServer(c.RestConf)
// server := rest.MustNewServer(cnf.RestConf)
// defer server.Stop()
// ctx := svc.NewServiceContext(c)
// ctx := svc.NewServiceContext(cnf)
// handler.RegisterHandlers(server, ctx)
// fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
// fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port)
// return server
// }

View File

@ -42,11 +42,17 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/user/basic-info",
Handler: UserBasicInfoHandler(serverCtx),
},
},
)
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodGet,
Path: "/user/address-list",
Handler: UserAddressListHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
)
}

View File

@ -1,34 +0,0 @@
package logic
import (
"log"
"testing"
"github.com/474420502/requests"
)
func TestCaseAddressList(t *testing.T) {
// http.NewRequest("POST", "http://localhost:8888/user/login", body io.Reader)
ses := requests.NewSession()
tp := ses.Post("http://localhost:8888/user/login")
resp, err := tp.SetBodyJson(map[string]interface{}{
"name": "devenv@sina.cn",
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
}).Execute()
if err != nil {
panic(err)
}
result := resp.Json()
token := result.Get("data.token")
ses.Header.Add("Authorization", token.String())
log.Println(string(resp.Content()))
resp, err = ses.Get("http://localhost:8888/user/address-list").Execute()
if err != nil {
panic(err)
}
log.Println(string(resp.Content()))
}

View File

@ -0,0 +1,58 @@
package logic_test
import (
"flag"
"fmt"
"fusenapi/server/home-user-auth/internal/config"
"fusenapi/server/home-user-auth/internal/handler"
"fusenapi/server/home-user-auth/internal/svc"
"testing"
"github.com/474420502/requests"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
)
var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
var cnf config.Config
var gserver *rest.Server
func init() {
gserver = GetTestServer()
}
func GetTestServer() *rest.Server {
flag.Parse()
conf.MustLoad(*testConfigFile, &cnf)
server := rest.MustNewServer(cnf.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(cnf)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port)
return server
}
func GetSesssionWithJwtToken(t *testing.T, server requests.ITestServer) *requests.Session {
ses := requests.NewSession()
tp := ses.Post(fmt.Sprintf("http://%s:%d/user/login", cnf.Host, cnf.Port))
tp.SetBodyJson(map[string]interface{}{
"name": "devenv@sina.cn",
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
})
resp, err := tp.TestInServer(server)
if err != nil {
t.Error(err)
}
result := resp.Json()
token := result.Get("data.token")
if !token.Exists() {
t.Error("data.token is not exists")
}
ses.Header.Add("Authorization", token.String())
return ses
}

View File

@ -1,57 +1,21 @@
package server_test
package logic_test
import (
"flag"
"fmt"
"fusenapi/server/home-user-auth/internal/config"
"fusenapi/server/home-user-auth/internal/handler"
"fusenapi/server/home-user-auth/internal/svc"
"testing"
"github.com/474420502/requests"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
"github.com/tidwall/gjson"
)
var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
var cnf config.Config
func GetTestServer() *rest.Server {
flag.Parse()
conf.MustLoad(*testConfigFile, &cnf)
server := rest.MustNewServer(cnf.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(cnf)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port)
return server
}
func TestCaseAddressList(t *testing.T) {
server := GetTestServer()
var err error
var resp *requests.Response
var result gjson.Result
ses := requests.NewSession()
tp := ses.Post(fmt.Sprintf("http://%s:%d/user/login", cnf.Host, cnf.Port))
tp.SetBodyJson(map[string]interface{}{
"name": "devenv@sina.cn",
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
})
resp, err := tp.TestInServer(server)
if err != nil {
t.Error(err)
}
result := resp.Json()
token := result.Get("data.token")
if !token.Exists() {
t.Error("data.token is not exists")
}
ses.Header.Add("Authorization", token.String())
ses := GetSesssionWithJwtToken(t, gserver)
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestInServer(server)
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestInServer(gserver)
if err != nil {
t.Error(err)
}

View File

@ -0,0 +1 @@
package logic_test

View File

@ -33,9 +33,17 @@ service home-user-auth {
@handler UserBasicInfoHandler
get /user/basic-info(request) returns (response);
// @handler UserAddressListHandler
// get /user/address-list(request) returns (response);
}
@server(
jwt: Auth
)
service home-user-auth {
@handler UserAddressListHandler
get /user/address-list(request) returns (response);
}
type RequestBasicInfoForm {

View File

@ -7,6 +7,7 @@ import (
"fmt"
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"github.com/zeromicro/go-zero/core/logx"
)
@ -47,6 +48,14 @@ func GenerateJwtToken(accessSecret string, accessExpire, nowSec int64, userid in
claims["exp"] = nowSec + accessExpire
claims["iat"] = nowSec
claims["userid"] = userid
if userid == 0 {
u, err := uuid.NewUUID()
if err != nil {
logx.Error(err)
return "", err
}
claims["guestid"] = u.String() // TODO: 未完成
}
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = claims
return token.SignedString([]byte(accessSecret))