1. TODO: guest_id 2. jwt的封装
This commit is contained in:
parent
b6095d67ad
commit
bdaa3bf48b
|
@ -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
|
||||
// }
|
|
@ -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
|
||||
// }
|
||||
|
|
|
@ -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),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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()))
|
||||
|
||||
}
|
58
server/home-user-auth/test/basic.go
Normal file
58
server/home-user-auth/test/basic.go
Normal 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
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
1
server/home-user-auth/test/userbasicinfologic_test.go
Normal file
1
server/home-user-auth/test/userbasicinfologic_test.go
Normal file
|
@ -0,0 +1 @@
|
|||
package logic_test
|
|
@ -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 {
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue
Block a user