debug toekn
This commit is contained in:
@@ -16,6 +16,11 @@ type Config struct {
|
||||
MainAddress string
|
||||
WebsocketAddr string
|
||||
|
||||
Debug struct {
|
||||
Password string
|
||||
}
|
||||
// Password: "fusen-test-pwd"
|
||||
|
||||
OAuth struct {
|
||||
Google struct {
|
||||
Appid string
|
||||
|
||||
@@ -27,6 +27,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/api/auth/accept-cookie",
|
||||
Handler: AcceptCookieHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/auth/debug/token/create",
|
||||
Handler: UserDebugTokenHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/auth/oauth2/login/google",
|
||||
|
||||
35
server/auth/internal/handler/userdebugtokenhandler.go
Normal file
35
server/auth/internal/handler/userdebugtokenhandler.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/auth/internal/logic"
|
||||
"fusenapi/server/auth/internal/svc"
|
||||
"fusenapi/server/auth/internal/types"
|
||||
)
|
||||
|
||||
func UserDebugTokenHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.RequestUserDebug
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewUserDebugTokenLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.UserDebugToken(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
63
server/auth/internal/logic/userdebugtokenlogic.go
Normal file
63
server/auth/internal/logic/userdebugtokenlogic.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/auth/internal/svc"
|
||||
"fusenapi/server/auth/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UserDebugTokenLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUserDebugTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserDebugTokenLogic {
|
||||
return &UserDebugTokenLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *UserDebugTokenLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *UserDebugTokenLogic) UserDebugToken(req *types.RequestUserDebug, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
if req.Password != l.svcCtx.Config.Debug.Password {
|
||||
return resp.SetStatusAddMessage(basic.CodeApiErr, "密码错误")
|
||||
}
|
||||
var accessExpire int64 = 3600
|
||||
if req.Exp != nil {
|
||||
accessExpire = *req.Exp
|
||||
}
|
||||
|
||||
debug := &auth.Debug{
|
||||
IsCache: req.IsCache,
|
||||
IsAllTemplateTag: req.IsAllTemplateTag,
|
||||
}
|
||||
|
||||
dtoken, err := auth.GenerateBaseJwtTokenUint64(auth.DefaultDebugJwtSecret, accessExpire, time.Now().UTC().Unix(), debug)
|
||||
if err != nil {
|
||||
return resp.SetStatusWithMessage(basic.CodeApiErr, "GenerateBaseJwtTokenUint64错误")
|
||||
}
|
||||
|
||||
return resp.SetStatus(basic.CodeOK, map[string]any{
|
||||
"debug_token": dtoken,
|
||||
})
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *UserDebugTokenLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
@@ -5,6 +5,13 @@ import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type RequestUserDebug struct {
|
||||
Password string `json:"password"` // 密码,内部使用都是明文
|
||||
Exp *int64 `json:"exp"` // 过期时间, 不发默认一天
|
||||
IsCache int64 `json:"is_cache"` // 是否缓存
|
||||
IsAllTemplateTag int64 `json:"is_all_template_tag"` // 是开启全部模板
|
||||
}
|
||||
|
||||
type RequestAuthDelete struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
@@ -90,10 +97,10 @@ type File struct {
|
||||
}
|
||||
|
||||
type Meta struct {
|
||||
TotalCount int64 `json:"totalCount"`
|
||||
PageCount int64 `json:"pageCount"`
|
||||
CurrentPage int `json:"currentPage"`
|
||||
PerPage int `json:"perPage"`
|
||||
TotalCount int64 `json:"total_count"`
|
||||
PageCount int64 `json:"page_count"`
|
||||
CurrentPage int `json:"current_page"`
|
||||
PerPage int `json:"per_page"`
|
||||
}
|
||||
|
||||
// Set 设置Response的Code和Message值
|
||||
|
||||
Reference in New Issue
Block a user