2023-07-20 07:21:03 +00:00
|
|
|
|
package basic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2023-08-28 03:28:09 +00:00
|
|
|
|
"fmt"
|
2023-08-25 08:44:10 +00:00
|
|
|
|
"log"
|
2023-08-16 02:22:12 +00:00
|
|
|
|
|
2023-08-25 07:37:35 +00:00
|
|
|
|
"fusenapi/shared"
|
2023-07-20 07:21:03 +00:00
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
|
"net/http"
|
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
|
|
"github.com/golang-jwt/jwt"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
2023-08-25 07:37:35 +00:00
|
|
|
|
"gorm.io/gorm"
|
2023-07-20 07:21:03 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type IJWTParse interface {
|
|
|
|
|
ParseJwtToken(r *http.Request) (jwt.MapClaims, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BeforeLogic(w http.ResponseWriter, r *http.Request, l reflect.Value) (isNext bool) {
|
|
|
|
|
|
|
|
|
|
m := l.MethodByName("BeforeLogic")
|
|
|
|
|
if m.IsValid() {
|
|
|
|
|
result := m.Call([]reflect.Value{reflect.ValueOf(w), reflect.ValueOf(r)})
|
|
|
|
|
if len(result) != 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 05:17:02 +00:00
|
|
|
|
func AfterLogic(w http.ResponseWriter, r *http.Request, l reflect.Value, resp *Response) bool {
|
2023-07-20 07:21:03 +00:00
|
|
|
|
|
|
|
|
|
m := l.MethodByName("AfterLogic")
|
|
|
|
|
if m.IsValid() {
|
2023-07-24 05:17:02 +00:00
|
|
|
|
m.Call([]reflect.Value{reflect.ValueOf(w), reflect.ValueOf(r), reflect.ValueOf(resp)})
|
2023-07-20 07:21:03 +00:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NormalAfterLogic(w http.ResponseWriter, r *http.Request, resp *Response) {
|
|
|
|
|
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
|
|
|
|
if resp != nil {
|
|
|
|
|
httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
|
} else {
|
|
|
|
|
err := errors.New("server logic is error, resp must not be nil")
|
|
|
|
|
httpx.ErrorCtx(r.Context(), w, err)
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 03:28:09 +00:00
|
|
|
|
func ParseJwtToken(r *http.Request, svcCtx any) (*auth.UserInfo, error) {
|
2023-08-25 08:19:47 +00:00
|
|
|
|
var userinfo *auth.UserInfo
|
2023-08-22 06:34:13 +00:00
|
|
|
|
var err error
|
2023-08-25 07:37:35 +00:00
|
|
|
|
// log.Println(io.ReadAll(r.Body))
|
2023-08-28 08:58:53 +00:00
|
|
|
|
// token := r.Header.Get("Authorization")
|
|
|
|
|
// userId, err := strconv.ParseInt(token, 10, 64)
|
2023-08-28 04:23:12 +00:00
|
|
|
|
|
2023-08-28 08:58:53 +00:00
|
|
|
|
var secret uint64 = 0
|
2023-10-17 09:25:48 +00:00
|
|
|
|
token, info, err := auth.ParseJwtTokenWithHeader[auth.UserInfo]("Authorization", r) //解析Token头, 和payload信息
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debugInfo, err := auth.ParseDebugJwtTokenWithHeader("Debug-Token", r) //解析Token头, 和payload信息
|
2023-08-28 08:58:53 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if info != nil {
|
|
|
|
|
if info.IsUser() {
|
|
|
|
|
// us, err := state.GetUserState(info.UserId) //获取缓存的用户状态
|
2023-08-28 09:25:27 +00:00
|
|
|
|
|
2023-08-28 09:20:08 +00:00
|
|
|
|
rsvcCtx := reflect.ValueOf(svcCtx)
|
|
|
|
|
if rsvcCtx.Kind() == reflect.Ptr {
|
|
|
|
|
rsvcCtx = rsvcCtx.Elem()
|
|
|
|
|
}
|
|
|
|
|
ctxValue := rsvcCtx.FieldByName("MysqlConn")
|
2023-08-28 08:58:53 +00:00
|
|
|
|
gdb := ctxValue.Interface().(*gorm.DB)
|
|
|
|
|
us, err := shared.GetUserState(info.UserId, gdb)
|
2023-08-28 04:23:12 +00:00
|
|
|
|
|
2023-08-28 08:58:53 +00:00
|
|
|
|
if err != nil {
|
2023-08-30 02:52:18 +00:00
|
|
|
|
// logx.Println("error", info)
|
|
|
|
|
logx.Error(err, ":", info)
|
2023-08-31 03:04:27 +00:00
|
|
|
|
return nil, errors.New("user not found:" + err.Error())
|
2023-08-25 07:37:35 +00:00
|
|
|
|
}
|
2023-08-28 08:58:53 +00:00
|
|
|
|
secret = us.PwdHash // 获取密码的hash做jwt, 便于重置密码的使用
|
|
|
|
|
|
|
|
|
|
} else if info.IsGuest() {
|
|
|
|
|
secret = auth.DefaultJwtSecret //获取默认的hash
|
2023-08-25 07:37:35 +00:00
|
|
|
|
}
|
2023-08-28 08:58:53 +00:00
|
|
|
|
}
|
2023-08-25 07:37:35 +00:00
|
|
|
|
|
2023-08-28 08:58:53 +00:00
|
|
|
|
if secret != 0 {
|
|
|
|
|
claims, err := auth.ParseJwtTokenUint64Secret(token, secret)
|
|
|
|
|
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(token)
|
2023-08-25 07:37:35 +00:00
|
|
|
|
if err != nil {
|
2023-08-28 08:58:53 +00:00
|
|
|
|
return nil, fmt.Errorf("unauthorized")
|
2023-08-25 07:37:35 +00:00
|
|
|
|
}
|
2023-08-28 08:58:53 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-08-25 08:19:47 +00:00
|
|
|
|
|
2023-08-28 08:58:53 +00:00
|
|
|
|
if claims != nil {
|
|
|
|
|
// 从token中获取对应的用户信息
|
|
|
|
|
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
|
|
|
|
|
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("unauthorized")
|
2023-08-25 08:19:47 +00:00
|
|
|
|
}
|
2023-08-25 07:37:35 +00:00
|
|
|
|
}
|
2023-08-28 08:58:53 +00:00
|
|
|
|
} else {
|
|
|
|
|
// 白板用户
|
|
|
|
|
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
|
2023-08-25 07:37:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 09:25:48 +00:00
|
|
|
|
if debugInfo != nil {
|
|
|
|
|
userinfo.Debug = debugInfo
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 03:28:09 +00:00
|
|
|
|
return userinfo, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicRequest any) (*auth.UserInfo, error) {
|
|
|
|
|
|
|
|
|
|
// 新的解析jwtToken
|
|
|
|
|
userinfo, err := ParseJwtToken(r, svcCtx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpx.OkJsonCtx(r.Context(), w, &Response{
|
2023-09-12 10:08:37 +00:00
|
|
|
|
Code: 401,
|
2023-08-28 03:28:09 +00:00
|
|
|
|
Message: err.Error(),
|
|
|
|
|
})
|
2023-08-28 07:20:46 +00:00
|
|
|
|
return nil, err
|
2023-08-28 03:28:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 09:25:48 +00:00
|
|
|
|
// Debug-Token
|
|
|
|
|
|
2023-07-20 07:21:03 +00:00
|
|
|
|
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
|
|
|
|
if err = httpx.Parse(r, LogicRequest); err != nil {
|
|
|
|
|
httpx.OkJsonCtx(r.Context(), w, &Response{
|
|
|
|
|
Code: 510,
|
|
|
|
|
Message: "parameter error",
|
|
|
|
|
})
|
2023-07-26 04:15:15 +00:00
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
2023-07-20 07:21:03 +00:00
|
|
|
|
}
|
2023-08-25 07:37:35 +00:00
|
|
|
|
// userinfo := &auth.UserInfo{UserId: 39}
|
2023-07-20 07:21:03 +00:00
|
|
|
|
return userinfo, err
|
|
|
|
|
}
|
2023-07-21 07:20:18 +00:00
|
|
|
|
|
|
|
|
|
func RequestParseBackend(w http.ResponseWriter, r *http.Request, svcCtx IJWTParse, LogicRequest any) (userinfo *auth.BackendUserInfo, err error) {
|
|
|
|
|
|
|
|
|
|
// 解析JWT token,并对空用户进行判断
|
|
|
|
|
claims, err := svcCtx.ParseJwtToken(r)
|
|
|
|
|
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpx.OkJsonCtx(r.Context(), w, &Response{
|
|
|
|
|
Code: 401, // 返回401状态码,表示未授权
|
|
|
|
|
Message: "unauthorized", // 返回未授权信息
|
|
|
|
|
})
|
|
|
|
|
logx.Info("unauthorized:", err.Error()) // 记录错误日志
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if claims != nil {
|
|
|
|
|
// 从token中获取对应的用户信息
|
|
|
|
|
userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims)
|
|
|
|
|
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpx.OkJsonCtx(r.Context(), w, &Response{
|
|
|
|
|
Code: 401,
|
|
|
|
|
Message: "unauthorized",
|
|
|
|
|
})
|
|
|
|
|
logx.Info("unauthorized:", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// var req types.RequestGoogleLogin
|
|
|
|
|
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
|
|
|
|
if err = httpx.Parse(r, LogicRequest); err != nil {
|
|
|
|
|
httpx.OkJsonCtx(r.Context(), w, &Response{
|
|
|
|
|
Code: 510,
|
|
|
|
|
Message: "parameter error",
|
|
|
|
|
})
|
|
|
|
|
logx.Info(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return userinfo, err
|
|
|
|
|
}
|