This commit is contained in:
eson 2023-08-28 12:11:57 +08:00
parent f2ce121aac
commit 6e03ee8c1b
2 changed files with 11 additions and 3 deletions

View File

@ -74,13 +74,11 @@ func ParseJwtToken(r *http.Request, svcCtx any) (*auth.UserInfo, error) {
}
if info != nil {
if info.IsUser() {
// us, err := state.GetUserState(info.UserId) //获取缓存的用户状态
reflect.ValueOf(svcCtx)
ctxValue := reflect.ValueOf(svcCtx).FieldByName("MysqlConn")
gdb := ctxValue.Interface().(*gorm.DB)
us, err := shared.GetUserState(info.UserId, gdb)
if err != nil {
logx.Error(err)
@ -94,6 +92,7 @@ func ParseJwtToken(r *http.Request, svcCtx any) (*auth.UserInfo, error) {
}
if secret != 0 {
log.Println("secret != 0", info)
claims, err := auth.ParseJwtTokenUint64Secret(token, secret)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil {

View File

@ -9,7 +9,7 @@ import (
)
func TestRequestParse(t *testing.T) {
a, us, err := auth.TParseJwtTokenHeader[auth.UserInfo]("saTGjruwq7SG4vnQVEo3vsZvbfhzx8zZ3zWA+8nWVdid5tssnYQNECiP+pYCK6YhZ+LRH8m7f7JXrgyqtpYQMOhVOcNWTYAClk0Jnft6+QIPegzY9+v4k7eVMiWf5c/x")
a, us, err := auth.TParseJwtTokenHeader[auth.UserInfo]("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjQ3MjYwMzQsImd1ZXN0X2lkIjo2NywiaWF0IjozMTUzNjAwMCwidXNlcl9pZCI6MH0.VNsqg1479iNk_aiAz_8VNiiSL3kr047dNcpCvwM56Ck")
log.Println(a, us, err)
}
@ -19,9 +19,18 @@ func TestHash(t *testing.T) {
h.Write([]byte(a))
s := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
key := auth.StringToHash(s)
log.Println(key)
// log.Println(auth.GenerateJwtTokenUint64(auth.StringToHash(s), 400000000, time.Now().UTC().Unix(), 39, 0))
authkey := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjIwOTMxOTIyMjYsImd1ZXN0X2lkIjowLCJpYXQiOjE2OTMxOTIyMjYsInVzZXJfaWQiOjM5fQ.DrvtD7gKB0gz1rAOAQHSnyBK3exTFqoLlacpZiadpB4"
log.Println(authkey)
log.Println(auth.TParseJwtTokenHeader[auth.UserInfo](authkey))
log.Println(auth.ParseJwtTokenUint64Secret(authkey, key))
}
func TestGuestIdToken(t *testing.T) {
a := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjQ3MzAzNDAsImd1ZXN0X2lkIjo3MywiaWF0IjozMTUzNjAwMCwidXNlcl9pZCI6MH0.XTMSjZpSwbBDxGsM2G4B386lV4IMrQDgobMsboCbWhI"
auth.TParseJwtTokenHeader[auth.UserInfo](a)
log.Println(auth.TParseJwtTokenHeader[auth.UserInfo](a))
log.Println(auth.ParseJwtTokenUint64Secret(a, auth.DefaultJwtSecret))
}