This commit is contained in:
eson 2023-08-28 16:58:53 +08:00
parent 9aa51c221c
commit 13663f6c40

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"strconv"
"fusenapi/shared" "fusenapi/shared"
"fusenapi/utils/auth" "fusenapi/utils/auth"
@ -58,14 +57,9 @@ func ParseJwtToken(r *http.Request, svcCtx any) (*auth.UserInfo, error) {
var userinfo *auth.UserInfo var userinfo *auth.UserInfo
var err error var err error
// log.Println(io.ReadAll(r.Body)) // log.Println(io.ReadAll(r.Body))
token := r.Header.Get("Authorization") // token := r.Header.Get("Authorization")
userId, err := strconv.ParseInt(token, 10, 64) // userId, err := strconv.ParseInt(token, 10, 64)
if err == nil {
userinfo = &auth.UserInfo{
UserId: userId,
}
} else {
var secret uint64 = 0 var secret uint64 = 0
token, info, err := auth.ParseJwtTokenHeader[auth.UserInfo](r) //解析Token头, 和payload信息 token, info, err := auth.ParseJwtTokenHeader[auth.UserInfo](r) //解析Token头, 和payload信息
if err != nil { if err != nil {
@ -80,7 +74,9 @@ func ParseJwtToken(r *http.Request, svcCtx any) (*auth.UserInfo, error) {
ctxValue := reflect.ValueOf(svcCtx).FieldByName("MysqlConn") ctxValue := reflect.ValueOf(svcCtx).FieldByName("MysqlConn")
gdb := ctxValue.Interface().(*gorm.DB) gdb := ctxValue.Interface().(*gorm.DB)
us, err := shared.GetUserState(info.UserId, gdb) us, err := shared.GetUserState(info.UserId, gdb)
if err != nil { if err != nil {
log.Println(info)
logx.Error(err) logx.Error(err)
return nil, err return nil, err
} }
@ -92,7 +88,6 @@ func ParseJwtToken(r *http.Request, svcCtx any) (*auth.UserInfo, error) {
} }
if secret != 0 { if secret != 0 {
log.Println("secret != 0", info)
claims, err := auth.ParseJwtTokenUint64Secret(token, secret) claims, err := auth.ParseJwtTokenUint64Secret(token, secret)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil { if err != nil {
@ -115,7 +110,6 @@ func ParseJwtToken(r *http.Request, svcCtx any) (*auth.UserInfo, error) {
// 白板用户 // 白板用户
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
} }
}
return userinfo, nil return userinfo, nil
} }